From 8fc0a6a88090d08d752c8a3a832252796e87786b Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Mon, 26 Aug 2024 07:33:54 +0900 Subject: [PATCH] refactor and format --- packages/oidc-auth/package.json | 5 +++-- packages/oidc-auth/src/index.ts | 24 ++++++++++++++++-------- yarn.lock | 25 ------------------------- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/packages/oidc-auth/package.json b/packages/oidc-auth/package.json index 877006e4d..e8b424ef4 100644 --- a/packages/oidc-auth/package.json +++ b/packages/oidc-auth/package.json @@ -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", @@ -49,5 +47,8 @@ }, "dependencies": { "oauth4webapi": "^2.6.0" + }, + "engines": { + "node": ">=18.0.0" } } diff --git a/packages/oidc-auth/src/index.ts b/packages/oidc-auth/src/index.ts index 07e171b2a..bcf67cddd 100644 --- a/packages/oidc-auth/src/index.ts +++ b/packages/oidc-auth/src/index.ts @@ -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' @@ -121,9 +121,9 @@ export const getAuth = async (c: Context): Promise => { 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) { @@ -138,7 +138,7 @@ export const getAuth = async (c: Context): Promise => { 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) @@ -147,7 +147,7 @@ export const getAuth = async (c: Context): Promise => { 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) @@ -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 } @@ -200,7 +204,7 @@ export const revokeSession = async (c: Context): Promise => { 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) @@ -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, + }) } }) } diff --git a/yarn.lock b/yarn.lock index d14af7e6d..ca3e20345 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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"