-
-
Notifications
You must be signed in to change notification settings - Fork 73
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: options to customize cookie names #272
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,8 +11,9 @@ const kGenerateCallbackUriParams = Symbol.for('fastify-oauth2.generate-callback- | |||||
|
||||||
const { promisify, callbackify } = require('node:util') | ||||||
|
||||||
const DEFAULT_VERIFIER_COOKIE_NAME = 'oauth2-code-verifier' | ||||||
const DEFAULT_REDIRECT_STATE_COOKIE_NAME = 'oauth2-redirect-state' | ||||||
const USER_AGENT = 'fastify-oauth2' | ||||||
const VERIFIER_COOKIE_NAME = 'oauth2-code-verifier' | ||||||
const PKCE_METHODS = ['S256', 'plain'] | ||||||
|
||||||
const random = (bytes = 32) => randomBytes(bytes).toString('base64url') | ||||||
|
@@ -25,7 +26,10 @@ function defaultGenerateStateFunction (request, callback) { | |||||
|
||||||
function defaultCheckStateFunction (request, callback) { | ||||||
const state = request.query.state | ||||||
const stateCookie = request.cookies['oauth2-redirect-state'] | ||||||
const stateCookie = | ||||||
request.cookies[ | ||||||
this.redirectStateCookieName || DEFAULT_REDIRECT_STATE_COOKIE_NAME | ||||||
] | ||||||
if (stateCookie && state === stateCookie) { | ||||||
callback() | ||||||
return | ||||||
|
@@ -98,6 +102,20 @@ function fastifyOauth2 (fastify, options, next) { | |||||
if (!options.discovery && !options.credentials.auth) { | ||||||
return next(new Error('options.discovery.issuer or credentials.auth have to be given')) | ||||||
} | ||||||
if ( | ||||||
options.verifierCookieName && | ||||||
typeof options.verifierCookieName !== 'string' | ||||||
) { | ||||||
return next(new Error('options.verifierCookieName should be a string')) | ||||||
} | ||||||
if ( | ||||||
options.redirectStateCookieName && | ||||||
typeof options.redirectStateCookieName !== 'string' | ||||||
) { | ||||||
return next( | ||||||
new Error('options.redirectStateCookieName should be a string') | ||||||
) | ||||||
} | ||||||
if (!fastify.hasReplyDecorator('cookie')) { | ||||||
fastify.register(require('@fastify/cookie')) | ||||||
} | ||||||
|
@@ -116,10 +134,14 @@ function fastifyOauth2 (fastify, options, next) { | |||||
tokenRequestParams = {}, | ||||||
scope, | ||||||
generateStateFunction = defaultGenerateStateFunction, | ||||||
checkStateFunction = defaultCheckStateFunction, | ||||||
checkStateFunction = defaultCheckStateFunction.bind({ | ||||||
redirectStateCookieName: configured.redirectStateCookieName | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, ok, I see. Sure I can move that default value from |
||||||
}), | ||||||
startRedirectPath, | ||||||
tags = [], | ||||||
schema = { tags } | ||||||
schema = { tags }, | ||||||
redirectStateCookieName = DEFAULT_REDIRECT_STATE_COOKIE_NAME, | ||||||
verifierCookieName = DEFAULT_VERIFIER_COOKIE_NAME | ||||||
} = configured | ||||||
|
||||||
if (userAgent) { | ||||||
|
@@ -153,7 +175,7 @@ function fastifyOauth2 (fastify, options, next) { | |||||
return | ||||||
} | ||||||
|
||||||
reply.setCookie('oauth2-redirect-state', state, cookieOpts) | ||||||
reply.setCookie(redirectStateCookieName, state, cookieOpts) | ||||||
|
||||||
// when PKCE extension is used | ||||||
let pkceParams = {} | ||||||
|
@@ -164,7 +186,7 @@ function fastifyOauth2 (fastify, options, next) { | |||||
code_challenge: challenge, | ||||||
code_challenge_method: configured.pkce | ||||||
} | ||||||
reply.setCookie(VERIFIER_COOKIE_NAME, verifier, cookieOpts) | ||||||
reply.setCookie(verifierCookieName, verifier, cookieOpts) | ||||||
} | ||||||
|
||||||
const urlOptions = Object.assign({}, generateCallbackUriParams(callbackUriParams, request, scope, state), { | ||||||
|
@@ -227,7 +249,7 @@ function fastifyOauth2 (fastify, options, next) { | |||||
|
||||||
function getAccessTokenFromAuthorizationCodeFlowCallbacked (request, reply, callback) { | ||||||
const code = request.query.code | ||||||
const pkceParams = configured.pkce ? { code_verifier: request.cookies['oauth2-code-verifier'] } : {} | ||||||
const pkceParams = configured.pkce ? { code_verifier: request.cookies[verifierCookieName] } : {} | ||||||
|
||||||
const _callback = typeof reply === 'function' ? reply : callback | ||||||
|
||||||
|
@@ -299,7 +321,7 @@ function fastifyOauth2 (fastify, options, next) { | |||||
} | ||||||
|
||||||
function clearCodeVerifierCookie (reply) { | ||||||
reply.clearCookie(VERIFIER_COOKIE_NAME, cookieOpts) | ||||||
reply.clearCookie(verifierCookieName, cookieOpts) | ||||||
} | ||||||
|
||||||
const pUserInfo = promisify(userInfoCallbacked) | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a potential security issue - we can't try to read both cookie
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, I don't think it is reading both. If you have provided
redirectStateCookieName
in options it will readrequest.cookies[this.redirectStateCookieName]
otherwise it uses a default value and takesrequest.cookies[DEFAULT_REDIRECT_STATE_COOKIE_NAME]
- never tries to read both.