From ca8d2fdae08e8dff923ec9b96ffee7c1ea6d4dfa Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Fri, 1 Feb 2019 16:13:16 +0100 Subject: [PATCH] chore: Update guide on integrating with Google Auth This is due to Google retiring google.plus APIs and the old passport libs used in the examples depended on these. Closes #386 --- docs/guides/google-auth-hook.md | 36 +++++++++++++++++++-------------- examples/google-auth-hook.js | 12 +++++++---- package.json | 2 ++ yarn.lock | 35 +++++++++++++++++++++++++++++++- 4 files changed, 65 insertions(+), 20 deletions(-) diff --git a/docs/guides/google-auth-hook.md b/docs/guides/google-auth-hook.md index 841cc6ee50..6694035663 100644 --- a/docs/guides/google-auth-hook.md +++ b/docs/guides/google-auth-hook.md @@ -43,12 +43,13 @@ http://localhost:4242/api/auth/callback ### Add dependencies -Add two dependencies [`passport`](https://www.npmjs.com/package/passport) and [`passport-google-oauth20`](https://www.npmjs.com/package/passport-google-oauth20) inside `index.js` file +Add two dependencies [`@passport-next/passport`](https://www.npmjs.com/package/@passport-next/passport) and [`@passport-next/passport-google-oauth2`](https://www.npmjs.com/package/@passport-next/passport-google-oauth2) inside `index.js` file ```js const unleash = require('unleash-server'); -const passport = require('passport'); -const GoogleStrategy = require('passport-google-oauth20').Strategy; +const passport = require('@passport-next/passport'); +const GoogleOAuth2Strategy = require('@passport-next/passport-google-oauth2') + .Strategy; ``` ### Configure the Google strategy for use by Passport.js @@ -61,7 +62,7 @@ const GOOGLE_CLIENT_SECRET = '...'; const GOOGLE_CALLBACK_URL = 'http://localhost:4242/api/auth/callback'; passport.use( - new GoogleStrategy( + new GoogleOAuth2Strategy( { clientID: GOOGLE_CLIENT_ID, clientSecret: GOOGLE_CLIENT_SECRET, @@ -93,8 +94,10 @@ let options = { preRouterHook: googleAdminAuth, }; -unleash.start(options).then(unleash => { - console.log(`Unleash started on http://localhost:${unleash.app.get('port')}`); +unleash.start(options).then(instance => { + console.log( + `Unleash started on http://localhost:${instance.app.get('port')}`, + ); }); ``` @@ -119,7 +122,7 @@ function googleAdminAuth(app) { // ... app.get( '/api/admin/login', - passport.authenticate('google', { scope: ['profile', 'email'] }), + passport.authenticate('google', { scope: ['email'] }), ); // ... } @@ -177,22 +180,23 @@ The `index.js` server file. ```js 'use strict'; +const fs = require('fs'); const unleash = require('unleash-server'); -const passport = require('passport'); -const GoogleStrategy = require('passport-google-oauth20').Strategy; +const passport = require('@passport-next/passport'); +const GoogleOAuth2Strategy = require('@passport-next/passport-google-oauth2'); const GOOGLE_CLIENT_ID = '...'; const GOOGLE_CLIENT_SECRET = '...'; const GOOGLE_CALLBACK_URL = 'http://localhost:4242/api/auth/callback'; passport.use( - new GoogleStrategy( + new GoogleOAuth2Strategy( { clientID: GOOGLE_CLIENT_ID, clientSecret: GOOGLE_CLIENT_SECRET, callbackURL: GOOGLE_CALLBACK_URL, }, - function(accessToken, refreshToken, profile, cb) { + (accessToken, refreshToken, profile, cb) => { cb( null, new unleash.User({ @@ -212,7 +216,7 @@ function googleAdminAuth(app) { app.get( '/api/admin/login', - passport.authenticate('google', { scope: ['profile', 'email'] }), + passport.authenticate('google', { scope: ['email'] }), ); app.get( '/api/auth/callback', @@ -242,7 +246,7 @@ function googleAdminAuth(app) { }); } -let options = { +const options = { enableLegacyRoutes: false, adminAuthentication: 'custom', preRouterHook: googleAdminAuth, @@ -252,7 +256,9 @@ if (process.env.DATABASE_URL_FILE) { options.databaseUrl = fs.readFileSync(process.env.DATABASE_URL_FILE); } -unleash.start(options).then(unleash => { - console.log(`Unleash started on http://localhost:${unleash.app.get('port')}`); +unleash.start(options).then(instance => { + console.log( + `Unleash started on http://localhost:${instance.app.get('port')}`, + ); }); ``` diff --git a/examples/google-auth-hook.js b/examples/google-auth-hook.js index 635b7e1cc4..c8bd2596fc 100644 --- a/examples/google-auth-hook.js +++ b/examples/google-auth-hook.js @@ -20,13 +20,14 @@ // const { User, AuthenticationRequired } = require('unleash-server'); const { User, AuthenticationRequired } = require('../lib/server-impl.js'); -const passport = require('passport'); -const GoogleOAuth2Strategy = require('passport-google-auth').Strategy; +const passport = require('@passport-next/passport'); +const GoogleOAuth2Strategy = require('@passport-next/passport-google-oauth2') + .Strategy; passport.use( new GoogleOAuth2Strategy( { - clientId: process.env.GOOGLE_CLIENT_ID, + clientID: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, callbackURL: process.env.GOOGLE_CALLBACK_URL, }, @@ -49,7 +50,10 @@ function enableGoogleOauth(app) { passport.serializeUser((user, done) => done(null, user)); passport.deserializeUser((user, done) => done(null, user)); - app.get('/api/admin/login', passport.authenticate('google')); + app.get( + '/api/admin/login', + passport.authenticate('google', { scope: ['email'] }) + ); app.get( '/api/auth/callback', diff --git a/package.json b/package.json index b21464ed62..b5dcd9b632 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,8 @@ "yargs": "^12.0.5" }, "devDependencies": { + "@passport-next/passport": "^2.1.0", + "@passport-next/passport-google-oauth2": "^1.0.0", "@types/node": "^10.12.15", "ava": "^1.2.0", "coveralls": "^3.0.2", diff --git a/yarn.lock b/yarn.lock index b678ff2c32..280c7b5ede 100644 --- a/yarn.lock +++ b/yarn.lock @@ -347,6 +347,34 @@ log-update "^2.3.0" strip-ansi "^3.0.1" +"@passport-next/passport-google-oauth2@^1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@passport-next/passport-google-oauth2/-/passport-google-oauth2-1.0.0.tgz#cadef72d9ef42d549a5578eab338febdf234f806" + integrity sha512-rbAtK0eGgjQhT/MqMSuN7Mxy4XTr1/LINk5rYPVezP3MfkVYuK20V0XW9fg1ikFMvyxSdmfLTyYduud7SfFSqw== + dependencies: + "@passport-next/passport-oauth2" "1.7.x" + +"@passport-next/passport-oauth2@1.7.x": + version "1.7.0" + resolved "https://registry.yarnpkg.com/@passport-next/passport-oauth2/-/passport-oauth2-1.7.0.tgz#48ef05df2a7980e888350a824e706ec177a40d93" + integrity sha512-0905TcHFwvqPyMsqTKDwjwddPJJcWkUs4fjbUNgnnxkfQ0/LX4FVwuMywOXiFNY12hhX92j8F5srGQC4f9UWug== + dependencies: + "@passport-next/passport-strategy" "1.1.x" + lodash ">=4.17.5" + oauth "0.9.x" + +"@passport-next/passport-strategy@1.1.x", "@passport-next/passport-strategy@1.x.x": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@passport-next/passport-strategy/-/passport-strategy-1.1.0.tgz#4c0df069e2ec9262791b9ef1e23320c1d73bdb74" + integrity sha512-2KhFjtPueJG6xVj2HnqXt9BlANOfYCVLyu+pXYjPGBDT8yk+vQwc/6tsceIj+mayKcoxMau2JimggXRPHgoc8w== + +"@passport-next/passport@^2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@passport-next/passport/-/passport-2.1.0.tgz#a698068a38b7ed8a2bf1854559f66e7916e09d75" + integrity sha512-FwemLM9lU1FO5MRTojgWqGFU6pmAywOg9DdQ6482jyoOLH9BCbziycMKQJPjQKgXgwwytn1h5HAA3m9kcMUykA== + dependencies: + "@passport-next/passport-strategy" "1.x.x" + "@samverschueren/stream-to-observable@^0.3.0": version "0.3.0" resolved "https://registry.yarnpkg.com/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.0.tgz#ecdf48d532c58ea477acfcab80348424f8d0662f" @@ -3542,7 +3570,7 @@ lodash.noop@^3.0.1: resolved "https://registry.yarnpkg.com/lodash.noop/-/lodash.noop-3.0.1.tgz#38188f4d650a3a474258439b96ec45b32617133c" integrity sha1-OBiPTWUKOkdCWEObluxFsyYXEzw= -lodash@^4.14.0, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.5: +lodash@>=4.17.5, lodash@^4.14.0, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.5: version "4.17.11" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg== @@ -4088,6 +4116,11 @@ oauth-sign@~0.9.0: resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455" integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ== +oauth@0.9.x: + version "0.9.15" + resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.9.15.tgz#bd1fefaf686c96b75475aed5196412ff60cfb9c1" + integrity sha1-vR/vr2hslrdUda7VGWQS/2DPucE= + object-assign@^4.0.1, object-assign@^4.1.0: version "4.1.1" resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"