Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(oidc-auth) Add initOidcAuthMiddleware and avoid mutating environment variables #980

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

rajsite
Copy link

@rajsite rajsite commented Feb 24, 2025

Resolves #979 and resolves #978

The author should do the following, if applicable

  • Add tests
  • Run tests
  • yarn changeset at the top of this repo and push the changeset
  • Follow the contribution guide

Copy link

changeset-bot bot commented Feb 24, 2025

🦋 Changeset detected

Latest commit: 0ae0742

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/oidc-auth Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@rajsite
Copy link
Author

rajsite commented Feb 24, 2025

@hnw would appreciate your review of this PR when you have a chance 🙂

@rajsite
Copy link
Author

rajsite commented Mar 5, 2025

Merged vitest changes. Would still appreciate a review / feedback @hnw (fyi previous reviewers for oidc-auth @yusukebe @maemaemae3 )

@rajsite rajsite changed the title feat(@hono/oidc-auth) Add setOidcAuthEnv and avoid mutating environment variables feat(@hono/oidc-auth) Add setOidcAuthEnvMiiddleware and avoid mutating environment variables Mar 10, 2025
@rajsite rajsite changed the title feat(@hono/oidc-auth) Add setOidcAuthEnvMiiddleware and avoid mutating environment variables feat(@hono/oidc-auth) Add setOidcAuthEnvMiddleware and avoid mutating environment variables Mar 10, 2025
@rajsite rajsite changed the title feat(@hono/oidc-auth) Add setOidcAuthEnvMiddleware and avoid mutating environment variables feat(oidc-auth) Add initOidcAuthMiddleware and avoid mutating environment variables Mar 12, 2025
Comment on lines +97 to +130
if (oidcAuthEnv.OIDC_AUTH_SECRET === undefined) {
throw new HTTPException(500, { message: 'Session secret is not provided' })
}
if (oidcAuthEnv.OIDC_AUTH_SECRET.length < 32) {
throw new HTTPException(500, {
message: 'Session secrets must be at least 32 characters long',
})
}
if (oidcAuthEnv.OIDC_ISSUER === undefined) {
throw new HTTPException(500, { message: 'OIDC issuer is not provided' })
}
if (oidcAuthEnv.OIDC_CLIENT_ID === undefined) {
throw new HTTPException(500, { message: 'OIDC client ID is not provided' })
}
if (oidcAuthEnv.OIDC_CLIENT_SECRET === undefined) {
throw new HTTPException(500, { message: 'OIDC client secret is not provided' })
}
oidcAuthEnv.OIDC_REDIRECT_URI = oidcAuthEnv.OIDC_REDIRECT_URI ?? defaultOidcRedirectUri
if (!oidcAuthEnv.OIDC_REDIRECT_URI.startsWith('/')) {
try {
new URL(oidcAuthEnv.OIDC_REDIRECT_URI)
} catch (e) {
throw new HTTPException(500, {
message: 'The OIDC redirect URI is invalid. It must be a full URL or an absolute path',
})
}
}
oidcAuthEnv.OIDC_COOKIE_PATH = oidcAuthEnv.OIDC_COOKIE_PATH ?? defaultOidcAuthCookiePath
oidcAuthEnv.OIDC_COOKIE_NAME = oidcAuthEnv.OIDC_COOKIE_NAME ?? defaultOidcAuthCookieName
oidcAuthEnv.OIDC_AUTH_REFRESH_INTERVAL =
oidcAuthEnv.OIDC_AUTH_REFRESH_INTERVAL ?? `${defaultRefreshInterval}`
oidcAuthEnv.OIDC_AUTH_EXPIRES = oidcAuthEnv.OIDC_AUTH_EXPIRES ?? `${defaultExpirationInterval}`
oidcAuthEnv.OIDC_SCOPES = oidcAuthEnv.OIDC_SCOPES ?? ''
c.set('oidcAuthEnv', oidcAuthEnv)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is unchanged, just moved

if (currentOidcAuthEnv !== undefined) {
throw new HTTPException(500, { message: 'OIDC Auth env is already configured' })
}
const ev = env<Readonly<OidcAuthEnv>>(c)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to take a Readonly reference to the environment variables to avoid accidental mutation

Comment on lines +84 to +96
const oidcAuthEnv = {
OIDC_AUTH_SECRET: config?.OIDC_AUTH_SECRET ?? ev.OIDC_AUTH_SECRET,
OIDC_AUTH_REFRESH_INTERVAL: config?.OIDC_AUTH_REFRESH_INTERVAL ?? ev.OIDC_AUTH_REFRESH_INTERVAL,
OIDC_AUTH_EXPIRES: config?.OIDC_AUTH_EXPIRES ?? ev.OIDC_AUTH_EXPIRES,
OIDC_ISSUER: config?.OIDC_ISSUER ?? ev.OIDC_ISSUER,
OIDC_CLIENT_ID: config?.OIDC_CLIENT_ID ?? ev.OIDC_CLIENT_ID,
OIDC_CLIENT_SECRET: config?.OIDC_CLIENT_SECRET ?? ev.OIDC_CLIENT_SECRET,
OIDC_REDIRECT_URI: config?.OIDC_REDIRECT_URI ?? ev.OIDC_REDIRECT_URI,
OIDC_SCOPES: config?.OIDC_SCOPES ?? ev.OIDC_SCOPES,
OIDC_COOKIE_PATH: config?.OIDC_COOKIE_PATH ?? ev.OIDC_COOKIE_PATH,
OIDC_COOKIE_NAME: config?.OIDC_COOKIE_NAME ?? ev.OIDC_COOKIE_NAME,
OIDC_COOKIE_DOMAIN: config?.OIDC_COOKIE_DOMAIN ?? ev.OIDC_COOKIE_DOMAIN,
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fallback to env variables when not provided in config

@rajsite
Copy link
Author

rajsite commented Mar 12, 2025

@hnw Sorry for the repeated mentions but I added some comments to help review. Please let me know if you have any concerns with the direction, etc an I can address them quickly! I appreciate your time and for sharing the plugin!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

oidc-auth mutates global environment variables oidc-auth programmatic configuration
3 participants