@shad-claiborne/hono-middleware-oidc 1.0.0
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/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +82 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shad Claiborne
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# This package is not intended for production environments.
|
|
2
|
+
`wrangler configuration`
|
|
3
|
+
```json
|
|
4
|
+
"vars": {
|
|
5
|
+
"HONO_OIDC_ISSUER": "https://some-idp",
|
|
6
|
+
"HONO_OIDC_CLIENT_ID": "client-id",
|
|
7
|
+
"HONO_OIDC_CLIENT_SECRET": "client-secret",
|
|
8
|
+
"HONO_OIDC_REDIRECT_URI": "https://your-authenticated-app/auth/callback",
|
|
9
|
+
"HONO_OIDC_COOKIE_SECRET": "cookie-secret",
|
|
10
|
+
"HONO_OIDC_ID_TOKEN_COOKIE": "id-token",
|
|
11
|
+
"HONO_OIDC_REFRESH_TOKEN_COOKIE": "refresh-token",
|
|
12
|
+
"HONO_OIDC_ACCESS_TOKEN_COOKIE": "access-token",
|
|
13
|
+
"HONO_OIDC_CODE_VERIFIER_COOKIE": "code-verifier"
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
### Usage
|
|
17
|
+
```ts
|
|
18
|
+
app.use('*', except('/auth/callback', withIdentity));
|
|
19
|
+
|
|
20
|
+
app.use('/auth/callback', forAuthorization);
|
|
21
|
+
|
|
22
|
+
app.get('/async/api/identity', async (c) => {
|
|
23
|
+
return c.json(c.get('identity'));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
app.get('/', async (c) => {
|
|
27
|
+
return c.text('hello world');
|
|
28
|
+
});
|
|
29
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const withIdentity: import("hono/types").MiddlewareHandler<any, string, {}, Response | (Response & import("hono/types").TypedResponse<undefined, 302, "redirect">)>;
|
|
2
|
+
export declare const forAuthorization: import("hono/types").MiddlewareHandler<any, string, {}, Response & import("hono/types").TypedResponse<undefined, 302, "redirect">>;
|
|
3
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { deleteCookie, getSignedCookie, setSignedCookie } from "hono/cookie";
|
|
2
|
+
import { createMiddleware } from "hono/factory";
|
|
3
|
+
import { createIdentityProvider, } from "@shad-claiborne/basic-oidc";
|
|
4
|
+
import randomstring from "randomstring";
|
|
5
|
+
import { env } from "hono/adapter";
|
|
6
|
+
export const withIdentity = createMiddleware(async (c, next) => {
|
|
7
|
+
const { HONO_OIDC_ISSUER, HONO_OIDC_CLIENT_ID, HONO_OIDC_CLIENT_SECRET, HONO_OIDC_REDIRECT_URI, HONO_OIDC_COOKIE_SECRET, HONO_OIDC_ACCESS_TOKEN_COOKIE, HONO_OIDC_REFRESH_TOKEN_COOKIE, HONO_OIDC_ID_TOKEN_COOKIE, HONO_OIDC_CODE_VERIFIER_COOKIE, } = env(c);
|
|
8
|
+
const provider = await createIdentityProvider(HONO_OIDC_ISSUER);
|
|
9
|
+
const client = provider.createClient(HONO_OIDC_CLIENT_ID, HONO_OIDC_CLIENT_SECRET);
|
|
10
|
+
const tokenSet = {
|
|
11
|
+
id_token: await getSignedCookie(c, HONO_OIDC_COOKIE_SECRET, HONO_OIDC_ID_TOKEN_COOKIE),
|
|
12
|
+
access_token: await getSignedCookie(c, HONO_OIDC_COOKIE_SECRET, HONO_OIDC_ACCESS_TOKEN_COOKIE),
|
|
13
|
+
refresh_token: await getSignedCookie(c, HONO_OIDC_COOKIE_SECRET, HONO_OIDC_REFRESH_TOKEN_COOKIE),
|
|
14
|
+
};
|
|
15
|
+
let id;
|
|
16
|
+
try {
|
|
17
|
+
id = await provider.getIdentity(client, tokenSet);
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
id = null;
|
|
21
|
+
}
|
|
22
|
+
if (id === null) {
|
|
23
|
+
try {
|
|
24
|
+
const tokenResponse = await client.refreshAccess(tokenSet);
|
|
25
|
+
id = await provider.getIdentity(client, tokenResponse);
|
|
26
|
+
if (tokenResponse.access_token)
|
|
27
|
+
await setSignedCookie(c, HONO_OIDC_ACCESS_TOKEN_COOKIE, tokenResponse.access_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: "strict" });
|
|
28
|
+
if (tokenResponse.refresh_token)
|
|
29
|
+
await setSignedCookie(c, HONO_OIDC_REFRESH_TOKEN_COOKIE, tokenResponse.refresh_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: "strict" });
|
|
30
|
+
if (tokenResponse.id_token)
|
|
31
|
+
await setSignedCookie(c, HONO_OIDC_ID_TOKEN_COOKIE, tokenResponse.id_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: "strict" });
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
const stateId = randomstring.generate(5);
|
|
35
|
+
const state = { originUrl: c.req.url };
|
|
36
|
+
await setSignedCookie(c, `_authstate-${stateId}`, JSON.stringify(state), HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: "strict" });
|
|
37
|
+
const codeVerifier = randomstring.generate(16);
|
|
38
|
+
await setSignedCookie(c, HONO_OIDC_CODE_VERIFIER_COOKIE, codeVerifier, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: "strict" });
|
|
39
|
+
const authRequest = client
|
|
40
|
+
.newAuthorizationRequest()
|
|
41
|
+
.setRedirectUri(HONO_OIDC_REDIRECT_URI)
|
|
42
|
+
.setResponseMode("query")
|
|
43
|
+
.setResponseType("code id_token")
|
|
44
|
+
.setScope(["profile"])
|
|
45
|
+
.setCodeChallenge(codeVerifier)
|
|
46
|
+
.setState(stateId);
|
|
47
|
+
const authRequestURL = authRequest.toURL();
|
|
48
|
+
return c.redirect(authRequestURL.toString());
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
c.set("identity", id);
|
|
52
|
+
await next();
|
|
53
|
+
});
|
|
54
|
+
export const forAuthorization = createMiddleware(async (c) => {
|
|
55
|
+
const { HONO_OIDC_ISSUER, HONO_OIDC_CLIENT_ID, HONO_OIDC_CLIENT_SECRET, HONO_OIDC_REDIRECT_URI, HONO_OIDC_COOKIE_SECRET, HONO_OIDC_ACCESS_TOKEN_COOKIE, HONO_OIDC_REFRESH_TOKEN_COOKIE, HONO_OIDC_ID_TOKEN_COOKIE, HONO_OIDC_CODE_VERIFIER_COOKIE, } = env(c);
|
|
56
|
+
const provider = await createIdentityProvider(HONO_OIDC_ISSUER);
|
|
57
|
+
const client = provider.createClient(HONO_OIDC_CLIENT_ID, HONO_OIDC_CLIENT_SECRET);
|
|
58
|
+
const requestURL = new URL(c.req.url), requestParams = requestURL.searchParams;
|
|
59
|
+
const authResponse = Object.fromEntries(requestParams);
|
|
60
|
+
const stateId = requestParams.get("state");
|
|
61
|
+
if (stateId === undefined)
|
|
62
|
+
throw new Error("state missing from authorization response");
|
|
63
|
+
const stateCookie = `_authstate-${stateId}`;
|
|
64
|
+
const stateJson = await getSignedCookie(c, HONO_OIDC_COOKIE_SECRET, stateCookie);
|
|
65
|
+
deleteCookie(c, stateCookie);
|
|
66
|
+
if (stateJson === false || stateJson === undefined)
|
|
67
|
+
throw new Error("state cookie failed");
|
|
68
|
+
const state = JSON.parse(stateJson);
|
|
69
|
+
const codeVerifier = await getSignedCookie(c, HONO_OIDC_COOKIE_SECRET, HONO_OIDC_CODE_VERIFIER_COOKIE);
|
|
70
|
+
deleteCookie(c, HONO_OIDC_CODE_VERIFIER_COOKIE);
|
|
71
|
+
if (codeVerifier === false || codeVerifier === undefined)
|
|
72
|
+
throw new Error("code verifier cookie failed");
|
|
73
|
+
const tokenResponse = await client.requestAccess(authResponse, { codeVerifier, redirectUri: HONO_OIDC_REDIRECT_URI });
|
|
74
|
+
if (tokenResponse.access_token)
|
|
75
|
+
await setSignedCookie(c, HONO_OIDC_ACCESS_TOKEN_COOKIE, tokenResponse.access_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: "strict" });
|
|
76
|
+
if (tokenResponse.refresh_token)
|
|
77
|
+
await setSignedCookie(c, HONO_OIDC_REFRESH_TOKEN_COOKIE, tokenResponse.refresh_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: "strict" });
|
|
78
|
+
if (tokenResponse.id_token)
|
|
79
|
+
await setSignedCookie(c, HONO_OIDC_ID_TOKEN_COOKIE, tokenResponse.id_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: "strict" });
|
|
80
|
+
return c.redirect(state.originUrl);
|
|
81
|
+
});
|
|
82
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shad-claiborne/hono-middleware-oidc",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "OIDC middleware for Hono",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"./identity-provider": {
|
|
13
|
+
"import": "./dist/identity-provider/index.js",
|
|
14
|
+
"types": "./dist/identity-provider/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/**/*.js",
|
|
20
|
+
"dist/**/*.d.ts"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"test": "vitest"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"hono",
|
|
28
|
+
"honojs",
|
|
29
|
+
"openid",
|
|
30
|
+
"oauth"
|
|
31
|
+
],
|
|
32
|
+
"author": "Shad Claiborne <me@shadclaiborne.com>",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/shad-claiborne/hono-middleware-oidc.git"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/axios": "^0.9.36",
|
|
40
|
+
"@types/crypto-js": "^4.2.2",
|
|
41
|
+
"@types/node": "^25.0.6",
|
|
42
|
+
"@types/randomstring": "^1.3.0",
|
|
43
|
+
"dotenv": "^17.2.3",
|
|
44
|
+
"msw": "^2.12.7",
|
|
45
|
+
"typescript": "^5.9.3",
|
|
46
|
+
"vitest": "^4.0.17"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@shad-claiborne/basic-oidc": "^1.1.9",
|
|
50
|
+
"axios": "^1.13.2",
|
|
51
|
+
"crypto-js": "^4.2.0",
|
|
52
|
+
"hono": "^4.11.3",
|
|
53
|
+
"randomstring": "^1.3.1"
|
|
54
|
+
}
|
|
55
|
+
}
|