sonr/app/gateway/islands/input_passkey.templ
Prad Nukala d69c2a9d53
feature/refactor ui (#1205)
* fix: correct HTTP error handling in gateway

* refactor: migrate database and ORM to internal modules

* feat: introduce taskfile build system for improved workflow management

* refactor: update taskfiles to use relative paths

* feat: add profile status field

* refactor: move rendering logic to context package

* fix: improve error handling in credentials retrieval

* refactor: optimize HTTP request handling in Wasm environment

* refactor: refactor config loading in motr command

* chore: add process-compose for service management

* chore: remove default task and update gum format command

* fix: update project dependencies

* refactor: improve code readability and maintainability

* refactor: consolidate error handling components

* refactor: update index handler to use new context package

* refactor: consolidate database scripts and move to deploy directory

* feat: Update flake.nix with development tools and environment configuration

* fix: ignore flake.lock file

* refactor: migrate build process to use taskfiles for improved modularity and maintainability

* refactor: improve GatewayContext and reorganize handlers

* refactor: Remove unused profile creation functions

* (chore): templ generation

* test: add test file for vaults.go

* maintenance: remove defunct Discord server link

* docs: update checks workflow documentation

* test: remove obsolete vaults test file

* refactor: move version bumping logic to release workflow
2024-12-22 17:01:11 -05:00

98 lines
3.0 KiB
Plaintext

package islands
templ InputPasskey(addr string, userHandle string, challenge string) {
<sl-button style="width: 100%;" onclick={ navigatorCredentialsCreate(addr, userHandle, challenge) }>
<sl-icon slot="prefix" name="passkey" library="sonr" style="font-size: 24px;" class="text-neutral-500"></sl-icon>
Register Passkey
</sl-button>
}
script navigatorCredentialsCreate(userId string, userHandle string, challenge string) {
const publicKey = {
challenge: Uint8Array.from(challenge, (c) => c.charCodeAt(0)),
rp: {
name: "Sonr.ID",
},
user: {
// Assuming that userId is ASCII-only
id: Uint8Array.from(userId, (c) => c.charCodeAt(0)),
name: userId,
displayName: userHandle,
},
pubKeyCredParams: [
{
type: "public-key",
alg: -7, // "ES256"
},
{
type: "public-key",
alg: -257, // "RS256"
},
],
authenticatorSelection: {
userVerification: "required",
residentKey: "required",
authenticatorAttachment: "platform",
},
timeout: 60000, // 1 minute
extensions: {
payment: {
isPayment: true,
},
largeBlob: {
supported: "preferred",
},
},
};
// Helper function to convert ArrayBuffer to Base64URL string
function arrayBufferToBase64URL(buffer) {
const bytes = new Uint8Array(buffer);
let str = '';
bytes.forEach(byte => { str += String.fromCharCode(byte) });
return btoa(str)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
navigator.credentials
.create({ publicKey })
.then((newCredentialInfo) => {
if (!(newCredentialInfo instanceof PublicKeyCredential)) {
throw new Error('Received credential is not a PublicKeyCredential');
}
const response = newCredentialInfo.response;
if (!(response instanceof AuthenticatorAttestationResponse)) {
throw new Error('Response is not an AuthenticatorAttestationResponse');
}
// Convert the credential data to a cross-platform compatible format
const credentialJSON = {
id: newCredentialInfo.id,
rawId: arrayBufferToBase64URL(newCredentialInfo.rawId),
type: newCredentialInfo.type,
authenticatorAttachment: newCredentialInfo.authenticatorAttachment || null,
transports: Array.isArray(response.getTransports) ? response.getTransports() : [],
clientExtensionResults: newCredentialInfo.getClientExtensionResults(),
response: {
attestationObject: arrayBufferToBase64URL(response.attestationObject),
clientDataJSON: arrayBufferToBase64URL(response.clientDataJSON)
}
};
// Set the form value with the stringified credential data
const credential = document.getElementById('credential-data');
credential.value = JSON.stringify(credentialJSON);
// Submit the form
const form = document.getElementById('passkey-form');
form.submit();
})
.catch((err) => {
console.error('Passkey creation failed:', err);
alert(`Failed to create passkey: ${err.message || 'Unknown error'}`);
});
}