@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 +6 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/index.cjs +100 -145
- package/dist/index.d.cts +22 -21
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +22 -21
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +91 -114
- package/dist/index.js.map +1 -0
- package/dist/react.cjs +353 -428
- package/dist/react.d.cts +67 -64
- package/dist/react.d.cts.map +1 -0
- package/dist/react.d.ts +68 -64
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +342 -383
- package/dist/react.js.map +1 -0
- package/package.json +12 -13
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
|
2
|
-
import { AdapterUser } from
|
|
3
|
-
import { JWT } from
|
|
4
|
-
import { Session } from
|
|
5
|
-
import { Context, MiddlewareHandler } from
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
session: Session;
|
|
22
|
+
token?: JWT;
|
|
23
|
+
user?: AdapterUser;
|
|
23
24
|
};
|
|
24
|
-
interface AuthConfig extends Omit<AuthConfig$1,
|
|
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 {
|
|
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
|
|
2
|
-
import { AdapterUser } from
|
|
3
|
-
import { JWT } from
|
|
4
|
-
import { Session } from
|
|
5
|
-
import { Context, MiddlewareHandler } from
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
session: Session;
|
|
22
|
+
token?: JWT;
|
|
23
|
+
user?: AdapterUser;
|
|
23
24
|
};
|
|
24
|
-
interface AuthConfig extends Omit<AuthConfig$1,
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
reqWithEnvUrl,
|
|
125
|
-
setEnvDefaults,
|
|
126
|
-
verifyAuth
|
|
127
|
-
};
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
export { authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, setEnvDefaults, verifyAuth };
|
|
104
|
+
//# sourceMappingURL=index.js.map
|