Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate server validation errors #58

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
hwhmeikle committed Jun 17, 2024
commit 40c747287e02474b9b0ade7afe96784d8ce74b33
11 changes: 5 additions & 6 deletions src/api/passkey-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
AddAuthenticatorRequest,
AddAuthenticatorResponse,
ApiError,
AuthenticationOptsRequest,
AuthenticationOptsResponse,
AuthsignalResponse,
Expand Down Expand Up @@ -31,7 +30,7 @@ export class PasskeyApiClient {
token,
username,
authenticatorAttachment,
}: {token: string} & RegistrationOptsRequest): Promise<RegistrationOptsResponse> {
}: {token: string} & RegistrationOptsRequest): Promise<AuthsignalResponse<RegistrationOptsResponse>> {
const body: RegistrationOptsRequest = Boolean(authenticatorAttachment)
? {username, authenticatorAttachment}
: {username};
Expand All @@ -48,7 +47,7 @@ export class PasskeyApiClient {
async authenticationOptions({
token,
challengeId,
}: {token?: string} & AuthenticationOptsRequest): Promise<AuthenticationOptsResponse> {
}: {token?: string} & AuthenticationOptsRequest): Promise<AuthsignalResponse<AuthenticationOptsResponse>> {
const body: AuthenticationOptsRequest = {challengeId};

const response = fetch(`${this.baseUrl}/client/user-authenticators/passkey/authentication-options`, {
Expand All @@ -64,7 +63,7 @@ export class PasskeyApiClient {
token,
challengeId,
registrationCredential,
}: {token: string} & AddAuthenticatorRequest): Promise<AddAuthenticatorResponse> {
}: {token: string} & AddAuthenticatorRequest): Promise<AuthsignalResponse<AddAuthenticatorResponse>> {
const body: AddAuthenticatorRequest = {
challengeId,
registrationCredential,
Expand Down Expand Up @@ -96,7 +95,7 @@ export class PasskeyApiClient {
return (await response).json();
}

async getPasskeyAuthenticator(credentialId: string): Promise<PasskeyAuthenticatorResponse> {
async getPasskeyAuthenticator(credentialId: string): Promise<AuthsignalResponse<PasskeyAuthenticatorResponse>> {
const response = await fetch(`${this.baseUrl}/client/user-authenticators/passkey?credentialId=${credentialId}`, {
method: "GET",
headers: this.buildHeaders(),
Expand All @@ -109,7 +108,7 @@ export class PasskeyApiClient {
return response.json();
}

async challenge(action: string): Promise<ChallengeResponse> {
async challenge(action: string): Promise<AuthsignalResponse<ChallengeResponse>> {
const response = fetch(`${this.baseUrl}/client/challenge`, {
method: "POST",
headers: this.buildHeaders(),
Expand Down
9 changes: 5 additions & 4 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export type ChallengeResponse = {
challengeId: string;
};

export type AuthsignalResponse<T> = {
data: T;
error?: string;
};
export type AuthsignalResponse<T> =
| T
| {
error: string;
};
31 changes: 26 additions & 5 deletions src/passkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class Passkey {
async signUp({userName, token, authenticatorAttachment = "platform"}: SignUpParams) {
const optionsResponse = await this.api.registrationOptions({username: userName, token, authenticatorAttachment});

if ("error" in optionsResponse) {
console.error(optionsResponse.error);
return;
}

const registrationResponse = await startRegistration(optionsResponse.options);

const addAuthenticatorResponse = await this.api.addAuthenticator({
Expand All @@ -36,11 +41,16 @@ export class Passkey {
token,
});

if (addAuthenticatorResponse?.isVerified) {
if ("error" in addAuthenticatorResponse) {
console.error(addAuthenticatorResponse.error);
return;
}

if (addAuthenticatorResponse.isVerified) {
this.storeCredentialAgainstDevice(registrationResponse);
}

return addAuthenticatorResponse?.accessToken;
return addAuthenticatorResponse.accessToken;
}

async signIn(): Promise<string | undefined>;
Expand All @@ -58,11 +68,21 @@ export class Passkey {

const challengeResponse = params?.action ? await this.api.challenge(params.action) : null;

if (challengeResponse && "error" in challengeResponse) {
console.error(challengeResponse.error);
return;
}

const optionsResponse = await this.api.authenticationOptions({
token: params?.token,
challengeId: challengeResponse?.challengeId,
});

if ("error" in optionsResponse) {
console.error(optionsResponse.error);
return;
}

const authenticationResponse = await startAuthentication(optionsResponse.options, params?.autofill);

const verifyResponse = await this.api.verify({
Expand All @@ -72,15 +92,16 @@ export class Passkey {
deviceId: this.anonymousId,
});

if (verifyResponse.error) {
if ("error" in verifyResponse) {
console.error(verifyResponse.error);
return;
}

if (verifyResponse?.data.isVerified) {
if (verifyResponse.isVerified) {
this.storeCredentialAgainstDevice(authenticationResponse);
}

return verifyResponse?.data.accessToken;
return verifyResponse.accessToken;
}

async isAvailableOnDevice() {
Expand Down