nuxt-auth-utils 0.5.26 → 0.5.27

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
@@ -236,6 +236,7 @@ It can also be set using environment variables:
236
236
  - LinkedIn
237
237
  - LiveChat
238
238
  - Microsoft
239
+ - OIDC / OpenID Connect (Generic)
239
240
  - Okta
240
241
  - Ory
241
242
  - PayPal
@@ -420,7 +421,7 @@ export default defineWebAuthnRegisterEventHandler({
420
421
  // If he registers a new account with credentials
421
422
  return z.object({
422
423
  // we want the userName to be a valid email
423
- userName: z.string().email()
424
+ userName: z.string().email()
424
425
  }).parse(userBody)
425
426
  },
426
427
  async onSuccess(event, { credential, user }) {
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "auth-utils",
3
3
  "configKey": "auth",
4
- "version": "0.5.26",
4
+ "version": "0.5.27",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { readFile, writeFile } from 'node:fs/promises';
2
1
  import { defineNuxtModule, createResolver, addComponentsDir, addImports, addPlugin, addServerPlugin, addServerImportsDir, logger, addServerHandler } from '@nuxt/kit';
3
- import { join } from 'pathe';
4
2
  import { defu } from 'defu';
3
+ import { readFile, writeFile } from 'node:fs/promises';
4
+ import { join } from 'pathe';
5
5
  import { randomUUID } from 'uncrypto';
6
6
  import { atprotoProviders, atprotoProviderDefaultClientMetadata, getClientMetadataFilename } from '../dist/runtime/utils/atproto.js';
7
7
 
@@ -393,6 +393,13 @@ const module$1 = defineNuxtModule({
393
393
  redirectURL: "",
394
394
  scope: []
395
395
  });
396
+ runtimeConfig.oauth.oidc = defu(runtimeConfig.oauth.oidc, {
397
+ clientId: "",
398
+ clientSecret: "",
399
+ openidConfig: "",
400
+ redirectURL: "",
401
+ scope: []
402
+ });
396
403
  }
397
404
  });
398
405
 
@@ -0,0 +1,216 @@
1
+ import type { OAuthConfig } from '#auth-utils';
2
+ export interface OAuthOidcConfig {
3
+ /**
4
+ * OAuth Client ID
5
+ *
6
+ * @default process.env.NUXT_OAUTH_OIDC_CLIENT_ID
7
+ */
8
+ clientId?: string;
9
+ /**
10
+ * OAuth Client secret.
11
+ * If unset, PKCE will be used where no client secret is needed.
12
+ *
13
+ * @default process.env.NUXT_OAUTH_OIDC_CLIENT_SECRET
14
+ */
15
+ clientSecret?: string;
16
+ /**
17
+ * OpenID configuration. If a string is passed, it is considered to be the full URL to the OpenID configuration endpoint
18
+ * where all required endpoints are listed and fetched from automatically.
19
+ *
20
+ * Alternatively, an object can be set with the required endpoint URLs.
21
+ *
22
+ * @default process.env.NUXT_OAUTH_OIDC_OPENID_CONFIG
23
+ * @example "https://my-provider.com/.well-known/openid-configuration"
24
+ */
25
+ openidConfig?: string | OIDCConfiguration;
26
+ /**
27
+ * OAuth Scope
28
+ *
29
+ * @default ['openid']
30
+ * @example ['openid', 'profile', 'email']
31
+ */
32
+ scope?: string[];
33
+ /**
34
+ * Redirect URL to to allow overriding for situations like prod failing to determine public hostname
35
+ *
36
+ * @default process.env.NUXT_OAUTH_OIDC_REDIRECT_URL
37
+ */
38
+ redirectURL?: string;
39
+ /**
40
+ * Additional custom params that are passed to the specific endpoint requests.
41
+ * Can be used to provide custom (query) params.
42
+ */
43
+ params?: Partial<Record<'authorization_endpoint' | 'token_endpoint' | 'userinfo_endpoint', object>>;
44
+ }
45
+ /**
46
+ * Standard OIDC claims.
47
+ *
48
+ * @see: https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
49
+ */
50
+ interface OidcUser {
51
+ /**
52
+ * Subject - Identifier for the End-User at the Issuer.
53
+ */
54
+ sub: string;
55
+ /**
56
+ * End-User's full name in displayable form including all name parts,
57
+ * possibly including titles and suffixes, ordered according to the
58
+ * End-User's locale and preferences.
59
+ */
60
+ name?: string;
61
+ /**
62
+ * Given name(s) or first name(s) of the End-User. Note that in some cultures,
63
+ * people can have multiple given names; all can be present, with the names
64
+ * being separated by space characters.
65
+ */
66
+ given_name?: string;
67
+ /**
68
+ * Surname(s) or last name(s) of the End-User. Note that in some cultures,
69
+ * people can have multiple family names or no family name; all can be present,
70
+ * with the names being separated by space characters.
71
+ */
72
+ family_name?: string;
73
+ /**
74
+ * Middle name(s) of the End-User. Note that in some cultures, people can have
75
+ * multiple middle names; all can be present, with the names being separated by
76
+ * space characters. Also note that in some cultures, middle names are not used.
77
+ */
78
+ middle_name?: string;
79
+ /**
80
+ * Casual name of the End-User that may or may not be the same as the given_name.
81
+ * For instance, a nickname value of Mike might be returned alongside a given_name
82
+ * value of Michael.
83
+ */
84
+ nickname?: string;
85
+ /**
86
+ * Shorthand name by which the End-User wishes to be referred to at the RP, such as
87
+ * janedoe or j.doe. This value MAY be any valid JSON string including special
88
+ * characters such as @, /, or whitespace. The RP MUST NOT rely upon this value
89
+ * being unique.
90
+ */
91
+ preferred_username?: string;
92
+ /**
93
+ * URL of the End-User's profile page. The contents of this Web page SHOULD be
94
+ * about the End-User.
95
+ */
96
+ profile?: string;
97
+ /**
98
+ * URL of the End-User's profile picture. This URL MUST refer to an image file
99
+ * (for example, a PNG, JPEG, or GIF image file), rather than to a Web page
100
+ * containing an image. Note that this URL SHOULD specifically reference a profile
101
+ * photo of the End-User suitable for displaying when describing the End-User,
102
+ * rather than an arbitrary photo taken by the End-User.
103
+ */
104
+ picture?: string;
105
+ /**
106
+ * URL of the End-User's Web page or blog. This Web page SHOULD contain information
107
+ * published by the End-User or an organization that the End-User is affiliated with.
108
+ */
109
+ website?: string;
110
+ /**
111
+ * End-User's preferred e-mail address. Its value MUST conform to the RFC 5322
112
+ * addr-spec syntax. The RP MUST NOT rely upon this value being unique.
113
+ */
114
+ email?: string;
115
+ /**
116
+ * True if the End-User's e-mail address has been verified; otherwise false.
117
+ * When this Claim Value is true, this means that the OP took affirmative steps
118
+ * to ensure that this e-mail address was controlled by the End-User at the time
119
+ * the verification was performed. The means by which an e-mail address is verified
120
+ * is context specific, and dependent upon the trust framework or contractual
121
+ * agreements within which the parties are operating.
122
+ */
123
+ email_verified?: boolean;
124
+ /**
125
+ * End-User's gender. Values defined by this specification are female and male.
126
+ * Other values MAY be used when neither of the defined values are applicable.
127
+ */
128
+ gender?: string;
129
+ /**
130
+ * End-User's birthday, represented as an ISO 8601-1 YYYY-MM-DD format. The year
131
+ * MAY be 0000, indicating that it is omitted. To represent only the year, YYYY
132
+ * format is allowed. Note that depending on the underlying platform's date related
133
+ * function, providing just year can result in varying month and day, so the
134
+ * implementers need to take this factor into account to correctly process the dates.
135
+ */
136
+ birthdate?: string;
137
+ /**
138
+ * String from IANA Time Zone Database representing the End-User's time zone.
139
+ * For example, Europe/Paris or America/Los_Angeles.
140
+ */
141
+ zoneinfo?: string;
142
+ /**
143
+ * End-User's locale, represented as a BCP47 language tag. This is typically an
144
+ * ISO 639 Alpha-2 language code in lowercase and an ISO 3166-1 Alpha-2 country
145
+ * code in uppercase, separated by a dash. For example, en-US or fr-CA. As a
146
+ * compatibility note, some implementations have used an underscore as the separator
147
+ * rather than a dash, for example, en_US; Relying Parties MAY choose to accept
148
+ * this locale syntax as well.
149
+ */
150
+ locale?: string;
151
+ /**
152
+ * End-User's preferred telephone number. E.164 is RECOMMENDED as the format of
153
+ * this Claim, for example, +1 (555) 555-5555 or +56 (2) 687 2400. If the phone
154
+ * number contains an extension, it is RECOMMENDED that the extension be represented
155
+ * using the RFC 3966 extension syntax, for example, +1 (555) 555-5555;ext=5678.
156
+ */
157
+ phone_number?: string;
158
+ /**
159
+ * True if the End-User's phone number has been verified; otherwise false. When
160
+ * this Claim Value is true, this means that the OP took affirmative steps to
161
+ * ensure that this phone number was controlled by the End-User at the time the
162
+ * verification was performed. The means by which a phone number is verified is
163
+ * context specific, and dependent upon the trust framework or contractual
164
+ * agreements within which the parties are operating. When true, the phone_number
165
+ * Claim MUST be in E.164 format and any extensions MUST be represented in RFC 3966 format.
166
+ */
167
+ phone_number_verified?: boolean;
168
+ /**
169
+ * End-User's preferred postal address.
170
+ */
171
+ address?: AddressClaim;
172
+ /**
173
+ * Time the End-User's information was last updated. Its value is a JSON number
174
+ * representing the number of seconds from 1970-01-01T00:00:00Z as measured in
175
+ * UTC until the date/time.
176
+ */
177
+ updated_at?: number;
178
+ }
179
+ /**
180
+ * Address claim structure as defined in OpenID Connect specification
181
+ */
182
+ interface AddressClaim {
183
+ /** Full mailing address, formatted for display or use on a mailing label */
184
+ formatted?: string;
185
+ /** Full street address component, which may include house number, street name, post office box, and multi-line extended street address information */
186
+ street_address?: string;
187
+ /** City or locality component */
188
+ locality?: string;
189
+ /** State, province, prefecture, or region component */
190
+ region?: string;
191
+ /** Zip code or postal code component */
192
+ postal_code?: string;
193
+ /** Country name component */
194
+ country?: string;
195
+ }
196
+ interface OidcTokens {
197
+ access_token: string;
198
+ token_type: string;
199
+ scope?: string;
200
+ id_token?: string;
201
+ refresh_token?: string;
202
+ expires_in?: number;
203
+ }
204
+ interface OIDCConfiguration {
205
+ authorization_endpoint: string;
206
+ token_endpoint: string;
207
+ userinfo_endpoint?: string;
208
+ }
209
+ /**
210
+ * Event handler for generic OAuth using OIDC and PKCE.
211
+ */
212
+ export declare function defineOAuthOidcEventHandler<TUser = OidcUser>({ config, onSuccess, onError }: OAuthConfig<OAuthOidcConfig, {
213
+ user: TUser;
214
+ tokens: OidcTokens;
215
+ }>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
216
+ export {};
@@ -0,0 +1,83 @@
1
+ import { useRuntimeConfig } from "#imports";
2
+ import { defu } from "defu";
3
+ import { createError, eventHandler, getQuery, sendRedirect } from "h3";
4
+ import { withQuery } from "ufo";
5
+ import { getOAuthRedirectURL, handleAccessTokenErrorResponse, handleInvalidState, handleMissingConfiguration, handlePkceVerifier, handleState, requestAccessToken } from "../utils.js";
6
+ export function defineOAuthOidcEventHandler({ config, onSuccess, onError }) {
7
+ return eventHandler(async (event) => {
8
+ config = defu(config, useRuntimeConfig(event).oauth.oidc, {
9
+ scope: ["openid"]
10
+ });
11
+ const query = getQuery(event);
12
+ if (query.error) {
13
+ const error = createError({
14
+ statusCode: 401,
15
+ message: `OIDC login failed: ${query.error || "Unknown error"}`,
16
+ data: query
17
+ });
18
+ if (!onError) throw error;
19
+ return onError(event, error);
20
+ }
21
+ if (!config.clientId || !config.openidConfig) {
22
+ return handleMissingConfiguration(event, "oidc", ["clientId", "openidConfig"], onError);
23
+ }
24
+ const oidcConfig = typeof config.openidConfig === "string" ? await $fetch(config.openidConfig) : config.openidConfig;
25
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
26
+ const state = await handleState(event);
27
+ const verifier = !config.clientSecret ? await handlePkceVerifier(event) : void 0;
28
+ if (!query.code) {
29
+ config.scope = config.scope || [];
30
+ const authQuery = {
31
+ client_id: config.clientId,
32
+ redirect_uri: redirectURL,
33
+ scope: config.scope.join(" "),
34
+ state,
35
+ response_type: "code",
36
+ ...config.params?.authorization_endpoint
37
+ };
38
+ if (verifier) {
39
+ authQuery.code_challenge = verifier.code_challenge;
40
+ authQuery.code_challenge_method = verifier.code_challenge_method;
41
+ }
42
+ return sendRedirect(
43
+ event,
44
+ withQuery(oidcConfig.authorization_endpoint, authQuery)
45
+ );
46
+ }
47
+ if (query.state !== state) {
48
+ return handleInvalidState(event, "oidc", onError);
49
+ }
50
+ const tokenQuery = {
51
+ grant_type: "authorization_code",
52
+ client_id: config.clientId,
53
+ client_secret: config.clientSecret,
54
+ redirect_uri: redirectURL,
55
+ code: query.code,
56
+ ...config.params?.token_endpoint
57
+ };
58
+ if (verifier) {
59
+ tokenQuery.code_verifier = verifier.code_verifier;
60
+ }
61
+ const tokens = await requestAccessToken(oidcConfig.token_endpoint, {
62
+ body: tokenQuery
63
+ });
64
+ if (tokens.error) {
65
+ return handleAccessTokenErrorResponse(event, "oidc", tokens, onError);
66
+ }
67
+ let user = {};
68
+ if (oidcConfig.userinfo_endpoint) {
69
+ user = await $fetch(oidcConfig.userinfo_endpoint, {
70
+ headers: {
71
+ Authorization: `${tokens.token_type} ${tokens.access_token}`
72
+ },
73
+ params: {
74
+ ...config.params?.userinfo_endpoint
75
+ }
76
+ });
77
+ }
78
+ return onSuccess(event, {
79
+ user,
80
+ tokens
81
+ });
82
+ });
83
+ }
@@ -1,14 +1,16 @@
1
+ import { useRuntimeConfig } from "#imports";
2
+ import { defu } from "defu";
1
3
  import { eventHandler, getQuery, sendRedirect } from "h3";
2
4
  import { withQuery } from "ufo";
3
- import { defu } from "defu";
4
- import { randomUUID } from "uncrypto";
5
5
  import {
6
- handleMissingConfiguration,
7
- handleAccessTokenErrorResponse,
8
6
  getOAuthRedirectURL,
7
+ handleAccessTokenErrorResponse,
8
+ handleInvalidState,
9
+ handleMissingConfiguration,
10
+ handlePkceVerifier,
11
+ handleState,
9
12
  requestAccessToken
10
13
  } from "../utils.js";
11
- import { useRuntimeConfig } from "#imports";
12
14
  export function defineOAuthXEventHandler({
13
15
  config,
14
16
  onSuccess,
@@ -19,16 +21,15 @@ export function defineOAuthXEventHandler({
19
21
  authorizationURL: "https://x.com/i/oauth2/authorize",
20
22
  tokenURL: "https://api.x.com/2/oauth2/token",
21
23
  userURL: "https://api.x.com/2/users/me",
22
- authorizationParams: {
23
- state: randomUUID(),
24
- code_challenge: randomUUID()
25
- }
24
+ authorizationParams: {}
26
25
  });
27
26
  const query = getQuery(event);
28
27
  if (!config.clientId || !config.clientSecret) {
29
28
  return handleMissingConfiguration(event, "x", ["clientId", "clientSecret"], onError);
30
29
  }
31
30
  const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
31
+ const pkce = await handlePkceVerifier(event);
32
+ const state = await handleState(event);
32
33
  if (!query.code) {
33
34
  config.scope = config.scope || [
34
35
  "tweet.read",
@@ -40,13 +41,18 @@ export function defineOAuthXEventHandler({
40
41
  withQuery(config.authorizationURL, {
41
42
  response_type: "code",
42
43
  client_id: config.clientId,
43
- code_challenge_method: "plain",
44
44
  redirect_uri: redirectURL,
45
45
  scope: config.scope.join(" "),
46
- ...config.authorizationParams
46
+ ...config.authorizationParams,
47
+ code_challenge: pkce.code_challenge,
48
+ code_challenge_method: pkce.code_challenge_method,
49
+ state
47
50
  })
48
51
  );
49
52
  }
53
+ if (query.state !== state) {
54
+ return handleInvalidState(event, "x", onError);
55
+ }
50
56
  const tokens = await requestAccessToken(config.tokenURL, {
51
57
  headers: {
52
58
  Authorization: `Basic ${Buffer.from(
@@ -55,7 +61,7 @@ export function defineOAuthXEventHandler({
55
61
  },
56
62
  params: {
57
63
  grant_type: "authorization_code",
58
- code_verifier: config.authorizationParams?.code_challenge,
64
+ code_verifier: pkce.code_verifier,
59
65
  redirect_uri: redirectURL,
60
66
  code: query.code
61
67
  }
@@ -26,7 +26,7 @@ export interface RequestAccessTokenOptions {
26
26
  *
27
27
  * @see https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.3
28
28
  */
29
- export declare function requestAccessToken(url: string, options: RequestAccessTokenOptions): Promise<any>;
29
+ export declare function requestAccessToken<T = any>(url: string, options: RequestAccessTokenOptions): Promise<T>;
30
30
  /**
31
31
  * Handle OAuth access token error response
32
32
  *
@@ -84,7 +84,8 @@ export function defineWebAuthnRegisterEventHandler({
84
84
  publicKey: bufferToBase64URLString(verification.registrationInfo.credential.publicKey.buffer),
85
85
  counter: verification.registrationInfo.credential.counter,
86
86
  backedUp: verification.registrationInfo.credentialBackedUp,
87
- transports: verification.registrationInfo.credential.transports
87
+ transports: verification.registrationInfo.credential.transports,
88
+ aaguid: verification.registrationInfo.aaguid
88
89
  },
89
90
  registrationInfo: verification.registrationInfo
90
91
  });
@@ -1,6 +1,6 @@
1
1
  import type { H3Event, H3Error } from 'h3';
2
2
  export type ATProtoProvider = 'bluesky';
3
- export type OAuthProvider = ATProtoProvider | 'atlassian' | 'auth0' | 'authentik' | 'azureb2c' | 'battledotnet' | 'cognito' | 'discord' | 'dropbox' | 'facebook' | 'gitea' | 'github' | 'gitlab' | 'google' | 'hubspot' | 'instagram' | 'kick' | 'keycloak' | 'line' | 'linear' | 'linkedin' | 'microsoft' | 'paypal' | 'polar' | 'spotify' | 'seznam' | 'steam' | 'strava' | 'tiktok' | 'twitch' | 'vk' | 'workos' | 'x' | 'xsuaa' | 'yandex' | 'zitadel' | 'apple' | 'livechat' | 'salesforce' | 'slack' | 'heroku' | 'roblox' | 'okta' | 'ory' | 'shopifyCustomer' | (string & {});
3
+ export type OAuthProvider = ATProtoProvider | 'atlassian' | 'auth0' | 'authentik' | 'azureb2c' | 'battledotnet' | 'cognito' | 'discord' | 'dropbox' | 'facebook' | 'gitea' | 'github' | 'gitlab' | 'google' | 'hubspot' | 'instagram' | 'kick' | 'keycloak' | 'line' | 'linear' | 'linkedin' | 'microsoft' | 'paypal' | 'polar' | 'spotify' | 'seznam' | 'steam' | 'strava' | 'tiktok' | 'twitch' | 'vk' | 'workos' | 'x' | 'xsuaa' | 'yandex' | 'zitadel' | 'apple' | 'livechat' | 'salesforce' | 'slack' | 'heroku' | 'roblox' | 'okta' | 'ory' | 'shopifyCustomer' | 'oicd' | (string & {});
4
4
  export type OnError = (event: H3Event, error: H3Error) => Promise<void> | void;
5
5
  export interface OAuthConfig<TConfig, TResult = {
6
6
  user: any;
@@ -8,6 +8,7 @@ export interface WebAuthnCredential {
8
8
  counter: number;
9
9
  backedUp: boolean;
10
10
  transports?: AuthenticatorTransportFuture[];
11
+ aaguid?: string;
11
12
  [key: string]: unknown;
12
13
  }
13
14
  export interface WebAuthnUser {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.5.26",
3
+ "version": "0.5.27",
4
4
  "description": "Add Authentication to Nuxt applications with secured & sealed cookies sessions.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "license": "MIT",
10
10
  "type": "module",
11
- "packageManager": "pnpm@10.25.0",
11
+ "packageManager": "pnpm@10.28.0",
12
12
  "exports": {
13
13
  ".": {
14
14
  "types": "./dist/types.d.mts",
@@ -40,10 +40,10 @@
40
40
  },
41
41
  "dependencies": {
42
42
  "@adonisjs/hash": "^9.1.1",
43
- "@nuxt/kit": "^4.2.1",
43
+ "@nuxt/kit": "^4.2.2",
44
44
  "defu": "^6.1.4",
45
45
  "h3": "^1.15.4",
46
- "hookable": "^5.5.3",
46
+ "hookable": "^6.0.1",
47
47
  "jose": "^6.1.3",
48
48
  "ofetch": "^1.5.1",
49
49
  "openid-client": "^6.8.1",
@@ -72,20 +72,20 @@
72
72
  }
73
73
  },
74
74
  "devDependencies": {
75
- "@iconify-json/simple-icons": "^1.2.62",
75
+ "@iconify-json/simple-icons": "^1.2.66",
76
76
  "@nuxt/devtools": "3.1.1",
77
- "@nuxt/eslint-config": "^1.11.0",
77
+ "@nuxt/eslint-config": "^1.12.1",
78
78
  "@nuxt/module-builder": "^1.0.2",
79
- "@nuxt/schema": "^4.2.1",
80
- "@nuxt/test-utils": "^3.21.0",
79
+ "@nuxt/schema": "^4.2.2",
80
+ "@nuxt/test-utils": "^3.23.0",
81
81
  "@types/node": "latest",
82
- "@nuxt/ui": "^4.2.1",
82
+ "@nuxt/ui": "^4.3.0",
83
83
  "@simplewebauthn/types": "^12.0.0",
84
84
  "changelogen": "^0.6.2",
85
- "eslint": "^9.39.1",
86
- "nuxt": "^4.2.1",
85
+ "eslint": "^9.39.2",
86
+ "nuxt": "^4.2.2",
87
87
  "typescript": "5.9.3",
88
- "vitest": "^4.0.15",
89
- "vue-tsc": "^3.1.8"
88
+ "vitest": "^4.0.17",
89
+ "vue-tsc": "^3.2.2"
90
90
  }
91
91
  }