nuxt-auth-utils 0.4.4 → 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/README.md +2 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +8 -2
- package/dist/runtime/app/composables/webauthn.d.ts +5 -0
- package/dist/runtime/app/composables/webauthn.js +12 -3
- package/dist/runtime/server/lib/webauthn/authenticate.js +9 -7
- package/dist/runtime/server/lib/webauthn/register.d.ts +1 -1
- package/dist/runtime/server/lib/webauthn/register.js +9 -5
- package/dist/runtime/types/webauthn.d.ts +24 -5
- package/package.json +15 -15
package/README.md
CHANGED
|
@@ -296,7 +296,7 @@ To enable WebAuthn you need to:
|
|
|
296
296
|
1. Install the peer dependencies:
|
|
297
297
|
|
|
298
298
|
```bash
|
|
299
|
-
npx nypm i @simplewebauthn/server @simplewebauthn/browser
|
|
299
|
+
npx nypm i @simplewebauthn/server@11 @simplewebauthn/browser@11
|
|
300
300
|
```
|
|
301
301
|
|
|
302
302
|
2. Enable it in your `nuxt.config.ts`
|
|
@@ -425,7 +425,7 @@ export default defineWebAuthnAuthenticateEventHandler({
|
|
|
425
425
|
```
|
|
426
426
|
|
|
427
427
|
> [!IMPORTANT]
|
|
428
|
-
> Webauthn uses challenges to prevent replay attacks. By default, this module does not make use if this feature. If you want to use challenges (**which is highly recommended**), the `storeChallenge` and `getChallenge` functions are provided. An attempt ID is created and sent with each
|
|
428
|
+
> Webauthn uses challenges to prevent replay attacks. By default, this module does not make use if this feature. If you want to use challenges (**which is highly recommended**), the `storeChallenge` and `getChallenge` functions are provided. An attempt ID is created and sent with each authentication request. You can use this ID to store the challenge in a database or KV store as shown in the example below.
|
|
429
429
|
|
|
430
430
|
> ```ts
|
|
431
431
|
> export default defineWebAuthnAuthenticateEventHandler({
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
2
|
-
import { defineNuxtModule, createResolver, addComponentsDir,
|
|
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
|
-
|
|
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"));
|
|
@@ -10,4 +10,9 @@ export declare function useWebAuthn(options?: {
|
|
|
10
10
|
* @default '/api/webauthn/authenticate'
|
|
11
11
|
*/
|
|
12
12
|
authenticateEndpoint?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Enable browser autofill for authentication
|
|
15
|
+
* @default false
|
|
16
|
+
*/
|
|
17
|
+
useBrowserAutofill?: boolean;
|
|
13
18
|
}): WebAuthnComposable;
|
|
@@ -7,7 +7,11 @@ import {
|
|
|
7
7
|
} from "@simplewebauthn/browser";
|
|
8
8
|
import { ref, onMounted } from "#imports";
|
|
9
9
|
export function useWebAuthn(options = {}) {
|
|
10
|
-
const {
|
|
10
|
+
const {
|
|
11
|
+
registerEndpoint = "/api/webauthn/register",
|
|
12
|
+
authenticateEndpoint = "/api/webauthn/authenticate",
|
|
13
|
+
useBrowserAutofill = false
|
|
14
|
+
} = options;
|
|
11
15
|
async function register(user) {
|
|
12
16
|
const { creationOptions, attemptId } = await $fetch(registerEndpoint, {
|
|
13
17
|
method: "POST",
|
|
@@ -16,7 +20,9 @@ export function useWebAuthn(options = {}) {
|
|
|
16
20
|
verify: false
|
|
17
21
|
}
|
|
18
22
|
});
|
|
19
|
-
const attestationResponse = await startRegistration(
|
|
23
|
+
const attestationResponse = await startRegistration({
|
|
24
|
+
optionsJSON: creationOptions
|
|
25
|
+
});
|
|
20
26
|
const verificationResponse = await $fetch(registerEndpoint, {
|
|
21
27
|
method: "POST",
|
|
22
28
|
body: {
|
|
@@ -36,7 +42,10 @@ export function useWebAuthn(options = {}) {
|
|
|
36
42
|
userName
|
|
37
43
|
}
|
|
38
44
|
});
|
|
39
|
-
const assertionResponse = await startAuthentication(
|
|
45
|
+
const assertionResponse = await startAuthentication({
|
|
46
|
+
optionsJSON: requestOptions,
|
|
47
|
+
useBrowserAutofill
|
|
48
|
+
});
|
|
40
49
|
const verificationResponse = await $fetch(authenticateEndpoint, {
|
|
41
50
|
method: "POST",
|
|
42
51
|
body: {
|
|
@@ -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,9 +49,11 @@ export function defineWebAuthnAuthenticateEventHandler({
|
|
|
49
49
|
expectedChallenge,
|
|
50
50
|
expectedOrigin: url.origin,
|
|
51
51
|
expectedRPID: url.hostname,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
requireUserVerification: false,
|
|
53
|
+
// TODO: make configurable https://simplewebauthn.dev/docs/advanced/passkeys#verifyauthenticationresponse
|
|
54
|
+
credential: {
|
|
55
|
+
id: credential.id,
|
|
56
|
+
publicKey: new Uint8Array(base64URLStringToBuffer(credential.publicKey)),
|
|
55
57
|
counter: credential.counter,
|
|
56
58
|
transports: credential.transports
|
|
57
59
|
}
|
|
@@ -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) {
|
|
@@ -76,11 +80,11 @@ export function defineWebAuthnRegisterEventHandler({
|
|
|
76
80
|
await onSuccess(event, {
|
|
77
81
|
user,
|
|
78
82
|
credential: {
|
|
79
|
-
id: verification.registrationInfo.
|
|
80
|
-
publicKey: bufferToBase64URLString(verification.registrationInfo.
|
|
81
|
-
counter: verification.registrationInfo.counter,
|
|
83
|
+
id: verification.registrationInfo.credential.id,
|
|
84
|
+
publicKey: bufferToBase64URLString(verification.registrationInfo.credential.publicKey),
|
|
85
|
+
counter: verification.registrationInfo.credential.counter,
|
|
82
86
|
backedUp: verification.registrationInfo.credentialBackedUp,
|
|
83
|
-
transports:
|
|
87
|
+
transports: verification.registrationInfo.credential.transports
|
|
84
88
|
},
|
|
85
89
|
registrationInfo: verification.registrationInfo
|
|
86
90
|
});
|
|
@@ -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
|
|
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) =>
|
|
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.
|
|
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.
|
|
8
|
+
"packageManager": "pnpm@9.12.3",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./dist/types.d.ts",
|
|
@@ -36,15 +36,15 @@
|
|
|
36
36
|
"@nuxt/kit": "^3.13.2",
|
|
37
37
|
"defu": "^6.1.4",
|
|
38
38
|
"hookable": "^5.5.3",
|
|
39
|
-
"ofetch": "^1.4.
|
|
39
|
+
"ofetch": "^1.4.1",
|
|
40
40
|
"ohash": "^1.1.4",
|
|
41
41
|
"pathe": "^1.1.2",
|
|
42
42
|
"scule": "^1.3.0",
|
|
43
43
|
"uncrypto": "^0.1.3"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@simplewebauthn/browser": "^
|
|
47
|
-
"@simplewebauthn/server": "^
|
|
46
|
+
"@simplewebauthn/browser": "^11.0.0",
|
|
47
|
+
"@simplewebauthn/server": "^11.0.0"
|
|
48
48
|
},
|
|
49
49
|
"peerDependenciesMeta": {
|
|
50
50
|
"@simplewebauthn/browser": {
|
|
@@ -55,20 +55,20 @@
|
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@iconify-json/simple-icons": "^1.2.
|
|
58
|
+
"@iconify-json/simple-icons": "^1.2.10",
|
|
59
59
|
"@nuxt/devtools": "latest",
|
|
60
|
-
"@nuxt/eslint-config": "^0.
|
|
60
|
+
"@nuxt/eslint-config": "^0.6.1",
|
|
61
61
|
"@nuxt/module-builder": "^0.8.4",
|
|
62
62
|
"@nuxt/schema": "^3.13.2",
|
|
63
|
-
"@nuxt/test-utils": "^3.14.
|
|
64
|
-
"@nuxt/ui": "^2.18.
|
|
65
|
-
"@nuxt/ui-pro": "^1.4.
|
|
66
|
-
"@simplewebauthn/types": "^
|
|
63
|
+
"@nuxt/test-utils": "^3.14.4",
|
|
64
|
+
"@nuxt/ui": "^2.18.7",
|
|
65
|
+
"@nuxt/ui-pro": "^1.4.4",
|
|
66
|
+
"@simplewebauthn/types": "^11.0.0",
|
|
67
67
|
"changelogen": "^0.5.7",
|
|
68
|
-
"eslint": "^9.
|
|
68
|
+
"eslint": "^9.13.0",
|
|
69
69
|
"nuxt": "^3.13.2",
|
|
70
|
-
"typescript": "^5.6.
|
|
71
|
-
"vitest": "^2.1.
|
|
72
|
-
"vue-tsc": "^2.1.
|
|
70
|
+
"typescript": "^5.6.3",
|
|
71
|
+
"vitest": "^2.1.4",
|
|
72
|
+
"vue-tsc": "^2.1.10"
|
|
73
73
|
}
|
|
74
74
|
}
|