summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/credential-management/support/fedcm-helper.sub.js
blob: 87a1337a06cbd63f7302fd4f93298d9ffcf072ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const alt_manifest_origin = 'https://{{hosts[alt][]}}:{{ports[https][0]}}';

// Set the identity provider cookie.
export function set_fedcm_cookie(host) {
  return new Promise(resolve => {
    if (host == undefined) {
      document.cookie = 'cookie=1; SameSite=Strict; Path=/credential-management/support; Secure';
      resolve();
    } else {
      let popup_window = window.open(host + '/credential-management/support/set_cookie');

      const popup_message_handler = (event) => {
        if (event.origin == host) {
          popup_window.close();
          window.removeEventListener('message', popup_message_handler);
          resolve();
        }
      };

      window.addEventListener('message', popup_message_handler);
    }
  });
}

// Set the alternate identity provider cookie.
export function set_alt_fedcm_cookie() {
  return set_fedcm_cookie(alt_manifest_origin);
}

// Returns FedCM CredentialRequestOptions for which navigator.credentials.get()
// succeeds.
export function default_request_options(manifest_filename) {
  if (manifest_filename === undefined) {
    manifest_filename = "manifest.py";
  }
  const manifest_path = `https://{{host}}:{{ports[https][0]}}/\
credential-management/support/fedcm/${manifest_filename}`;
  return {
    identity: {
      providers: [{
        configURL: manifest_path,
        clientId: '1',
        nonce: '2',
      }]
    }
  };
}

// Returns alternate FedCM CredentialRequestOptions for which navigator.credentials.get()
// succeeds.
export function default_alt_request_options(manifest_filename) {
  if (manifest_filename === undefined) {
    manifest_filename = "manifest.py";
  }
  const manifest_path = `${alt_manifest_origin}/\
credential-management/support/fedcm/${manifest_filename}`;
  return {
    identity: {
      providers: [{
        configURL: manifest_path,
        clientId: '1',
        nonce: '2',
      }]
    }
  };
}

// Test wrapper which does FedCM-specific setup.
export function fedcm_test(test_func, test_name) {
  promise_test(async t => {
    await set_fedcm_cookie();
    await set_alt_fedcm_cookie();
    await test_func(t);
  }, test_name);
}

function select_manifest_impl(manifest_url) {
  const url_query = (manifest_url === undefined)
      ? '' : '?manifest_url=${manifest_url}';

  return new Promise(resolve => {
    const img = document.createElement('img');
    img.src = 'support/fedcm/select_manifest_in_root_manifest.py?${url_query}';
    img.addEventListener('error', resolve);
    document.body.appendChild(img);
  });
}

// Sets the manifest returned by the next fetch of /.well-known/web_identity
// select_manifest() only affects the next fetch and not any subsequent fetches
// (ex second next fetch).
export function select_manifest(test, test_options) {
  // Add cleanup in case that /.well-known/web_identity is not fetched at all.
  test.add_cleanup(async () => {
    await select_manifest_impl();
  });
  const manifest_url = test_options.identity.providers[0].configURL;
  return select_manifest_impl(manifest_url);
}