Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions common/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,12 @@ module.exports = function(User) {
};

/**
* Create a short lived acess token for temporary login. Allows users
* Create a short lived access token for temporary login. Allows users
* to change passwords if forgotten.
*
* @options {Object} options
* @prop {String} email The user's email address
* @property {String} realm The user's realm (optional)
* @callback {Function} callback
* @param {Error} err
* @promise
Expand All @@ -589,7 +590,13 @@ module.exports = function(User) {
} catch (err) {
return cb(err);
}
UserModel.findOne({where: {email: options.email}}, function(err, user) {
var where = {
email: options.email,
};
if (options.realm) {
where.realm = options.realm;
}
UserModel.findOne({where: where}, function(err, user) {
if (err) {
return cb(err);
}
Expand Down
54 changes: 54 additions & 0 deletions test/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ describe('User', function() {
email: 'foo1@bar.com', password: 'bar1', emailVerified: true};
var validCredentialsEmailVerifiedOverREST = {
email: 'foo2@bar.com', password: 'bar2', emailVerified: true};
var validCredentialsWithRealm = {
email: 'foo3@bar.com', password: 'bar', realm: 'foobar'};
var validCredentialsWithTTL = {email: 'foo@bar.com', password: 'bar', ttl: 3600};
var validCredentialsWithTTLAndScope = {
email: 'foo@bar.com', password: 'bar', ttl: 3600, scope: 'all'};
Expand Down Expand Up @@ -1924,6 +1926,58 @@ describe('User', function() {
done();
});
});

describe('User.resetPassword(options, cb) requiring realm', function() {
var realmUser;

beforeEach(function(done) {
User.create(validCredentialsWithRealm, function(err, u) {
if (err) return done(err);

realmUser = u;
done();
});
});

it('Reports when email is not found in realm', function(done) {
User.resetPassword({
email: realmUser.email,
realm: 'unknown',
}, function(err) {
assert(err);
assert.equal(err.code, 'EMAIL_NOT_FOUND');
assert.equal(err.statusCode, 404);

done();
});
});

it('Creates a temp accessToken to allow user in realm to change password', function(done) {
var calledBack = false;

User.resetPassword({
email: realmUser.email,
realm: realmUser.realm,
}, function() {
calledBack = true;
});

User.once('resetPasswordRequest', function(info) {
assert(info.email);
assert(info.accessToken);
assert(info.accessToken.id);
assert.equal(info.accessToken.ttl / 60, 15);
assert(calledBack);
info.accessToken.user(function(err, user) {
if (err) return done(err);

assert.equal(user.email, realmUser.email);

done();
});
});
});
});
});
});

Expand Down