Skip to content

Commit 53fd496

Browse files
authored
Merge pull request #322 from FlowFuse/user-role-auth
Include user role in profile object when authenticating
2 parents 9b7518b + cdde551 commit 53fd496

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

lib/auth/adminAuth.js

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = (options) => {
1111
const clientID = options.clientID
1212
const clientSecret = options.clientSecret
1313
const forgeURL = options.forgeURL
14+
const teamID = options.teamID
1415
const baseURL = new URL(options.baseURL)
1516
let basePath = baseURL.pathname || ''
1617
if (basePath.endsWith('/')) {
@@ -20,6 +21,7 @@ module.exports = (options) => {
2021
const authorizationURL = `${forgeURL}/account/authorize`
2122
const tokenURL = `${forgeURL}/account/token`
2223
const userInfoURL = `${forgeURL}/api/v1/user`
24+
const userTeamRoleURL = `${forgeURL}/api/v1/teams/${teamID}/user`
2325

2426
const oa = new OAuth2(clientID, clientSecret, '', authorizationURL, tokenURL)
2527

@@ -61,6 +63,7 @@ module.exports = (options) => {
6163
tokenURL,
6264
callbackURL,
6365
userInfoURL,
66+
userTeamRoleURL,
6467
scope: `editor-${version}`,
6568
clientID,
6669
clientSecret,

lib/auth/httpAuthMiddleware.js

+2
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ module.exports = {
9696
const authorizationURL = `${options.forgeURL}/account/authorize`
9797
const tokenURL = `${options.forgeURL}/account/token`
9898
const userInfoURL = `${options.forgeURL}/api/v1/user`
99+
const userTeamRoleURL = `${options.forgeURL}/api/v1/teams/${options.teamID}/user`
99100
const version = require('../../package.json').version
100101

101102
passport.use('FlowFuse', new Strategy({
102103
authorizationURL,
103104
tokenURL,
104105
callbackURL,
105106
userInfoURL,
107+
userTeamRoleURL,
106108
scope: `httpAuth-${version}`,
107109
clientID: options.clientID,
108110
clientSecret: options.clientSecret,

lib/auth/strategy.js

+43-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ const util = require('util')
22
const url = require('url')
33
const OAuth2Strategy = require('passport-oauth2')
44

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+
522
function Strategy (options, verify) {
623
this.options = options
724
this._base = Object.getPrototypeOf(Strategy.prototype)
@@ -43,25 +60,42 @@ Strategy.prototype.authenticate = function (req, options) {
4360
return this.__authenticate(req, strategyOptions)
4461
}
4562

46-
Strategy.prototype.userProfile = function (accessToken, done) {
63+
Strategy.prototype.sendAPIRequest = function (url, accessToken, done) {
4764
this._oauth2.useAuthorizationHeaderforGET(true)
48-
this._oauth2.get(this.options.userInfoURL, accessToken, (err, body) => {
65+
this._oauth2.get(url, accessToken, (err, body) => {
4966
if (err) {
5067
return done(err)
5168
}
5269
try {
5370
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)
6172
} catch (e) {
6273
done(e)
6374
}
6475
})
6576
}
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+
}
66100

67101
module.exports = { Strategy }

lib/runtimeSettings.js

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function getSettingsFile (settings) {
5050
forgeURL: '${settings.forgeURL}',
5151
clientID: '${settings.clientID}',
5252
clientSecret: '${settings.clientSecret}',
53+
teamID: '${settings.teamID}',
5354
projectId: '${settings.projectID}'
5455
})`
5556
projectSettings.httpNodeMiddleware = 'httpNodeMiddleware: flowforgeAuthMiddleware,'
@@ -273,6 +274,7 @@ module.exports = {
273274
adminAuth: require('@flowfuse/nr-launcher/adminAuth')({
274275
baseURL: '${settings.baseURL}',
275276
forgeURL: '${settings.forgeURL}',
277+
teamID: '${settings.teamID}',
276278
clientID: '${settings.clientID}',
277279
clientSecret: '${settings.clientSecret}'
278280
}),

0 commit comments

Comments
 (0)