91 lines
3.0 KiB
JavaScript
91 lines
3.0 KiB
JavaScript
|
(function(HydraWebAuthn) {
|
||
|
|
||
|
HydraWebAuthn.base64ToArrayBuffer = function(base64) {
|
||
|
var binaryString = HydraWebAuthn.fromBase64URL(base64);
|
||
|
var bytes = new Uint8Array(binaryString.length);
|
||
|
for (var i = 0; i < binaryString.length; i++) {
|
||
|
bytes[i] = binaryString.charCodeAt(i);
|
||
|
}
|
||
|
return bytes.buffer;
|
||
|
}
|
||
|
|
||
|
HydraWebAuthn.arrayBufferToBase64 = function( buffer ) {
|
||
|
var binary = '';
|
||
|
var bytes = new Uint8Array( buffer );
|
||
|
var len = bytes.byteLength;
|
||
|
for (var i = 0; i < len; i++) {
|
||
|
binary += String.fromCharCode( bytes[ i ] );
|
||
|
}
|
||
|
return window.btoa( binary );
|
||
|
}
|
||
|
|
||
|
HydraWebAuthn.toJSONCredentials = function(credentials) {
|
||
|
return {
|
||
|
id: credentials.id,
|
||
|
rawId: HydraWebAuthn.toBase64URL(credentials.rawId),
|
||
|
authenticatorAttachment: credentials.authenticatorAttachment,
|
||
|
type: "public-key",
|
||
|
clientExtensionResults: (() => {
|
||
|
const results = credentials.getClientExtensionResults();
|
||
|
return Object.keys(results).reduce((json, key) => {
|
||
|
json[key] = HydraWebAuthn.urlsafe(HydraWebAuthn.arrayBufferToBase64(results[key]))
|
||
|
return json;
|
||
|
}, {})
|
||
|
})(),
|
||
|
response: {
|
||
|
attestationObject: HydraWebAuthn.urlsafe(HydraWebAuthn.arrayBufferToBase64(credentials.response.attestationObject)),
|
||
|
clientDataJSON: HydraWebAuthn.urlsafe(HydraWebAuthn.arrayBufferToBase64(credentials.response.clientDataJSON))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
HydraWebAuthn.toJSONAssertion = function(assertion) {
|
||
|
return {
|
||
|
id: assertion.id,
|
||
|
rawId: HydraWebAuthn.urlsafe(HydraWebAuthn.arrayBufferToBase64(assertion.rawId)),
|
||
|
type: "public-key",
|
||
|
response: {
|
||
|
authenticatorData: HydraWebAuthn.urlsafe(HydraWebAuthn.arrayBufferToBase64(assertion.response.authenticatorData)),
|
||
|
clientDataJSON: HydraWebAuthn.urlsafe(HydraWebAuthn.arrayBufferToBase64(assertion.response.clientDataJSON)),
|
||
|
signature: HydraWebAuthn.urlsafe(HydraWebAuthn.arrayBufferToBase64(assertion.response.signature)),
|
||
|
userHandle: assertion.response.userHandle
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
HydraWebAuthn.base64 = function(str) {
|
||
|
return HydraWebAuthn.bytesToBase64(new TextEncoder().encode(str));
|
||
|
}
|
||
|
|
||
|
HydraWebAuthn.toBase64URL = function(str) {
|
||
|
return HydraWebAuthn.urlsafe(HydraWebAuthn.base64(str));
|
||
|
}
|
||
|
|
||
|
HydraWebAuthn.fromBase64URL = function(str) {
|
||
|
str = str
|
||
|
.replace(/-/g, '+')
|
||
|
.replace(/_/g, '/');
|
||
|
|
||
|
var pad = str.length % 4;
|
||
|
if(pad) {
|
||
|
if(pad === 1) {
|
||
|
throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
|
||
|
}
|
||
|
str += new Array(5-pad).join('=');
|
||
|
}
|
||
|
|
||
|
return atob(str);
|
||
|
}
|
||
|
|
||
|
HydraWebAuthn.urlsafe = function(base64) {
|
||
|
return base64.replace(/\+/g, '-')
|
||
|
.replace(/\//g, '_')
|
||
|
.replace(/=+$/, '')
|
||
|
}
|
||
|
|
||
|
HydraWebAuthn.bytesToBase64 = function(bytes) {
|
||
|
const binString = String.fromCodePoint(...bytes);
|
||
|
return btoa(binString);
|
||
|
}
|
||
|
|
||
|
}(window.HydraWebAuthn = {}))
|