@hono/auth-js 1.0.8 → 1.0.10
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/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +38 -10
- package/dist/index.mjs +39 -11
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -25,7 +25,7 @@ interface AuthConfig extends Omit<AuthConfig$1, 'raw'> {
|
|
|
25
25
|
}
|
|
26
26
|
type ConfigHandler = (c: Context) => AuthConfig;
|
|
27
27
|
declare function setEnvDefaults(env: AuthEnv, config: AuthConfig): void;
|
|
28
|
-
declare function reqWithEnvUrl(req: Request, authUrl?: string): Request
|
|
28
|
+
declare function reqWithEnvUrl(req: Request, authUrl?: string): Promise<Request>;
|
|
29
29
|
declare function getAuthUser(c: Context): Promise<AuthUser | null>;
|
|
30
30
|
declare function verifyAuth(): MiddlewareHandler;
|
|
31
31
|
declare function initAuthConfig(cb: ConfigHandler): MiddlewareHandler;
|
package/dist/index.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ interface AuthConfig extends Omit<AuthConfig$1, 'raw'> {
|
|
|
25
25
|
}
|
|
26
26
|
type ConfigHandler = (c: Context) => AuthConfig;
|
|
27
27
|
declare function setEnvDefaults(env: AuthEnv, config: AuthConfig): void;
|
|
28
|
-
declare function reqWithEnvUrl(req: Request, authUrl?: string): Request
|
|
28
|
+
declare function reqWithEnvUrl(req: Request, authUrl?: string): Promise<Request>;
|
|
29
29
|
declare function getAuthUser(c: Context): Promise<AuthUser | null>;
|
|
30
30
|
declare function verifyAuth(): MiddlewareHandler;
|
|
31
31
|
declare function initAuthConfig(cb: ConfigHandler): MiddlewareHandler;
|
package/dist/index.js
CHANGED
|
@@ -37,35 +37,62 @@ function setEnvDefaults(env2, config) {
|
|
|
37
37
|
config.basePath ||= "/api/auth";
|
|
38
38
|
(0, import_core2.setEnvDefaults)(env2, config);
|
|
39
39
|
}
|
|
40
|
-
function
|
|
40
|
+
async function cloneRequest(input, request, headers) {
|
|
41
|
+
if ((0, import_adapter.getRuntimeKey)() === "bun") {
|
|
42
|
+
return new Request(input, {
|
|
43
|
+
method: request.method,
|
|
44
|
+
headers: headers ?? new Headers(request.headers),
|
|
45
|
+
body: request.method === "GET" || request.method === "HEAD" ? void 0 : await request.blob(),
|
|
46
|
+
// @ts-ignore: TS2353
|
|
47
|
+
referrer: "referrer" in request ? request.referrer : void 0,
|
|
48
|
+
// deno-lint-ignore no-explicit-any
|
|
49
|
+
referrerPolicy: request.referrerPolicy,
|
|
50
|
+
mode: request.mode,
|
|
51
|
+
credentials: request.credentials,
|
|
52
|
+
// @ts-ignore: TS2353
|
|
53
|
+
cache: request.cache,
|
|
54
|
+
redirect: request.redirect,
|
|
55
|
+
integrity: request.integrity,
|
|
56
|
+
keepalive: request.keepalive,
|
|
57
|
+
signal: request.signal
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return new Request(input, request);
|
|
61
|
+
}
|
|
62
|
+
async function reqWithEnvUrl(req, authUrl) {
|
|
41
63
|
if (authUrl) {
|
|
42
64
|
const reqUrlObj = new URL(req.url);
|
|
43
65
|
const authUrlObj = new URL(authUrl);
|
|
44
66
|
const props = ["hostname", "protocol", "port", "password", "username"];
|
|
45
67
|
props.forEach((prop) => reqUrlObj[prop] = authUrlObj[prop]);
|
|
46
|
-
return
|
|
68
|
+
return cloneRequest(reqUrlObj.href, req);
|
|
47
69
|
} else {
|
|
48
70
|
const url = new URL(req.url);
|
|
49
|
-
const
|
|
50
|
-
const
|
|
71
|
+
const headers = new Headers(req.headers);
|
|
72
|
+
const proto = headers.get("x-forwarded-proto");
|
|
73
|
+
const host = headers.get("x-forwarded-host") ?? headers.get("host");
|
|
51
74
|
if (proto != null)
|
|
52
75
|
url.protocol = proto.endsWith(":") ? proto : proto + ":";
|
|
53
|
-
if (host) {
|
|
76
|
+
if (host != null) {
|
|
54
77
|
url.host = host;
|
|
55
78
|
const portMatch = host.match(/:(\d+)$/);
|
|
56
79
|
if (portMatch)
|
|
57
80
|
url.port = portMatch[1];
|
|
58
81
|
else
|
|
59
82
|
url.port = "";
|
|
83
|
+
headers.delete("x-forwarded-host");
|
|
84
|
+
headers.delete("Host");
|
|
85
|
+
headers.set("Host", host);
|
|
60
86
|
}
|
|
61
|
-
return
|
|
87
|
+
return cloneRequest(url.href, req, headers);
|
|
62
88
|
}
|
|
63
89
|
}
|
|
64
90
|
async function getAuthUser(c) {
|
|
65
91
|
const config = c.get("authConfig");
|
|
66
|
-
|
|
92
|
+
const ctxEnv = (0, import_adapter.env)(c);
|
|
67
93
|
setEnvDefaults(ctxEnv, config);
|
|
68
|
-
const
|
|
94
|
+
const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
|
|
95
|
+
const origin = new URL(authReq.url).origin;
|
|
69
96
|
const request = new Request(`${origin}${config.basePath}/session`, {
|
|
70
97
|
headers: { cookie: c.req.header("cookie") ?? "" }
|
|
71
98
|
});
|
|
@@ -110,12 +137,13 @@ function initAuthConfig(cb) {
|
|
|
110
137
|
function authHandler() {
|
|
111
138
|
return async (c) => {
|
|
112
139
|
const config = c.get("authConfig");
|
|
113
|
-
|
|
140
|
+
const ctxEnv = (0, import_adapter.env)(c);
|
|
114
141
|
setEnvDefaults(ctxEnv, config);
|
|
115
142
|
if (!config.secret || config.secret.length === 0) {
|
|
116
143
|
throw new import_http_exception.HTTPException(500, { message: "Missing AUTH_SECRET" });
|
|
117
144
|
}
|
|
118
|
-
const
|
|
145
|
+
const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
|
|
146
|
+
const res = await (0, import_core.Auth)(authReq, config);
|
|
119
147
|
return new Response(res.body, res);
|
|
120
148
|
};
|
|
121
149
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { Auth } from "@auth/core";
|
|
3
|
-
import { env } from "hono/adapter";
|
|
3
|
+
import { env, getRuntimeKey } from "hono/adapter";
|
|
4
4
|
import { HTTPException } from "hono/http-exception";
|
|
5
5
|
import { setEnvDefaults as coreSetEnvDefaults } from "@auth/core";
|
|
6
6
|
function setEnvDefaults(env2, config) {
|
|
@@ -8,35 +8,62 @@ function setEnvDefaults(env2, config) {
|
|
|
8
8
|
config.basePath ||= "/api/auth";
|
|
9
9
|
coreSetEnvDefaults(env2, config);
|
|
10
10
|
}
|
|
11
|
-
function
|
|
11
|
+
async function cloneRequest(input, request, headers) {
|
|
12
|
+
if (getRuntimeKey() === "bun") {
|
|
13
|
+
return new Request(input, {
|
|
14
|
+
method: request.method,
|
|
15
|
+
headers: headers ?? new Headers(request.headers),
|
|
16
|
+
body: request.method === "GET" || request.method === "HEAD" ? void 0 : await request.blob(),
|
|
17
|
+
// @ts-ignore: TS2353
|
|
18
|
+
referrer: "referrer" in request ? request.referrer : void 0,
|
|
19
|
+
// deno-lint-ignore no-explicit-any
|
|
20
|
+
referrerPolicy: request.referrerPolicy,
|
|
21
|
+
mode: request.mode,
|
|
22
|
+
credentials: request.credentials,
|
|
23
|
+
// @ts-ignore: TS2353
|
|
24
|
+
cache: request.cache,
|
|
25
|
+
redirect: request.redirect,
|
|
26
|
+
integrity: request.integrity,
|
|
27
|
+
keepalive: request.keepalive,
|
|
28
|
+
signal: request.signal
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return new Request(input, request);
|
|
32
|
+
}
|
|
33
|
+
async function reqWithEnvUrl(req, authUrl) {
|
|
12
34
|
if (authUrl) {
|
|
13
35
|
const reqUrlObj = new URL(req.url);
|
|
14
36
|
const authUrlObj = new URL(authUrl);
|
|
15
37
|
const props = ["hostname", "protocol", "port", "password", "username"];
|
|
16
38
|
props.forEach((prop) => reqUrlObj[prop] = authUrlObj[prop]);
|
|
17
|
-
return
|
|
39
|
+
return cloneRequest(reqUrlObj.href, req);
|
|
18
40
|
} else {
|
|
19
41
|
const url = new URL(req.url);
|
|
20
|
-
const
|
|
21
|
-
const
|
|
42
|
+
const headers = new Headers(req.headers);
|
|
43
|
+
const proto = headers.get("x-forwarded-proto");
|
|
44
|
+
const host = headers.get("x-forwarded-host") ?? headers.get("host");
|
|
22
45
|
if (proto != null)
|
|
23
46
|
url.protocol = proto.endsWith(":") ? proto : proto + ":";
|
|
24
|
-
if (host) {
|
|
47
|
+
if (host != null) {
|
|
25
48
|
url.host = host;
|
|
26
49
|
const portMatch = host.match(/:(\d+)$/);
|
|
27
50
|
if (portMatch)
|
|
28
51
|
url.port = portMatch[1];
|
|
29
52
|
else
|
|
30
53
|
url.port = "";
|
|
54
|
+
headers.delete("x-forwarded-host");
|
|
55
|
+
headers.delete("Host");
|
|
56
|
+
headers.set("Host", host);
|
|
31
57
|
}
|
|
32
|
-
return
|
|
58
|
+
return cloneRequest(url.href, req, headers);
|
|
33
59
|
}
|
|
34
60
|
}
|
|
35
61
|
async function getAuthUser(c) {
|
|
36
62
|
const config = c.get("authConfig");
|
|
37
|
-
|
|
63
|
+
const ctxEnv = env(c);
|
|
38
64
|
setEnvDefaults(ctxEnv, config);
|
|
39
|
-
const
|
|
65
|
+
const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
|
|
66
|
+
const origin = new URL(authReq.url).origin;
|
|
40
67
|
const request = new Request(`${origin}${config.basePath}/session`, {
|
|
41
68
|
headers: { cookie: c.req.header("cookie") ?? "" }
|
|
42
69
|
});
|
|
@@ -81,12 +108,13 @@ function initAuthConfig(cb) {
|
|
|
81
108
|
function authHandler() {
|
|
82
109
|
return async (c) => {
|
|
83
110
|
const config = c.get("authConfig");
|
|
84
|
-
|
|
111
|
+
const ctxEnv = env(c);
|
|
85
112
|
setEnvDefaults(ctxEnv, config);
|
|
86
113
|
if (!config.secret || config.secret.length === 0) {
|
|
87
114
|
throw new HTTPException(500, { message: "Missing AUTH_SECRET" });
|
|
88
115
|
}
|
|
89
|
-
const
|
|
116
|
+
const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
|
|
117
|
+
const res = await Auth(authReq, config);
|
|
90
118
|
return new Response(res.body, res);
|
|
91
119
|
};
|
|
92
120
|
}
|