nuxt-auth-utils 0.5.1 → 0.5.2

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/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "auth-utils",
3
3
  "configKey": "auth",
4
- "version": "0.5.1",
4
+ "version": "0.5.2",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -225,6 +225,12 @@ const module = defineNuxtModule({
225
225
  clientSecret: "",
226
226
  redirectURL: ""
227
227
  });
228
+ runtimeConfig.oauth.zitadel = defu(runtimeConfig.oauth.zitadel, {
229
+ clientId: "",
230
+ clientSecret: "",
231
+ domain: "",
232
+ redirectURL: ""
233
+ });
228
234
  }
229
235
  });
230
236
 
@@ -0,0 +1,37 @@
1
+ import type { OAuthConfig } from '#auth-utils';
2
+ export interface OAuthZitadelConfig {
3
+ /**
4
+ * ZITADEL OAuth Client ID
5
+ * @default process.env.NUXT_OAUTH_ZITADEL_CLIENT_ID
6
+ */
7
+ clientId?: string;
8
+ /**
9
+ * ZITADEL OAuth Client Secret
10
+ * @default process.env.NUXT_OAUTH_ZITADEL_CLIENT_SECRET
11
+ */
12
+ clientSecret?: string;
13
+ /**
14
+ * ZITADEL OAuth Domain
15
+ * @example <your-zitadel-instance>.zitadel.cloud
16
+ * @default process.env.NUXT_OAUTH_ZITADEL_DOMAIN
17
+ */
18
+ domain?: string;
19
+ /**
20
+ * ZITADEL OAuth Scope
21
+ * @default ['openid']
22
+ * @see https://zitadel.com/docs/apis/openidoauth/scopes
23
+ * @example ['openid', 'profile', 'email']
24
+ */
25
+ scope?: string[];
26
+ /**
27
+ * Extra authorization parameters to provide to the authorization URL
28
+ * @example { ui_locales: 'de-CH de en' }
29
+ */
30
+ authorizationParams?: Record<string, string>;
31
+ /**
32
+ * Redirect URL to allow overriding for situations like prod failing to determine public hostname
33
+ * @default process.env.NUXT_OAUTH_ZITADEL_REDIRECT_URL or current URL
34
+ */
35
+ redirectURL?: string;
36
+ }
37
+ export declare function defineOAuthZitadelEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthZitadelConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
@@ -0,0 +1,77 @@
1
+ import { eventHandler, getQuery, sendRedirect } from "h3";
2
+ import { withQuery } from "ufo";
3
+ import { defu } from "defu";
4
+ import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from "../utils.js";
5
+ import { useRuntimeConfig, createError } from "#imports";
6
+ export function defineOAuthZitadelEventHandler({ config, onSuccess, onError }) {
7
+ return eventHandler(async (event) => {
8
+ config = defu(config, useRuntimeConfig(event).oauth?.zitadel, {
9
+ authorizationParams: {}
10
+ });
11
+ const query = getQuery(event);
12
+ if (query.error) {
13
+ const error = createError({
14
+ statusCode: 401,
15
+ message: `Zitadel 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.clientSecret || !config.domain) {
22
+ return handleMissingConfiguration(event, "zitadel", ["clientId", "clientSecret", "issuerUrl"], onError);
23
+ }
24
+ const authorizationURL = `https://${config.domain}/oauth/v2/authorize`;
25
+ const tokenURL = `https://${config.domain}/oauth/v2/token`;
26
+ const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
27
+ if (!query.code) {
28
+ config.scope = config.scope || ["openid"];
29
+ return sendRedirect(
30
+ event,
31
+ withQuery(authorizationURL, {
32
+ response_type: "code",
33
+ client_id: config.clientId,
34
+ redirect_uri: redirectURL,
35
+ scope: config.scope.join(" "),
36
+ ...config.authorizationParams
37
+ })
38
+ );
39
+ }
40
+ const tokens = await requestAccessToken(tokenURL, {
41
+ headers: {
42
+ "Authorization": `Basic ${Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64")}`,
43
+ "Content-Type": "application/x-www-form-urlencoded"
44
+ },
45
+ body: {
46
+ grant_type: "authorization_code",
47
+ client_id: config.clientId,
48
+ redirect_uri: redirectURL,
49
+ code: query.code
50
+ }
51
+ });
52
+ if (tokens.error) {
53
+ return handleAccessTokenErrorResponse(event, "zitadel", tokens, onError);
54
+ }
55
+ const accessToken = tokens.access_token;
56
+ const user = await $fetch(`https://${config.domain}/oidc/v1/userinfo`, {
57
+ headers: {
58
+ Authorization: `Bearer ${accessToken}`,
59
+ Accept: "application/json"
60
+ }
61
+ });
62
+ console.log("Zitadel user:", user);
63
+ if (!user) {
64
+ const error = createError({
65
+ statusCode: 500,
66
+ message: "Could not get Zitadel user",
67
+ data: tokens
68
+ });
69
+ if (!onError) throw error;
70
+ return onError(event, error);
71
+ }
72
+ return onSuccess(event, {
73
+ user,
74
+ tokens
75
+ });
76
+ });
77
+ }
@@ -1,2 +1,2 @@
1
- declare const _default: import("nitropack").NitroAppPlugin;
1
+ declare const _default: import("nitropack/types").NitroAppPlugin;
2
2
  export default _default;
@@ -1,5 +1,5 @@
1
1
  import type { H3Event, H3Error } from 'h3';
2
- export type OAuthProvider = 'auth0' | 'battledotnet' | 'cognito' | 'discord' | 'dropbox' | 'facebook' | 'github' | 'gitlab' | 'google' | 'instagram' | 'keycloak' | 'linear' | 'linkedin' | 'microsoft' | 'paypal' | 'polar' | 'spotify' | 'steam' | 'tiktok' | 'twitch' | 'vk' | 'x' | 'xsuaa' | 'yandex' | (string & {});
2
+ export type OAuthProvider = 'auth0' | 'battledotnet' | 'cognito' | 'discord' | 'dropbox' | 'facebook' | 'github' | 'gitlab' | 'google' | 'instagram' | 'keycloak' | 'linear' | 'linkedin' | 'microsoft' | 'paypal' | 'polar' | 'spotify' | 'steam' | 'tiktok' | 'twitch' | 'vk' | 'x' | 'xsuaa' | 'yandex' | 'zitadel' | (string & {});
3
3
  export type OnError = (event: H3Event, error: H3Error) => Promise<void> | void;
4
4
  export interface OAuthConfig<TConfig, TUser = any, TTokens = any> {
5
5
  config?: TConfig;
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "nuxt-auth-utils",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Add Authentication to Nuxt applications with secured & sealed cookies sessions.",
5
- "repository": "atinux/nuxt-auth-utils",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/atinux/nuxt-auth-utils.git"
8
+ },
6
9
  "license": "MIT",
7
10
  "type": "module",
8
11
  "packageManager": "pnpm@9.12.3",
@@ -33,7 +36,7 @@
33
36
  },
34
37
  "dependencies": {
35
38
  "@adonisjs/hash": "^9.0.5",
36
- "@nuxt/kit": "^3.13.2",
39
+ "@nuxt/kit": "^3.14.0",
37
40
  "defu": "^6.1.4",
38
41
  "hookable": "^5.5.3",
39
42
  "ofetch": "^1.4.1",
@@ -55,18 +58,18 @@
55
58
  }
56
59
  },
57
60
  "devDependencies": {
58
- "@iconify-json/simple-icons": "^1.2.10",
61
+ "@iconify-json/simple-icons": "^1.2.11",
59
62
  "@nuxt/devtools": "latest",
60
63
  "@nuxt/eslint-config": "^0.6.1",
61
64
  "@nuxt/module-builder": "^0.8.4",
62
- "@nuxt/schema": "^3.13.2",
65
+ "@nuxt/schema": "^3.14.0",
63
66
  "@nuxt/test-utils": "^3.14.4",
64
67
  "@nuxt/ui": "^2.18.7",
65
68
  "@nuxt/ui-pro": "^1.4.4",
66
69
  "@simplewebauthn/types": "^11.0.0",
67
70
  "changelogen": "^0.5.7",
68
- "eslint": "^9.13.0",
69
- "nuxt": "^3.13.2",
71
+ "eslint": "^9.14.0",
72
+ "nuxt": "^3.14.0",
70
73
  "typescript": "^5.6.3",
71
74
  "vitest": "^2.1.4",
72
75
  "vue-tsc": "^2.1.10"