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

feat(webauthn): allow userName in register handler options #275

Closed
Closed
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
19 changes: 14 additions & 5 deletions src/runtime/server/lib/webauthn/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import defu from 'defu'
import { bufferToBase64URLString } from '@simplewebauthn/browser'
import { getRandomValues } from 'uncrypto'
import { useRuntimeConfig } from '#imports'
import { getUserSession, useRuntimeConfig } from '#imports'

Check failure on line 8 in src/runtime/server/lib/webauthn/register.ts

View workflow job for this annotation

GitHub Actions / test

'"#imports"' has no exported member named 'getUserSession'. Did you mean 'useUserSession'?

import type { WebAuthnUser, WebAuthnRegisterEventHandlerOptions } from '#auth-utils'
import type { RegistrationBody } from '~/src/runtime/types/webauthn'

Expand All @@ -19,17 +20,25 @@
onError,
}: WebAuthnRegisterEventHandlerOptions<T>) {
return eventHandler(async (event) => {
const { user: sessionUser } = await getUserSession(event)
const url = getRequestURL(event)
const body = await readBody<RegistrationBody<T>>(event)
if (body.verify === undefined || !body.user?.userName)

// Check for existing session user's email or body user's userName
if (!sessionUser?.email && (body.verify === undefined || !body.user?.userName)) {
throw createError({
message: 'Invalid request, missing userName or verify property',
message: 'No authenticated user found and missing userName in request',
statusCode: 400,
})
}

// Use session user's email as userName if available, otherwise use body user
let user = sessionUser?.email
? { ...body.user, userName: sessionUser.email }
: body.user

let user = body.user
if (validateUser) {
user = await validateUserData(body.user, validateUser)
user = await validateUserData(user, validateUser)
}

const _config = defu(await getOptions?.(event, body) ?? {}, useRuntimeConfig(event).webauthn.register, {
Expand Down
Loading