Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript">
- const translationString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
- function webSafeBase64ToBase64(webSafeBase64) {
- base64 = webSafeBase64.replaceAll('-', '+').replaceAll('_', '/');
- while (base64.length % 4)
- base64 += '=';
- return base64;
- };
- function base64ToWebSafeBase64(base64) {
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");;
- };
- function unit8ArrayToUrlSafeBase64 (unit8Array) {
- const extraBytes = unit8Array.length % 3
- let output = ''
- let buffer
- function encode (pos) {
- return translationString.charAt(pos)
- }
- function threeBytesToBase64 (threeBytes) {
- return encode(threeBytes >> 18) + encode(threeBytes >> 12 & 0x3F) + encode(threeBytes >> 6 & 0x3F) + encode(threeBytes & 0x3F)
- }
- for (let i = 0, length = unit8Array.length - extraBytes; i < length; i += 3) {
- buffer = (unit8Array[i] << 16) + (unit8Array[i + 1] << 8) + (unit8Array[i + 2])
- output += threeBytesToBase64(buffer)
- }
- // pad the end with zeros, but make sure to not forget the extra bytes
- switch (extraBytes) {
- case 1:
- buffer = unit8Array[unit8Array.length - 1]
- output = output + encode(buffer >> 2) + encode((buffer << 4) & 0x3F) + '=='
- break
- case 2:
- buffer = (unit8Array[unit8Array.length - 2] << 8) + (unit8Array[unit8Array.length - 1])
- output = output + encode(buffer >> 10) + encode((buffer >> 4) & 0x3F) + encode((buffer << 2) & 0x3F) + '='
- break
- default:
- break
- }
- return base64ToWebSafeBase64(output);
- }
- function webSafeBase64ToBase64(webSafeBase64) {
- let base64 = webSafeBase64.replaceAll('-', '+').replaceAll('_', '/');
- while (base64.length % 4)
- base64 += '=';
- return base64;
- };
- function submitWebAuthnAssertionAsJson(webAuthnAssertionAsJson) {
- const fudiscrForm = document.getElementById("fudiscr-form");
- fudiscrForm._submit_function_ = fudiscrForm.submit;
- fudiscrForm.setAttribute("method", "POST");
- fudiscrForm.setAttribute("action", "$flowExecutionUrl");
- const fudisWebAuthnAssertionAsJsonInputField = document.createElement("input");
- fudisWebAuthnAssertionAsJsonInputField.setAttribute("type", "hidden");
- fudisWebAuthnAssertionAsJsonInputField.setAttribute("name", "fudis_web_authn_assertion_input")
- fudisWebAuthnAssertionAsJsonInputField.setAttribute("id", "fudis_web_authn_assertion_input");
- fudisWebAuthnAssertionAsJsonInputField.setAttribute("value", base64ToWebSafeBase64(btoa(webAuthnAssertionAsJson)));
- fudiscrForm.appendChild(fudisWebAuthnAssertionAsJsonInputField);
- const eventIdField = document.createElement("input");
- eventIdField.setAttribute("name", "_eventId_proceed");
- fudiscrForm.appendChild(eventIdField);
- fudiscrForm._submit_function_();
- }
- function getWebAuthnAssertion() {
- const publicKeyCredentialRequestOptions = {
- allowCredentials: [
- #foreach ($webAuthnDescriptor in $webAuthnDescriptors)
- {
- id: Uint8Array.from(atob(webSafeBase64ToBase64("$webAuthnDescriptor.getId()")), c => c.charCodeAt(0)),
- type: "$webAuthnDescriptor.getCredentialType()",
- transports: [
- #foreach ($transportTypes in $webAuthnDescriptor.getTransportTypes())
- "$transportTypes"#if($foreach.hasNext()),#end
- #end
- ]
- }#if($foreach.hasNext()),#end
- #end
- ],
- challenge: Uint8Array.from(atob(webSafeBase64ToBase64("$webAuthnChallenge")), c => c.charCodeAt(0)),
- rpId: "$webAuthnRpId",
- userVerification: "$webAuthnUserVerificationType",
- timeout: $webAuthnTimeout
- }
- navigator
- .credentials
- .get({publicKey: publicKeyCredentialRequestOptions})
- .then(function (webAuthnAssertion) {
- let webAuthnAssertionForJson = new Object();
- webAuthnAssertionForJson.authenticatorData = unit8ArrayToUrlSafeBase64(new Uint8Array(webAuthnAssertion.response.authenticatorData));
- webAuthnAssertionForJson.clientDataJson = unit8ArrayToUrlSafeBase64(new Uint8Array(webAuthnAssertion.response.clientDataJSON));
- webAuthnAssertionForJson.rawId = unit8ArrayToUrlSafeBase64(new Uint8Array(webAuthnAssertion.rawId));
- webAuthnAssertionForJson.signature = unit8ArrayToUrlSafeBase64(new Uint8Array(webAuthnAssertion.response.signature));
- webAuthnAssertionForJson.userHandle = unit8ArrayToUrlSafeBase64(new Uint8Array(webAuthnAssertion.response.userHandle));
- submitWebAuthnAssertionAsJson(JSON.stringify(webAuthnAssertionForJson));
- })
- .catch(function (error) {
- if (error.name == "AbortError") {
- document.getElementById("get_web_authn_assertion").childNodes[0].nodeValue = '#springMessageText(
- "fudiscr.view.insert-response.web_authn.next" ,"Validate WebAuthn-Token")';
- } else {
- document.getElementById("get_web_authn_assertion").childNodes[0].nodeValue = error.name + ' - #springMessageText(
- "fudiscr.view.insert-response.web_authn.error.please-try-again" ,"Error in validation of WebAuthn token, please try again.")';
- console.log(error.name + " - " + error.message);
- }
- });
- }
- </script>
Add Comment
Please, Sign In to add comment