Support custom username field.

This commit is contained in:
Julien Fontanet 2015-09-16 11:12:42 +02:00
parent 3d00b4ffbe
commit 1f454ababf
2 changed files with 26 additions and 1 deletions

View File

@ -38,6 +38,11 @@ plugins:
# Issuer string to supply the identity provider.
issuer: 'xen-orchestra'
# Field to use as the name of the user.
#
# Default: uid.
usernameField: 'uid'
```
## Development

View File

@ -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 {
constructor (conf) {
this._usernameField = extract(conf, 'usernameField', 'uid')
this._conf = conf
}
load (xo) {
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 {
done(null, await xo.registerUser('saml', profile.uid))
done(null, await xo.registerUser('saml', name))
} catch (error) {
done(error.message)
}