|
|
@@ -145,12 +145,12 @@ class PassportService {
|
|
|
if (!req.form.isValid) {
|
|
|
return callback({ message: 'Incorrect credentials.' });
|
|
|
}
|
|
|
- const username = loginForm.username;
|
|
|
+ const ldapAccountId = loginForm.username;
|
|
|
const password = loginForm.password;
|
|
|
|
|
|
// user bind
|
|
|
if (isUserBind) {
|
|
|
- bindDN = bindDN.replace(/{{username}}/, username);
|
|
|
+ bindDN = bindDN.replace(/{{username}}/, ldapAccountId);
|
|
|
bindCredentials = password;
|
|
|
}
|
|
|
|
|
|
@@ -158,7 +158,8 @@ class PassportService {
|
|
|
const opts = {
|
|
|
usernameField: PassportService.USERNAME_FIELD,
|
|
|
passwordField: PassportService.PASSWORD_FIELD,
|
|
|
- server: { url, bindDN, bindCredentials, searchBase, searchFilter }
|
|
|
+ server: { url, bindDN, bindCredentials, searchBase, searchFilter },
|
|
|
+ passReqToCallback: true,
|
|
|
};
|
|
|
debug('ldap configuration: ', opts);
|
|
|
callback(null, opts);
|
|
|
@@ -166,10 +167,12 @@ class PassportService {
|
|
|
};
|
|
|
|
|
|
passport.use(new LdapStrategy(getLDAPConfiguration,
|
|
|
- (ldapUserInfo, done) => {
|
|
|
- debug("LDAP authentication has successed", ldapUserInfo);
|
|
|
+ (req, ldapAccountInfo, done) => {
|
|
|
+ debug("LDAP authentication has successed", ldapAccountInfo);
|
|
|
|
|
|
- this.findOrRegisterUserByLdapInfo(ldapUserInfo)
|
|
|
+ const ldapAccountId = req.body.loginForm.username;
|
|
|
+
|
|
|
+ this.findOrRegisterUserByLdapInfo(ldapAccountId, ldapAccountInfo)
|
|
|
.then((user) => {
|
|
|
done(null, user);
|
|
|
})
|
|
|
@@ -183,35 +186,25 @@ class PassportService {
|
|
|
debug('LdapStrategy: setup is done');
|
|
|
}
|
|
|
|
|
|
- findOrRegisterUserByLdapInfo(ldapUserInfo) {
|
|
|
+ /**
|
|
|
+ * find the ExternalAccount or register if not found
|
|
|
+ *
|
|
|
+ * @param {string} ldapAccountId
|
|
|
+ * @param {object} ldapAccountInfo
|
|
|
+ * @returns
|
|
|
+ * @memberof PassportService
|
|
|
+ */
|
|
|
+ findOrRegisterUserByLdapInfo(ldapAccountId, ldapAccountInfo) {
|
|
|
const User = this.crowi.model('User');
|
|
|
const ExternalAccount = this.crowi.model('ExternalAccount');
|
|
|
|
|
|
- const accountId = ldapUserInfo['uid'];
|
|
|
-
|
|
|
- return ExternalAccount.findOne({ providerType: 'ldap', accountId: accountId })
|
|
|
- .then((account) => {
|
|
|
- if (account != null) {
|
|
|
- debug(`LdapStrategy: accountId '${accountId}' is found `, account);
|
|
|
- return account;
|
|
|
- }
|
|
|
- else {
|
|
|
- debug(`LdapStrategy: accountId '${accountId}' is not found, it is going to be registered.`);
|
|
|
-
|
|
|
- // TODO ensure to be able to select the way to determine username
|
|
|
- const username = ldapUserInfo['uid'];
|
|
|
-
|
|
|
- return User.createUser('', username, undefined, undefined, undefined)
|
|
|
- .then((user) => {
|
|
|
- return ExternalAccount.create({ providerType: 'ldap', accountId, user: user._id });
|
|
|
- });
|
|
|
- }
|
|
|
- })
|
|
|
- .then((account) => {
|
|
|
- return account.populate('user').execPopulate();
|
|
|
+ return ExternalAccount.findOrRegister('ldap', ldapAccountId, () => {
|
|
|
+ // TODO ensure to be able to select the way to determine username
|
|
|
+ const username = ldapAccountInfo['uid'];
|
|
|
+ return User.createUser('', username, undefined, undefined, undefined);
|
|
|
})
|
|
|
.then((account) => {
|
|
|
- return account.user;
|
|
|
+ return account.getPopulatedUser();
|
|
|
});
|
|
|
}
|
|
|
|