-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement PKCE extension for Authorization Code Grant (RFC 7636)
See protocol details here: https://www.rfc-editor.org/rfc/rfc7636.html
- Loading branch information
1 parent
f7db10d
commit 3203054
Showing
6 changed files
with
288 additions
and
21 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict' | ||
|
||
const fastify = require('fastify')({ logger: { level: 'trace' } }) | ||
const sget = require('simple-get') | ||
|
||
const cookieOpts = { | ||
// domain: 'localhost', | ||
path: '/', | ||
secure: true, | ||
sameSite: 'lax', | ||
httpOnly: true | ||
} | ||
|
||
// const oauthPlugin = require('fastify-oauth2') | ||
fastify.register(require('@fastify/cookie'), { | ||
secret: ['my-secret'], | ||
parseOptions: cookieOpts | ||
}) | ||
|
||
const oauthPlugin = require('..') | ||
fastify.register(oauthPlugin, { | ||
name: 'googleOAuth2', | ||
scope: ['openid', 'profile', 'email'], | ||
credentials: { | ||
client: { | ||
id: process.env.CLIENT_ID, | ||
secret: process.env.CLIENT_SECRET | ||
}, | ||
auth: oauthPlugin.GOOGLE_CONFIGURATION | ||
}, | ||
startRedirectPath: '/login/google', | ||
callbackUri: 'http://localhost:3000/interaction/callback/google', | ||
cookie: cookieOpts, | ||
pkce: 'S256' // "plain" is also supported in this library, see your own provider's .well-known/openid-configuration endpoint (if it's exposed) to understand which methods are supported | ||
}) | ||
|
||
fastify.get('/interaction/callback/google', function (request, reply) { | ||
// Note that in this example a "reply" is also passed, it's so that code verifier cookie can be cleaned before | ||
// token is requested from token endpoint | ||
this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply, (err, result) => { | ||
if (err) { | ||
reply.send(err) | ||
return | ||
} | ||
|
||
sget.concat({ | ||
url: 'https://www.googleapis.com/oauth2/v2/userinfo', | ||
method: 'GET', | ||
headers: { | ||
Authorization: 'Bearer ' + result.token.access_token | ||
}, | ||
json: true | ||
}, function (err, res, data) { | ||
if (err) { | ||
reply.send(err) | ||
return | ||
} | ||
reply.send(data) | ||
}) | ||
}) | ||
}) | ||
|
||
fastify.listen({ port: 3000 }) | ||
fastify.log.info('go to http://localhost:3000/login/google') |
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
Oops, something went wrong.