@@ -2,6 +2,23 @@ const util = require('util')
2
2
const url = require ( 'url' )
3
3
const OAuth2Strategy = require ( 'passport-oauth2' )
4
4
5
+ const Roles = {
6
+ None : 0 ,
7
+ Dashboard : 5 ,
8
+ Viewer : 10 ,
9
+ Member : 30 ,
10
+ Owner : 50 ,
11
+ Admin : 99
12
+ }
13
+ const RoleNames = {
14
+ [ Roles . None ] : 'none' ,
15
+ [ Roles . Dashboard ] : 'dashboard' ,
16
+ [ Roles . Viewer ] : 'viewer' ,
17
+ [ Roles . Member ] : 'member' ,
18
+ [ Roles . Owner ] : 'owner' ,
19
+ [ Roles . Admin ] : 'admin'
20
+ }
21
+
5
22
function Strategy ( options , verify ) {
6
23
this . options = options
7
24
this . _base = Object . getPrototypeOf ( Strategy . prototype )
@@ -43,25 +60,42 @@ Strategy.prototype.authenticate = function (req, options) {
43
60
return this . __authenticate ( req , strategyOptions )
44
61
}
45
62
46
- Strategy . prototype . userProfile = function ( accessToken , done ) {
63
+ Strategy . prototype . sendAPIRequest = function ( url , accessToken , done ) {
47
64
this . _oauth2 . useAuthorizationHeaderforGET ( true )
48
- this . _oauth2 . get ( this . options . userInfoURL , accessToken , ( err , body ) => {
65
+ this . _oauth2 . get ( url , accessToken , ( err , body ) => {
49
66
if ( err ) {
50
67
return done ( err )
51
68
}
52
69
try {
53
70
const json = JSON . parse ( body )
54
- done ( null , {
55
- username : json . username ,
56
- email : json . email ,
57
- image : json . avatar ,
58
- name : json . name ,
59
- userId : json . id
60
- } )
71
+ done ( null , json )
61
72
} catch ( e ) {
62
73
done ( e )
63
74
}
64
75
} )
65
76
}
77
+ Strategy . prototype . userProfile = function ( accessToken , done ) {
78
+ this . _oauth2 . useAuthorizationHeaderforGET ( true )
79
+ this . sendAPIRequest ( this . options . userInfoURL , accessToken , ( err , userProfile ) => {
80
+ if ( err ) {
81
+ console . log ( 'Authentication error:' , err )
82
+ return done ( err )
83
+ }
84
+ this . sendAPIRequest ( this . options . userTeamRoleURL , accessToken , ( err , userTeamRole ) => {
85
+ if ( err ) {
86
+ console . log ( 'Authentication error:' , err )
87
+ return done ( err )
88
+ }
89
+ done ( null , {
90
+ username : userProfile . username ,
91
+ email : userProfile . email ,
92
+ image : userProfile . avatar ,
93
+ name : userProfile . name ,
94
+ userId : userProfile . id ,
95
+ role : RoleNames [ userTeamRole . role ] || ''
96
+ } )
97
+ } )
98
+ } )
99
+ }
66
100
67
101
module . exports = { Strategy }
0 commit comments