forked from microsoft/playwright-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserContextWebAuthnTests.cs
More file actions
193 lines (176 loc) · 8.69 KB
/
Copy pathBrowserContextWebAuthnTests.cs
File metadata and controls
193 lines (176 loc) · 8.69 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* MIT License
*
* Copyright (c) Microsoft Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System.Text.Json;
namespace Microsoft.Playwright.Tests;
///<playwright-file>browsercontext-webauthn.spec.ts</playwright-file>
public class BrowserContextWebAuthnTests : BrowserTestEx
{
// Server.Prefix is http://localhost:<port>.
private const string RpId = "localhost";
[PlaywrightTest("browsercontext-webauthn.spec.ts", "should not intercept navigator.credentials without install()")]
public async Task ShouldNotInterceptNavigatorCredentialsWithoutInstall()
{
await using var context = await Browser.NewContextAsync();
// Seed a credential, but do not install the interceptor.
await context.Credentials.CreateAsync(RpId);
var page = await context.NewPageAsync();
await page.GotoAsync(Server.EmptyPage);
Assert.IsFalse(await page.EvaluateAsync<bool>("() => globalThis.__pwWebAuthnInstalled === true"));
}
[PlaywrightTest("browsercontext-webauthn.spec.ts", "should seed a known credential and authenticate")]
public async Task ShouldSeedAKnownCredentialAndAuthenticate()
{
// This is the easiest way to create credentials. In practice, this
// probably comes from environment.
await using var source = await Browser.NewContextAsync();
var known = await source.Credentials.CreateAsync(RpId);
// A fresh context imports the known credential and signs in with it.
await using var context = await Browser.NewContextAsync();
await context.Credentials.CreateAsync(known.RpId, new()
{
Id = known.Id,
UserHandle = known.UserHandle,
PrivateKey = known.PrivateKey,
PublicKey = known.PublicKey,
});
await context.Credentials.InstallAsync();
var page = await context.NewPageAsync();
await page.GotoAsync(Server.EmptyPage);
var result = await page.EvaluateAsync<JsonElement>(@"async ({ rpId, credentialId }) => {
const b64UrlToBytes = (s) => {
let str = s.replace(/-/g, '+').replace(/_/g, '/');
while (str.length % 4)
str += '=';
const bin = atob(str);
const u8 = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++)
u8[i] = bin.charCodeAt(i);
return u8;
};
const challenge = crypto.getRandomValues(new Uint8Array(32));
const cred = await navigator.credentials.get({
publicKey: {
challenge,
rpId,
allowCredentials: [{ type: 'public-key', id: b64UrlToBytes(credentialId) }],
userVerification: 'preferred',
},
});
const resp = cred.response;
return {
id: cred.id,
type: cred.type,
hasClientData: resp.clientDataJSON.byteLength > 0,
hasAuthData: resp.authenticatorData.byteLength > 0,
hasSignature: resp.signature.byteLength > 0,
authDataFlags: new Uint8Array(resp.authenticatorData)[32],
};
}", new { rpId = RpId, credentialId = known.Id });
Assert.AreEqual(known.Id, result.GetProperty("id").GetString());
Assert.AreEqual("public-key", result.GetProperty("type").GetString());
Assert.IsTrue(result.GetProperty("hasClientData").GetBoolean());
Assert.IsTrue(result.GetProperty("hasAuthData").GetBoolean());
Assert.IsTrue(result.GetProperty("hasSignature").GetBoolean());
// UP (0x01) | UV (0x04) = 0x05
Assert.AreEqual(0x05, result.GetProperty("authDataFlags").GetInt32() & 0x05);
// After the credential is deleted, the page can no longer authenticate with it.
await context.Credentials.DeleteAsync(known.Id);
Assert.IsEmpty(await context.Credentials.GetAsync());
var error = await page.EvaluateAsync<string>(@"async ({ rpId, credentialId }) => {
const b64UrlToBytes = (s) => {
let str = s.replace(/-/g, '+').replace(/_/g, '/');
while (str.length % 4)
str += '=';
const bin = atob(str);
const u8 = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++)
u8[i] = bin.charCodeAt(i);
return u8;
};
const challenge = crypto.getRandomValues(new Uint8Array(32));
try {
await navigator.credentials.get({
publicKey: {
challenge,
rpId,
allowCredentials: [{ type: 'public-key', id: b64UrlToBytes(credentialId) }],
},
});
return 'no-error';
} catch (e) {
return e.name;
}
}", new { rpId = RpId, credentialId = known.Id });
Assert.AreEqual("NotAllowedError", error);
}
[PlaywrightTest("browsercontext-webauthn.spec.ts", "should capture a page-created credential and reuse it in another context")]
public async Task ShouldCaptureAPageCreatedCredentialAndReuseItInAnotherContext()
{
// Setup context: the app registers a passkey via navigator.credentials.create().
await using var setupContext = await Browser.NewContextAsync();
await setupContext.Credentials.InstallAsync();
var setupPage = await setupContext.NewPageAsync();
await setupPage.GotoAsync(Server.EmptyPage);
var createdId = await setupPage.EvaluateAsync<string>(@"async ({ rpId }) => {
const challenge = crypto.getRandomValues(new Uint8Array(32));
const created = await navigator.credentials.create({
publicKey: {
challenge,
rp: { id: rpId, name: 'Test RP' },
user: { id: new Uint8Array([1, 2, 3, 4]), name: 'u', displayName: 'User' },
pubKeyCredParams: [{ type: 'public-key', alg: -7 }],
authenticatorSelection: { residentKey: 'required', userVerification: 'preferred' },
},
});
return created.id;
}", new { rpId = RpId });
var credentials = await setupContext.Credentials.GetAsync(new() { RpId = RpId });
Assert.AreEqual(1, credentials.Count);
var captured = credentials[0];
Assert.AreEqual(createdId, captured.Id);
StringAssert.IsMatch("^[A-Za-z0-9_-]+$", captured.PrivateKey);
StringAssert.IsMatch("^[A-Za-z0-9_-]+$", captured.PublicKey);
// Reuse the captured passkey in a fresh context and sign in with it.
await using var context = await Browser.NewContextAsync();
await context.Credentials.CreateAsync(captured.RpId, new()
{
Id = captured.Id,
UserHandle = captured.UserHandle,
PrivateKey = captured.PrivateKey,
PublicKey = captured.PublicKey,
});
await context.Credentials.InstallAsync();
var page = await context.NewPageAsync();
await page.GotoAsync(Server.EmptyPage);
var gotId = await page.EvaluateAsync<string>(@"async ({ rpId }) => {
const challenge = crypto.getRandomValues(new Uint8Array(32));
// No allowCredentials — relies on the re-seeded credential being discoverable.
const cred = await navigator.credentials.get({
publicKey: { challenge, rpId, userVerification: 'preferred' },
});
return cred.id;
}", new { rpId = RpId });
Assert.AreEqual(createdId, gotId);
}
}