33 lines
985 B
JavaScript
33 lines
985 B
JavaScript
// WebAuthn utility functions
|
|
|
|
// Convert Base64URL string to ArrayBuffer
|
|
export function base64urlToBuffer(base64url) {
|
|
const base64 = base64url.replace(/-/g, '+').replace(/_/g, '/')
|
|
const padLen = (4 - (base64.length % 4)) % 4
|
|
const padded = base64 + '='.repeat(padLen)
|
|
const binary = atob(padded)
|
|
const buffer = new ArrayBuffer(binary.length)
|
|
const bytes = new Uint8Array(buffer)
|
|
for (let i = 0; i < binary.length; i++) {
|
|
bytes[i] = binary.charCodeAt(i)
|
|
}
|
|
return buffer
|
|
}
|
|
|
|
// Convert ArrayBuffer to Base64URL string
|
|
export function bufferToBase64url(buffer) {
|
|
const bytes = new Uint8Array(buffer)
|
|
let binary = ''
|
|
for (let i = 0; i < bytes.byteLength; i++) {
|
|
binary += String.fromCharCode(bytes[i])
|
|
}
|
|
return btoa(binary)
|
|
.replace(/\+/g, '-')
|
|
.replace(/\//g, '_')
|
|
.replace(/=/g, '')
|
|
}
|
|
|
|
// Get CSRF token from meta tag
|
|
export function getCsrfToken() {
|
|
return document.querySelector("meta[name='csrf-token']").getAttribute('content')
|
|
}
|