nuxt-auth-utils 0.5.2 → 0.5.4
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 +1 -0
- package/dist/module.d.mts +5 -0
- package/dist/module.d.ts +5 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +6 -0
- package/dist/runtime/app/composables/session.js +29 -21
- package/dist/runtime/server/lib/oauth/authentik.d.ts +25 -0
- package/dist/runtime/server/lib/oauth/authentik.js +72 -0
- package/dist/runtime/server/plugins/oauth.d.ts +1 -1
- package/dist/runtime/types/oauth-config.d.ts +1 -1
- package/package.json +9 -8
package/README.md
CHANGED
package/dist/module.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
2
|
import { ScryptConfig } from '@adonisjs/hash/types';
|
|
3
|
+
import { SessionConfig } from 'h3';
|
|
3
4
|
|
|
4
5
|
interface ModuleOptions {
|
|
5
6
|
/**
|
|
@@ -22,6 +23,10 @@ declare module 'nuxt/schema' {
|
|
|
22
23
|
hash: {
|
|
23
24
|
scrypt: ScryptConfig;
|
|
24
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* Session configuration
|
|
28
|
+
*/
|
|
29
|
+
session: SessionConfig;
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
32
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
package/dist/module.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
2
|
import { ScryptConfig } from '@adonisjs/hash/types';
|
|
3
|
+
import { SessionConfig } from 'h3';
|
|
3
4
|
|
|
4
5
|
interface ModuleOptions {
|
|
5
6
|
/**
|
|
@@ -22,6 +23,10 @@ declare module 'nuxt/schema' {
|
|
|
22
23
|
hash: {
|
|
23
24
|
scrypt: ScryptConfig;
|
|
24
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* Session configuration
|
|
28
|
+
*/
|
|
29
|
+
session: SessionConfig;
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
32
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -231,6 +231,12 @@ const module = defineNuxtModule({
|
|
|
231
231
|
domain: "",
|
|
232
232
|
redirectURL: ""
|
|
233
233
|
});
|
|
234
|
+
runtimeConfig.oauth.authentik = defu(runtimeConfig.oauth.authentik, {
|
|
235
|
+
clientId: "",
|
|
236
|
+
clientSecret: "",
|
|
237
|
+
domain: "",
|
|
238
|
+
redirectURL: ""
|
|
239
|
+
});
|
|
234
240
|
}
|
|
235
241
|
});
|
|
236
242
|
|
|
@@ -1,9 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
const useAuthReadyState = () => useState("nuxt-auth-ready", () => false);
|
|
1
|
+
import { appendResponseHeader } from "h3";
|
|
2
|
+
import { useState, computed, useRequestFetch, useRequestEvent } from "#imports";
|
|
4
3
|
export function useUserSession() {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
4
|
+
const serverEvent = import.meta.server ? useRequestEvent() : null;
|
|
5
|
+
const sessionState = useState("nuxt-session", () => ({}));
|
|
6
|
+
const authReadyState = useState("nuxt-auth-ready", () => false);
|
|
7
|
+
const clear = async () => {
|
|
8
|
+
await useRequestFetch()("/api/_auth/session", {
|
|
9
|
+
method: "DELETE",
|
|
10
|
+
onResponse({ response: { headers } }) {
|
|
11
|
+
if (import.meta.server && serverEvent) {
|
|
12
|
+
for (const setCookie of headers.getSetCookie()) {
|
|
13
|
+
appendResponseHeader(serverEvent, "Set-Cookie", setCookie);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
sessionState.value = {};
|
|
19
|
+
};
|
|
20
|
+
const fetch = async () => {
|
|
21
|
+
sessionState.value = await useRequestFetch()("/api/_auth/session", {
|
|
22
|
+
headers: {
|
|
23
|
+
accept: "application/json"
|
|
24
|
+
},
|
|
25
|
+
retry: false
|
|
26
|
+
}).catch(() => ({}));
|
|
27
|
+
if (!authReadyState.value) {
|
|
28
|
+
authReadyState.value = true;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
7
31
|
return {
|
|
8
32
|
ready: computed(() => authReadyState.value),
|
|
9
33
|
loggedIn: computed(() => Boolean(sessionState.value.user)),
|
|
@@ -13,19 +37,3 @@ export function useUserSession() {
|
|
|
13
37
|
clear
|
|
14
38
|
};
|
|
15
39
|
}
|
|
16
|
-
async function fetch() {
|
|
17
|
-
const authReadyState = useAuthReadyState();
|
|
18
|
-
useSessionState().value = await useRequestFetch()("/api/_auth/session", {
|
|
19
|
-
headers: {
|
|
20
|
-
Accept: "text/json"
|
|
21
|
-
},
|
|
22
|
-
retry: false
|
|
23
|
-
}).catch(() => ({}));
|
|
24
|
-
if (!authReadyState.value) {
|
|
25
|
-
authReadyState.value = true;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
async function clear() {
|
|
29
|
-
await $fetch("/api/_auth/session", { method: "DELETE" });
|
|
30
|
-
useSessionState().value = {};
|
|
31
|
-
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { OAuthConfig } from '#auth-utils';
|
|
2
|
+
export interface OAuthAuthentikConfig {
|
|
3
|
+
/**
|
|
4
|
+
* Authentik OAuth Client ID
|
|
5
|
+
* @default process.env.NUXT_OAUTH_AUTHENTIK_CLIENT_ID
|
|
6
|
+
*/
|
|
7
|
+
clientId?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Authentik OAuth Client Secret
|
|
10
|
+
* @default process.env.NUXT_OAUTH_AUTHENTIK_CLIENT_SECRET
|
|
11
|
+
*/
|
|
12
|
+
clientSecret?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Authentik OAuth Domain
|
|
15
|
+
* @example https://<your-authentik-instance>
|
|
16
|
+
* @default process.env.NUXT_OAUTH_AUTHENTIK_DOMAIN
|
|
17
|
+
*/
|
|
18
|
+
domain?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Redirect URL to allow overriding for situations like prod failing to determine public hostname
|
|
21
|
+
* @default process.env.NUXT_OAUTH_AUTHENTIK_REDIRECT_URL or current URL
|
|
22
|
+
*/
|
|
23
|
+
redirectURL?: string;
|
|
24
|
+
}
|
|
25
|
+
export declare function defineOAuthAuthentikEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthAuthentikConfig>): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|
|
@@ -0,0 +1,72 @@
|
|
|
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 defineOAuthAuthentikEventHandler({ config, onSuccess, onError }) {
|
|
7
|
+
return eventHandler(async (event) => {
|
|
8
|
+
config = defu(config, useRuntimeConfig(event).oauth?.authentik);
|
|
9
|
+
const query = getQuery(event);
|
|
10
|
+
if (query.error) {
|
|
11
|
+
const error = createError({
|
|
12
|
+
statusCode: 401,
|
|
13
|
+
message: `Authentik login failed: ${query.error || "Unknown error"}`,
|
|
14
|
+
data: query
|
|
15
|
+
});
|
|
16
|
+
if (!onError) throw error;
|
|
17
|
+
return onError(event, error);
|
|
18
|
+
}
|
|
19
|
+
if (!config.clientId || !config.clientSecret || !config.domain) {
|
|
20
|
+
return handleMissingConfiguration(event, "authentik", ["clientId", "clientSecret", "domain"], onError);
|
|
21
|
+
}
|
|
22
|
+
const authorizationURL = `https://${config.domain}/application/o/authorize/`;
|
|
23
|
+
const tokenURL = `https://${config.domain}/application/o/token/`;
|
|
24
|
+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event);
|
|
25
|
+
if (!query.code) {
|
|
26
|
+
return sendRedirect(
|
|
27
|
+
event,
|
|
28
|
+
withQuery(authorizationURL, {
|
|
29
|
+
response_type: "code",
|
|
30
|
+
client_id: config.clientId,
|
|
31
|
+
redirect_uri: redirectURL,
|
|
32
|
+
scope: ["openid", "profile", "email"].join(" ")
|
|
33
|
+
})
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
const tokens = await requestAccessToken(tokenURL, {
|
|
37
|
+
headers: {
|
|
38
|
+
"Authorization": `Basic ${Buffer.from(`${config.clientId}:${config.clientSecret}`).toString("base64")}`,
|
|
39
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
40
|
+
},
|
|
41
|
+
body: {
|
|
42
|
+
grant_type: "authorization_code",
|
|
43
|
+
client_id: config.clientId,
|
|
44
|
+
redirect_uri: redirectURL,
|
|
45
|
+
code: query.code
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
if (tokens.error) {
|
|
49
|
+
return handleAccessTokenErrorResponse(event, "authentik", tokens, onError);
|
|
50
|
+
}
|
|
51
|
+
const accessToken = tokens.access_token;
|
|
52
|
+
const user = await $fetch(`https://${config.domain}/application/o/userinfo/`, {
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: `Bearer ${accessToken}`,
|
|
55
|
+
Accept: "application/json"
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
if (!user) {
|
|
59
|
+
const error = createError({
|
|
60
|
+
statusCode: 500,
|
|
61
|
+
message: "Could not get Authentik user",
|
|
62
|
+
data: tokens
|
|
63
|
+
});
|
|
64
|
+
if (!onError) throw error;
|
|
65
|
+
return onError(event, error);
|
|
66
|
+
}
|
|
67
|
+
return onSuccess(event, {
|
|
68
|
+
user,
|
|
69
|
+
tokens
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("nitropack
|
|
1
|
+
declare const _default: import("nitropack").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' | 'zitadel' | (string & {});
|
|
2
|
+
export type OAuthProvider = 'auth0' | 'authentik' | '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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-auth-utils",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
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@9.
|
|
11
|
+
"packageManager": "pnpm@9.13.0",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
14
|
"types": "./dist/types.d.ts",
|
|
@@ -36,8 +36,9 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@adonisjs/hash": "^9.0.5",
|
|
39
|
-
"@nuxt/kit": "^3.14.
|
|
39
|
+
"@nuxt/kit": "^3.14.159",
|
|
40
40
|
"defu": "^6.1.4",
|
|
41
|
+
"h3": "^1.13.0",
|
|
41
42
|
"hookable": "^5.5.3",
|
|
42
43
|
"ofetch": "^1.4.1",
|
|
43
44
|
"ohash": "^1.1.4",
|
|
@@ -62,16 +63,16 @@
|
|
|
62
63
|
"@nuxt/devtools": "latest",
|
|
63
64
|
"@nuxt/eslint-config": "^0.6.1",
|
|
64
65
|
"@nuxt/module-builder": "^0.8.4",
|
|
65
|
-
"@nuxt/schema": "^3.14.
|
|
66
|
+
"@nuxt/schema": "^3.14.159",
|
|
66
67
|
"@nuxt/test-utils": "^3.14.4",
|
|
67
|
-
"@nuxt/ui": "^2.
|
|
68
|
-
"@nuxt/ui-pro": "^1.
|
|
68
|
+
"@nuxt/ui": "^2.19.2",
|
|
69
|
+
"@nuxt/ui-pro": "^1.5.0",
|
|
69
70
|
"@simplewebauthn/types": "^11.0.0",
|
|
70
71
|
"changelogen": "^0.5.7",
|
|
71
72
|
"eslint": "^9.14.0",
|
|
72
|
-
"nuxt": "^3.14.
|
|
73
|
+
"nuxt": "^3.14.159",
|
|
73
74
|
"typescript": "^5.6.3",
|
|
74
|
-
"vitest": "^2.1.
|
|
75
|
+
"vitest": "^2.1.5",
|
|
75
76
|
"vue-tsc": "^2.1.10"
|
|
76
77
|
}
|
|
77
78
|
}
|