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

Make authenticatorAttachment configurable #54

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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