Skip to content

Commit 22bc7b2

Browse files
committed
refactor(api): Use passwordHash from User model
1 parent 416e52e commit 22bc7b2

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

api/src/identity-access-management/domain/models/User.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ class User {
9696
(authenticationMethod) => authenticationMethod.identityProvider === NON_OIDC_IDENTITY_PROVIDERS.PIX.code,
9797
);
9898

99-
return pixAuthenticationMethod ? pixAuthenticationMethod.authenticationComplement.shouldChangePassword : null;
99+
return pixAuthenticationMethod ? pixAuthenticationMethod.authenticationComplement?.shouldChangePassword : null;
100+
}
101+
102+
get passwordHash() {
103+
const pixAuthenticationMethod = this.authenticationMethods.find(
104+
(authenticationMethod) => authenticationMethod.identityProvider === NON_OIDC_IDENTITY_PROVIDERS.PIX.code,
105+
);
106+
107+
return pixAuthenticationMethod ? pixAuthenticationMethod.authenticationComplement?.password : null;
100108
}
101109

102110
get shouldSeeDataProtectionPolicyInformationBanner() {

api/src/identity-access-management/domain/services/pix-authentication-service.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ async function getUserByUsernameAndPassword({
1818
dependencies = { userLoginRepository, cryptoService },
1919
}) {
2020
const foundUser = await userRepository.getByUsernameOrEmailWithRolesAndPassword(username);
21-
const passwordHash = foundUser.authenticationMethods[0].authenticationComplement.password;
2221

2322
let userLogin = await dependencies.userLoginRepository.findByUserId(foundUser.id);
2423
if (!userLogin) {
2524
userLogin = await dependencies.userLoginRepository.create({ userId: foundUser.id });
2625
}
2726

2827
try {
29-
await dependencies.cryptoService.checkPassword({
30-
password,
31-
passwordHash,
32-
});
28+
const passwordHash = foundUser.passwordHash;
29+
await dependencies.cryptoService.checkPassword({ password, passwordHash });
3330
} catch (error) {
3431
if (error instanceof PasswordNotMatching) {
3532
userLogin.incrementFailureCount();

api/tests/identity-access-management/unit/domain/models/User.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,43 @@ describe('Unit | Identity Access Management | Domain | Model | User', function (
428428
});
429429
});
430430

431+
describe('#passwordHash', function () {
432+
context('when there is a Pix authentication method', function () {
433+
it('returns the password hash', function () {
434+
// given
435+
const hashedPassword = 'xxx';
436+
const pixAuthenticationMethod =
437+
domainBuilder.buildAuthenticationMethod.withPixAsIdentityProviderAndHashedPassword({ hashedPassword });
438+
439+
// when
440+
const user = new User({
441+
id: 1,
442+
authenticationMethods: [pixAuthenticationMethod],
443+
});
444+
445+
// then
446+
expect(user.passwordHash).to.equal(hashedPassword);
447+
});
448+
});
449+
450+
context('when there is no Pix authentication method', function () {
451+
it('returns null', function () {
452+
// given
453+
const poleEmploiAuthenticationMethod =
454+
domainBuilder.buildAuthenticationMethod.withPoleEmploiAsIdentityProvider();
455+
456+
// when
457+
const user = new User({
458+
id: 1,
459+
authenticationMethods: [poleEmploiAuthenticationMethod],
460+
});
461+
462+
// then
463+
expect(user.passwordHash).to.be.null;
464+
});
465+
});
466+
});
467+
431468
describe('#shouldSeeDataProtectionPolicyInformationBanner', function () {
432469
context('when user has not seen data protection policy but data protection date is not setted', function () {
433470
it('should return false', function () {

0 commit comments

Comments
 (0)