|
| 1 | +import type { |
| 2 | + AttemptBiometricCredentialParams, |
| 3 | + BiometricCredentialJSON, |
| 4 | + BiometricCredentialJSONSnapshot, |
| 5 | + BiometricCredentialPlatform, |
| 6 | + BiometricCredentialResource, |
| 7 | + BiometricCredentialStatus, |
| 8 | + PrepareBiometricCredentialParams, |
| 9 | + TrustedDeviceAlgorithm, |
| 10 | + TrustedDeviceChallengeJSON, |
| 11 | + TrustedDeviceChallengeResource, |
| 12 | +} from '@clerk/shared/types'; |
| 13 | + |
| 14 | +import { unixEpochToDate } from '../../utils/date'; |
| 15 | +import { BaseResource } from './internal'; |
| 16 | +import { trustedDeviceChallengeFromJSON } from './TrustedDeviceChallenge'; |
| 17 | + |
| 18 | +const PATH_ROOT = '/me/biometric_credentials'; |
| 19 | + |
| 20 | +function toEnrollmentBody(params: PrepareBiometricCredentialParams | AttemptBiometricCredentialParams) { |
| 21 | + const { publicKeyJwk, ...rest } = params; |
| 22 | + return { |
| 23 | + ...rest, |
| 24 | + publicKeyJwk: typeof publicKeyJwk === 'string' ? publicKeyJwk : JSON.stringify(publicKeyJwk), |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +export class BiometricCredential extends BaseResource implements BiometricCredentialResource { |
| 29 | + id!: string; |
| 30 | + pathRoot = PATH_ROOT; |
| 31 | + platform!: BiometricCredentialPlatform; |
| 32 | + appIdentifier!: string; |
| 33 | + name: string | null = null; |
| 34 | + algorithm!: TrustedDeviceAlgorithm; |
| 35 | + status!: BiometricCredentialStatus; |
| 36 | + lastUsedAt: Date | null = null; |
| 37 | + revokedAt: Date | null = null; |
| 38 | + createdAt!: Date; |
| 39 | + updatedAt!: Date; |
| 40 | + |
| 41 | + public constructor(data: BiometricCredentialJSON | BiometricCredentialJSONSnapshot) { |
| 42 | + super(); |
| 43 | + this.fromJSON(data); |
| 44 | + } |
| 45 | + |
| 46 | + static async list(): Promise<BiometricCredentialResource[]> { |
| 47 | + const json = (await BaseResource._fetch({ path: PATH_ROOT, method: 'GET' })) |
| 48 | + ?.response as unknown as BiometricCredentialJSON[]; |
| 49 | + return (json || []).map(credential => new BiometricCredential(credential)); |
| 50 | + } |
| 51 | + |
| 52 | + static async prepare(params: PrepareBiometricCredentialParams): Promise<TrustedDeviceChallengeResource> { |
| 53 | + const json = ( |
| 54 | + await BaseResource._fetch({ |
| 55 | + path: `${PATH_ROOT}/prepare`, |
| 56 | + method: 'POST', |
| 57 | + body: toEnrollmentBody(params) as any, |
| 58 | + }) |
| 59 | + )?.response as unknown as TrustedDeviceChallengeJSON; |
| 60 | + return trustedDeviceChallengeFromJSON(json) as TrustedDeviceChallengeResource; |
| 61 | + } |
| 62 | + |
| 63 | + static async attempt(params: AttemptBiometricCredentialParams): Promise<BiometricCredentialResource> { |
| 64 | + const json = ( |
| 65 | + await BaseResource._fetch({ |
| 66 | + path: `${PATH_ROOT}/attempt`, |
| 67 | + method: 'POST', |
| 68 | + body: toEnrollmentBody(params) as any, |
| 69 | + }) |
| 70 | + )?.response as unknown as BiometricCredentialJSON; |
| 71 | + return new BiometricCredential(json); |
| 72 | + } |
| 73 | + |
| 74 | + static async revoke(biometricCredentialId: string): Promise<BiometricCredentialResource> { |
| 75 | + const json = ( |
| 76 | + await BaseResource._fetch({ |
| 77 | + path: `${PATH_ROOT}/${biometricCredentialId}`, |
| 78 | + method: 'DELETE', |
| 79 | + }) |
| 80 | + )?.response as unknown as BiometricCredentialJSON; |
| 81 | + return new BiometricCredential(json); |
| 82 | + } |
| 83 | + |
| 84 | + protected fromJSON(data: BiometricCredentialJSON | BiometricCredentialJSONSnapshot | null): this { |
| 85 | + if (!data) { |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + this.id = data.id; |
| 90 | + this.platform = data.platform; |
| 91 | + this.appIdentifier = data.app_identifier; |
| 92 | + this.name = data.name ?? null; |
| 93 | + this.algorithm = data.algorithm; |
| 94 | + this.status = data.status; |
| 95 | + this.lastUsedAt = data.last_used_at ? unixEpochToDate(data.last_used_at) : null; |
| 96 | + this.revokedAt = data.revoked_at ? unixEpochToDate(data.revoked_at) : null; |
| 97 | + this.createdAt = unixEpochToDate(data.created_at); |
| 98 | + this.updatedAt = unixEpochToDate(data.updated_at); |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public __internal_toSnapshot(): BiometricCredentialJSONSnapshot { |
| 103 | + return { |
| 104 | + object: 'trusted_device', |
| 105 | + id: this.id, |
| 106 | + platform: this.platform, |
| 107 | + app_identifier: this.appIdentifier, |
| 108 | + name: this.name, |
| 109 | + algorithm: this.algorithm, |
| 110 | + status: this.status, |
| 111 | + last_used_at: this.lastUsedAt?.getTime() ?? null, |
| 112 | + revoked_at: this.revokedAt?.getTime() ?? null, |
| 113 | + created_at: this.createdAt.getTime(), |
| 114 | + updated_at: this.updatedAt.getTime(), |
| 115 | + }; |
| 116 | + } |
| 117 | +} |
0 commit comments