Skip to content

Commit 5aed71f

Browse files
stevesweetsStephen SweetlandUzlopak
authored
feat: allow default cookie options to be overridden (#223)
* feat: allow default cookie options to be overwritten * docs: document cookie options override * Update index.js Co-authored-by: Uzlopak <[email protected]> * refactor cookie opts declaration * test: add test cases * improve typings * simplify * use CookieSerializeOptions --------- Co-authored-by: Stephen Sweetland <[email protected]> Co-authored-by: Uzlopak <[email protected]>
1 parent 47f2b69 commit 5aed71f

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ fastify.register(require('@fastify/cookie'), cookieOptions)
6161
fastify.register(oauthPlugin, oauthOptions)
6262
```
6363

64+
Cookies are by default `httpOnly`, `sameSite: Lax`. If this does not suit your use case, it is possible to override the default cookie settings by providing options in the configuration object, for example
65+
66+
```js
67+
fastify.register(oauthPlugin, {
68+
...,
69+
cookie: {
70+
secure: true,
71+
sameSite: 'none'
72+
}
73+
})
74+
```
75+
6476
### Preset configurations
6577

6678
You can choose some default setup to assign to `auth` option.

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ function fastifyOauth2 (fastify, options, next) {
6161
if (options.schema && typeof options.schema !== 'object') {
6262
return next(new Error('options.schema should be a object'))
6363
}
64+
if (options.cookie && typeof options.cookie !== 'object') {
65+
return next(new Error('options.cookie should be an object'))
66+
}
6467

6568
if (!fastify.hasReplyDecorator('cookie')) {
6669
fastify.register(require('@fastify/cookie'))
@@ -78,14 +81,12 @@ function fastifyOauth2 (fastify, options, next) {
7881
const startRedirectPath = options.startRedirectPath
7982
const tags = options.tags || []
8083
const schema = options.schema || { tags }
84+
const cookieOpts = Object.assign({ httpOnly: true, sameSite: 'lax' }, options.cookie)
8185

8286
function generateAuthorizationUri (request, reply) {
8387
const state = generateStateFunction(request)
8488

85-
reply.setCookie('oauth2-redirect-state', state, {
86-
httpOnly: true,
87-
sameSite: 'lax'
88-
})
89+
reply.setCookie('oauth2-redirect-state', state, cookieOpts)
8990

9091
const urlOptions = Object.assign({}, generateCallbackUriParams(callbackUriParams, request, scope, state), {
9192
redirect_uri: callbackUri,

test/index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,28 @@ t.test('options.schema should be a object', t => {
602602
})
603603
})
604604

605+
t.test('options.cookie should be an object', t => {
606+
t.plan(1)
607+
608+
const fastify = createFastify({ logger: { level: 'silent' } })
609+
610+
fastify.register(fastifyOauth2, {
611+
name: 'the-name',
612+
credentials: {
613+
client: {
614+
id: 'my-client-id',
615+
secret: 'my-secret'
616+
},
617+
auth: fastifyOauth2.GITHUB_CONFIGURATION
618+
},
619+
callbackUri: '/callback',
620+
cookie: 1
621+
})
622+
.ready(err => {
623+
t.strictSame(err.message, 'options.cookie should be an object')
624+
})
625+
})
626+
605627
t.test('options.schema', t => {
606628
const fastify = createFastify({ logger: { level: 'silent' }, exposeHeadRoutes: false })
607629

types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify';
2+
import { CookieSerializeOptions } from "@fastify/cookie";
23

34
interface FastifyOauth2 extends FastifyPluginCallback<fastifyOauth2.FastifyOAuth2Options> {
45
APPLE_CONFIGURATION: fastifyOauth2.ProviderConfiguration;
@@ -30,6 +31,7 @@ declare namespace fastifyOauth2 {
3031
startRedirectPath?: string;
3132
tags?: string[];
3233
schema?: object;
34+
cookie?: CookieSerializeOptions;
3335
}
3436

3537
export interface Token {

types/index.test-d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const OAuth2Options: FastifyOAuth2Options = {
3131
generateStateFunction: () => {},
3232
checkStateFunction: () => {},
3333
startRedirectPath: '/login/testOauth',
34+
cookie: {
35+
secure: true,
36+
sameSite: 'none'
37+
}
3438
};
3539

3640
const server = fastify();

0 commit comments

Comments
 (0)