|
| 1 | +import { |
| 2 | + AuthenticationResponseJSON, |
| 3 | + PublicKeyCredentialCreationOptionsJSON, |
| 4 | + PublicKeyCredentialRequestOptionsJSON, |
| 5 | + RegistrationResponseJSON, |
| 6 | +} from "@simplewebauthn/types"; |
| 7 | +import {buildHeaders, handleTokenExpired} from "./helpers"; |
| 8 | +import {AddAuthenticatorResponse, ErrorResponse, VerifyResponse} from "./types/passkey"; |
| 9 | +import {ApiClientOptions} from "./types/shared"; |
| 10 | + |
| 11 | +export class SecurityKeyApiClient { |
| 12 | + tenantId: string; |
| 13 | + baseUrl: string; |
| 14 | + onTokenExpired?: () => void; |
| 15 | + |
| 16 | + constructor({baseUrl, tenantId, onTokenExpired}: ApiClientOptions) { |
| 17 | + this.tenantId = tenantId; |
| 18 | + this.baseUrl = baseUrl; |
| 19 | + this.onTokenExpired = onTokenExpired; |
| 20 | + } |
| 21 | + |
| 22 | + async registrationOptions({token}: {token: string}): Promise<PublicKeyCredentialCreationOptionsJSON | ErrorResponse> { |
| 23 | + const response = await fetch(`${this.baseUrl}/client/user-authenticators/security-key/registration-options`, { |
| 24 | + method: "POST", |
| 25 | + headers: buildHeaders({token, tenantId: this.tenantId}), |
| 26 | + body: JSON.stringify({}), |
| 27 | + }); |
| 28 | + |
| 29 | + const responseJson = await response.json(); |
| 30 | + |
| 31 | + handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); |
| 32 | + |
| 33 | + return responseJson; |
| 34 | + } |
| 35 | + |
| 36 | + async authenticationOptions({ |
| 37 | + token, |
| 38 | + }: { |
| 39 | + token?: string; |
| 40 | + }): Promise<PublicKeyCredentialRequestOptionsJSON | ErrorResponse> { |
| 41 | + const response = await fetch(`${this.baseUrl}/client/user-authenticators/security-key/authentication-options`, { |
| 42 | + method: "POST", |
| 43 | + headers: buildHeaders({token, tenantId: this.tenantId}), |
| 44 | + body: JSON.stringify({}), |
| 45 | + }); |
| 46 | + |
| 47 | + const responseJson = await response.json(); |
| 48 | + |
| 49 | + handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); |
| 50 | + |
| 51 | + return responseJson; |
| 52 | + } |
| 53 | + |
| 54 | + async addAuthenticator({ |
| 55 | + token, |
| 56 | + registrationCredential, |
| 57 | + }: { |
| 58 | + token: string; |
| 59 | + registrationCredential: RegistrationResponseJSON; |
| 60 | + }): Promise<AddAuthenticatorResponse | ErrorResponse> { |
| 61 | + const response = await fetch(`${this.baseUrl}/client/user-authenticators/security-key`, { |
| 62 | + method: "POST", |
| 63 | + headers: buildHeaders({token, tenantId: this.tenantId}), |
| 64 | + body: JSON.stringify(registrationCredential), |
| 65 | + }); |
| 66 | + |
| 67 | + const responseJson = await response.json(); |
| 68 | + |
| 69 | + handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); |
| 70 | + |
| 71 | + return responseJson; |
| 72 | + } |
| 73 | + |
| 74 | + async verify({ |
| 75 | + token, |
| 76 | + authenticationCredential, |
| 77 | + }: { |
| 78 | + token?: string; |
| 79 | + authenticationCredential: AuthenticationResponseJSON; |
| 80 | + }): Promise<VerifyResponse | ErrorResponse> { |
| 81 | + const response = await fetch(`${this.baseUrl}/client/verify/security-key`, { |
| 82 | + method: "POST", |
| 83 | + headers: buildHeaders({token, tenantId: this.tenantId}), |
| 84 | + body: JSON.stringify(authenticationCredential), |
| 85 | + }); |
| 86 | + |
| 87 | + const responseJson = await response.json(); |
| 88 | + |
| 89 | + handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired}); |
| 90 | + |
| 91 | + return responseJson; |
| 92 | + } |
| 93 | +} |
0 commit comments