Skip to content

Commit

Permalink
Make authenticatorAttachment configurable (#54)
Browse files Browse the repository at this point in the history
* Make `authenticatorAttachment` configurable

* Bump version
  • Loading branch information
hwhmeikle committed Mar 1, 2024
1 parent 8209710 commit 947fbdf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@authsignal/browser",
"version": "0.3.7",
"version": "0.4.0",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
12 changes: 10 additions & 2 deletions src/api/passkey-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ export class PasskeyApiClient {
this.baseUrl = baseUrl;
}

async registrationOptions({token, userName}: RegistrationOptsRequest): Promise<RegistrationOptsResponse> {
async registrationOptions({
token,
userName,
authenticatorAttachment,
}: RegistrationOptsRequest): Promise<RegistrationOptsResponse> {
const body = Boolean(authenticatorAttachment)
? {username: userName, authenticatorAttachment}
: {username: userName};

const response = fetch(`${this.baseUrl}/client/user-authenticators/passkey/registration-options`, {
method: "POST",
headers: this.buildHeaders(token),
body: JSON.stringify({username: userName}),
body: JSON.stringify(body),
});

return (await response).json();
Expand Down
2 changes: 2 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
AuthenticationResponseJSON,
AuthenticatorAttachment,
PublicKeyCredentialCreationOptionsJSON,
RegistrationResponseJSON,
} from "@simplewebauthn/types";

export type RegistrationOptsRequest = {
userName?: string;
token: string;
authenticatorAttachment?: AuthenticatorAttachment | null;
};

export type RegistrationOptsResponse = {
Expand Down
7 changes: 4 additions & 3 deletions src/passkey.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {startAuthentication, startRegistration} from "@simplewebauthn/browser";

import {PasskeyApiClient} from "./api";
import {AuthenticationResponseJSON, RegistrationResponseJSON} from "@simplewebauthn/types";
import {AuthenticationResponseJSON, RegistrationResponseJSON, AuthenticatorAttachment} from "@simplewebauthn/types";

type PasskeyOptions = {
baseUrl: string;
Expand All @@ -11,6 +11,7 @@ type PasskeyOptions = {
type SignUpParams = {
userName?: string;
token: string;
authenticatorAttachment?: AuthenticatorAttachment | null;
};

export class Passkey {
Expand All @@ -21,8 +22,8 @@ export class Passkey {
this.api = new PasskeyApiClient({baseUrl, tenantId});
}

async signUp({userName, token}: SignUpParams) {
const optionsResponse = await this.api.registrationOptions({userName, token});
async signUp({userName, token, authenticatorAttachment = "platform"}: SignUpParams) {
const optionsResponse = await this.api.registrationOptions({userName, token, authenticatorAttachment});

const registrationResponse = await startRegistration(optionsResponse.options);

Expand Down

0 comments on commit 947fbdf

Please sign in to comment.