Skip to content

Commit 38a46e6

Browse files
aelgasserAbderraouf El GasserNGPixel
authored
feat: sync groups with SAML provider (#6299)
* feat: added implementation for group mapping in SAML strategies --------- Co-authored-by: Abderraouf El Gasser <abderraouf.elgasser@iktos.com> Co-authored-by: Nicolas Giard <github@ngpixel.com>
1 parent fd91caf commit 38a46e6

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

server/modules/authentication/saml/authentication.js

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ module.exports = {
5656
picture: _.get(profile, conf.mappingPicture, '')
5757
}
5858
})
59+
60+
// map users provider groups to wiki groups with the same name, and remove any groups that don't match
61+
// Code copied from the LDAP implementation with a slight variation on the field we extract the value from
62+
// In SAML v2 groups come in profile.attributes and can be 1 string or an array of strings
63+
if (conf.mapGroups) {
64+
const maybeArrayOfGroups = _.get(profile.attributes, conf.mappingGroups)
65+
const groups = (maybeArrayOfGroups && !_.isArray(maybeArrayOfGroups)) ? [maybeArrayOfGroups] : maybeArrayOfGroups
66+
67+
if (groups && _.isArray(groups)) {
68+
const currentGroups = (await user.$relatedQuery('groups').select('groups.id')).map(g => g.id)
69+
const expectedGroups = Object.values(WIKI.auth.groups).filter(g => groups.includes(g.name)).map(g => g.id)
70+
for (const groupId of _.difference(expectedGroups, currentGroups)) {
71+
await user.$relatedQuery('groups').relate(groupId)
72+
}
73+
for (const groupId of _.difference(currentGroups, expectedGroups)) {
74+
await user.$relatedQuery('groups').unrelate().where('groupId', groupId)
75+
}
76+
}
77+
}
78+
5979
cb(null, user)
6080
} catch (err) {
6181
cb(err, null)

server/modules/authentication/saml/definition.yml

+12
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,15 @@ props:
162162
default: 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/picture'
163163
hint: The field storing the user avatar picture. Can be a variable name or a URI-formatted string.
164164
order: 43
165+
mapGroups:
166+
type: Boolean
167+
title: Map Groups
168+
hint: Map groups matching names from the provider user groups. User Groups Field Mapping must also be defined for this to work. Note this will remove any groups the user has that doesn't match any group from the provider.
169+
default: false
170+
order: 44
171+
mappingGroups:
172+
title: User Groups Field Mapping
173+
type: String
174+
default: 'memberOf'
175+
hint: The field storing the user groups attribute (when Map Groups is enabled). Can be a variable name or a URI-formatted string.
176+
order: 45

0 commit comments

Comments
 (0)