nuxt-auth-utils 0.5.0 → 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 +1 -1
- package/dist/module.mjs +14 -2
- package/dist/runtime/server/lib/oauth/zitadel.d.ts +37 -0
- package/dist/runtime/server/lib/oauth/zitadel.js +77 -0
- package/dist/runtime/server/lib/webauthn/authenticate.js +6 -4
- package/dist/runtime/server/lib/webauthn/register.d.ts +1 -1
- package/dist/runtime/server/lib/webauthn/register.js +5 -1
- package/dist/runtime/server/plugins/oauth.d.ts +1 -1
- package/dist/runtime/types/oauth-config.d.ts +1 -1
- package/dist/runtime/types/webauthn.d.ts +24 -5
- package/package.json +14 -11
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
2
|
-
import { defineNuxtModule, createResolver, addComponentsDir,
|
|
2
|
+
import { defineNuxtModule, createResolver, addComponentsDir, addImports, addPlugin, addServerPlugin, addServerImportsDir, logger, addServerHandler } from '@nuxt/kit';
|
|
3
3
|
import { join } from 'pathe';
|
|
4
4
|
import { defu } from 'defu';
|
|
5
5
|
import { randomUUID } from 'uncrypto';
|
|
@@ -21,8 +21,14 @@ const module = defineNuxtModule({
|
|
|
21
21
|
nuxt.options.alias["#auth-utils"] = resolver.resolve(
|
|
22
22
|
"./runtime/types/index"
|
|
23
23
|
);
|
|
24
|
+
const composables = [
|
|
25
|
+
{ name: "useUserSession", from: resolver.resolve("./runtime/app/composables/session") }
|
|
26
|
+
];
|
|
27
|
+
if (options.webAuthn) {
|
|
28
|
+
composables.push({ name: "useWebAuthn", from: resolver.resolve("./runtime/app/composables/webauthn") });
|
|
29
|
+
}
|
|
24
30
|
addComponentsDir({ path: resolver.resolve("./runtime/app/components") });
|
|
25
|
-
|
|
31
|
+
addImports(composables);
|
|
26
32
|
addPlugin(resolver.resolve("./runtime/app/plugins/session.server"));
|
|
27
33
|
addPlugin(resolver.resolve("./runtime/app/plugins/session.client"));
|
|
28
34
|
addServerPlugin(resolver.resolve("./runtime/server/plugins/oauth"));
|
|
@@ -219,6 +225,12 @@ const module = defineNuxtModule({
|
|
|
219
225
|
clientSecret: "",
|
|
220
226
|
redirectURL: ""
|
|
221
227
|
});
|
|
228
|
+
runtimeConfig.oauth.zitadel = defu(runtimeConfig.oauth.zitadel, {
|
|
229
|
+
clientId: "",
|
|
230
|
+
clientSecret: "",
|
|
231
|
+
domain: "",
|
|
232
|
+
redirectURL: ""
|
|
233
|
+
});
|
|
222
234
|
}
|
|
223
235
|
});
|
|
224
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
|
+
}
|
|
@@ -16,17 +16,17 @@ export function defineWebAuthnAuthenticateEventHandler({
|
|
|
16
16
|
return eventHandler(async (event) => {
|
|
17
17
|
const url = getRequestURL(event);
|
|
18
18
|
const body = await readBody(event);
|
|
19
|
-
const _config = defu(await getOptions?.(event) ?? {}, useRuntimeConfig(event).webauthn.authenticate, {
|
|
19
|
+
const _config = defu(await getOptions?.(event, body) ?? {}, useRuntimeConfig(event).webauthn.authenticate, {
|
|
20
20
|
rpID: url.hostname
|
|
21
21
|
});
|
|
22
|
-
if (allowCredentials && body.userName) {
|
|
23
|
-
_config.allowCredentials = await allowCredentials(event, body.userName);
|
|
24
|
-
}
|
|
25
22
|
if (!storeChallenge) {
|
|
26
23
|
_config.challenge = "";
|
|
27
24
|
}
|
|
28
25
|
try {
|
|
29
26
|
if (!body.verify) {
|
|
27
|
+
if (allowCredentials && body.userName) {
|
|
28
|
+
_config.allowCredentials = await allowCredentials(event, body.userName);
|
|
29
|
+
}
|
|
30
30
|
const options = await generateAuthenticationOptions(_config);
|
|
31
31
|
const attemptId = bufferToBase64URLString(getRandomValues(new Uint8Array(32)));
|
|
32
32
|
if (storeChallenge) {
|
|
@@ -49,6 +49,8 @@ export function defineWebAuthnAuthenticateEventHandler({
|
|
|
49
49
|
expectedChallenge,
|
|
50
50
|
expectedOrigin: url.origin,
|
|
51
51
|
expectedRPID: url.hostname,
|
|
52
|
+
requireUserVerification: false,
|
|
53
|
+
// TODO: make configurable https://simplewebauthn.dev/docs/advanced/passkeys#verifyauthenticationresponse
|
|
52
54
|
credential: {
|
|
53
55
|
id: credential.id,
|
|
54
56
|
publicKey: new Uint8Array(base64URLStringToBuffer(credential.publicKey)),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ValidateFunction } from 'h3';
|
|
2
2
|
import type { WebAuthnUser, WebAuthnRegisterEventHandlerOptions } from '#auth-utils';
|
|
3
|
-
export declare function defineWebAuthnRegisterEventHandler<T extends WebAuthnUser>({ storeChallenge, getChallenge, getOptions, validateUser, onSuccess, onError, }: WebAuthnRegisterEventHandlerOptions<T>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void | import("@simplewebauthn/server").VerifiedRegistrationResponse | {
|
|
3
|
+
export declare function defineWebAuthnRegisterEventHandler<T extends WebAuthnUser>({ storeChallenge, getChallenge, getOptions, validateUser, excludeCredentials, onSuccess, onError, }: WebAuthnRegisterEventHandlerOptions<T>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void | import("@simplewebauthn/server").VerifiedRegistrationResponse | {
|
|
4
4
|
creationOptions: import("@simplewebauthn/types").PublicKeyCredentialCreationOptionsJSON;
|
|
5
5
|
attemptId: string;
|
|
6
6
|
}>>;
|
|
@@ -9,6 +9,7 @@ export function defineWebAuthnRegisterEventHandler({
|
|
|
9
9
|
getChallenge,
|
|
10
10
|
getOptions,
|
|
11
11
|
validateUser,
|
|
12
|
+
excludeCredentials,
|
|
12
13
|
onSuccess,
|
|
13
14
|
onError
|
|
14
15
|
}) {
|
|
@@ -24,7 +25,7 @@ export function defineWebAuthnRegisterEventHandler({
|
|
|
24
25
|
if (validateUser) {
|
|
25
26
|
user = await validateUserData(body.user, validateUser);
|
|
26
27
|
}
|
|
27
|
-
const _config = defu(await getOptions?.(event) ?? {}, useRuntimeConfig(event).webauthn.register, {
|
|
28
|
+
const _config = defu(await getOptions?.(event, body) ?? {}, useRuntimeConfig(event).webauthn.register, {
|
|
28
29
|
rpID: url.hostname,
|
|
29
30
|
rpName: url.hostname,
|
|
30
31
|
userName: user.userName,
|
|
@@ -38,6 +39,9 @@ export function defineWebAuthnRegisterEventHandler({
|
|
|
38
39
|
}
|
|
39
40
|
try {
|
|
40
41
|
if (!body.verify) {
|
|
42
|
+
if (excludeCredentials) {
|
|
43
|
+
_config.excludeCredentials = await excludeCredentials(event, user.userName);
|
|
44
|
+
}
|
|
41
45
|
const options = await generateRegistrationOptions(_config);
|
|
42
46
|
const attemptId = bufferToBase64URLString(getRandomValues(new Uint8Array(32)));
|
|
43
47
|
if (storeChallenge) {
|
|
@@ -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;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AuthenticatorTransportFuture } from '@simplewebauthn/types';
|
|
1
|
+
import type { AuthenticationResponseJSON, AuthenticatorTransportFuture, RegistrationResponseJSON } from '@simplewebauthn/types';
|
|
2
2
|
import type { Ref } from 'vue';
|
|
3
3
|
import type { H3Event, H3Error, ValidateFunction } from 'h3';
|
|
4
4
|
import type { GenerateAuthenticationOptionsOpts, GenerateRegistrationOptionsOpts, VerifiedAuthenticationResponse, VerifiedRegistrationResponse } from '@simplewebauthn/server';
|
|
@@ -15,7 +15,7 @@ export interface WebAuthnUser {
|
|
|
15
15
|
displayName?: string;
|
|
16
16
|
[key: string]: unknown;
|
|
17
17
|
}
|
|
18
|
-
type
|
|
18
|
+
type CredentialsList = NonNullable<GenerateAuthenticationOptionsOpts['allowCredentials']>;
|
|
19
19
|
type WebAuthnEventHandlerBase<T extends Record<PropertyKey, unknown>> = {
|
|
20
20
|
storeChallenge: (event: H3Event, challenge: string, attemptId: string) => void | Promise<void>;
|
|
21
21
|
getChallenge: (event: H3Event, attemptId: string) => string | Promise<string>;
|
|
@@ -27,21 +27,40 @@ type WebAuthnEventHandlerBase<T extends Record<PropertyKey, unknown>> = {
|
|
|
27
27
|
onSuccess: (event: H3Event, data: T) => void | Promise<void>;
|
|
28
28
|
onError?: (event: H3Event, error: H3Error) => void | Promise<void>;
|
|
29
29
|
};
|
|
30
|
+
export type RegistrationBody<T extends WebAuthnUser> = {
|
|
31
|
+
user: T;
|
|
32
|
+
verify: false;
|
|
33
|
+
} | {
|
|
34
|
+
user: T;
|
|
35
|
+
verify: true;
|
|
36
|
+
attemptId: string;
|
|
37
|
+
response: RegistrationResponseJSON;
|
|
38
|
+
};
|
|
30
39
|
export type WebAuthnRegisterEventHandlerOptions<T extends WebAuthnUser> = WebAuthnEventHandlerBase<{
|
|
31
40
|
user: T;
|
|
32
41
|
credential: WebAuthnCredential;
|
|
33
42
|
registrationInfo: Exclude<VerifiedRegistrationResponse['registrationInfo'], undefined>;
|
|
34
43
|
}> & {
|
|
35
|
-
getOptions?: (event: H3Event) => GenerateRegistrationOptionsOpts | Promise<GenerateRegistrationOptionsOpts
|
|
44
|
+
getOptions?: (event: H3Event, body: RegistrationBody<T>) => Partial<GenerateRegistrationOptionsOpts> | Promise<Partial<GenerateRegistrationOptionsOpts>>;
|
|
36
45
|
validateUser?: ValidateFunction<T>;
|
|
46
|
+
excludeCredentials?: (event: H3Event, userName: string) => CredentialsList | Promise<CredentialsList>;
|
|
47
|
+
};
|
|
48
|
+
export type AuthenticationBody = {
|
|
49
|
+
verify: false;
|
|
50
|
+
userName?: string;
|
|
51
|
+
} | {
|
|
52
|
+
verify: true;
|
|
53
|
+
attemptId: string;
|
|
54
|
+
userName?: string;
|
|
55
|
+
response: AuthenticationResponseJSON;
|
|
37
56
|
};
|
|
38
57
|
export type WebAuthnAuthenticateEventHandlerOptions<T extends WebAuthnCredential> = WebAuthnEventHandlerBase<{
|
|
39
58
|
credential: T;
|
|
40
59
|
authenticationInfo: Exclude<VerifiedAuthenticationResponse['authenticationInfo'], undefined>;
|
|
41
60
|
}> & {
|
|
42
|
-
getOptions?: (event: H3Event) => Partial<GenerateAuthenticationOptionsOpts> | Promise<Partial<GenerateAuthenticationOptionsOpts>>;
|
|
61
|
+
getOptions?: (event: H3Event, body: AuthenticationBody) => Partial<GenerateAuthenticationOptionsOpts> | Promise<Partial<GenerateAuthenticationOptionsOpts>>;
|
|
43
62
|
getCredential: (event: H3Event, credentialID: string) => T | Promise<T>;
|
|
44
|
-
allowCredentials?: (event: H3Event, userName: string) =>
|
|
63
|
+
allowCredentials?: (event: H3Event, userName: string) => CredentialsList | Promise<CredentialsList>;
|
|
45
64
|
};
|
|
46
65
|
export interface WebAuthnComposable {
|
|
47
66
|
/**
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-auth-utils",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Add Authentication to Nuxt applications with secured & sealed cookies sessions.",
|
|
5
|
-
"repository":
|
|
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
|
-
"packageManager": "pnpm@9.12.
|
|
11
|
+
"packageManager": "pnpm@9.12.3",
|
|
9
12
|
"exports": {
|
|
10
13
|
".": {
|
|
11
14
|
"types": "./dist/types.d.ts",
|
|
@@ -33,7 +36,7 @@
|
|
|
33
36
|
},
|
|
34
37
|
"dependencies": {
|
|
35
38
|
"@adonisjs/hash": "^9.0.5",
|
|
36
|
-
"@nuxt/kit": "^3.
|
|
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,20 +58,20 @@
|
|
|
55
58
|
}
|
|
56
59
|
},
|
|
57
60
|
"devDependencies": {
|
|
58
|
-
"@iconify-json/simple-icons": "^1.2.
|
|
61
|
+
"@iconify-json/simple-icons": "^1.2.11",
|
|
59
62
|
"@nuxt/devtools": "latest",
|
|
60
|
-
"@nuxt/eslint-config": "^0.6.
|
|
63
|
+
"@nuxt/eslint-config": "^0.6.1",
|
|
61
64
|
"@nuxt/module-builder": "^0.8.4",
|
|
62
|
-
"@nuxt/schema": "^3.
|
|
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.
|
|
69
|
-
"nuxt": "^3.
|
|
71
|
+
"eslint": "^9.14.0",
|
|
72
|
+
"nuxt": "^3.14.0",
|
|
70
73
|
"typescript": "^5.6.3",
|
|
71
|
-
"vitest": "^2.1.
|
|
72
|
-
"vue-tsc": "^2.1.
|
|
74
|
+
"vitest": "^2.1.4",
|
|
75
|
+
"vue-tsc": "^2.1.10"
|
|
73
76
|
}
|
|
74
77
|
}
|