Skip to content

Commit e7831f6

Browse files
committed
Allow password reset request for users in realms
1 parent 63df861 commit e7831f6

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

common/models/user.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,12 @@ module.exports = function(User) {
546546
};
547547

548548
/**
549-
* Create a short lived acess token for temporary login. Allows users
549+
* Create a short lived access token for temporary login. Allows users
550550
* to change passwords if forgotten.
551551
*
552552
* @options {Object} options
553-
* @prop {String} email The user's email address
553+
* @property {String} email The user's email address
554+
* @property {String} realm The user's realm (optional)
554555
* @callback {Function} callback
555556
* @param {Error} err
556557
*/
@@ -575,7 +576,13 @@ module.exports = function(User) {
575576
} catch (err) {
576577
return cb(err);
577578
}
578-
UserModel.findOne({ where: { email: options.email }}, function(err, user) {
579+
var where = {
580+
email: options.email
581+
};
582+
if (options.realm) {
583+
where.realm = options.realm;
584+
}
585+
UserModel.findOne({ where: where }, function(err, user) {
579586
if (err) {
580587
return cb(err);
581588
}

test/user.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('User', function() {
1515
var validCredentials = {email: validCredentialsEmail, password: 'bar'};
1616
var validCredentialsEmailVerified = {email: '[email protected]', password: 'bar1', emailVerified: true};
1717
var validCredentialsEmailVerifiedOverREST = {email: '[email protected]', password: 'bar2', emailVerified: true};
18+
var validCredentialsWithRealm = {email: '[email protected]', password: 'bar', realm: 'foobar'};
1819
var validCredentialsWithTTL = {email: '[email protected]', password: 'bar', ttl: 3600};
1920
var validCredentialsWithTTLAndScope = {email: '[email protected]', password: 'bar', ttl: 3600, scope: 'all'};
2021
var validMixedCaseEmailCredentials = {email: '[email protected]', password: 'bar'};
@@ -1878,6 +1879,58 @@ describe('User', function() {
18781879
});
18791880
});
18801881
});
1882+
1883+
describe('User.resetPassword(options, cb) requiring realm', function() {
1884+
var realmUser;
1885+
1886+
beforeEach(function(done) {
1887+
User.create(validCredentialsWithRealm, function(err, u) {
1888+
if (err) return done(err);
1889+
1890+
realmUser = u;
1891+
done();
1892+
});
1893+
});
1894+
1895+
it('Reports when email is not found in realm', function(done) {
1896+
User.resetPassword({
1897+
email: realmUser.email,
1898+
realm: 'unknown'
1899+
}, function(err) {
1900+
assert(err);
1901+
assert.equal(err.code, 'EMAIL_NOT_FOUND');
1902+
assert.equal(err.statusCode, 404);
1903+
1904+
done();
1905+
});
1906+
});
1907+
1908+
it('Creates a temp accessToken to allow a user in realm to change password', function(done) {
1909+
var calledBack = false;
1910+
1911+
User.resetPassword({
1912+
email: realmUser.email,
1913+
realm: realmUser.realm
1914+
}, function() {
1915+
calledBack = true;
1916+
});
1917+
1918+
User.once('resetPasswordRequest', function(info) {
1919+
assert(info.email);
1920+
assert(info.accessToken);
1921+
assert(info.accessToken.id);
1922+
assert.equal(info.accessToken.ttl / 60, 15);
1923+
assert(calledBack);
1924+
info.accessToken.user(function(err, user) {
1925+
if (err) return done(err);
1926+
1927+
assert.equal(user.email, realmUser.email);
1928+
1929+
done();
1930+
});
1931+
});
1932+
});
1933+
});
18811934
});
18821935

18831936
describe('Email Update', function() {

0 commit comments

Comments
 (0)