Skip to content

Commit

Permalink
feat: oauth2 add groups mapping (#6053)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Giard <github@ngpixel.com>
  • Loading branch information
utix and NGPixel authored Jan 29, 2023
1 parent 43a797d commit 1da80ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 13 additions & 0 deletions server/modules/authentication/oauth2/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ module.exports = {
email: _.get(profile, conf.emailClaim)
}
})
if (conf.mapGroups) {
const groups = _.get(profile, conf.groupsClaim)
if (groups && _.isArray(groups)) {
const currentGroups = (await user.$relatedQuery('groups').select('groups.id')).map(g => g.id)
const expectedGroups = Object.values(WIKI.auth.groups).filter(g => groups.includes(g.name)).map(g => g.id)
for (const groupId of _.difference(expectedGroups, currentGroups)) {
await user.$relatedQuery('groups').relate(groupId)
}
for (const groupId of _.difference(currentGroups, expectedGroups)) {
await user.$relatedQuery('groups').unrelate().where('groupId', groupId)
}
}
}
cb(null, user)
} catch (err) {
cb(err, null)
Expand Down
21 changes: 17 additions & 4 deletions server/modules/authentication/oauth2/definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,38 @@ props:
default: email
maxWidth: 500
order: 8
mapGroups:
type: Boolean
title: Map Groups
hint: Map groups matching names from the groups claim value
default: false
order: 9
groupsClaim:
type: String
title: Groups Claim
hint: Field containing the group names
default: groups
maxWidth: 500
order: 10
logoutURL:
type: String
title: Logout URL
hint: (optional) Logout URL on the OAuth2 provider where the user will be redirected to complete the logout process.
order: 9
order: 11
scope:
type: String
title: Scope
hint: (optional) Application Client permission scopes.
order: 10
order: 12
useQueryStringForAccessToken:
type: Boolean
default: false
title: Pass access token via GET query string to User Info Endpoint
hint: (optional) Pass the access token in an `access_token` parameter attached to the GET query string of the User Info Endpoint URL. Otherwise the access token will be passed in the Authorization header.
order: 11
order: 13
enableCSRFProtection:
type: Boolean
default: true
title: Enable CSRF protection
hint: Pass a nonce state parameter during authentication to protect against CSRF attacks.
order: 12
order: 14

0 comments on commit 1da80ea

Please sign in to comment.