|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const Router = require('express').Router |
| 4 | +const passport = require('passport') |
| 5 | +const OAuth2Strategy = require('passport-oauth2').Strategy |
| 6 | +const config = require('../../../config') |
| 7 | +const {setReturnToFromReferer, passportGeneralCallback} = require('../utils') |
| 8 | + |
| 9 | +let oauth2Auth = module.exports = Router() |
| 10 | + |
| 11 | +class OAuth2CustomStrategy extends OAuth2Strategy { |
| 12 | + constructor (options, verify) { |
| 13 | + options.customHeaders = options.customHeaders || {} |
| 14 | + super(options, verify) |
| 15 | + this.name = 'oauth2' |
| 16 | + this._userProfileURL = options.userProfileURL |
| 17 | + this._oauth2.useAuthorizationHeaderforGET(true) |
| 18 | + } |
| 19 | + |
| 20 | + userProfile (accessToken, done) { |
| 21 | + this._oauth2.get(this._userProfileURL, accessToken, function (err, body, res) { |
| 22 | + var json |
| 23 | + |
| 24 | + if (err) { |
| 25 | + return done(new passport.InternalOAuthError('Failed to fetch user profile', err)) |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + json = JSON.parse(body) |
| 30 | + } catch (ex) { |
| 31 | + return done(new Error('Failed to parse user profile')) |
| 32 | + } |
| 33 | + |
| 34 | + let profile = parseProfile(json) |
| 35 | + profile.provider = 'oauth2' |
| 36 | + |
| 37 | + done(null, profile) |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function extractProfileAttribute (data, path) { |
| 43 | + // can handle stuff like `attrs[0].name` |
| 44 | + path = path.split('.') |
| 45 | + for (const segment of path) { |
| 46 | + const m = segment.match(/([\d\w]+)\[(.*)\]/) |
| 47 | + data = m ? data[m[1]][m[2]] : data[segment] |
| 48 | + } |
| 49 | + return data |
| 50 | +} |
| 51 | + |
| 52 | +function parseProfile (data) { |
| 53 | + const username = extractProfileAttribute(data, config.oauth2.userProfileUsernameAttr) |
| 54 | + const displayName = extractProfileAttribute(data, config.oauth2.userProfileDisplayNameAttr) |
| 55 | + const email = extractProfileAttribute(data, config.oauth2.userProfileEmailAttr) |
| 56 | + |
| 57 | + return { |
| 58 | + id: username, |
| 59 | + username: username, |
| 60 | + displayName: displayName, |
| 61 | + email: email |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +OAuth2CustomStrategy.prototype.userProfile = function (accessToken, done) { |
| 66 | + this._oauth2.get(this._userProfileURL, accessToken, function (err, body, res) { |
| 67 | + var json |
| 68 | + |
| 69 | + if (err) { |
| 70 | + return done(new passport.InternalOAuthError('Failed to fetch user profile', err)) |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + json = JSON.parse(body) |
| 75 | + } catch (ex) { |
| 76 | + return done(new Error('Failed to parse user profile')) |
| 77 | + } |
| 78 | + |
| 79 | + let profile = parseProfile(json) |
| 80 | + profile.provider = 'oauth2' |
| 81 | + |
| 82 | + done(null, profile) |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +passport.use(new OAuth2CustomStrategy({ |
| 87 | + authorizationURL: config.oauth2.authorizationURL, |
| 88 | + tokenURL: config.oauth2.tokenURL, |
| 89 | + clientID: config.oauth2.clientID, |
| 90 | + clientSecret: config.oauth2.clientSecret, |
| 91 | + callbackURL: config.serverURL + '/auth/oauth2/callback', |
| 92 | + userProfileURL: config.oauth2.userProfileURL |
| 93 | +}, passportGeneralCallback)) |
| 94 | + |
| 95 | +oauth2Auth.get('/auth/oauth2', function (req, res, next) { |
| 96 | + setReturnToFromReferer(req) |
| 97 | + passport.authenticate('oauth2')(req, res, next) |
| 98 | +}) |
| 99 | + |
| 100 | +// github auth callback |
| 101 | +oauth2Auth.get('/auth/oauth2/callback', |
| 102 | + passport.authenticate('oauth2', { |
| 103 | + successReturnToOrRedirect: config.serverurl + '/', |
| 104 | + failureRedirect: config.serverurl + '/' |
| 105 | + }) |
| 106 | +) |
0 commit comments