Support custom username field.
This commit is contained in:
parent
3d00b4ffbe
commit
1f454ababf
@ -38,6 +38,11 @@ plugins:
|
|||||||
|
|
||||||
# Issuer string to supply the identity provider.
|
# Issuer string to supply the identity provider.
|
||||||
issuer: 'xen-orchestra'
|
issuer: 'xen-orchestra'
|
||||||
|
|
||||||
|
# Field to use as the name of the user.
|
||||||
|
#
|
||||||
|
# Default: uid.
|
||||||
|
usernameField: 'uid'
|
||||||
```
|
```
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
@ -2,15 +2,35 @@ import {Strategy} from 'passport-saml'
|
|||||||
|
|
||||||
// ===================================================================
|
// ===================================================================
|
||||||
|
|
||||||
|
function extract (obj, prop, defaultValue = undefined) {
|
||||||
|
if (prop in obj) {
|
||||||
|
const value = obj[prop]
|
||||||
|
delete obj[prop]
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================================================================
|
||||||
|
|
||||||
class AuthSamlXoPlugin {
|
class AuthSamlXoPlugin {
|
||||||
constructor (conf) {
|
constructor (conf) {
|
||||||
|
this._usernameField = extract(conf, 'usernameField', 'uid')
|
||||||
this._conf = conf
|
this._conf = conf
|
||||||
}
|
}
|
||||||
|
|
||||||
load (xo) {
|
load (xo) {
|
||||||
xo.registerPassportStrategy(new Strategy(this._conf, async (profile, done) => {
|
xo.registerPassportStrategy(new Strategy(this._conf, async (profile, done) => {
|
||||||
|
const name = profile[this._usernameField]
|
||||||
|
if (!name) {
|
||||||
|
done('no name found for this user')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
done(null, await xo.registerUser('saml', profile.uid))
|
done(null, await xo.registerUser('saml', name))
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
done(error.message)
|
done(error.message)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user