Skip to content

Commit

Permalink
refactor and format
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed Aug 25, 2024
1 parent a02e424 commit 8fc0a6a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 35 deletions.
5 changes: 3 additions & 2 deletions packages/oidc-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@
"hono": ">=3.*"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240821.1",
"@types/jest": "^29.5.11",
"@types/jsonwebtoken": "^9.0.5",
"@types/node": "^22.5.0",
"hono": "^4.0.1",
"jest": "^29.7.0",
"jsonwebtoken": "^9.0.2",
Expand All @@ -49,5 +47,8 @@
},
"dependencies": {
"oauth4webapi": "^2.6.0"
},
"engines": {
"node": ">=18.0.0"
}
}
24 changes: 16 additions & 8 deletions packages/oidc-auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import type { Context, MiddlewareHandler } from 'hono'
import { env } from 'hono/adapter'
import { getCookie, setCookie, deleteCookie } from 'hono/cookie'
import { deleteCookie, getCookie, setCookie } from 'hono/cookie'
import { createMiddleware } from 'hono/factory'
import { HTTPException } from 'hono/http-exception'
import { sign, verify } from 'hono/jwt'
Expand Down Expand Up @@ -121,9 +121,9 @@ export const getAuth = async (c: Context): Promise<OidcAuth | null> => {
return null
}
try {
auth = await verify(session_jwt, env.OIDC_AUTH_SECRET) as OidcAuth
auth = await verify(session_jwt, env.OIDC_AUTH_SECRET)
} catch (e) {
deleteCookie(c, oidcAuthCookieName, { path: env.OIDC_COOKIE_PATH ?? '/'} )
deleteCookie(c, oidcAuthCookieName, { path: env.OIDC_COOKIE_PATH ?? '/' })
return null
}
if (auth === null || auth.rtkexp === undefined || auth.ssnexp === undefined) {
Expand All @@ -138,7 +138,7 @@ export const getAuth = async (c: Context): Promise<OidcAuth | null> => {
if (auth.rtkexp < now) {
// Refresh the token if it has expired
if (auth.rtk === undefined || auth.rtk === '') {
deleteCookie(c, oidcAuthCookieName, { path: env.OIDC_COOKIE_PATH ?? '/'})
deleteCookie(c, oidcAuthCookieName, { path: env.OIDC_COOKIE_PATH ?? '/' })
return null
}
const as = await getAuthorizationServer(c)
Expand All @@ -147,7 +147,7 @@ export const getAuth = async (c: Context): Promise<OidcAuth | null> => {
const result = await oauth2.processRefreshTokenResponse(as, client, response)
if (oauth2.isOAuth2Error(result)) {
// The refresh_token might be expired or revoked
deleteCookie(c, oidcAuthCookieName, { path: env.OIDC_COOKIE_PATH ?? '/'})
deleteCookie(c, oidcAuthCookieName, { path: env.OIDC_COOKIE_PATH ?? '/' })
return null
}
auth = await updateAuth(c, auth, result)
Expand Down Expand Up @@ -187,7 +187,11 @@ const updateAuth = async (
ssnexp: orig?.ssnexp || Math.floor(Date.now() / 1000) + authExpires,
}
const session_jwt = await sign(updated, env.OIDC_AUTH_SECRET)
setCookie(c, oidcAuthCookieName, session_jwt, { path: env.OIDC_COOKIE_PATH ?? '/', httpOnly: true, secure: true })
setCookie(c, oidcAuthCookieName, session_jwt, {
path: env.OIDC_COOKIE_PATH ?? '/',
httpOnly: true,
secure: true,
})
c.set('oidcAuthJwt', session_jwt)
return updated
}
Expand All @@ -200,7 +204,7 @@ export const revokeSession = async (c: Context): Promise<void> => {
if (session_jwt !== undefined) {
const env = getOidcAuthEnv(c)
deleteCookie(c, oidcAuthCookieName, { path: env.OIDC_COOKIE_PATH ?? '/' })
const auth: OidcAuth = await verify(session_jwt, env.OIDC_AUTH_SECRET) as OidcAuth
const auth = await verify(session_jwt, env.OIDC_AUTH_SECRET)
if (auth.rtk !== undefined && auth.rtk !== '') {
// revoke refresh token
const as = await getAuthorizationServer(c)
Expand Down Expand Up @@ -376,7 +380,11 @@ export const oidcAuthMiddleware = (): MiddlewareHandler => {
// Workaround to set the session cookie when the response is returned by the origin server
const session_jwt = c.get('oidcAuthJwt')
if (session_jwt !== undefined) {
setCookie(c, oidcAuthCookieName, session_jwt, { path: env.OIDC_COOKIE_PATH ?? '/', httpOnly: true, secure: true })
setCookie(c, oidcAuthCookieName, session_jwt, {
path: env.OIDC_COOKIE_PATH ?? '/',
httpOnly: true,
secure: true,
})
}
})
}
25 changes: 0 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -978,13 +978,6 @@ __metadata:
languageName: node
linkType: hard

"@cloudflare/workers-types@npm:^4.20240821.1":
version: 4.20240821.1
resolution: "@cloudflare/workers-types@npm:4.20240821.1"
checksum: 5abf7cfb4241ee7babd382807f6061ab4315c7703957673289238234a5e4ed7c24154afe8b3cda15c0e60ea52e4c2e44b3554a10e854f85a5f5ea2d5bf5cde46
languageName: node
linkType: hard

"@colors/colors@npm:1.5.0":
version: 1.5.0
resolution: "@colors/colors@npm:1.5.0"
Expand Down Expand Up @@ -2433,10 +2426,8 @@ __metadata:
version: 0.0.0-use.local
resolution: "@hono/oidc-auth@workspace:packages/oidc-auth"
dependencies:
"@cloudflare/workers-types": "npm:^4.20240821.1"
"@types/jest": "npm:^29.5.11"
"@types/jsonwebtoken": "npm:^9.0.5"
"@types/node": "npm:^22.5.0"
hono: "npm:^4.0.1"
jest: "npm:^29.7.0"
jsonwebtoken: "npm:^9.0.2"
Expand Down Expand Up @@ -4660,15 +4651,6 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^22.5.0":
version: 22.5.0
resolution: "@types/node@npm:22.5.0"
dependencies:
undici-types: "npm:~6.19.2"
checksum: 45aa75c5e71645fac42dced4eff7f197c3fdfff6e8a9fdacd0eb2e748ff21ee70ffb73982f068a58e8d73b2c088a63613142c125236cdcf3c072ea97eada1559
languageName: node
linkType: hard

"@types/normalize-package-data@npm:^2.4.0":
version: 2.4.4
resolution: "@types/normalize-package-data@npm:2.4.4"
Expand Down Expand Up @@ -19163,13 +19145,6 @@ __metadata:
languageName: node
linkType: hard

"undici-types@npm:~6.19.2":
version: 6.19.8
resolution: "undici-types@npm:6.19.8"
checksum: 078afa5990fba110f6824823ace86073b4638f1d5112ee26e790155f481f2a868cc3e0615505b6f4282bdf74a3d8caad715fd809e870c2bb0704e3ea6082f344
languageName: node
linkType: hard

"undici@npm:5.20.0":
version: 5.20.0
resolution: "undici@npm:5.20.0"
Expand Down

0 comments on commit 8fc0a6a

Please sign in to comment.