@thecodeorigin/auth 0.0.5 → 0.0.7
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/runtime/app/composables/useAuth.d.ts +4 -1
- package/dist/runtime/app/composables/useAuth.js +8 -5
- package/dist/runtime/server/routes/sign-in.get.js +8 -4
- package/dist/runtime/server/utils/idp.js +3 -3
- package/dist/runtime/server/utils/oidc.d.ts +2 -0
- package/dist/runtime/server/utils/oidc.js +7 -3
- package/package.json +1 -1
package/dist/module.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ImpersonationCandidate, RpOrganization } from '../../../contract/index.js';
|
|
2
|
+
export type SignInPrompt = 'login' | 'select_account' | 'consent' | 'none';
|
|
2
3
|
export declare function useAuth(): {
|
|
3
4
|
session: import("vue").Ref<{
|
|
4
5
|
user: {
|
|
@@ -100,7 +101,9 @@ export declare function useAuth(): {
|
|
|
100
101
|
}>;
|
|
101
102
|
impersonate: (userId: string) => Promise<void>;
|
|
102
103
|
stopImpersonating: () => Promise<void>;
|
|
103
|
-
signIn: (redirect?: string
|
|
104
|
+
signIn: (redirect?: string, options?: {
|
|
105
|
+
prompt?: SignInPrompt;
|
|
106
|
+
}) => string | false | void | import("vue-router").RouteLocationAsRelativeGeneric | import("vue-router").RouteLocationAsPathGeneric | Promise<false | void | import("vue-router").NavigationFailure>;
|
|
104
107
|
signOut: () => Promise<void>;
|
|
105
108
|
refresh: () => Promise<void>;
|
|
106
109
|
};
|
|
@@ -32,11 +32,14 @@ export function useAuth() {
|
|
|
32
32
|
async function stopImpersonating() {
|
|
33
33
|
session.value = await $fetch("/api/_auth/stop-impersonating", { method: "POST" });
|
|
34
34
|
}
|
|
35
|
-
function signIn(redirect) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
)
|
|
35
|
+
function signIn(redirect, options) {
|
|
36
|
+
const params = new URLSearchParams();
|
|
37
|
+
if (redirect)
|
|
38
|
+
params.set("redirect", redirect);
|
|
39
|
+
if (options?.prompt)
|
|
40
|
+
params.set("prompt", options.prompt);
|
|
41
|
+
const qs = params.toString();
|
|
42
|
+
return navigateTo(qs ? `${routes.signIn}?${qs}` : routes.signIn, { external: true });
|
|
40
43
|
}
|
|
41
44
|
async function signOut() {
|
|
42
45
|
await $fetch("/api/_auth/sign-out", { method: "POST" }).catch(() => {
|
|
@@ -1,25 +1,29 @@
|
|
|
1
1
|
import { createError, defineEventHandler, getQuery, sendRedirect, setCookie } from "h3";
|
|
2
2
|
import { useRuntimeConfig } from "nitropack/runtime";
|
|
3
3
|
import { withQuery } from "ufo";
|
|
4
|
-
import { callbackRedirectUri, pkceChallenge, randomString, safePath } from "../utils/oidc.js";
|
|
4
|
+
import { callbackRedirectUri, idpBaseUrl, pkceChallenge, randomString, safePath } from "../utils/oidc.js";
|
|
5
|
+
const ALLOWED_PROMPTS = /* @__PURE__ */ new Set(["login", "select_account", "consent", "none"]);
|
|
5
6
|
export default defineEventHandler(async (event) => {
|
|
6
7
|
const { auth: runtimeConfig, public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
|
|
7
8
|
if (!publicRuntimeConfig.domain || !publicRuntimeConfig.clientId || !runtimeConfig.clientSecret)
|
|
8
9
|
throw createError({ statusCode: 503, statusMessage: "Auth not configured" });
|
|
10
|
+
const query = getQuery(event);
|
|
9
11
|
const state = randomString(32);
|
|
10
12
|
const verifier = randomString(64);
|
|
11
|
-
const redirectTo = safePath(
|
|
13
|
+
const redirectTo = safePath(query.redirect, publicRuntimeConfig.routes.home);
|
|
14
|
+
const prompt = typeof query.prompt === "string" && ALLOWED_PROMPTS.has(query.prompt) ? query.prompt : void 0;
|
|
12
15
|
const opts = { httpOnly: true, secure: !import.meta.dev, sameSite: "lax", maxAge: 600, path: publicRuntimeConfig.routes.callback };
|
|
13
16
|
setCookie(event, "tco_state", state, opts);
|
|
14
17
|
setCookie(event, "tco_verifier", verifier, opts);
|
|
15
18
|
setCookie(event, "tco_redirect", redirectTo, opts);
|
|
16
|
-
return sendRedirect(event, withQuery(
|
|
19
|
+
return sendRedirect(event, withQuery(`${idpBaseUrl()}/api/auth/oauth2/authorize`, {
|
|
17
20
|
client_id: publicRuntimeConfig.clientId,
|
|
18
21
|
redirect_uri: callbackRedirectUri(event),
|
|
19
22
|
response_type: "code",
|
|
20
23
|
scope: publicRuntimeConfig.scopes.join(" "),
|
|
21
24
|
state,
|
|
22
25
|
code_challenge: await pkceChallenge(verifier),
|
|
23
|
-
code_challenge_method: "S256"
|
|
26
|
+
code_challenge_method: "S256",
|
|
27
|
+
...prompt ? { prompt } : {}
|
|
24
28
|
}));
|
|
25
29
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createError } from "h3";
|
|
2
2
|
import { useRuntimeConfig } from "nitropack/runtime";
|
|
3
3
|
import { $fetch } from "ofetch";
|
|
4
|
+
import { idpBaseUrl } from "./oidc.js";
|
|
4
5
|
import { readSessionRecordById, writeSessionRecord } from "./session.js";
|
|
5
6
|
const SKEW_MS = 6e4;
|
|
6
7
|
async function refresh(rec) {
|
|
@@ -9,7 +10,7 @@ async function refresh(rec) {
|
|
|
9
10
|
const { auth: runtimeConfig, public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
|
|
10
11
|
try {
|
|
11
12
|
const t = await $fetch(
|
|
12
|
-
|
|
13
|
+
`${idpBaseUrl()}/api/auth/oauth2/token`,
|
|
13
14
|
{
|
|
14
15
|
method: "POST",
|
|
15
16
|
headers: {
|
|
@@ -28,12 +29,11 @@ async function refresh(rec) {
|
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
export async function idpFetch(event, id, rec, path, opts = {}) {
|
|
31
|
-
const { public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
|
|
32
32
|
if (!rec.isImpersonation && Date.now() > rec.accessExpiresAt - SKEW_MS) {
|
|
33
33
|
if (await refresh(rec))
|
|
34
34
|
await writeSessionRecord(id, rec);
|
|
35
35
|
}
|
|
36
|
-
const call = () => $fetch(
|
|
36
|
+
const call = () => $fetch(`${idpBaseUrl()}${path}`, {
|
|
37
37
|
method: opts.method,
|
|
38
38
|
body: opts.body,
|
|
39
39
|
query: opts.query,
|
|
@@ -25,6 +25,8 @@ declare module 'nitropack' {
|
|
|
25
25
|
export declare function randomString(len?: number): string;
|
|
26
26
|
export declare function pkceChallenge(verifier: string): Promise<string>;
|
|
27
27
|
export declare function callbackRedirectUri(event: H3Event): string;
|
|
28
|
+
/** IdP origin. `domain` may already carry a scheme (e.g. `http://localhost:3000`); otherwise assume https. */
|
|
29
|
+
export declare function idpBaseUrl(): string;
|
|
28
30
|
export declare function exchangeCode(code: string, verifier: string, redirectUri: string): Promise<{
|
|
29
31
|
access_token: string;
|
|
30
32
|
refresh_token?: string;
|
|
@@ -16,10 +16,15 @@ export function callbackRedirectUri(event) {
|
|
|
16
16
|
const { public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
|
|
17
17
|
return `${getRequestProtocol(event)}://${getRequestHost(event)}${publicRuntimeConfig.routes.callback}`;
|
|
18
18
|
}
|
|
19
|
+
export function idpBaseUrl() {
|
|
20
|
+
const { public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
|
|
21
|
+
const domain = publicRuntimeConfig.domain;
|
|
22
|
+
return /^https?:\/\//.test(domain) ? domain : `https://${domain}`;
|
|
23
|
+
}
|
|
19
24
|
export async function exchangeCode(code, verifier, redirectUri) {
|
|
20
25
|
const { auth: runtimeConfig, public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
|
|
21
26
|
return $fetch(
|
|
22
|
-
|
|
27
|
+
`${idpBaseUrl()}/api/auth/oauth2/token`,
|
|
23
28
|
{
|
|
24
29
|
method: "POST",
|
|
25
30
|
headers: {
|
|
@@ -36,8 +41,7 @@ export async function exchangeCode(code, verifier, redirectUri) {
|
|
|
36
41
|
);
|
|
37
42
|
}
|
|
38
43
|
export async function fetchUserinfo(accessToken) {
|
|
39
|
-
|
|
40
|
-
return $fetch(`https://${publicRuntimeConfig.domain}/api/auth/oauth2/userinfo`, {
|
|
44
|
+
return $fetch(`${idpBaseUrl()}/api/auth/oauth2/userinfo`, {
|
|
41
45
|
headers: { Authorization: `Bearer ${accessToken}` }
|
|
42
46
|
});
|
|
43
47
|
}
|
package/package.json
CHANGED