Skip to content

Commit

Permalink
Set a valid rpID when generating and validating registration options
Browse files Browse the repository at this point in the history
  • Loading branch information
beverloo committed Apr 16, 2024
1 parent 5b38e95 commit f32dffe
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 12 deletions.
8 changes: 1 addition & 7 deletions app/api/auth/confirmIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

import { generateAuthenticationOptions } from '@simplewebauthn/server';
import { isoBase64URL } from '@simplewebauthn/server/helpers';
import { notFound } from 'next/navigation';
import { z } from 'zod';

import type { ActionProps } from '../Action';
import type { ApiDefinition, ApiRequest, ApiResponse } from '../Types';
import { Log, LogType, LogSeverity } from '@lib/Log';
import { determineEnvironment } from '@lib/Environment';
import { isValidActivatedUser } from '@lib/auth/Authentication';
import { retrieveCredentials } from './passkeys/PasskeyUtils';
import { storeUserChallenge } from './passkeys/PasskeyUtils';
Expand Down Expand Up @@ -70,10 +68,6 @@ export async function confirmIdentity(request: Request, props: ActionProps): Pro
if (!user)
return { success: false };

const environment = await determineEnvironment();
if (!environment)
notFound();

let authenticationOptions = undefined;

const credentials = await retrieveCredentials(user);
Expand All @@ -83,7 +77,7 @@ export async function confirmIdentity(request: Request, props: ActionProps): Pro
id: isoBase64URL.fromBuffer(credential.credentialId),
// TODO: `transports`?
})),
rpID: environment.environmentName,
rpID: props.origin.replace(/\:.*?$/g, ''), // must be a domain
userVerification: 'preferred',
});

Expand Down
6 changes: 1 addition & 5 deletions app/api/auth/passkeys/createChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function createChallenge(request: Request, props: ActionProps): Pro

const options = await generateRegistrationOptions({
rpName: `AnimeCon ${environment.environmentTitle}`,
rpID: environment.environmentName,
rpID: props.origin.replace(/\:.*?$/g, ''), // must be a domain
userID: isoUint8Array.fromUTF8String(`${props.user.userId}`),
userName: props.user.username,
userDisplayName: `${props.user.firstName} ${props.user.lastName}`,
Expand All @@ -67,10 +67,6 @@ export async function createChallenge(request: Request, props: ActionProps): Pro
})),
});

// Don't use the "relying party identifier": it defaults to the current domain, which is the
// behaviour we want as we're a multi-tenant system that wants to work on localhost too.
options.rp.id = undefined;

if (!options || !options.challenge)
return { success: false, error: 'Unable to generate a registration response' };

Expand Down

0 comments on commit f32dffe

Please sign in to comment.