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 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/nuxt-todos-edge)
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
  <!-- - [📖 &nbsp;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 hasing](#password-hashing)
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
- > By default, the webauthn event handlers will store the challenge in a short lived, encrypted session cookie. This is not recommended for applications that require strong security guarantees. On a secure connection (https) it is highly unlikely for this to cause problems. However, if the connection is not secure, there is a possibility of a man-in-the-middle attack. To prevent this, you should use a database or KV store to store the challenge instead. For this the `storeChallenge` and `getChallenge` functions are provided.
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "auth-utils",
3
3
  "configKey": "auth",
4
- "version": "0.4.0",
4
+ "version": "0.4.1",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "2.0.0"
@@ -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
- else
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 challenge;
42
- if (getChallenge)
43
- challenge = await getChallenge(event, body.attemptId);
44
- else
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: challenge,
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?.(event, options.challenge, attemptId);
43
- else
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
- else
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,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.4.0",
3
+ "version": "0.4.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",
@@ -1,3 +0,0 @@
1
- import { type H3Event } from 'h3';
2
- export declare function storeChallengeAsSession(event: H3Event, challenge: string, challengeId: string): Promise<void>;
3
- export declare function getChallengeFromSession(event: H3Event, challengeId: string): Promise<string>;
@@ -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
- }