nuxt-auth-utils 0.5.29 → 0.5.31

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.
Files changed (57) hide show
  1. package/README.md +1 -1
  2. package/dist/module.json +1 -1
  3. package/dist/runtime/app/plugins/session.client.js +1 -1
  4. package/dist/runtime/server/lib/oauth/apple.js +12 -3
  5. package/dist/runtime/server/lib/oauth/atlassian.js +7 -4
  6. package/dist/runtime/server/lib/oauth/auth0.js +7 -2
  7. package/dist/runtime/server/lib/oauth/authentik.js +7 -2
  8. package/dist/runtime/server/lib/oauth/azureb2c.js +2 -2
  9. package/dist/runtime/server/lib/oauth/battledotnet.js +7 -5
  10. package/dist/runtime/server/lib/oauth/box.js +2 -2
  11. package/dist/runtime/server/lib/oauth/cognito.js +7 -2
  12. package/dist/runtime/server/lib/oauth/discord.js +7 -2
  13. package/dist/runtime/server/lib/oauth/dropbox.js +7 -2
  14. package/dist/runtime/server/lib/oauth/facebook.js +7 -3
  15. package/dist/runtime/server/lib/oauth/gitea.js +8 -1
  16. package/dist/runtime/server/lib/oauth/github.js +2 -2
  17. package/dist/runtime/server/lib/oauth/gitlab.js +8 -1
  18. package/dist/runtime/server/lib/oauth/google.js +7 -3
  19. package/dist/runtime/server/lib/oauth/heroku.js +2 -2
  20. package/dist/runtime/server/lib/oauth/hubspot.js +8 -1
  21. package/dist/runtime/server/lib/oauth/instagram.js +7 -2
  22. package/dist/runtime/server/lib/oauth/keycloak.js +7 -2
  23. package/dist/runtime/server/lib/oauth/kick.js +6 -3
  24. package/dist/runtime/server/lib/oauth/line.js +7 -3
  25. package/dist/runtime/server/lib/oauth/linear.js +7 -2
  26. package/dist/runtime/server/lib/oauth/linkedin.js +7 -2
  27. package/dist/runtime/server/lib/oauth/livechat.js +9 -5
  28. package/dist/runtime/server/lib/oauth/microsoft.js +7 -3
  29. package/dist/runtime/server/lib/oauth/oidc.js +2 -2
  30. package/dist/runtime/server/lib/oauth/okta.js +3 -4
  31. package/dist/runtime/server/lib/oauth/ory.js +2 -2
  32. package/dist/runtime/server/lib/oauth/osu.js +2 -2
  33. package/dist/runtime/server/lib/oauth/paypal.js +7 -2
  34. package/dist/runtime/server/lib/oauth/polar.js +7 -2
  35. package/dist/runtime/server/lib/oauth/riotgames.js +2 -2
  36. package/dist/runtime/server/lib/oauth/roblox.js +7 -2
  37. package/dist/runtime/server/lib/oauth/salesforce.js +2 -2
  38. package/dist/runtime/server/lib/oauth/seznam.js +6 -2
  39. package/dist/runtime/server/lib/oauth/shopifyCustomer.js +4 -1
  40. package/dist/runtime/server/lib/oauth/slack.js +2 -2
  41. package/dist/runtime/server/lib/oauth/spotify.js +7 -2
  42. package/dist/runtime/server/lib/oauth/steam.js +6 -2
  43. package/dist/runtime/server/lib/oauth/strava.js +8 -1
  44. package/dist/runtime/server/lib/oauth/tiktok.js +7 -2
  45. package/dist/runtime/server/lib/oauth/twitch.js +7 -2
  46. package/dist/runtime/server/lib/oauth/vk.js +7 -1
  47. package/dist/runtime/server/lib/oauth/workos.js +7 -2
  48. package/dist/runtime/server/lib/oauth/xsuaa.js +7 -2
  49. package/dist/runtime/server/lib/oauth/yandex.js +8 -1
  50. package/dist/runtime/server/lib/oauth/zitadel.js +2 -2
  51. package/dist/runtime/server/lib/utils.d.ts +5 -1
  52. package/dist/runtime/server/lib/utils.js +11 -9
  53. package/dist/runtime/server/utils/session.d.ts +5 -5
  54. package/dist/runtime/types/event.d.ts +4 -0
  55. package/dist/runtime/types/event.js +0 -0
  56. package/dist/runtime/types/index.d.ts +1 -0
  57. package/package.json +4 -4
@@ -1,7 +1,7 @@
1
1
  import { eventHandler, getQuery, sendRedirect, getRequestIP, getRequestHeader } from "h3";
2
2
  import { withQuery } from "ufo";
3
3
  import { defu } from "defu";
4
- import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, handleInvalidState, handleState, requestAccessToken } from "../utils.js";
5
5
  import { useRuntimeConfig } from "#imports";
6
6
  export function defineOAuthWorkOSEventHandler({ config, onSuccess, onError }) {
7
7
  return eventHandler(async (event) => {
@@ -14,6 +14,7 @@ export function defineOAuthWorkOSEventHandler({ config, onSuccess, onError }) {
14
14
  if (query.error) {
15
15
  return handleAccessTokenErrorResponse(event, "workos", query, onError);
16
16
  }
17
+ const state = await handleState(event);
17
18
  if (!query.code) {
18
19
  return sendRedirect(
19
20
  event,
@@ -24,10 +25,14 @@ export function defineOAuthWorkOSEventHandler({ config, onSuccess, onError }) {
24
25
  redirect_uri: redirectURL || void 0,
25
26
  connection_id: config.connectionId || void 0,
26
27
  screen_hint: config.screenHint || "sign-in",
27
- organization_id: config.organizationId || void 0
28
+ organization_id: config.organizationId || void 0,
29
+ state
28
30
  })
29
31
  );
30
32
  }
33
+ if (query.state !== state) {
34
+ return handleInvalidState(event, "workos", onError);
35
+ }
31
36
  const authenticateResponse = await requestAccessToken("https://api.workos.com/user_management/authenticate", {
32
37
  headers: {
33
38
  "Content-Type": "application/json"
@@ -1,7 +1,7 @@
1
1
  import { eventHandler, getQuery, sendRedirect } from "h3";
2
2
  import { withQuery } from "ufo";
3
3
  import { defu } from "defu";
4
- import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, handleInvalidState, handleState, requestAccessToken } from "../utils.js";
5
5
  import { useRuntimeConfig } from "#imports";
6
6
  export function defineOAuthXSUAAEventHandler({ config, onSuccess, onError }) {
7
7
  return eventHandler(async (event) => {
@@ -13,6 +13,7 @@ export function defineOAuthXSUAAEventHandler({ config, onSuccess, onError }) {
13
13
  const authorizationURL = `https://${config.domain}/oauth/authorize`;
14
14
  const tokenURL = `https://${config.domain}/oauth/token`;
15
15
  const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
16
+ const state = await handleState(event);
16
17
  if (!query.code) {
17
18
  config.scope = config.scope || [];
18
19
  return sendRedirect(
@@ -21,10 +22,14 @@ export function defineOAuthXSUAAEventHandler({ config, onSuccess, onError }) {
21
22
  response_type: "code",
22
23
  client_id: config.clientId,
23
24
  redirect_uri: redirectURL,
24
- scope: config.scope.join(" ")
25
+ scope: config.scope.join(" "),
26
+ state
25
27
  })
26
28
  );
27
29
  }
30
+ if (query.state !== state) {
31
+ return handleInvalidState(event, "xsuaa", onError);
32
+ }
28
33
  const tokens = await requestAccessToken(tokenURL, {
29
34
  body: {
30
35
  grant_type: "authorization_code",
@@ -5,6 +5,8 @@ import {
5
5
  handleMissingConfiguration,
6
6
  handleAccessTokenErrorResponse,
7
7
  getOAuthRedirectURL,
8
+ handleInvalidState,
9
+ handleState,
8
10
  requestAccessToken
9
11
  } from "../utils.js";
10
12
  import { useRuntimeConfig } from "#imports";
@@ -29,6 +31,7 @@ export function defineOAuthYandexEventHandler({
29
31
  );
30
32
  }
31
33
  const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
34
+ const state = await handleState(event);
32
35
  if (!query.code) {
33
36
  config.scope = config.scope || [];
34
37
  if (config.emailRequired && !config.scope.includes("login:email")) {
@@ -40,10 +43,14 @@ export function defineOAuthYandexEventHandler({
40
43
  response_type: "code",
41
44
  client_id: config.clientId,
42
45
  redirect_uri: redirectURL,
43
- scope: config.scope.join(" ")
46
+ scope: config.scope.join(" "),
47
+ state
44
48
  })
45
49
  );
46
50
  }
51
+ if (query.state !== state) {
52
+ return handleInvalidState(event, "yandex", onError);
53
+ }
47
54
  const tokens = await requestAccessToken(config.tokenURL, {
48
55
  body: {
49
56
  grant_type: "authorization_code",
@@ -36,10 +36,10 @@ export function defineOAuthZitadelEventHandler({ config, onSuccess, onError }) {
36
36
  client_id: config.clientId,
37
37
  redirect_uri: redirectURL,
38
38
  scope: config.scope.join(" "),
39
- state,
40
39
  code_challenge: verifier.code_challenge,
41
40
  code_challenge_method: verifier.code_challenge_method,
42
- ...config.authorizationParams
41
+ ...config.authorizationParams,
42
+ state
43
43
  })
44
44
  );
45
45
  }
@@ -70,7 +70,11 @@ export declare function handlePkceVerifier(event: H3Event): Promise<{
70
70
  code_challenge?: undefined;
71
71
  code_challenge_method?: undefined;
72
72
  }>;
73
- export declare function handleState(event: H3Event): Promise<string | undefined>;
73
+ interface HandleStateOptions {
74
+ isCallback?: boolean;
75
+ sameSite?: 'lax' | 'none' | 'strict';
76
+ }
77
+ export declare function handleState(event: H3Event, options?: HandleStateOptions): Promise<string | undefined>;
74
78
  export declare function parseJwt(token: string): jose.JWTPayload;
75
79
  export declare function handleNonce(event: H3Event): string | undefined;
76
80
  export {};
@@ -101,21 +101,23 @@ export async function handlePkceVerifier(event) {
101
101
  };
102
102
  }
103
103
  const verifier = getCookie(event, "nuxt-auth-pkce");
104
- deleteCookie(event, "nuxt-auth-pkce");
104
+ deleteCookie(event, "nuxt-auth-pkce", { path: "/" });
105
105
  return { code_verifier: verifier };
106
106
  }
107
- export async function handleState(event) {
107
+ export async function handleState(event, options = {}) {
108
108
  const query = getQuery(event);
109
- if (query.state) {
109
+ const isCallback = options.isCallback ?? Boolean(query.code);
110
+ if (isCallback) {
110
111
  const state2 = getCookie(event, "nuxt-auth-state");
111
- deleteCookie(event, "nuxt-auth-state");
112
+ deleteCookie(event, "nuxt-auth-state", { path: "/" });
112
113
  return state2;
113
114
  }
114
- const state = encodeBase64Url(getRandomBytes(8));
115
+ const state = encodeBase64Url(getRandomBytes());
116
+ const sameSite = options.sameSite || "lax";
115
117
  setCookie(event, "nuxt-auth-state", state, {
116
118
  httpOnly: true,
117
- secure: !isDevelopment,
118
- sameSite: "lax",
119
+ secure: sameSite === "none" || !isDevelopment,
120
+ sameSite,
119
121
  maxAge: OAUTH_COOKIE_MAX_AGE,
120
122
  path: "/"
121
123
  });
@@ -128,10 +130,10 @@ export function handleNonce(event) {
128
130
  const query = getQuery(event);
129
131
  if (query.code) {
130
132
  const nonce2 = getCookie(event, "nuxt-auth-nonce");
131
- deleteCookie(event, "nuxt-auth-nonce");
133
+ deleteCookie(event, "nuxt-auth-nonce", { path: "/" });
132
134
  return nonce2;
133
135
  }
134
- const nonce = encodeBase64Url(getRandomBytes(8));
136
+ const nonce = encodeBase64Url(getRandomBytes());
135
137
  setCookie(event, "nuxt-auth-nonce", nonce, {
136
138
  httpOnly: true,
137
139
  secure: !isDevelopment,
@@ -1,8 +1,8 @@
1
1
  import type { H3Event, SessionConfig } from 'h3';
2
2
  import { useSession } from 'h3';
3
3
  import type { OmitWithIndexSignature } from '../../types/utils.js';
4
- import type { UserSession, UserSessionRequired } from '#auth-utils';
5
- type UseSessionEvent = Parameters<typeof useSession>[0];
4
+ import type { SessionEvent, UserSession, UserSessionRequired } from '#auth-utils';
5
+ type UseSessionEvent = Parameters<typeof useSession>[0] | SessionEvent;
6
6
  export interface SessionHooks {
7
7
  /**
8
8
  * Called when fetching the session from the API
@@ -28,19 +28,19 @@ export declare function getUserSession(event: UseSessionEvent): Promise<UserSess
28
28
  * @param data User session data, please only store public information since it can be decoded with API calls
29
29
  * @see https://github.com/atinux/nuxt-auth-utils
30
30
  */
31
- export declare function setUserSession(event: H3Event, data: OmitWithIndexSignature<UserSession, 'id'>, config?: Partial<SessionConfig>): Promise<UserSession>;
31
+ export declare function setUserSession(event: SessionEvent, data: OmitWithIndexSignature<UserSession, 'id'>, config?: Partial<SessionConfig>): Promise<UserSession>;
32
32
  /**
33
33
  * Replace a user session
34
34
  * @param event The Request (h3) event
35
35
  * @param data User session data, please only store public information since it can be decoded with API calls
36
36
  */
37
- export declare function replaceUserSession(event: H3Event, data: OmitWithIndexSignature<UserSession, 'id'>, config?: Partial<SessionConfig>): Promise<UserSession>;
37
+ export declare function replaceUserSession(event: SessionEvent, data: OmitWithIndexSignature<UserSession, 'id'>, config?: Partial<SessionConfig>): Promise<UserSession>;
38
38
  /**
39
39
  * Clear the user session and removing the session cookie
40
40
  * @param event The Request (h3) event
41
41
  * @returns true if the session was cleared
42
42
  */
43
- export declare function clearUserSession(event: H3Event, config?: Partial<SessionConfig>): Promise<boolean>;
43
+ export declare function clearUserSession(event: SessionEvent, config?: Partial<SessionConfig>): Promise<boolean>;
44
44
  /**
45
45
  * Require a user session, throw a 401 error if the user is not logged in
46
46
  * @param event
@@ -0,0 +1,4 @@
1
+ import type { H3Event } from 'h3';
2
+ import type { RequestEvent } from '@nuxt/schema';
3
+ export type { RequestEvent };
4
+ export type SessionEvent = H3Event | RequestEvent;
File without changes
@@ -1,3 +1,4 @@
1
1
  export type { User, UserSession, UserSessionRequired, UserSessionComposable, SecureSessionData } from './session.js';
2
+ export type { RequestEvent, SessionEvent } from './event.js';
2
3
  export type { OAuthConfig, OAuthProvider, ATProtoProvider, OnError } from './oauth-config.js';
3
4
  export type { WebAuthnCredential, WebAuthnRegisterEventHandlerOptions, WebAuthnAuthenticateEventHandlerOptions, WebAuthnComposable, WebAuthnUser, } from './webauthn.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.5.29",
3
+ "version": "0.5.31",
4
4
  "description": "Add Authentication to Nuxt applications with secured & sealed cookies sessions.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -41,6 +41,7 @@
41
41
  "dependencies": {
42
42
  "@adonisjs/hash": "^9.1.1",
43
43
  "@nuxt/kit": "^4.3.1",
44
+ "@nuxt/schema": "^4.6.0",
44
45
  "defu": "^6.1.4",
45
46
  "h3": "^1.15.4",
46
47
  "hookable": "^6.0.1",
@@ -76,14 +77,13 @@
76
77
  "@nuxt/devtools": "3.2.1",
77
78
  "@nuxt/eslint-config": "^1.15.1",
78
79
  "@nuxt/module-builder": "^1.0.2",
79
- "@nuxt/schema": "^4.3.1",
80
80
  "@nuxt/test-utils": "^4.0.0",
81
- "@types/node": "latest",
82
81
  "@nuxt/ui": "^4.4.0",
83
82
  "@simplewebauthn/types": "^12.0.0",
83
+ "@types/node": "latest",
84
84
  "changelogen": "^0.6.2",
85
85
  "eslint": "^10.0.0",
86
- "nuxt": "^4.3.1",
86
+ "nuxt": "^4.6.0",
87
87
  "typescript": "5.9.3",
88
88
  "vitest": "^4.0.18",
89
89
  "vue-tsc": "^3.2.4"