-
Notifications
You must be signed in to change notification settings - Fork 1
/
username-email.html
148 lines (131 loc) · 5.19 KB
/
username-email.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Username + Email</title>
<script src="webauthn.js"></script>
</head>
<body>
<h1>Username + Email</h1>
<a href="/index.html">
<button>back</button>
</a>
<div class="container">
<form id="signupForm">
<h2>Sign-up</h2>
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" autocomplete="username webauthn"><br>
<label for="signUpEmail">Email:</label><br>
<input type="email" id="signUpEmail" name="email" autocomplete="email webauthn"><br>
<button type="submit" id="register">Sign-up</button>
</form>
<form id="loginForm">
<h2>Login</h2>
<label for="loginUsername">Username:</label><br>
<input type="text" id="loginUsername" name="username" autocomplete="username webauthn"><br>
<label for="loginEmail">Email:</label><br>
<input type="email" id="loginEmail" name="email" autocomplete="email webauthn"><br>
<button type="submit" id="login">Login</button>
</form>
<div id="status"></div>
</div>
<script>
async function register() {
const username = document.getElementById('signUpEmail').value.trim();
if (!username) {
updateStatus('Username is required for registration');
return;
}
// Simulate getting challenge and other data from server
const publicKeyCredentialCreationOptions = {
rp: {
name: "Example Corp",
id: origin.hostname,
},
user: {
id: new TextEncoder().encode(username),
name: username,
displayName: username
},
challenge: Uint8Array.from("randomChallengeString", c => c.charCodeAt(0)),
pubKeyCredParams: [
{type: "public-key", alg: -7}, // ES256 algorithm
],
authenticatorSelection: {
authenticatorAttachment: "platform",
requireResidentKey: false,
userVerification: "preferred"
},
timeout: 60000,
attestation: "direct"
};
try {
const credential = await navigator.credentials.create({publicKey: publicKeyCredentialCreationOptions});
console.log("Credential Created", credential);
updateStatus(`Registration successful for ${username}`);
// Here, send the `credential` object to the server for verification and storage
} catch (err) {
console.error("Registration error", err);
updateStatus(`Registration failed: ${err.message}`);
}
}
document.addEventListener('DOMContentLoaded', async () => {
// Attempt automated login check after page loads
try {
conditionalMediationLogin();
} catch (err) {
console.error("Auto login check failed", err);
updateStatus('Failed to check for automatic login');
}
});
async function regularLogin() {
const username = document.getElementById('username').value.trim();
if (!username) {
updateStatus('Username is required for login');
return;
}
// Adjusted publicKeyCredentialRequestOptions for conditional UI
const publicKeyCredentialRequestOptions = {
challenge: Uint8Array.from("randomChallengeString", c => c.charCodeAt(0)),
timeout: 60000,
userVerification: "preferred",
};
try {
const assertion = await navigator.credentials.get({publicKey: publicKeyCredentialRequestOptions});
console.log("Assertion obtained", assertion);
updateStatus(`Login successful for ${username}`);
// Here, send the `assertion` object to the server for verification
} catch (err) {
console.error("Login error", err);
updateStatus(`Login failed: ${err.message}`);
}
}
async function conditionalMediationLogin() {
// Adjusted publicKeyCredentialRequestOptions for conditional UI
const publicKeyCredentialRequestOptions = {
challenge: Uint8Array.from("randomChallengeString", c => c.charCodeAt(0)),
timeout: 60000,
userVerification: "preferred",
};
try {
const assertion = await navigator.credentials.get({
publicKey: publicKeyCredentialRequestOptions,
mediation: "conditional"
});
console.log("Assertion obtained", assertion);
updateStatus(`Login successful`);
// Here, send the `assertion` object to the server for verification
} catch (err) {
console.error("Login error", err);
updateStatus(`Login failed: ${err.message}`);
}
}
function updateStatus(message) {
document.getElementById('status').innerText = message;
}
document.getElementById('register').addEventListener('click', register);
document.getElementById('login').addEventListener('click', regularLogin);
</script>
</body>
</html>