@@ -4,6 +4,7 @@ const { Strategy, InternalOAuthError } = require('passport-oauth2')
4
4
const config = require ( '../../config' )
5
5
6
6
function parseProfile ( data ) {
7
+ const id = extractProfileAttribute ( data , config . oauth2 . userProfileIdAttr )
7
8
const username = extractProfileAttribute ( data , config . oauth2 . userProfileUsernameAttr )
8
9
const displayName = extractProfileAttribute ( data , config . oauth2 . userProfileDisplayNameAttr )
9
10
const email = extractProfileAttribute ( data , config . oauth2 . userProfileEmailAttr )
@@ -14,7 +15,7 @@ function parseProfile (data) {
14
15
}
15
16
16
17
return {
17
- id : username ,
18
+ id : id || username ,
18
19
username : username ,
19
20
displayName : displayName ,
20
21
email : email ,
@@ -41,6 +42,16 @@ function extractProfileAttribute (data, path) {
41
42
return data
42
43
}
43
44
45
+ function checkAuthorization ( data , done ) {
46
+ const roles = extractProfileAttribute ( data , config . oauth2 . rolesClaim )
47
+
48
+ if ( config . oauth2 . accessRole && roles ) {
49
+ if ( ! roles . includes ( config . oauth2 . accessRole ) ) {
50
+ return done ( 'Permission denied' , null )
51
+ }
52
+ }
53
+ }
54
+
44
55
class OAuth2CustomStrategy extends Strategy {
45
56
constructor ( options , verify ) {
46
57
options . customHeaders = options . customHeaders || { }
@@ -59,6 +70,7 @@ class OAuth2CustomStrategy extends Strategy {
59
70
let profile , json
60
71
try {
61
72
json = JSON . parse ( body )
73
+ checkAuthorization ( json , done )
62
74
profile = parseProfile ( json )
63
75
} catch ( ex ) {
64
76
return done ( new InternalOAuthError ( 'Failed to parse user profile' + ex . toString ( ) ) )
0 commit comments