Skip to content

Commit ccb9c21

Browse files
authored
add: Schema and Tags Options (#65)
1 parent 90b3399 commit ccb9c21

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ fastify.register(oauthPlugin, {
8585
})
8686
```
8787

88+
### Schema configuration
89+
90+
You can specify your own schema for the `startRedirectPath` end-point. It allows you to create a well-documented document when using `fastify-swagger` together.
91+
Note: `schema` option will override the `tags` option without merge them.
92+
93+
```js
94+
fastify.register(oauthPlugin, {
95+
name: 'facebookOAuth2',
96+
credentials: {
97+
client: {
98+
id: '<CLIENT_ID>',
99+
secret: '<CLIENT_SECRET>'
100+
},
101+
auth: oauthPlugin.FACEBOOK_CONFIGURATION
102+
},
103+
// register a fastify url to start the redirect flow
104+
startRedirectPath: '/login/facebook',
105+
// facebook redirect here after the user login
106+
callbackUri: 'http://localhost:3000/login/facebook/callback',
107+
// add tags for the schema
108+
tags: ['facebook', 'oauth2'],
109+
// add schema
110+
schema: {
111+
tags: ['facebook', 'oauth2'] // this will take the precedence
112+
}
113+
})
114+
```
115+
88116
## Set custom state
89117

90118
The `generateStateFunction` accepts a function to generate the `state` parameter for the OAUTH flow. This function receives the Fastify's `request` object as parameter.

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ declare namespace fastifyOauth2 {
6666
generateStateFunction?: Function;
6767
checkStateFunction?: Function;
6868
startRedirectPath: string;
69+
tags?: string[];
70+
schema?: object;
6971
}
7072
}
7173

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ const oauthPlugin = fp(function (fastify, options, next) {
4545
if (!options.generateStateFunction ^ !options.checkStateFunction) {
4646
return next(new Error('options.checkStateFunction and options.generateStateFunction have to be given'))
4747
}
48+
if (options.tags && !Array.isArray(options.tags)) {
49+
return next(new Error('options.tags should be a array'))
50+
}
51+
if (options.schema && typeof options.schema !== 'object') {
52+
return next(new Error('options.schema should be a object'))
53+
}
4854

4955
const name = options.name
5056
const credentials = options.credentials
@@ -54,6 +60,8 @@ const oauthPlugin = fp(function (fastify, options, next) {
5460
const generateStateFunction = options.generateStateFunction || defaultGenerateStateFunction
5561
const checkStateFunction = options.checkStateFunction || defaultCheckStateFunction
5662
const startRedirectPath = options.startRedirectPath
63+
const tags = options.tags || []
64+
const schema = options.schema || { tags: tags }
5765

5866
function generateAuthorizationUri (requestObject) {
5967
const state = generateStateFunction(requestObject)
@@ -116,7 +124,7 @@ const oauthPlugin = fp(function (fastify, options, next) {
116124
const oauth2 = oauth2Module.create(credentials)
117125

118126
if (startRedirectPath) {
119-
fastify.get(startRedirectPath, startRedirectHandler)
127+
fastify.get(startRedirectPath, { schema }, startRedirectHandler)
120128
}
121129

122130
try {

index.test-d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Server, IncomingMessage, ServerResponse } from 'http';
88
*/
99
const auth = fastifyOauth2.GOOGLE_CONFIGURATION;
1010
const scope = ['r_emailaddress', 'r_basicprofile'];
11+
const tags = ['oauth2', 'oauth'];
1112
const credentials = {
1213
client: {
1314
id: 'test_id',
@@ -42,6 +43,7 @@ declare module 'fastify' {
4243
*/
4344
expectType<fastifyOauth2.ProviderConfiguration>(auth);
4445
expectType<string[]>(scope);
46+
expectType<string[]>(tags);
4547
expectType<fastifyOauth2.Credentials>(credentials);
4648

4749
expectError(fastifyOauth2()); // error because missing required arguments

test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,81 @@ t.test('options.generateStateFunction ^ options.checkStateFunction', t => {
468468
})
469469
})
470470

471+
t.test('options.tags should be a array', t => {
472+
t.plan(1)
473+
474+
const fastify = createFastify({ logger: { level: 'silent' } })
475+
476+
fastify.register(oauthPlugin, {
477+
name: 'the-name',
478+
credentials: {
479+
client: {
480+
id: 'my-client-id',
481+
secret: 'my-secret'
482+
},
483+
auth: oauthPlugin.GITHUB_CONFIGURATION
484+
},
485+
callbackUri: '/callback',
486+
tags: 'invalid tags'
487+
})
488+
.ready(err => {
489+
t.strictSame(err.message, 'options.tags should be a array')
490+
})
491+
})
492+
493+
t.test('options.schema should be a object', t => {
494+
t.plan(1)
495+
496+
const fastify = createFastify({ logger: { level: 'silent' } })
497+
498+
fastify.register(oauthPlugin, {
499+
name: 'the-name',
500+
credentials: {
501+
client: {
502+
id: 'my-client-id',
503+
secret: 'my-secret'
504+
},
505+
auth: oauthPlugin.GITHUB_CONFIGURATION
506+
},
507+
callbackUri: '/callback',
508+
schema: 1
509+
})
510+
.ready(err => {
511+
t.strictSame(err.message, 'options.schema should be a object')
512+
})
513+
})
514+
515+
t.test('options.schema', t => {
516+
const fastify = createFastify({ logger: { level: 'silent' } })
517+
518+
fastify.addHook('onRoute', function (routeOptions) {
519+
t.strictSame(routeOptions.schema, { tags: ['oauth2', 'oauth'] })
520+
t.end()
521+
})
522+
523+
fastify.register(oauthPlugin, {
524+
name: 'the-name',
525+
credentials: {
526+
client: {
527+
id: 'my-client-id',
528+
secret: 'my-secret'
529+
},
530+
auth: oauthPlugin.GITHUB_CONFIGURATION
531+
},
532+
startRedirectPath: '/login/github',
533+
callbackUri: '/callback',
534+
callbackUriParams: {
535+
access_type: 'offline'
536+
},
537+
scope: ['notifications'],
538+
schema: {
539+
tags: ['oauth2', 'oauth']
540+
}
541+
})
542+
543+
fastify.ready()
544+
})
545+
471546
t.test('already decorated', t => {
472547
t.plan(1)
473548

0 commit comments

Comments
 (0)