@thecodeorigin/auth 0.0.6 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thecodeorigin/auth",
3
3
  "configKey": "auth",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
@@ -1,7 +1,7 @@
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
5
  const ALLOWED_PROMPTS = /* @__PURE__ */ new Set(["login", "select_account", "consent", "none"]);
6
6
  export default defineEventHandler(async (event) => {
7
7
  const { auth: runtimeConfig, public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
@@ -16,7 +16,7 @@ export default defineEventHandler(async (event) => {
16
16
  setCookie(event, "tco_state", state, opts);
17
17
  setCookie(event, "tco_verifier", verifier, opts);
18
18
  setCookie(event, "tco_redirect", redirectTo, opts);
19
- return sendRedirect(event, withQuery(`https://${publicRuntimeConfig.domain}/api/auth/oauth2/authorize`, {
19
+ return sendRedirect(event, withQuery(`${idpBaseUrl()}/api/auth/oauth2/authorize`, {
20
20
  client_id: publicRuntimeConfig.clientId,
21
21
  redirect_uri: callbackRedirectUri(event),
22
22
  response_type: "code",
@@ -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
- `https://${publicRuntimeConfig.domain}/api/auth/oauth2/token`,
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(`https://${publicRuntimeConfig.domain}${path}`, {
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
- `https://${publicRuntimeConfig.domain}/api/auth/oauth2/token`,
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
- const { public: { auth: publicRuntimeConfig } } = useRuntimeConfig();
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thecodeorigin/auth",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "private": false,
6
6
  "description": "THECODEORIGIN Authentication Portal client module",
7
7
  "repository": "https://github.com/thecodeorigin/auth",