A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(api): let clients request the passkey relying party (#3810)
Hampus080992134684
diff --git a/fluxer_api/src/api/auth/AuthController.ts b/fluxer_api/src/api/auth/AuthController.tsindex ea5ff1477..435b31051 100644--- a/fluxer_api/src/api/auth/AuthController.ts+++ b/fluxer_api/src/api/auth/AuthController.ts@@ -80,6 +80,7 @@ import {WebAuthnAuthenticationOptionsResponse,WebAuthnMfaRequest,} from '@fluxer/schema/src/domains/auth/AuthSchemas';+import {WebAuthnRpIdQuery} from '@fluxer/schema/src/domains/auth/WebAuthnSchemas';import type {Context} from 'hono';function optionalString(value: unknown): string | null {@@ -554,6 +555,7 @@ export function AuthController(app: HonoApp) {'/auth/webauthn/authentication-options',LocalAuthMiddleware,RateLimitMiddleware(RateLimitConfigs.AUTH_WEBAUTHN_OPTIONS),+ Validator('query', WebAuthnRpIdQuery),OpenAPI({operationId: 'get_webauthn_authentication_options',summary: 'Get WebAuthn authentication options',@@ -569,6 +571,7 @@ export function AuthController(app: HonoApp) {await AuthMfa.generateWebAuthnAuthenticationOptionsDiscoverable(ctx.get('apiContext'),ctx.req.header('origin'),+ ctx.req.valid('query').rp_id,),);},@@ -601,6 +604,7 @@ export function AuthController(app: HonoApp) {'/auth/login/mfa/webauthn/authentication-options',RateLimitMiddleware(RateLimitConfigs.AUTH_LOGIN_MFA),Validator('json', MfaTicketRequest),+ Validator('query', WebAuthnRpIdQuery),MfaTicketAuthMiddleware,OpenAPI({operationId: 'get_webauthn_mfa_options',@@ -618,6 +622,7 @@ export function AuthController(app: HonoApp) {ctx.get('apiContext'),ctx.req.valid('json').ticket,ctx.req.header('origin'),+ ctx.req.valid('query').rp_id,),);},diff --git a/fluxer_api/src/api/auth/AuthMfa.ts b/fluxer_api/src/api/auth/AuthMfa.tsindex 0fef9c0b3..edca74bab 100644--- a/fluxer_api/src/api/auth/AuthMfa.ts+++ b/fluxer_api/src/api/auth/AuthMfa.ts@@ -217,13 +217,14 @@ export async function generateWebAuthnRegistrationOptions(ctx: ApiContext,userId: UserID,origin: string | null | undefined,+ requestedRpId?: string | null,): Promise<PublicKeyCredentialCreationOptionsJSON> {const existingCredentials = await ctx.services.users.listWebAuthnCredentials(userId);if (visibleWebAuthnCredentials(existingCredentials).length >= 10) {throw new WebAuthnCredentialLimitReachedError();}return createWebAuthnRegistrationOptions(ctx, userId, {- rpId: originRpId(ctx, origin),+ rpId: originRpId(ctx, origin, requestedRpId),context: 'registration',excludeCredentials: existingCredentials,});@@ -451,8 +452,9 @@ function selectCredentialRpOrThrow(ctx: ApiContext,origin: string | null | undefined,credentials: Array<WebAuthnCredential>,+ requestedRpId?: string | null,): CredentialRpSelection {- const selection = selectCredentialRp(ctx, origin, credentials);+ const selection = selectCredentialRp(ctx, origin, credentials, requestedRpId);if (selection.credentials.length === 0) {throw new NoPasskeysRegisteredError();}@@ -462,9 +464,10 @@ function selectCredentialRpOrThrow(export async function generateWebAuthnAuthenticationOptionsDiscoverable(ctx: ApiContext,origin: string | null | undefined,+ requestedRpId?: string | null,): Promise<PublicKeyCredentialRequestOptionsJSON> {return generateWebAuthnAuthenticationOptions(ctx, {- selection: {rpId: originRpId(ctx, origin), credentials: null},+ selection: {rpId: originRpId(ctx, origin, requestedRpId), credentials: null},context: 'discoverable',});}@@ -488,6 +491,7 @@ export async function generateWebAuthnAuthenticationOptionsForMfa(ctx: ApiContext,ticket: string,origin: string | null | undefined,+ requestedRpId?: string | null,): Promise<PublicKeyCredentialRequestOptionsJSON> {const {users, cache} = ctx.services;const userIdStr = await cache.get<string>(`mfa-ticket:${ticket}`);@@ -497,7 +501,7 @@ export async function generateWebAuthnAuthenticationOptionsForMfa(const userId = createUserID(BigInt(userIdStr));const credentials = await users.listWebAuthnCredentials(userId);return generateWebAuthnAuthenticationOptions(ctx, {- selection: selectCredentialRpOrThrow(ctx, origin, credentials),+ selection: selectCredentialRpOrThrow(ctx, origin, credentials, requestedRpId),context: 'mfa',userId,ticket,@@ -578,10 +582,11 @@ export async function generateWebAuthnOptionsForSudo(ctx: ApiContext,userId: UserID,origin: string | null | undefined,+ requestedRpId?: string | null,): Promise<PublicKeyCredentialRequestOptionsJSON> {const credentials = await ctx.services.users.listWebAuthnCredentials(userId);return generateWebAuthnAuthenticationOptions(ctx, {- selection: selectCredentialRpOrThrow(ctx, origin, credentials),+ selection: selectCredentialRpOrThrow(ctx, origin, credentials, requestedRpId),context: 'sudo',userId,});diff --git a/fluxer_api/src/api/auth/services/PasskeyRelyingParty.ts b/fluxer_api/src/api/auth/services/PasskeyRelyingParty.tsindex dbb6027a8..19b0daba4 100644--- a/fluxer_api/src/api/auth/services/PasskeyRelyingParty.ts+++ b/fluxer_api/src/api/auth/services/PasskeyRelyingParty.ts@@ -32,8 +32,25 @@ export function visibleWebAuthnCredentials(credentials: Array<WebAuthnCredentialreturn credentials.filter((credential) => credential.supersededBy === null);}-export function originRpId(ctx: ApiContext, origin: string | null | undefined): string {- return isPasskeyTargetOrigin(ctx, origin) ? PASSKEY_MIGRATION_RP_ID : ctx.services.config.auth.passkeys.rpId;+function requestedPasskeyRpId(ctx: ApiContext, rpId: string | null | undefined): string | null {+ if (ctx.services.config.instance.selfHosted || !rpId) return null;+ return rpId === PASSKEY_MIGRATION_RP_ID || rpId === ctx.services.config.auth.passkeys.rpId ? rpId : null;+}++function prefersMigrationRp(+ ctx: ApiContext,+ origin: string | null | undefined,+ requestedRpId: string | null | undefined,+): boolean {+ const requested = requestedPasskeyRpId(ctx, requestedRpId);+ if (requested !== null) return requested === PASSKEY_MIGRATION_RP_ID;+ return isPasskeyTargetOrigin(ctx, origin);+}++export function originRpId(ctx: ApiContext, origin: string | null | undefined, requestedRpId?: string | null): string {+ return prefersMigrationRp(ctx, origin, requestedRpId)+ ? PASSKEY_MIGRATION_RP_ID+ : ctx.services.config.auth.passkeys.rpId;}export async function isPasskeyMigrationActive(ctx: ApiContext, origin: string | null | undefined): Promise<boolean> {@@ -50,10 +67,11 @@ export function selectCredentialRp(ctx: ApiContext,origin: string | null | undefined,credentials: Array<WebAuthnCredential>,+ requestedRpId?: string | null,): CredentialRpSelection {const legacyRpId = ctx.services.config.auth.passkeys.rpId;const visible = visibleWebAuthnCredentials(credentials);- if (isPasskeyTargetOrigin(ctx, origin)) {+ if (prefersMigrationRp(ctx, origin, requestedRpId)) {const target = credentialGroup(ctx, visible, PASSKEY_MIGRATION_RP_ID);return target.credentials.length > 0 ? target : credentialGroup(ctx, visible, legacyRpId);}diff --git a/fluxer_api/src/api/openapi/openapi.json b/fluxer_api/src/api/openapi/openapi.jsonindex 9de5fdf4b..1d88e795d 100644--- a/fluxer_api/src/api/openapi/openapi.json+++ b/fluxer_api/src/api/openapi/openapi.json@@ -1098,6 +1098,18 @@}},"description": "Retrieve WebAuthn challenge and options for multi-factor authentication. Requires the MFA ticket from initial login.",+ "parameters": [+ {+ "name": "rp_id",+ "in": "query",+ "required": false,+ "schema": {+ "description": "Relying party ID to use instead of the one picked from the Origin header. Only the instance relying party and the migration relying party are honoured, and only on the official instance. Any other value is ignored",+ "anyOf": [{"type": "string", "maxLength": 253}, {"type": "null"}]+ },+ "description": "Relying party ID to use instead of the one picked from the Origin header. Only the instance relying party and the migration relying party are honoured, and only on the official instance. Any other value is ignored"+ }+ ],"requestBody": {"required": true,"content": {"application/json": {"schema": {"$ref": "#/components/schemas/MfaTicketRequest"}}}@@ -2471,7 +2483,19 @@"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}}},- "description": "Retrieve WebAuthn authentication challenge and options for passwordless login with biometrics or security keys."+ "description": "Retrieve WebAuthn authentication challenge and options for passwordless login with biometrics or security keys.",+ "parameters": [+ {+ "name": "rp_id",+ "in": "query",+ "required": false,+ "schema": {+ "description": "Relying party ID to use instead of the one picked from the Origin header. Only the instance relying party and the migration relying party are honoured, and only on the official instance. Any other value is ignored",+ "anyOf": [{"type": "string", "maxLength": 253}, {"type": "null"}]+ },+ "description": "Relying party ID to use instead of the one picked from the Origin header. Only the instance relying party and the migration relying party are honoured, and only on the official instance. Any other value is ignored"+ }+ ]}},"/channels/messages/bulk": {@@ -21941,6 +21965,18 @@},"description": "Generate challenge and options to register a new WebAuthn credential. Requires sudo mode verification.","security": [{"sessionToken": []}],+ "parameters": [+ {+ "name": "rp_id",+ "in": "query",+ "required": false,+ "schema": {+ "description": "Relying party ID to use instead of the one picked from the Origin header. Only the instance relying party and the migration relying party are honoured, and only on the official instance. Any other value is ignored",+ "anyOf": [{"type": "string", "maxLength": 253}, {"type": "null"}]+ },+ "description": "Relying party ID to use instead of the one picked from the Origin header. Only the instance relying party and the migration relying party are honoured, and only on the official instance. Any other value is ignored"+ }+ ],"requestBody": {"required": false,"content": {"application/json": {"schema": {"$ref": "#/components/schemas/SudoVerificationSchema"}}}@@ -24588,7 +24624,19 @@}},"description": "Generate WebAuthn challenge for sudo mode verification using a registered security key or biometric device.",- "security": [{"sessionToken": []}]+ "security": [{"sessionToken": []}],+ "parameters": [+ {+ "name": "rp_id",+ "in": "query",+ "required": false,+ "schema": {+ "description": "Relying party ID to use instead of the one picked from the Origin header. Only the instance relying party and the migration relying party are honoured, and only on the official instance. Any other value is ignored",+ "anyOf": [{"type": "string", "maxLength": 253}, {"type": "null"}]+ },+ "description": "Relying party ID to use instead of the one picked from the Origin header. Only the instance relying party and the migration relying party are honoured, and only on the official instance. Any other value is ignored"+ }+ ]}},"/users/@me/terms-acceptance": {diff --git a/fluxer_api/src/api/user/controllers/UserAuthController.ts b/fluxer_api/src/api/user/controllers/UserAuthController.tsindex 05dedab3d..0ce6127db 100644--- a/fluxer_api/src/api/user/controllers/UserAuthController.ts+++ b/fluxer_api/src/api/user/controllers/UserAuthController.ts@@ -49,6 +49,7 @@ import {PasskeyMigrationCompleteRequest,PasskeyMigrationResponse,} from '@fluxer/schema/src/domains/auth/PasskeyMigrationSchemas';+import {WebAuthnRpIdQuery} from '@fluxer/schema/src/domains/auth/WebAuthnSchemas';import {CredentialIdParam} from '@fluxer/schema/src/domains/common/CommonParamSchemas';import {EmptyBodyRequest} from '@fluxer/schema/src/domains/user/UserRequestSchemas';import {@@ -365,6 +366,7 @@ export function UserAuthController(app: HonoApp) {DefaultUserOnly,SudoModeMiddleware,Validator('json', SudoVerificationSchema),+ Validator('query', WebAuthnRpIdQuery),OpenAPI({operationId: 'get_webauthn_registration_options',summary: 'Get WebAuthn registration options',@@ -382,7 +384,9 @@ export function UserAuthController(app: HonoApp) {issueSudoToken: false,});return ctx.json(- await ctx.get('userAuthRequestService').generateWebAuthnRegistrationOptions(user, ctx.req.header('origin')),+ await ctx+ .get('userAuthRequestService')+ .generateWebAuthnRegistrationOptions(user, ctx.req.header('origin'), ctx.req.valid('query').rp_id),);},);@@ -589,6 +593,7 @@ export function UserAuthController(app: HonoApp) {RateLimitMiddleware(RateLimitConfigs.SUDO_WEBAUTHN_OPTIONS),LoginRequired,DefaultUserOnly,+ Validator('query', WebAuthnRpIdQuery),OpenAPI({operationId: 'get_sudo_webauthn_authentication_options',summary: 'Get sudo WebAuthn authentication options',@@ -601,7 +606,9 @@ export function UserAuthController(app: HonoApp) {}),async (ctx) => {return ctx.json(- await ctx.get('userAuthRequestService').getSudoWebAuthnOptions(ctx.get('user'), ctx.req.header('origin')),+ await ctx+ .get('userAuthRequestService')+ .getSudoWebAuthnOptions(ctx.get('user'), ctx.req.header('origin'), ctx.req.valid('query').rp_id),);},);diff --git a/fluxer_api/src/api/user/services/UserAuthRequestService.ts b/fluxer_api/src/api/user/services/UserAuthRequestService.tsindex 431eb93ce..1990fe365 100644--- a/fluxer_api/src/api/user/services/UserAuthRequestService.ts+++ b/fluxer_api/src/api/user/services/UserAuthRequestService.ts@@ -107,9 +107,10 @@ export class UserAuthRequestService {async generateWebAuthnRegistrationOptions(user: User,origin: string | undefined,+ requestedRpId?: string | null,): Promise<WebAuthnChallengeResponse> {assertCanRegisterPasskey(user);- const options = await AuthMfa.generateWebAuthnRegistrationOptions(this.apiContext, user.id, origin);+ const options = await AuthMfa.generateWebAuthnRegistrationOptions(this.apiContext, user.id, origin, requestedRpId);return {...options};}@@ -144,8 +145,12 @@ export class UserAuthRequestService {return AuthMfa.getAvailableMfaMethods(this.apiContext, user.id);}- async getSudoWebAuthnOptions(user: User, origin: string | undefined): Promise<WebAuthnChallengeResponse> {- const options = await AuthMfa.generateWebAuthnOptionsForSudo(this.apiContext, user.id, origin);+ async getSudoWebAuthnOptions(+ user: User,+ origin: string | undefined,+ requestedRpId?: string | null,+ ): Promise<WebAuthnChallengeResponse> {+ const options = await AuthMfa.generateWebAuthnOptionsForSudo(this.apiContext, user.id, origin, requestedRpId);return {...options};}diff --git a/packages/schema/src/domains/auth/WebAuthnSchemas.ts b/packages/schema/src/domains/auth/WebAuthnSchemas.tsindex 481c039d0..fd463f648 100644--- a/packages/schema/src/domains/auth/WebAuthnSchemas.ts+++ b/packages/schema/src/domains/auth/WebAuthnSchemas.ts@@ -64,3 +64,15 @@ export const WebAuthnAuthenticationOptions = z.looseObject({}).optional(),}) satisfies z.ZodType<PublicKeyCredentialRequestOptionsJSON>;++export const WebAuthnRpIdQuery = z.object({+ rp_id: z+ .string()+ .max(253)+ .nullish()+ .describe(+ 'Relying party ID to use instead of the one picked from the Origin header. Only the instance relying party and the migration relying party are honoured, and only on the official instance. Any other value is ignored',+ ),+});++export type WebAuthnRpIdQuery = z.infer<typeof WebAuthnRpIdQuery>;