| 1 |
<!-- |
| 2 |
// Copyright 2024 OpenPubkey |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
// SPDX-License-Identifier: Apache-2.0 |
| 17 |
--> |
| 18 |
|
| 19 |
<!DOCTYPE html> |
| 20 |
<html lang="en"> |
| 21 |
<head> |
| 22 |
</head> |
| 23 |
<body> |
| 24 |
<div id="status">Authenticating with MFA...</div> |
| 25 |
|
| 26 |
<meta charset="UTF-8"> |
| 27 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 28 |
<script> |
| 29 |
// Convert a Base64 URL-encoded string to an ArrayBuffer |
| 30 |
function base64UrlDecode(value) { |
| 31 |
let avlue = value.replace(/\-/g, '+') // Replace '-' with '+' |
| 32 |
.replace(/\_/g, '/') // Replace '_' with '/' |
| 33 |
.replace(/=+$/, ''); // Remove padding |
| 34 |
return Uint8Array.from(atob(avlue), c => c.charCodeAt(0)); |
| 35 |
} |
| 36 |
|
| 37 |
// Convert an ArrayBuffer to Base64 URL-encoded string |
| 38 |
function base64UrlEncode(buffer) { |
| 39 |
let binary = ''; |
| 40 |
let bytes = new Uint8Array(buffer); |
| 41 |
let len = bytes.byteLength; |
| 42 |
for (let i = 0; i < len; i++) { |
| 43 |
binary += String.fromCharCode(bytes[i]); |
| 44 |
} |
| 45 |
|
| 46 |
return window.btoa(binary) |
| 47 |
.replace(/\+/g, '-') // Replace '+' with '-' |
| 48 |
.replace(/\//g, '_') // Replace '/' with '_' |
| 49 |
.replace(/=+$/, ''); // Remove padding |
| 50 |
} |
| 51 |
|
| 52 |
async function register(authid) { |
| 53 |
let response = await fetch(`/register/begin?authid=${authid}`, { |
| 54 |
method: 'GET' |
| 55 |
}); |
| 56 |
|
| 57 |
if (!response.ok) { |
| 58 |
throw new Error(`HTTP error! status: ${response.status}`); |
| 59 |
} |
| 60 |
|
| 61 |
let data = await response.json(); |
| 62 |
data.publicKey.user.id = base64UrlDecode(data.publicKey.user.id); |
| 63 |
data.publicKey.challenge = base64UrlDecode(data.publicKey.challenge) |
| 64 |
|
| 65 |
credentials = await navigator.credentials.create(data); |
| 66 |
let credentialsForServer = { |
| 67 |
id: credentials.id, |
| 68 |
type: credentials.type, |
| 69 |
rawId: base64UrlEncode(credentials.rawId), |
| 70 |
response: { |
| 71 |
clientDataJSON: base64UrlEncode(credentials.response.clientDataJSON), |
| 72 |
attestationObject: base64UrlEncode(credentials.response.attestationObject) |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
// Send credentials back to server to save |
| 77 |
let res = await fetch(`/register/finish?authid=${authid}`, { |
| 78 |
method: 'POST', |
| 79 |
headers: { |
| 80 |
'Content-Type': 'application/json' |
| 81 |
}, |
| 82 |
body: JSON.stringify(credentialsForServer) |
| 83 |
}); |
| 84 |
|
| 85 |
if (!res.ok) { |
| 86 |
throw new Error(`HTTP error! status: ${res.status}`); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
async function login(authid) { |
| 91 |
let response = await fetch(`/login/begin?authid=${authid}`, { |
| 92 |
method: 'GET' |
| 93 |
}); |
| 94 |
|
| 95 |
if (!response.ok) { |
| 96 |
throw new Error(`HTTP error! status: ${response.status}`); |
| 97 |
} |
| 98 |
|
| 99 |
let data = await response.json(); |
| 100 |
data.publicKey.challenge = base64UrlDecode(data.publicKey.challenge) |
| 101 |
data.publicKey.allowCredentials.forEach(function (listItem) { |
| 102 |
listItem.id = base64UrlDecode(listItem.id) |
| 103 |
}); |
| 104 |
|
| 105 |
let assertion = await navigator.credentials.get(data); |
| 106 |
|
| 107 |
// Send credentials back to server to save |
| 108 |
let res = await fetch(`/login/finish?authid=${authid}`, { |
| 109 |
method: 'POST', |
| 110 |
headers: { |
| 111 |
'Content-Type': 'application/json' |
| 112 |
}, |
| 113 |
body: JSON.stringify({ |
| 114 |
id: assertion.id, |
| 115 |
rawId: base64UrlEncode(assertion.rawId), |
| 116 |
type: assertion.type, |
| 117 |
response: { |
| 118 |
authenticatorData: base64UrlEncode(assertion.response.authenticatorData), |
| 119 |
clientDataJSON: base64UrlEncode(assertion.response.clientDataJSON), |
| 120 |
signature: base64UrlEncode(assertion.response.signature), |
| 121 |
userHandle: base64UrlEncode(assertion.response.userHandle), |
| 122 |
} |
| 123 |
}) |
| 124 |
}); |
| 125 |
|
| 126 |
if (!res.ok) { |
| 127 |
throw new Error(`HTTP error! status: ${res.status}`); |
| 128 |
} |
| 129 |
|
| 130 |
let authcodeData = await res.json(); |
| 131 |
window.location.replace(authcodeData.redirect_uri); |
| 132 |
} |
| 133 |
|
| 134 |
document.addEventListener('DOMContentLoaded', async (event) => { |
| 135 |
const statusDiv = document.getElementById('status'); |
| 136 |
|
| 137 |
const queryString = window.location.search; |
| 138 |
let searchParams = new URLSearchParams(queryString); |
| 139 |
const authid = searchParams.get('authid') |
| 140 |
|
| 141 |
const response = await fetch(`/check-registration?authid=${authid}`, { |
| 142 |
method: 'GET', |
| 143 |
}); |
| 144 |
|
| 145 |
let data = await response.json(); |
| 146 |
|
| 147 |
if (!data.isRegistered) { |
| 148 |
try { |
| 149 |
statusDiv.textContent = "Registering user with WebAuthn..."; |
| 150 |
await register(authid); |
| 151 |
} catch(error) { |
| 152 |
statusDiv.textContent = `Error during registration: ${error}`; |
| 153 |
return |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
try { |
| 158 |
statusDiv.textContent = "Authenticating user with WebAuthn..."; |
| 159 |
let response = await login(authid); |
| 160 |
statusDiv.textContent = "MFA authentication complete!"; |
| 161 |
} catch(error) { |
| 162 |
statusDiv.textContent = `Error during login: ${error}`; |
| 163 |
return |
| 164 |
} |
| 165 |
}) |
| 166 |
</script> |
| 167 |
</body> |
| 168 |
</html> |