|
2 | 2 |
|
3 | 3 | const Router = require('express').Router |
4 | 4 | const passport = require('passport') |
5 | | -const { Strategy, InternalOAuthError } = require('passport-oauth2') |
| 5 | + |
6 | 6 | const config = require('../../config') |
7 | 7 | const { setReturnToFromReferer, passportGeneralCallback } = require('../utils') |
| 8 | +const { OAuth2CustomStrategy } = require('./strategy') |
8 | 9 |
|
9 | 10 | const oauth2Auth = module.exports = Router() |
10 | 11 |
|
11 | | -class OAuth2CustomStrategy extends Strategy { |
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 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 | | - const 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 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 | | - const profile = parseProfile(json) |
80 | | - profile.provider = 'oauth2' |
81 | | - |
82 | | - done(null, profile) |
83 | | - }) |
84 | | -} |
85 | | - |
86 | 12 | passport.use(new OAuth2CustomStrategy({ |
87 | 13 | authorizationURL: config.oauth2.authorizationURL, |
88 | 14 | tokenURL: config.oauth2.tokenURL, |
89 | 15 | clientID: config.oauth2.clientID, |
90 | 16 | clientSecret: config.oauth2.clientSecret, |
91 | 17 | callbackURL: config.serverURL + '/auth/oauth2/callback', |
92 | | - userProfileURL: config.oauth2.userProfileURL |
| 18 | + userProfileURL: config.oauth2.userProfileURL, |
| 19 | + scope: config.oauth2.scope |
93 | 20 | }, passportGeneralCallback)) |
94 | 21 |
|
95 | 22 | oauth2Auth.get('/auth/oauth2', function (req, res, next) { |
|
0 commit comments