@shad-claiborne/hono-middleware-oidc 1.0.0 → 1.0.2

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.
Files changed (2) hide show
  1. package/dist/index.js +21 -13
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ export const withIdentity = createMiddleware(async (c, next) => {
14
14
  };
15
15
  let id;
16
16
  try {
17
- id = await provider.getIdentity(client, tokenSet);
17
+ id = await provider.getIdentity(tokenSet);
18
18
  }
19
19
  catch (err) {
20
20
  id = null;
@@ -22,20 +22,25 @@ export const withIdentity = createMiddleware(async (c, next) => {
22
22
  if (id === null) {
23
23
  try {
24
24
  const tokenResponse = await client.refreshAccess(tokenSet);
25
- id = await provider.getIdentity(client, tokenResponse);
25
+ id = await provider.getIdentity(tokenResponse);
26
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" });
27
+ await setSignedCookie(c, HONO_OIDC_ACCESS_TOKEN_COOKIE, tokenResponse.access_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: 'Lax' });
28
+ if (tokenResponse.refresh_token) {
29
+ const maxAge = tokenResponse.refresh_token_expires_in ?? (24 * 60 * 60);
30
+ await setSignedCookie(c, HONO_OIDC_REFRESH_TOKEN_COOKIE, tokenResponse.refresh_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: 'Lax', maxAge });
31
+ }
32
+ if (tokenResponse.id_token) {
33
+ const idToken = await provider.decodeIdentityToken(tokenResponse.id_token);
34
+ const maxAge = idToken.exp - Math.floor(Date.now() / 1000);
35
+ await setSignedCookie(c, HONO_OIDC_ID_TOKEN_COOKIE, tokenResponse.id_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: 'Lax', maxAge });
36
+ }
32
37
  }
33
38
  catch (err) {
34
39
  const stateId = randomstring.generate(5);
35
40
  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" });
41
+ await setSignedCookie(c, `_authstate-${stateId}`, JSON.stringify(state), HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: 'Lax' });
37
42
  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" });
43
+ await setSignedCookie(c, HONO_OIDC_CODE_VERIFIER_COOKIE, codeVerifier, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: 'Lax' });
39
44
  const authRequest = client
40
45
  .newAuthorizationRequest()
41
46
  .setRedirectUri(HONO_OIDC_REDIRECT_URI)
@@ -72,11 +77,14 @@ export const forAuthorization = createMiddleware(async (c) => {
72
77
  throw new Error("code verifier cookie failed");
73
78
  const tokenResponse = await client.requestAccess(authResponse, { codeVerifier, redirectUri: HONO_OIDC_REDIRECT_URI });
74
79
  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" });
80
+ await setSignedCookie(c, HONO_OIDC_ACCESS_TOKEN_COOKIE, tokenResponse.access_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: 'Lax', maxAge: tokenResponse.expires_in });
76
81
  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" });
82
+ await setSignedCookie(c, HONO_OIDC_REFRESH_TOKEN_COOKIE, tokenResponse.refresh_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: 'Lax' });
83
+ if (tokenResponse.id_token) {
84
+ const idToken = await provider.decodeIdentityToken(tokenResponse.id_token);
85
+ const maxAge = idToken.exp - Math.floor(Date.now() / 1000);
86
+ await setSignedCookie(c, HONO_OIDC_ID_TOKEN_COOKIE, tokenResponse.id_token, HONO_OIDC_COOKIE_SECRET, { httpOnly: true, secure: true, sameSite: 'Lax', maxAge });
87
+ }
80
88
  return c.redirect(state.originUrl);
81
89
  });
82
90
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shad-claiborne/hono-middleware-oidc",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "OIDC middleware for Hono",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -46,7 +46,7 @@
46
46
  "vitest": "^4.0.17"
47
47
  },
48
48
  "dependencies": {
49
- "@shad-claiborne/basic-oidc": "^1.1.9",
49
+ "@shad-claiborne/basic-oidc": "^1.1.13",
50
50
  "axios": "^1.13.2",
51
51
  "crypto-js": "^4.2.0",
52
52
  "hono": "^4.11.3",