|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const fastify = require('fastify')({ logger: { level: 'trace' } }) |
| 4 | +const sget = require('simple-get') |
| 5 | + |
| 6 | +const cookieOpts = { |
| 7 | + // domain: 'localhost', |
| 8 | + path: '/', |
| 9 | + secure: true, |
| 10 | + sameSite: 'lax', |
| 11 | + httpOnly: true |
| 12 | +} |
| 13 | + |
| 14 | +// const oauthPlugin = require('fastify-oauth2') |
| 15 | +fastify.register(require('@fastify/cookie'), { |
| 16 | + secret: ['my-secret'], |
| 17 | + parseOptions: cookieOpts |
| 18 | +}) |
| 19 | + |
| 20 | +const oauthPlugin = require('..') |
| 21 | +fastify.register(oauthPlugin, { |
| 22 | + name: 'googleOAuth2', |
| 23 | + // when provided, this userAgent will also be used at discovery endpoint |
| 24 | + // to fully omit for whatever reason, set it to false |
| 25 | + userAgent: 'my custom app (v1.0.0)', |
| 26 | + scope: ['openid', 'profile', 'email'], |
| 27 | + credentials: { |
| 28 | + client: { |
| 29 | + id: process.env.CLIENT_ID, |
| 30 | + secret: process.env.CLIENT_SECRET |
| 31 | + } |
| 32 | + }, |
| 33 | + startRedirectPath: '/login/google', |
| 34 | + callbackUri: 'http://localhost:3000/interaction/callback/google', |
| 35 | + cookie: cookieOpts, |
| 36 | + // pkce: 'S256' let discovery handle it itself |
| 37 | + discovery: { |
| 38 | + /* |
| 39 | + When OIDC provider is mounted at root: |
| 40 | + with trailing slash (99% of the cases) |
| 41 | + - 'https://accounts.google.com/' |
| 42 | + */ |
| 43 | + issuer: 'https://accounts.google.com' |
| 44 | + /* |
| 45 | + also these variants work: |
| 46 | + When OIDC provider is mounted at root: |
| 47 | + with trailing slash |
| 48 | + - 'https://accounts.google.com/' |
| 49 | +
|
| 50 | + When given explicit metadata endpoint: |
| 51 | + - issuer: 'https://accounts.google.com/.well-known/openid-configuration' |
| 52 | +
|
| 53 | + When OIDC provider is nested at some route: |
| 54 | + - with trailing slash |
| 55 | + 'https://id.mycustomdomain.com/nested/' |
| 56 | + - without trailing slash |
| 57 | + 'https://id.mycustomdomain.com/nested' |
| 58 | + */ |
| 59 | + } |
| 60 | +}) |
| 61 | + |
| 62 | +fastify.get('/interaction/callback/google', function (request, reply) { |
| 63 | + // Note that in this example a "reply" is also passed, it's so that code verifier cookie can be cleaned before |
| 64 | + // token is requested from token endpoint |
| 65 | + this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply, (err, result) => { |
| 66 | + if (err) { |
| 67 | + reply.send(err) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + sget.concat({ |
| 72 | + url: 'https://www.googleapis.com/oauth2/v2/userinfo', |
| 73 | + method: 'GET', |
| 74 | + headers: { |
| 75 | + Authorization: 'Bearer ' + result.token.access_token |
| 76 | + }, |
| 77 | + json: true |
| 78 | + }, function (err, res, data) { |
| 79 | + if (err) { |
| 80 | + reply.send(err) |
| 81 | + return |
| 82 | + } |
| 83 | + reply.send(data) |
| 84 | + }) |
| 85 | + }) |
| 86 | +}) |
| 87 | + |
| 88 | +fastify.listen({ port: 3000 }) |
| 89 | +fastify.log.info('go to http://localhost:3000/login/google') |
0 commit comments