nuxt-auth-utils 0.4.0 → 0.4.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 +4 -3
- package/dist/module.json +1 -1
- package/dist/runtime/server/lib/webauthn/authenticate.js +10 -10
- package/dist/runtime/server/lib/webauthn/register.js +9 -9
- package/package.json +1 -1
- package/dist/runtime/server/lib/webauthn/utils.d.ts +0 -3
- package/dist/runtime/server/lib/webauthn/utils.js +0 -35
package/README.md
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
Add Authentication to Nuxt applications with secured & sealed cookies sessions.
|
|
9
9
|
|
|
10
10
|
- [Release Notes](/CHANGELOG.md)
|
|
11
|
-
- [Demo](https://github.com/atinux/
|
|
11
|
+
- [Demo with OAuth](https://github.com/atinux/atidone)
|
|
12
|
+
- [Demo with Passkeys](https://github.com/atinux/todo-passkeys)
|
|
12
13
|
<!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/nuxt-auth-utils?file=playground%2Fapp.vue) -->
|
|
13
14
|
<!-- - [📖 Documentation](https://example.com) -->
|
|
14
15
|
|
|
@@ -16,7 +17,7 @@ Add Authentication to Nuxt applications with secured & sealed cookies sessions.
|
|
|
16
17
|
|
|
17
18
|
- [Hybrid Rendering](#hybrid-rendering) support (SSR / CSR / SWR / Prerendering)
|
|
18
19
|
- [20+ OAuth Providers](#supported-oauth-providers)
|
|
19
|
-
- [Password
|
|
20
|
+
- [Password Hashing](#password-hashing)
|
|
20
21
|
- [WebAuthn (passkey)](#webauthn-passkey)
|
|
21
22
|
- [`useUserSession()` Vue composable](#vue-composable)
|
|
22
23
|
- [Tree-shakable server utils](#server-utils)
|
|
@@ -422,7 +423,7 @@ export default defineWebAuthnAuthenticateEventHandler({
|
|
|
422
423
|
```
|
|
423
424
|
|
|
424
425
|
> [!IMPORTANT]
|
|
425
|
-
>
|
|
426
|
+
> 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 autentication request. You can use this ID to store the challenge in a database or KV store as shown in the example below.
|
|
426
427
|
|
|
427
428
|
> ```ts
|
|
428
429
|
> export default defineWebAuthnAuthenticateEventHandler({
|
package/dist/module.json
CHANGED
|
@@ -3,7 +3,6 @@ import { generateAuthenticationOptions, verifyAuthenticationResponse } from "@si
|
|
|
3
3
|
import defu from "defu";
|
|
4
4
|
import { getRandomValues } from "uncrypto";
|
|
5
5
|
import { base64URLStringToBuffer, bufferToBase64URLString } from "@simplewebauthn/browser";
|
|
6
|
-
import { storeChallengeAsSession, getChallengeFromSession } from "./utils.js";
|
|
7
6
|
import { useRuntimeConfig } from "#imports";
|
|
8
7
|
export function defineWebAuthnAuthenticateEventHandler({
|
|
9
8
|
storeChallenge,
|
|
@@ -23,14 +22,16 @@ export function defineWebAuthnAuthenticateEventHandler({
|
|
|
23
22
|
if (allowCredentials && body.userName) {
|
|
24
23
|
_config.allowCredentials = await allowCredentials(event, body.userName);
|
|
25
24
|
}
|
|
25
|
+
if (!storeChallenge) {
|
|
26
|
+
_config.challenge = "";
|
|
27
|
+
}
|
|
26
28
|
try {
|
|
27
29
|
if (!body.verify) {
|
|
28
30
|
const options = await generateAuthenticationOptions(_config);
|
|
29
31
|
const attemptId = bufferToBase64URLString(getRandomValues(new Uint8Array(32)));
|
|
30
|
-
if (storeChallenge)
|
|
32
|
+
if (storeChallenge) {
|
|
31
33
|
await storeChallenge(event, options.challenge, attemptId);
|
|
32
|
-
|
|
33
|
-
await storeChallengeAsSession(event, options.challenge, attemptId);
|
|
34
|
+
}
|
|
34
35
|
return {
|
|
35
36
|
requestOptions: options,
|
|
36
37
|
attemptId
|
|
@@ -38,15 +39,14 @@ export function defineWebAuthnAuthenticateEventHandler({
|
|
|
38
39
|
}
|
|
39
40
|
if (!body.attemptId)
|
|
40
41
|
throw createError({ statusCode: 400 });
|
|
41
|
-
let
|
|
42
|
-
if (getChallenge)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
challenge = await getChallengeFromSession(event, body.attemptId);
|
|
42
|
+
let expectedChallenge = "";
|
|
43
|
+
if (getChallenge) {
|
|
44
|
+
expectedChallenge = await getChallenge(event, body.attemptId);
|
|
45
|
+
}
|
|
46
46
|
const credential = await getCredential(event, body.response.id);
|
|
47
47
|
const verification = await verifyAuthenticationResponse({
|
|
48
48
|
response: body.response,
|
|
49
|
-
expectedChallenge
|
|
49
|
+
expectedChallenge,
|
|
50
50
|
expectedOrigin: url.origin,
|
|
51
51
|
expectedRPID: url.hostname,
|
|
52
52
|
authenticator: {
|
|
@@ -3,7 +3,6 @@ import { generateRegistrationOptions, verifyRegistrationResponse } from "@simple
|
|
|
3
3
|
import defu from "defu";
|
|
4
4
|
import { bufferToBase64URLString } from "@simplewebauthn/browser";
|
|
5
5
|
import { getRandomValues } from "uncrypto";
|
|
6
|
-
import { storeChallengeAsSession, getChallengeFromSession } from "./utils.js";
|
|
7
6
|
import { useRuntimeConfig } from "#imports";
|
|
8
7
|
export function defineWebAuthnRegisterEventHandler({
|
|
9
8
|
storeChallenge,
|
|
@@ -34,14 +33,16 @@ export function defineWebAuthnRegisterEventHandler({
|
|
|
34
33
|
userVerification: "preferred"
|
|
35
34
|
}
|
|
36
35
|
});
|
|
36
|
+
if (!storeChallenge) {
|
|
37
|
+
_config.challenge = "";
|
|
38
|
+
}
|
|
37
39
|
try {
|
|
38
40
|
if (!body.verify) {
|
|
39
41
|
const options = await generateRegistrationOptions(_config);
|
|
40
42
|
const attemptId = bufferToBase64URLString(getRandomValues(new Uint8Array(32)));
|
|
41
|
-
if (storeChallenge)
|
|
42
|
-
await storeChallenge
|
|
43
|
-
|
|
44
|
-
await storeChallengeAsSession(event, options.challenge, attemptId);
|
|
43
|
+
if (storeChallenge) {
|
|
44
|
+
await storeChallenge(event, options.challenge, attemptId);
|
|
45
|
+
}
|
|
45
46
|
return {
|
|
46
47
|
creationOptions: options,
|
|
47
48
|
attemptId
|
|
@@ -53,11 +54,10 @@ export function defineWebAuthnRegisterEventHandler({
|
|
|
53
54
|
statusCode: 400
|
|
54
55
|
});
|
|
55
56
|
}
|
|
56
|
-
let expectedChallenge;
|
|
57
|
-
if (getChallenge)
|
|
57
|
+
let expectedChallenge = "";
|
|
58
|
+
if (getChallenge) {
|
|
58
59
|
expectedChallenge = await getChallenge(event, body.attemptId);
|
|
59
|
-
|
|
60
|
-
expectedChallenge = await getChallengeFromSession(event, body.attemptId);
|
|
60
|
+
}
|
|
61
61
|
const verification = await verifyRegistrationResponse({
|
|
62
62
|
response: body.response,
|
|
63
63
|
expectedChallenge,
|
package/package.json
CHANGED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { useSession, createError } from "h3";
|
|
2
|
-
import { useRuntimeConfig } from "#imports";
|
|
3
|
-
function useChallengeSession(event) {
|
|
4
|
-
return useSession(event, {
|
|
5
|
-
name: "webauthn",
|
|
6
|
-
password: useRuntimeConfig(event).session.password,
|
|
7
|
-
cookie: {
|
|
8
|
-
httpOnly: true,
|
|
9
|
-
secure: true,
|
|
10
|
-
maxAge: 180
|
|
11
|
-
// 3 minutes
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
export async function storeChallengeAsSession(event, challenge, challengeId) {
|
|
16
|
-
const challengeSession = await useChallengeSession(event);
|
|
17
|
-
await challengeSession.update({
|
|
18
|
-
data: {
|
|
19
|
-
challenge,
|
|
20
|
-
challengeId
|
|
21
|
-
},
|
|
22
|
-
expires: Date.now() + 180 * 1e3
|
|
23
|
-
// 3 minutes
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
export async function getChallengeFromSession(event, challengeId) {
|
|
27
|
-
const challengeSession = await useChallengeSession(event);
|
|
28
|
-
const challenge = challengeSession.data;
|
|
29
|
-
await challengeSession.clear();
|
|
30
|
-
if (challenge.expires < Date.now())
|
|
31
|
-
throw createError({ statusCode: 400, message: "Challenge expired" });
|
|
32
|
-
if (challenge.data.challengeId !== challengeId)
|
|
33
|
-
throw createError({ statusCode: 400, message: "Challenge id mismatch" });
|
|
34
|
-
return challenge.data.challenge;
|
|
35
|
-
}
|