Skip to content

Commit

Permalink
Merge pull request #1626 from joachimmathes/oauth2_authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
jackycute authored Jun 5, 2023
2 parents ae38fd3 + 0ec949d commit be1f4cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/auth/oauth2/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { Strategy, InternalOAuthError } = require('passport-oauth2')
const config = require('../../config')

function parseProfile (data) {
const id = extractProfileAttribute(data, config.oauth2.userProfileIdAttr)
const username = extractProfileAttribute(data, config.oauth2.userProfileUsernameAttr)
const displayName = extractProfileAttribute(data, config.oauth2.userProfileDisplayNameAttr)
const email = extractProfileAttribute(data, config.oauth2.userProfileEmailAttr)
Expand All @@ -14,7 +15,7 @@ function parseProfile (data) {
}

return {
id: username,
id: id || username,
username: username,
displayName: displayName,
email: email,
Expand All @@ -41,6 +42,16 @@ function extractProfileAttribute (data, path) {
return data
}

function checkAuthorization (data, done) {
const roles = extractProfileAttribute(data, config.oauth2.rolesClaim)

if (config.oauth2.accessRole && roles) {
if (!roles.includes(config.oauth2.accessRole)) {
return done('Permission denied', null)
}
}
}

class OAuth2CustomStrategy extends Strategy {
constructor (options, verify) {
options.customHeaders = options.customHeaders || {}
Expand All @@ -59,6 +70,7 @@ class OAuth2CustomStrategy extends Strategy {
let profile, json
try {
json = JSON.parse(body)
checkAuthorization(json, done)
profile = parseProfile(json)
} catch (ex) {
return done(new InternalOAuthError('Failed to parse user profile' + ex.toString()))
Expand Down
3 changes: 3 additions & 0 deletions lib/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ module.exports = {
userProfileURL: process.env.CMD_OAUTH2_USER_PROFILE_URL,
scope: process.env.CMD_OAUTH2_SCOPE,
state: process.env.CMD_OAUTH2_STATE,
rolesClaim: process.env.CMD_OAUTH2_ROLES_CLAIM,
accessRole: process.env.CMD_OAUTH2_ACCESS_ROLE,
userProfileIdAttr: process.env.CMD_OAUTH2_USER_PROFILE_ID_ATTR,
userProfileUsernameAttr: process.env.CMD_OAUTH2_USER_PROFILE_USERNAME_ATTR,
userProfileDisplayNameAttr: process.env.CMD_OAUTH2_USER_PROFILE_DISPLAY_NAME_ATTR,
userProfileEmailAttr: process.env.CMD_OAUTH2_USER_PROFILE_EMAIL_ATTR,
Expand Down

0 comments on commit be1f4cf

Please sign in to comment.