Skip to content
Browse files

Fix email validation for LDAP

issue 1963
  • Loading branch information...
1 parent da30797 commit cea4b9a6369817f141dca12397852f3f1947fe96 @loay loay committed
Showing with 35 additions and 1 deletion.
  1. +4 −1 lib/models/user-identity.js
  2. +31 −0 test/model.user-identity.test.js
View
5 lib/models/user-identity.js
@@ -44,8 +44,11 @@
function profileToUser(provider, profile, options) {
// Let's create a user for that
- var email = (profile.username || profile.id) + '@loopback.' +
+ var profileEmail = profile.emails && profile.emails[0] &&
+ profile.emails[0].value;
+ var generatedEmail = (profile.username || profile.id) + '@loopback.' +
(profile.provider || provider) + '.com';
+ var email = provider === 'ldap' ? profileEmail : generatedEmail;
var username = provider + '.' + (profile.username || profile.id);
var password = utils.generateKey('password');
var userObj = {
View
31 test/model.user-identity.test.js
@@ -161,4 +161,35 @@ describe('UserIdentity', function () {
});
});
+ it('supports ldap login', function(done) {
+ var identity = { emails: [{value: 'fooldap@bar.com'}], id: 'f123ldap',
+ username: 'xyzldap'};
+ var credentials = {accessToken: 'atldap1', refreshToken: 'rtldap1'};
+ var options = {autoLogin: false};
+ UserIdentity.login('ldap', 'ldap', identity, credentials, options,
+ function(err, user, identity, token) {
+ if (err) return done(err);
+
+ assert.equal(user.username, 'ldap.xyzldap');
+ assert.equal(user.email, 'fooldap@bar.com');
+
+ assert.equal(identity.externalId, 'f123ldap');
+ assert.equal(identity.provider, 'ldap');
+ assert.deepEqual(identity.credentials, {accessToken: 'atldap1',
+ refreshToken: 'rtldap1'});
+
+ assert.equal(user.id, identity.userId);
+ assert(!token);
+
+ // Follow the belongsTo relation
+ identity.user(function(err, user) {
+ if (err) return done(err);
+
+ assert.equal(user.username, 'ldap.xyzldap');
+ assert.equal(user.email, 'fooldap@bar.com');
+
+ done();

Leave a line note

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ });
+ });
+ });
});

0 comments on commit cea4b9a

Please sign in to comment.
Something went wrong with that request. Please try again.