@hono/auth-js 1.1.0 → 1.1.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @hono/auth-js
2
2
 
3
+ ## 1.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1743](https://github.com/honojs/middleware/pull/1743) [`af44b77ef957abe7b5b4eec43a1059f9ff18c91c`](https://github.com/honojs/middleware/commit/af44b77ef957abe7b5b4eec43a1059f9ff18c91c) Thanks [@te2wow](https://github.com/te2wow)! - Handle absolute URL in basePath of AuthConfigManager.setConfig()
8
+
3
9
  ## 1.1.0
4
10
 
5
11
  ### Minor Changes
@@ -0,0 +1,30 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+
25
+ Object.defineProperty(exports, '__toESM', {
26
+ enumerable: true,
27
+ get: function () {
28
+ return __toESM;
29
+ }
30
+ });
package/dist/index.cjs CHANGED
@@ -1,157 +1,112 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ const require_chunk = require('./chunk-CUT6urMc.cjs');
2
+ let __auth_core = require("@auth/core");
3
+ __auth_core = require_chunk.__toESM(__auth_core);
4
+ let hono_adapter = require("hono/adapter");
5
+ hono_adapter = require_chunk.__toESM(hono_adapter);
6
+ let hono_http_exception = require("hono/http-exception");
7
+ hono_http_exception = require_chunk.__toESM(hono_http_exception);
19
8
 
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- authHandler: () => authHandler,
24
- getAuthUser: () => getAuthUser,
25
- initAuthConfig: () => initAuthConfig,
26
- reqWithEnvUrl: () => reqWithEnvUrl,
27
- setEnvDefaults: () => setEnvDefaults,
28
- verifyAuth: () => verifyAuth
29
- });
30
- module.exports = __toCommonJS(index_exports);
31
- var import_core = require("@auth/core");
32
- var import_adapter = require("hono/adapter");
33
- var import_http_exception = require("hono/http-exception");
34
- function setEnvDefaults(env2, config) {
35
- config.secret ??= env2.AUTH_SECRET;
36
- (0, import_core.setEnvDefaults)(env2, config);
9
+ //#region src/index.ts
10
+ function setEnvDefaults(env$1, config) {
11
+ config.secret ??= env$1.AUTH_SECRET;
12
+ (0, __auth_core.setEnvDefaults)(env$1, config);
37
13
  }
38
14
  function reqWithEnvUrl(req, authUrl) {
39
- if (authUrl) {
40
- const reqUrlObj = new URL(req.url);
41
- const authUrlObj = new URL(authUrl);
42
- const props = ["hostname", "protocol", "port", "password", "username"];
43
- for (const prop of props) {
44
- if (authUrlObj[prop]) {
45
- reqUrlObj[prop] = authUrlObj[prop];
46
- }
47
- }
48
- return new Request(reqUrlObj.href, req);
49
- }
50
- const url = new URL(req.url);
51
- const newReq = new Request(url.href, req);
52
- const proto = newReq.headers.get("x-forwarded-proto");
53
- const host = newReq.headers.get("x-forwarded-host") ?? newReq.headers.get("host");
54
- if (proto != null) {
55
- url.protocol = proto.endsWith(":") ? proto : `${proto}:`;
56
- }
57
- if (host != null) {
58
- url.host = host;
59
- const portMatch = host.match(/:(\d+)$/);
60
- if (portMatch) {
61
- url.port = portMatch[1];
62
- } else {
63
- url.port = "";
64
- }
65
- newReq.headers.delete("x-forwarded-host");
66
- newReq.headers.delete("Host");
67
- newReq.headers.set("Host", host);
68
- }
69
- return new Request(url.href, newReq);
15
+ if (authUrl) {
16
+ const reqUrlObj = new URL(req.url);
17
+ const authUrlObj = new URL(authUrl);
18
+ for (const prop of [
19
+ "hostname",
20
+ "protocol",
21
+ "port",
22
+ "password",
23
+ "username"
24
+ ]) if (authUrlObj[prop]) reqUrlObj[prop] = authUrlObj[prop];
25
+ return new Request(reqUrlObj.href, req);
26
+ }
27
+ const url = new URL(req.url);
28
+ const newReq = new Request(url.href, req);
29
+ const proto = newReq.headers.get("x-forwarded-proto");
30
+ const host = newReq.headers.get("x-forwarded-host") ?? newReq.headers.get("host");
31
+ if (proto != null) url.protocol = proto.endsWith(":") ? proto : `${proto}:`;
32
+ if (host != null) {
33
+ url.host = host;
34
+ const portMatch = host.match(/:(\d+)$/);
35
+ if (portMatch) url.port = portMatch[1];
36
+ else url.port = "";
37
+ newReq.headers.delete("x-forwarded-host");
38
+ newReq.headers.delete("Host");
39
+ newReq.headers.set("Host", host);
40
+ }
41
+ return new Request(url.href, newReq);
70
42
  }
71
43
  async function getAuthUser(c) {
72
- const config = c.get("authConfig");
73
- const ctxEnv = (0, import_adapter.env)(c);
74
- setEnvDefaults(ctxEnv, config);
75
- const authReq = reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
76
- const origin = new URL(authReq.url).origin;
77
- const request = new Request(`${origin}${config.basePath}/session`, {
78
- headers: { cookie: c.req.header("cookie") ?? "" }
79
- });
80
- let authUser = {};
81
- const response = await (0, import_core.Auth)(request, {
82
- ...config,
83
- callbacks: {
84
- ...config.callbacks,
85
- async session(...args) {
86
- authUser = args[0];
87
- const session2 = await config.callbacks?.session?.(...args) ?? args[0].session;
88
- const user = args[0].user ?? args[0].token;
89
- return { user, ...session2 };
90
- }
91
- }
92
- });
93
- const session = await response.json();
94
- return session?.user ? authUser : null;
44
+ const config = c.get("authConfig");
45
+ const ctxEnv = (0, hono_adapter.env)(c);
46
+ setEnvDefaults(ctxEnv, config);
47
+ const authReq = reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
48
+ const origin = new URL(authReq.url).origin;
49
+ const request = new Request(`${origin}${config.basePath}/session`, { headers: { cookie: c.req.header("cookie") ?? "" } });
50
+ let authUser = {};
51
+ return (await (await (0, __auth_core.Auth)(request, {
52
+ ...config,
53
+ callbacks: {
54
+ ...config.callbacks,
55
+ async session(...args) {
56
+ authUser = args[0];
57
+ const session = await config.callbacks?.session?.(...args) ?? args[0].session;
58
+ return {
59
+ user: args[0].user ?? args[0].token,
60
+ ...session
61
+ };
62
+ }
63
+ }
64
+ })).json())?.user ? authUser : null;
95
65
  }
96
66
  function verifyAuth() {
97
- return async (c, next) => {
98
- const authUser = await getAuthUser(c);
99
- const isAuth = !!authUser?.token || !!authUser?.user;
100
- if (!isAuth) {
101
- const res = new Response("Unauthorized", {
102
- status: 401
103
- });
104
- throw new import_http_exception.HTTPException(401, { res });
105
- }
106
- c.set("authUser", authUser);
107
- await next();
108
- };
67
+ return async (c, next) => {
68
+ const authUser = await getAuthUser(c);
69
+ if (!(!!authUser?.token || !!authUser?.user)) throw new hono_http_exception.HTTPException(401, { res: new Response("Unauthorized", { status: 401 }) });
70
+ c.set("authUser", authUser);
71
+ await next();
72
+ };
109
73
  }
110
74
  function initAuthConfig(cb) {
111
- return async (c, next) => {
112
- const config = await cb(c);
113
- c.set("authConfig", config);
114
- await next();
115
- };
75
+ return async (c, next) => {
76
+ const config = await cb(c);
77
+ c.set("authConfig", config);
78
+ await next();
79
+ };
116
80
  }
117
81
  function authHandler() {
118
- return async (c) => {
119
- const config = c.get("authConfig");
120
- const ctxEnv = (0, import_adapter.env)(c);
121
- setEnvDefaults(ctxEnv, config);
122
- if (!config.secret || config.secret.length === 0) {
123
- throw new import_http_exception.HTTPException(500, { message: "Missing AUTH_SECRET" });
124
- }
125
- const body = c.req.raw.body ? await c.req.blob() : void 0;
126
- const res = await (0, import_core.Auth)(
127
- reqWithEnvUrl(
128
- new Request(c.req.raw.url, {
129
- body,
130
- cache: c.req.raw.cache,
131
- credentials: c.req.raw.credentials,
132
- headers: c.req.raw.headers,
133
- integrity: c.req.raw.integrity,
134
- keepalive: c.req.raw.keepalive,
135
- method: c.req.raw.method,
136
- mode: c.req.raw.mode,
137
- redirect: c.req.raw.redirect,
138
- referrer: c.req.raw.referrer,
139
- referrerPolicy: c.req.raw.referrerPolicy,
140
- signal: c.req.raw.signal
141
- }),
142
- ctxEnv.AUTH_URL
143
- ),
144
- config
145
- );
146
- return new Response(res.body, res);
147
- };
82
+ return async (c) => {
83
+ const config = c.get("authConfig");
84
+ const ctxEnv = (0, hono_adapter.env)(c);
85
+ setEnvDefaults(ctxEnv, config);
86
+ if (!config.secret || config.secret.length === 0) throw new hono_http_exception.HTTPException(500, { message: "Missing AUTH_SECRET" });
87
+ const body = c.req.raw.body ? await c.req.blob() : void 0;
88
+ const res = await (0, __auth_core.Auth)(reqWithEnvUrl(new Request(c.req.raw.url, {
89
+ body,
90
+ cache: c.req.raw.cache,
91
+ credentials: c.req.raw.credentials,
92
+ headers: c.req.raw.headers,
93
+ integrity: c.req.raw.integrity,
94
+ keepalive: c.req.raw.keepalive,
95
+ method: c.req.raw.method,
96
+ mode: c.req.raw.mode,
97
+ redirect: c.req.raw.redirect,
98
+ referrer: c.req.raw.referrer,
99
+ referrerPolicy: c.req.raw.referrerPolicy,
100
+ signal: c.req.raw.signal
101
+ }), ctxEnv.AUTH_URL), config);
102
+ return new Response(res.body, res);
103
+ };
148
104
  }
149
- // Annotate the CommonJS export names for ESM import in node:
150
- 0 && (module.exports = {
151
- authHandler,
152
- getAuthUser,
153
- initAuthConfig,
154
- reqWithEnvUrl,
155
- setEnvDefaults,
156
- verifyAuth
157
- });
105
+
106
+ //#endregion
107
+ exports.authHandler = authHandler;
108
+ exports.getAuthUser = getAuthUser;
109
+ exports.initAuthConfig = initAuthConfig;
110
+ exports.reqWithEnvUrl = reqWithEnvUrl;
111
+ exports.setEnvDefaults = setEnvDefaults;
112
+ exports.verifyAuth = verifyAuth;
package/dist/index.d.cts CHANGED
@@ -1,28 +1,28 @@
1
- import { AuthConfig as AuthConfig$1 } from '@auth/core';
2
- import { AdapterUser } from '@auth/core/adapters';
3
- import { JWT } from '@auth/core/jwt';
4
- import { Session } from '@auth/core/types';
5
- import { Context, MiddlewareHandler } from 'hono';
1
+ import { AuthConfig as AuthConfig$1 } from "@auth/core";
2
+ import { AdapterUser } from "@auth/core/adapters";
3
+ import { JWT } from "@auth/core/jwt";
4
+ import { Session } from "@auth/core/types";
5
+ import { Context, MiddlewareHandler } from "hono";
6
6
 
7
- declare module 'hono' {
8
- interface ContextVariableMap {
9
- authUser: AuthUser;
10
- authConfig: AuthConfig;
11
- }
7
+ //#region src/index.d.ts
8
+ declare module "hono" {
9
+ interface ContextVariableMap {
10
+ authUser: AuthUser;
11
+ authConfig: AuthConfig;
12
+ }
12
13
  }
13
14
  type AuthEnv = {
14
- AUTH_URL?: string;
15
- AUTH_SECRET: string;
16
- AUTH_REDIRECT_PROXY_URL?: string;
17
- [key: string]: string | undefined;
15
+ AUTH_URL?: string;
16
+ AUTH_SECRET: string;
17
+ AUTH_REDIRECT_PROXY_URL?: string;
18
+ [key: string]: string | undefined;
18
19
  };
19
20
  type AuthUser = {
20
- session: Session;
21
- token?: JWT;
22
- user?: AdapterUser;
21
+ session: Session;
22
+ token?: JWT;
23
+ user?: AdapterUser;
23
24
  };
24
- interface AuthConfig extends Omit<AuthConfig$1, 'raw'> {
25
- }
25
+ interface AuthConfig extends Omit<AuthConfig$1, "raw"> {}
26
26
  type ConfigHandler = (c: Context) => AuthConfig | Promise<AuthConfig>;
27
27
  declare function setEnvDefaults(env: AuthEnv, config: AuthConfig): void;
28
28
  declare function reqWithEnvUrl(req: Request, authUrl?: string): Request;
@@ -30,5 +30,6 @@ declare function getAuthUser(c: Context): Promise<AuthUser | null>;
30
30
  declare function verifyAuth(): MiddlewareHandler;
31
31
  declare function initAuthConfig(cb: ConfigHandler): MiddlewareHandler;
32
32
  declare function authHandler(): MiddlewareHandler;
33
-
34
- export { type AuthConfig, type AuthEnv, type AuthUser, type ConfigHandler, authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, setEnvDefaults, verifyAuth };
33
+ //#endregion
34
+ export { AuthConfig, AuthEnv, AuthUser, ConfigHandler, authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, setEnvDefaults, verifyAuth };
35
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;IAKgD,QAAA,EAMlC,QANkC;IAAA,UAAA,EAOhC,UAPgC;;;AAOhC,KAIJ,OAAA,GAJI;EAIhB,QAAY,CAAA,EAAA,MAAA;EAOZ,WAAY,EAAA,MAAA;EACD,uBAAA,CAAA,EAAA,MAAA;EACD,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,MAAA,GAAA,SAAA;CACD;AAAA,KAHG,QAAA,GAGH;EAGT,OAAiB,EALN,OAKM;EAEjB,KAAY,CAAA,EANF,GAME;EAAoB,IAAA,CAAA,EALvB,WAKuB;CAAY;AAAqB,UAFhD,UAAA,SAAmB,IAE6B,CAFxB,YAEwB,EAAA,KAAA,CAAA,CAAA,CAAA;AAAR,KAA7C,aAAA,GAA6C,CAAA,CAAA,EAAzB,OAAyB,EAAA,GAAb,UAAa,GAAA,OAAA,CAAQ,UAAR,CAAA;AAAA,iBAEzC,cAAA,CAFyC,GAAA,EAErB,OAFqB,EAAA,MAAA,EAEJ,UAFI,CAAA,EAAA,IAAA;AAEzC,iBAKA,aAAA,CALoB,GAAA,EAKD,OALkB,EAAA,OAAA,CAAA,EAAA,MAAA,CAAA,EAKU,OALV;AAKrC,iBAkCM,WAAA,CAlCa,CAAA,EAkCE,OAlC0B,CAAA,EAkChB,OAlCgB,CAkCR,QAlCQ,GAAA,IAAA,CAAA;AAkCzC,iBA8BN,UAAA,CAAA,CA9BM,EA8BQ,iBA9BR;AAAe,iBA8CrB,cAAA,CA9CqB,EAAA,EA8CF,aA9CE,CAAA,EA8Cc,iBA9Cd;AAAkB,iBAsDvC,WAAA,CAAA,CAtDuC,EAsDxB,iBAtDwB"}
package/dist/index.d.ts CHANGED
@@ -1,28 +1,28 @@
1
- import { AuthConfig as AuthConfig$1 } from '@auth/core';
2
- import { AdapterUser } from '@auth/core/adapters';
3
- import { JWT } from '@auth/core/jwt';
4
- import { Session } from '@auth/core/types';
5
- import { Context, MiddlewareHandler } from 'hono';
1
+ import { AuthConfig as AuthConfig$1 } from "@auth/core";
2
+ import { AdapterUser } from "@auth/core/adapters";
3
+ import { JWT } from "@auth/core/jwt";
4
+ import { Session } from "@auth/core/types";
5
+ import { Context, MiddlewareHandler } from "hono";
6
6
 
7
- declare module 'hono' {
8
- interface ContextVariableMap {
9
- authUser: AuthUser;
10
- authConfig: AuthConfig;
11
- }
7
+ //#region src/index.d.ts
8
+ declare module "hono" {
9
+ interface ContextVariableMap {
10
+ authUser: AuthUser;
11
+ authConfig: AuthConfig;
12
+ }
12
13
  }
13
14
  type AuthEnv = {
14
- AUTH_URL?: string;
15
- AUTH_SECRET: string;
16
- AUTH_REDIRECT_PROXY_URL?: string;
17
- [key: string]: string | undefined;
15
+ AUTH_URL?: string;
16
+ AUTH_SECRET: string;
17
+ AUTH_REDIRECT_PROXY_URL?: string;
18
+ [key: string]: string | undefined;
18
19
  };
19
20
  type AuthUser = {
20
- session: Session;
21
- token?: JWT;
22
- user?: AdapterUser;
21
+ session: Session;
22
+ token?: JWT;
23
+ user?: AdapterUser;
23
24
  };
24
- interface AuthConfig extends Omit<AuthConfig$1, 'raw'> {
25
- }
25
+ interface AuthConfig extends Omit<AuthConfig$1, "raw"> {}
26
26
  type ConfigHandler = (c: Context) => AuthConfig | Promise<AuthConfig>;
27
27
  declare function setEnvDefaults(env: AuthEnv, config: AuthConfig): void;
28
28
  declare function reqWithEnvUrl(req: Request, authUrl?: string): Request;
@@ -30,5 +30,6 @@ declare function getAuthUser(c: Context): Promise<AuthUser | null>;
30
30
  declare function verifyAuth(): MiddlewareHandler;
31
31
  declare function initAuthConfig(cb: ConfigHandler): MiddlewareHandler;
32
32
  declare function authHandler(): MiddlewareHandler;
33
-
34
- export { type AuthConfig, type AuthEnv, type AuthUser, type ConfigHandler, authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, setEnvDefaults, verifyAuth };
33
+ //#endregion
34
+ export { AuthConfig, AuthEnv, AuthUser, ConfigHandler, authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, setEnvDefaults, verifyAuth };
35
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;IAKgD,QAAA,EAMlC,QANkC;IAAA,UAAA,EAOhC,UAPgC;;;AAOhC,KAIJ,OAAA,GAJI;EAIhB,QAAY,CAAA,EAAA,MAAA;EAOZ,WAAY,EAAA,MAAA;EACD,uBAAA,CAAA,EAAA,MAAA;EACD,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,MAAA,GAAA,SAAA;CACD;AAAA,KAHG,QAAA,GAGH;EAGT,OAAiB,EALN,OAKM;EAEjB,KAAY,CAAA,EANF,GAME;EAAoB,IAAA,CAAA,EALvB,WAKuB;CAAY;AAAqB,UAFhD,UAAA,SAAmB,IAE6B,CAFxB,YAEwB,EAAA,KAAA,CAAA,CAAA,CAAA;AAAR,KAA7C,aAAA,GAA6C,CAAA,CAAA,EAAzB,OAAyB,EAAA,GAAb,UAAa,GAAA,OAAA,CAAQ,UAAR,CAAA;AAAA,iBAEzC,cAAA,CAFyC,GAAA,EAErB,OAFqB,EAAA,MAAA,EAEJ,UAFI,CAAA,EAAA,IAAA;AAEzC,iBAKA,aAAA,CALoB,GAAA,EAKD,OALkB,EAAA,OAAA,CAAA,EAAA,MAAA,CAAA,EAKU,OALV;AAKrC,iBAkCM,WAAA,CAlCa,CAAA,EAkCE,OAlC0B,CAAA,EAkChB,OAlCgB,CAkCR,QAlCQ,GAAA,IAAA,CAAA;AAkCzC,iBA8BN,UAAA,CAAA,CA9BM,EA8BQ,iBA9BR;AAAe,iBA8CrB,cAAA,CA9CqB,EAAA,EA8CF,aA9CE,CAAA,EA8Cc,iBA9Cd;AAAkB,iBAsDvC,WAAA,CAAA,CAtDuC,EAsDxB,iBAtDwB"}
package/dist/index.js CHANGED
@@ -1,127 +1,104 @@
1
- // src/index.ts
2
- import { Auth, setEnvDefaults as coreSetEnvDefaults } from "@auth/core";
1
+ import { Auth, setEnvDefaults as setEnvDefaults$1 } from "@auth/core";
3
2
  import { env } from "hono/adapter";
4
3
  import { HTTPException } from "hono/http-exception";
5
- function setEnvDefaults(env2, config) {
6
- config.secret ??= env2.AUTH_SECRET;
7
- coreSetEnvDefaults(env2, config);
4
+
5
+ //#region src/index.ts
6
+ function setEnvDefaults(env$1, config) {
7
+ config.secret ??= env$1.AUTH_SECRET;
8
+ setEnvDefaults$1(env$1, config);
8
9
  }
9
10
  function reqWithEnvUrl(req, authUrl) {
10
- if (authUrl) {
11
- const reqUrlObj = new URL(req.url);
12
- const authUrlObj = new URL(authUrl);
13
- const props = ["hostname", "protocol", "port", "password", "username"];
14
- for (const prop of props) {
15
- if (authUrlObj[prop]) {
16
- reqUrlObj[prop] = authUrlObj[prop];
17
- }
18
- }
19
- return new Request(reqUrlObj.href, req);
20
- }
21
- const url = new URL(req.url);
22
- const newReq = new Request(url.href, req);
23
- const proto = newReq.headers.get("x-forwarded-proto");
24
- const host = newReq.headers.get("x-forwarded-host") ?? newReq.headers.get("host");
25
- if (proto != null) {
26
- url.protocol = proto.endsWith(":") ? proto : `${proto}:`;
27
- }
28
- if (host != null) {
29
- url.host = host;
30
- const portMatch = host.match(/:(\d+)$/);
31
- if (portMatch) {
32
- url.port = portMatch[1];
33
- } else {
34
- url.port = "";
35
- }
36
- newReq.headers.delete("x-forwarded-host");
37
- newReq.headers.delete("Host");
38
- newReq.headers.set("Host", host);
39
- }
40
- return new Request(url.href, newReq);
11
+ if (authUrl) {
12
+ const reqUrlObj = new URL(req.url);
13
+ const authUrlObj = new URL(authUrl);
14
+ for (const prop of [
15
+ "hostname",
16
+ "protocol",
17
+ "port",
18
+ "password",
19
+ "username"
20
+ ]) if (authUrlObj[prop]) reqUrlObj[prop] = authUrlObj[prop];
21
+ return new Request(reqUrlObj.href, req);
22
+ }
23
+ const url = new URL(req.url);
24
+ const newReq = new Request(url.href, req);
25
+ const proto = newReq.headers.get("x-forwarded-proto");
26
+ const host = newReq.headers.get("x-forwarded-host") ?? newReq.headers.get("host");
27
+ if (proto != null) url.protocol = proto.endsWith(":") ? proto : `${proto}:`;
28
+ if (host != null) {
29
+ url.host = host;
30
+ const portMatch = host.match(/:(\d+)$/);
31
+ if (portMatch) url.port = portMatch[1];
32
+ else url.port = "";
33
+ newReq.headers.delete("x-forwarded-host");
34
+ newReq.headers.delete("Host");
35
+ newReq.headers.set("Host", host);
36
+ }
37
+ return new Request(url.href, newReq);
41
38
  }
42
39
  async function getAuthUser(c) {
43
- const config = c.get("authConfig");
44
- const ctxEnv = env(c);
45
- setEnvDefaults(ctxEnv, config);
46
- const authReq = reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
47
- const origin = new URL(authReq.url).origin;
48
- const request = new Request(`${origin}${config.basePath}/session`, {
49
- headers: { cookie: c.req.header("cookie") ?? "" }
50
- });
51
- let authUser = {};
52
- const response = await Auth(request, {
53
- ...config,
54
- callbacks: {
55
- ...config.callbacks,
56
- async session(...args) {
57
- authUser = args[0];
58
- const session2 = await config.callbacks?.session?.(...args) ?? args[0].session;
59
- const user = args[0].user ?? args[0].token;
60
- return { user, ...session2 };
61
- }
62
- }
63
- });
64
- const session = await response.json();
65
- return session?.user ? authUser : null;
40
+ const config = c.get("authConfig");
41
+ const ctxEnv = env(c);
42
+ setEnvDefaults(ctxEnv, config);
43
+ const authReq = reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
44
+ const origin = new URL(authReq.url).origin;
45
+ const request = new Request(`${origin}${config.basePath}/session`, { headers: { cookie: c.req.header("cookie") ?? "" } });
46
+ let authUser = {};
47
+ return (await (await Auth(request, {
48
+ ...config,
49
+ callbacks: {
50
+ ...config.callbacks,
51
+ async session(...args) {
52
+ authUser = args[0];
53
+ const session = await config.callbacks?.session?.(...args) ?? args[0].session;
54
+ return {
55
+ user: args[0].user ?? args[0].token,
56
+ ...session
57
+ };
58
+ }
59
+ }
60
+ })).json())?.user ? authUser : null;
66
61
  }
67
62
  function verifyAuth() {
68
- return async (c, next) => {
69
- const authUser = await getAuthUser(c);
70
- const isAuth = !!authUser?.token || !!authUser?.user;
71
- if (!isAuth) {
72
- const res = new Response("Unauthorized", {
73
- status: 401
74
- });
75
- throw new HTTPException(401, { res });
76
- }
77
- c.set("authUser", authUser);
78
- await next();
79
- };
63
+ return async (c, next) => {
64
+ const authUser = await getAuthUser(c);
65
+ if (!(!!authUser?.token || !!authUser?.user)) throw new HTTPException(401, { res: new Response("Unauthorized", { status: 401 }) });
66
+ c.set("authUser", authUser);
67
+ await next();
68
+ };
80
69
  }
81
70
  function initAuthConfig(cb) {
82
- return async (c, next) => {
83
- const config = await cb(c);
84
- c.set("authConfig", config);
85
- await next();
86
- };
71
+ return async (c, next) => {
72
+ const config = await cb(c);
73
+ c.set("authConfig", config);
74
+ await next();
75
+ };
87
76
  }
88
77
  function authHandler() {
89
- return async (c) => {
90
- const config = c.get("authConfig");
91
- const ctxEnv = env(c);
92
- setEnvDefaults(ctxEnv, config);
93
- if (!config.secret || config.secret.length === 0) {
94
- throw new HTTPException(500, { message: "Missing AUTH_SECRET" });
95
- }
96
- const body = c.req.raw.body ? await c.req.blob() : void 0;
97
- const res = await Auth(
98
- reqWithEnvUrl(
99
- new Request(c.req.raw.url, {
100
- body,
101
- cache: c.req.raw.cache,
102
- credentials: c.req.raw.credentials,
103
- headers: c.req.raw.headers,
104
- integrity: c.req.raw.integrity,
105
- keepalive: c.req.raw.keepalive,
106
- method: c.req.raw.method,
107
- mode: c.req.raw.mode,
108
- redirect: c.req.raw.redirect,
109
- referrer: c.req.raw.referrer,
110
- referrerPolicy: c.req.raw.referrerPolicy,
111
- signal: c.req.raw.signal
112
- }),
113
- ctxEnv.AUTH_URL
114
- ),
115
- config
116
- );
117
- return new Response(res.body, res);
118
- };
78
+ return async (c) => {
79
+ const config = c.get("authConfig");
80
+ const ctxEnv = env(c);
81
+ setEnvDefaults(ctxEnv, config);
82
+ if (!config.secret || config.secret.length === 0) throw new HTTPException(500, { message: "Missing AUTH_SECRET" });
83
+ const body = c.req.raw.body ? await c.req.blob() : void 0;
84
+ const res = await Auth(reqWithEnvUrl(new Request(c.req.raw.url, {
85
+ body,
86
+ cache: c.req.raw.cache,
87
+ credentials: c.req.raw.credentials,
88
+ headers: c.req.raw.headers,
89
+ integrity: c.req.raw.integrity,
90
+ keepalive: c.req.raw.keepalive,
91
+ method: c.req.raw.method,
92
+ mode: c.req.raw.mode,
93
+ redirect: c.req.raw.redirect,
94
+ referrer: c.req.raw.referrer,
95
+ referrerPolicy: c.req.raw.referrerPolicy,
96
+ signal: c.req.raw.signal
97
+ }), ctxEnv.AUTH_URL), config);
98
+ return new Response(res.body, res);
99
+ };
119
100
  }
120
- export {
121
- authHandler,
122
- getAuthUser,
123
- initAuthConfig,
124
- reqWithEnvUrl,
125
- setEnvDefaults,
126
- verifyAuth
127
- };
101
+
102
+ //#endregion
103
+ export { authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, setEnvDefaults, verifyAuth };
104
+ //# sourceMappingURL=index.js.map