nuxt-auth-utils 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "auth-utils",
3
3
  "configKey": "auth",
4
- "version": "0.5.0",
4
+ "version": "0.5.1",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { readFile, writeFile } from 'node:fs/promises';
2
- import { defineNuxtModule, createResolver, addComponentsDir, addImportsDir, addPlugin, addServerPlugin, addServerImportsDir, logger, addServerHandler } from '@nuxt/kit';
2
+ import { defineNuxtModule, createResolver, addComponentsDir, addImports, addPlugin, addServerPlugin, addServerImportsDir, logger, addServerHandler } from '@nuxt/kit';
3
3
  import { join } from 'pathe';
4
4
  import { defu } from 'defu';
5
5
  import { randomUUID } from 'uncrypto';
@@ -21,8 +21,14 @@ const module = defineNuxtModule({
21
21
  nuxt.options.alias["#auth-utils"] = resolver.resolve(
22
22
  "./runtime/types/index"
23
23
  );
24
+ const composables = [
25
+ { name: "useUserSession", from: resolver.resolve("./runtime/app/composables/session") }
26
+ ];
27
+ if (options.webAuthn) {
28
+ composables.push({ name: "useWebAuthn", from: resolver.resolve("./runtime/app/composables/webauthn") });
29
+ }
24
30
  addComponentsDir({ path: resolver.resolve("./runtime/app/components") });
25
- addImportsDir(resolver.resolve("./runtime/app/composables"));
31
+ addImports(composables);
26
32
  addPlugin(resolver.resolve("./runtime/app/plugins/session.server"));
27
33
  addPlugin(resolver.resolve("./runtime/app/plugins/session.client"));
28
34
  addServerPlugin(resolver.resolve("./runtime/server/plugins/oauth"));
@@ -16,17 +16,17 @@ export function defineWebAuthnAuthenticateEventHandler({
16
16
  return eventHandler(async (event) => {
17
17
  const url = getRequestURL(event);
18
18
  const body = await readBody(event);
19
- const _config = defu(await getOptions?.(event) ?? {}, useRuntimeConfig(event).webauthn.authenticate, {
19
+ const _config = defu(await getOptions?.(event, body) ?? {}, useRuntimeConfig(event).webauthn.authenticate, {
20
20
  rpID: url.hostname
21
21
  });
22
- if (allowCredentials && body.userName) {
23
- _config.allowCredentials = await allowCredentials(event, body.userName);
24
- }
25
22
  if (!storeChallenge) {
26
23
  _config.challenge = "";
27
24
  }
28
25
  try {
29
26
  if (!body.verify) {
27
+ if (allowCredentials && body.userName) {
28
+ _config.allowCredentials = await allowCredentials(event, body.userName);
29
+ }
30
30
  const options = await generateAuthenticationOptions(_config);
31
31
  const attemptId = bufferToBase64URLString(getRandomValues(new Uint8Array(32)));
32
32
  if (storeChallenge) {
@@ -49,6 +49,8 @@ export function defineWebAuthnAuthenticateEventHandler({
49
49
  expectedChallenge,
50
50
  expectedOrigin: url.origin,
51
51
  expectedRPID: url.hostname,
52
+ requireUserVerification: false,
53
+ // TODO: make configurable https://simplewebauthn.dev/docs/advanced/passkeys#verifyauthenticationresponse
52
54
  credential: {
53
55
  id: credential.id,
54
56
  publicKey: new Uint8Array(base64URLStringToBuffer(credential.publicKey)),
@@ -1,6 +1,6 @@
1
1
  import type { ValidateFunction } from 'h3';
2
2
  import type { WebAuthnUser, WebAuthnRegisterEventHandlerOptions } from '#auth-utils';
3
- export declare function defineWebAuthnRegisterEventHandler<T extends WebAuthnUser>({ storeChallenge, getChallenge, getOptions, validateUser, onSuccess, onError, }: WebAuthnRegisterEventHandlerOptions<T>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void | import("@simplewebauthn/server").VerifiedRegistrationResponse | {
3
+ export declare function defineWebAuthnRegisterEventHandler<T extends WebAuthnUser>({ storeChallenge, getChallenge, getOptions, validateUser, excludeCredentials, onSuccess, onError, }: WebAuthnRegisterEventHandlerOptions<T>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void | import("@simplewebauthn/server").VerifiedRegistrationResponse | {
4
4
  creationOptions: import("@simplewebauthn/types").PublicKeyCredentialCreationOptionsJSON;
5
5
  attemptId: string;
6
6
  }>>;
@@ -9,6 +9,7 @@ export function defineWebAuthnRegisterEventHandler({
9
9
  getChallenge,
10
10
  getOptions,
11
11
  validateUser,
12
+ excludeCredentials,
12
13
  onSuccess,
13
14
  onError
14
15
  }) {
@@ -24,7 +25,7 @@ export function defineWebAuthnRegisterEventHandler({
24
25
  if (validateUser) {
25
26
  user = await validateUserData(body.user, validateUser);
26
27
  }
27
- const _config = defu(await getOptions?.(event) ?? {}, useRuntimeConfig(event).webauthn.register, {
28
+ const _config = defu(await getOptions?.(event, body) ?? {}, useRuntimeConfig(event).webauthn.register, {
28
29
  rpID: url.hostname,
29
30
  rpName: url.hostname,
30
31
  userName: user.userName,
@@ -38,6 +39,9 @@ export function defineWebAuthnRegisterEventHandler({
38
39
  }
39
40
  try {
40
41
  if (!body.verify) {
42
+ if (excludeCredentials) {
43
+ _config.excludeCredentials = await excludeCredentials(event, user.userName);
44
+ }
41
45
  const options = await generateRegistrationOptions(_config);
42
46
  const attemptId = bufferToBase64URLString(getRandomValues(new Uint8Array(32)));
43
47
  if (storeChallenge) {
@@ -1,4 +1,4 @@
1
- import type { AuthenticatorTransportFuture } from '@simplewebauthn/types';
1
+ import type { AuthenticationResponseJSON, AuthenticatorTransportFuture, RegistrationResponseJSON } from '@simplewebauthn/types';
2
2
  import type { Ref } from 'vue';
3
3
  import type { H3Event, H3Error, ValidateFunction } from 'h3';
4
4
  import type { GenerateAuthenticationOptionsOpts, GenerateRegistrationOptionsOpts, VerifiedAuthenticationResponse, VerifiedRegistrationResponse } from '@simplewebauthn/server';
@@ -15,7 +15,7 @@ export interface WebAuthnUser {
15
15
  displayName?: string;
16
16
  [key: string]: unknown;
17
17
  }
18
- type AllowCredentials = NonNullable<GenerateAuthenticationOptionsOpts['allowCredentials']>;
18
+ type CredentialsList = NonNullable<GenerateAuthenticationOptionsOpts['allowCredentials']>;
19
19
  type WebAuthnEventHandlerBase<T extends Record<PropertyKey, unknown>> = {
20
20
  storeChallenge: (event: H3Event, challenge: string, attemptId: string) => void | Promise<void>;
21
21
  getChallenge: (event: H3Event, attemptId: string) => string | Promise<string>;
@@ -27,21 +27,40 @@ type WebAuthnEventHandlerBase<T extends Record<PropertyKey, unknown>> = {
27
27
  onSuccess: (event: H3Event, data: T) => void | Promise<void>;
28
28
  onError?: (event: H3Event, error: H3Error) => void | Promise<void>;
29
29
  };
30
+ export type RegistrationBody<T extends WebAuthnUser> = {
31
+ user: T;
32
+ verify: false;
33
+ } | {
34
+ user: T;
35
+ verify: true;
36
+ attemptId: string;
37
+ response: RegistrationResponseJSON;
38
+ };
30
39
  export type WebAuthnRegisterEventHandlerOptions<T extends WebAuthnUser> = WebAuthnEventHandlerBase<{
31
40
  user: T;
32
41
  credential: WebAuthnCredential;
33
42
  registrationInfo: Exclude<VerifiedRegistrationResponse['registrationInfo'], undefined>;
34
43
  }> & {
35
- getOptions?: (event: H3Event) => GenerateRegistrationOptionsOpts | Promise<GenerateRegistrationOptionsOpts>;
44
+ getOptions?: (event: H3Event, body: RegistrationBody<T>) => Partial<GenerateRegistrationOptionsOpts> | Promise<Partial<GenerateRegistrationOptionsOpts>>;
36
45
  validateUser?: ValidateFunction<T>;
46
+ excludeCredentials?: (event: H3Event, userName: string) => CredentialsList | Promise<CredentialsList>;
47
+ };
48
+ export type AuthenticationBody = {
49
+ verify: false;
50
+ userName?: string;
51
+ } | {
52
+ verify: true;
53
+ attemptId: string;
54
+ userName?: string;
55
+ response: AuthenticationResponseJSON;
37
56
  };
38
57
  export type WebAuthnAuthenticateEventHandlerOptions<T extends WebAuthnCredential> = WebAuthnEventHandlerBase<{
39
58
  credential: T;
40
59
  authenticationInfo: Exclude<VerifiedAuthenticationResponse['authenticationInfo'], undefined>;
41
60
  }> & {
42
- getOptions?: (event: H3Event) => Partial<GenerateAuthenticationOptionsOpts> | Promise<Partial<GenerateAuthenticationOptionsOpts>>;
61
+ getOptions?: (event: H3Event, body: AuthenticationBody) => Partial<GenerateAuthenticationOptionsOpts> | Promise<Partial<GenerateAuthenticationOptionsOpts>>;
43
62
  getCredential: (event: H3Event, credentialID: string) => T | Promise<T>;
44
- allowCredentials?: (event: H3Event, userName: string) => AllowCredentials | Promise<AllowCredentials>;
63
+ allowCredentials?: (event: H3Event, userName: string) => CredentialsList | Promise<CredentialsList>;
45
64
  };
46
65
  export interface WebAuthnComposable {
47
66
  /**
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Add Authentication to Nuxt applications with secured & sealed cookies sessions.",
5
5
  "repository": "atinux/nuxt-auth-utils",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
- "packageManager": "pnpm@9.12.2",
8
+ "packageManager": "pnpm@9.12.3",
9
9
  "exports": {
10
10
  ".": {
11
11
  "types": "./dist/types.d.ts",
@@ -55,9 +55,9 @@
55
55
  }
56
56
  },
57
57
  "devDependencies": {
58
- "@iconify-json/simple-icons": "^1.2.9",
58
+ "@iconify-json/simple-icons": "^1.2.10",
59
59
  "@nuxt/devtools": "latest",
60
- "@nuxt/eslint-config": "^0.6.0",
60
+ "@nuxt/eslint-config": "^0.6.1",
61
61
  "@nuxt/module-builder": "^0.8.4",
62
62
  "@nuxt/schema": "^3.13.2",
63
63
  "@nuxt/test-utils": "^3.14.4",
@@ -68,7 +68,7 @@
68
68
  "eslint": "^9.13.0",
69
69
  "nuxt": "^3.13.2",
70
70
  "typescript": "^5.6.3",
71
- "vitest": "^2.1.3",
72
- "vue-tsc": "^2.1.6"
71
+ "vitest": "^2.1.4",
72
+ "vue-tsc": "^2.1.10"
73
73
  }
74
74
  }