|
| 1 | +import type { PasskeyJSON, PasskeyResource, PasskeyVerificationResource } from '@clerk/types'; |
| 2 | + |
| 3 | +import { unixEpochToDate } from '../../utils/date'; |
| 4 | +import type { PublicKeyCredentialWithAuthenticatorAttestationResponse } from '../../utils/passkeys'; |
| 5 | +import { |
| 6 | + isWebAuthnPlatformAuthenticatorSupported, |
| 7 | + serializePublicKeyCredential, |
| 8 | + webAuthnCreateCredential, |
| 9 | +} from '../../utils/passkeys'; |
| 10 | +import { BaseResource, ClerkRuntimeError, PasskeyVerification } from './internal'; |
| 11 | + |
| 12 | +export class Passkey extends BaseResource implements PasskeyResource { |
| 13 | + id!: string; |
| 14 | + pathRoot = '/me/passkeys'; |
| 15 | + credentialId: string | null = null; |
| 16 | + verification: PasskeyVerificationResource | null = null; |
| 17 | + name: string | null = null; |
| 18 | + lastUsedAt: Date | null = null; |
| 19 | + createdAt!: Date; |
| 20 | + updatedAt!: Date; |
| 21 | + |
| 22 | + public constructor(data: PasskeyJSON) { |
| 23 | + super(); |
| 24 | + this.fromJSON(data); |
| 25 | + } |
| 26 | + |
| 27 | + private static async create() { |
| 28 | + return BaseResource._fetch({ |
| 29 | + path: `/me/passkeys`, |
| 30 | + method: 'POST', |
| 31 | + }).then(res => new Passkey(res?.response as PasskeyJSON)); |
| 32 | + } |
| 33 | + |
| 34 | + private static async attemptVerification( |
| 35 | + passkeyId: string, |
| 36 | + credential: PublicKeyCredentialWithAuthenticatorAttestationResponse, |
| 37 | + ) { |
| 38 | + const jsonPublicKeyCredential = serializePublicKeyCredential(credential); |
| 39 | + return BaseResource._fetch({ |
| 40 | + path: `/me/passkeys/${passkeyId}/attempt_verification`, |
| 41 | + method: 'POST', |
| 42 | + body: { strategy: 'passkey', publicKeyCredential: JSON.stringify(jsonPublicKeyCredential) } as any, |
| 43 | + }).then(res => new Passkey(res?.response as PasskeyJSON)); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * TODO-PASSKEYS: Implement this later |
| 48 | + * |
| 49 | + * GET /v1/me/passkeys |
| 50 | + */ |
| 51 | + static async get() {} |
| 52 | + |
| 53 | + /** |
| 54 | + * Developers should not be able to create a new Passkeys from an already instanced object |
| 55 | + */ |
| 56 | + static async registerPasskey() { |
| 57 | + /** |
| 58 | + * The UI should always prevent from this method being called if WebAuthn is not supported. |
| 59 | + * As a precaution we need to check if WebAuthn is supported. |
| 60 | + */ |
| 61 | + |
| 62 | + /** |
| 63 | + * TODO-PASSKEYS: First simply check if webauthn is supported and check for this only when |
| 64 | + * publicKey?.authenticatorSelection.authenticatorAttachment === 'platform' |
| 65 | + */ |
| 66 | + if (!(await isWebAuthnPlatformAuthenticatorSupported())) { |
| 67 | + throw new ClerkRuntimeError('Platform authenticator is not supported', { |
| 68 | + code: 'passkeys_unsupported_platform_authenticator', |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + const passkey = await this.create(); |
| 73 | + |
| 74 | + const { verification } = passkey; |
| 75 | + |
| 76 | + const publicKey = verification?.publicKey; |
| 77 | + |
| 78 | + // This should never occur such a fail-safe |
| 79 | + if (!publicKey) { |
| 80 | + // TODO-PASSKEYS: Implement this later |
| 81 | + throw 'Missing key'; |
| 82 | + } |
| 83 | + |
| 84 | + // Invoke the WebAuthn create() method. |
| 85 | + const { publicKeyCredential, error } = await webAuthnCreateCredential(publicKey); |
| 86 | + |
| 87 | + if (!publicKeyCredential) { |
| 88 | + throw error; |
| 89 | + } |
| 90 | + |
| 91 | + return this.attemptVerification(passkey.id, publicKeyCredential); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * TODO-PASSKEYS: Implement this later |
| 96 | + * |
| 97 | + * PATCH /v1/me/passkeys/{passkeyIdentificationID} |
| 98 | + */ |
| 99 | + update = (): Promise<PasskeyResource> => this._basePatch(); |
| 100 | + |
| 101 | + /** |
| 102 | + * TODO-PASSKEYS: Implement this later |
| 103 | + * |
| 104 | + * DELETE /v1/me/passkeys/{passkeyIdentificationID} |
| 105 | + */ |
| 106 | + destroy = (): Promise<void> => this._baseDelete(); |
| 107 | + |
| 108 | + /** |
| 109 | + * TODO-PASSKEYS: Implement this later |
| 110 | + * |
| 111 | + * GET /v1/me/passkeys/{passkeyIdentificationID} |
| 112 | + */ |
| 113 | + reload = () => this._baseGet(); |
| 114 | + |
| 115 | + protected fromJSON(data: PasskeyJSON | null): this { |
| 116 | + if (!data) { |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + this.id = data.id; |
| 121 | + this.credentialId = data.credential_id; |
| 122 | + this.name = data.name; |
| 123 | + this.lastUsedAt = data.last_used_at ? unixEpochToDate(data.last_used_at) : null; |
| 124 | + this.createdAt = unixEpochToDate(data.created_at); |
| 125 | + this.updatedAt = unixEpochToDate(data.updated_at); |
| 126 | + |
| 127 | + if (data.verification) { |
| 128 | + this.verification = new PasskeyVerification(data.verification); |
| 129 | + } |
| 130 | + return this; |
| 131 | + } |
| 132 | +} |
0 commit comments