Skip to content

Commit

Permalink
refactor(authentication-service): fix sonar code smell (#1598)
Browse files Browse the repository at this point in the history
fix sonar code smell

GH-1597
  • Loading branch information
Surbhi-sharma1 authored Aug 10, 2023
1 parent 0b32429 commit 48d7afc
Showing 1 changed file with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import {inject} from '@loopback/context';
import {AnyObject, repository} from '@loopback/repository';
import {AnyObject, DataObject, Model, repository} from '@loopback/repository';
import {
HttpErrors,
RequestContext,
Expand Down Expand Up @@ -372,34 +372,15 @@ export class LoginController {
let changePasswordResponse: User;

if (req.oldPassword) {
let oldPassword = req.oldPassword;
let newPassword = req.password;
if (process.env.PRIVATE_DECRYPTION_KEY) {
const decryptedOldPassword = await this.userRepo.decryptPassword(
req.oldPassword,
);
const decryptedNewPassword = await this.userRepo.decryptPassword(
req.password,
);
oldPassword = decryptedOldPassword;
newPassword = decryptedNewPassword;
}
changePasswordResponse = await this.userRepo.updatePassword(
changePasswordResponse = await this.getPasswordResponse(
req.username,
oldPassword,
newPassword,
req.password,
req.oldPassword,
);
} else {
let newPassword = req.password;
if (process.env.PRIVATE_DECRYPTION_KEY) {
const decryptedPassword = await this.userRepo.decryptPassword(
req.password,
);
newPassword = decryptedPassword;
}
changePasswordResponse = await this.userRepo.changePassword(
changePasswordResponse = await this.getPasswordResponse(
req.username,
newPassword,
req.password,
);
}

Expand Down Expand Up @@ -428,7 +409,34 @@ export class LoginController {
success: true,
});
}

async getPasswordResponse(
userName: string,
password: string,
prevPassword?: string,
): Promise<User<DataObject<Model>>> {
if (prevPassword) {
let oldPassword = prevPassword;
let newPassword = password;
if (process.env.PRIVATE_DECRYPTION_KEY) {
const decryptedOldPassword = await this.userRepo.decryptPassword(
oldPassword,
);
const decryptedNewPassword = await this.userRepo.decryptPassword(
password,
);
oldPassword = decryptedOldPassword;
newPassword = decryptedNewPassword;
}
return this.userRepo.updatePassword(userName, oldPassword, newPassword);
} else {
let newPassword = password;
if (process.env.PRIVATE_DECRYPTION_KEY) {
const decryptedPassword = await this.userRepo.decryptPassword(password);
newPassword = decryptedPassword;
}
return this.userRepo.changePassword(userName, newPassword);
}
}
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
Expand Down

0 comments on commit 48d7afc

Please sign in to comment.