| 1 | package org.hplr.user.core.usecases.service; | |
| 2 | ||
| 3 | import io.jsonwebtoken.Jwts; | |
| 4 | import io.jsonwebtoken.SignatureAlgorithm; | |
| 5 | import io.jsonwebtoken.security.Keys; | |
| 6 | import lombok.Getter; | |
| 7 | import lombok.RequiredArgsConstructor; | |
| 8 | import org.hplr.library.core.model.StringValidator; | |
| 9 | import org.hplr.library.core.util.ConstValues; | |
| 10 | import org.hplr.library.exception.HPLRIllegalStateException; | |
| 11 | import org.hplr.library.exception.HPLRValidationException; | |
| 12 | import org.hplr.user.core.model.Administrator; | |
| 13 | import org.hplr.user.core.usecases.port.dto.*; | |
| 14 | import org.hplr.user.core.usecases.port.in.LoginAdministratorUseCaseInterface; | |
| 15 | import org.hplr.user.core.usecases.port.out.command.SaveLastAdministratorLoginDateCommandInterface; | |
| 16 | import org.hplr.user.core.usecases.port.out.query.SelectAdministratorByEmailQueryInterface; | |
| 17 | import org.springframework.beans.factory.annotation.Value; | |
| 18 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |
| 19 | import org.springframework.stereotype.Service; | |
| 20 | ||
| 21 | import java.time.Instant; | |
| 22 | import java.time.LocalDateTime; | |
| 23 | import java.time.temporal.ChronoUnit; | |
| 24 | import java.util.Date; | |
| 25 | import java.util.NoSuchElementException; | |
| 26 | import java.util.Optional; | |
| 27 | import java.util.stream.Collectors; | |
| 28 | ||
| 29 | @RequiredArgsConstructor | |
| 30 | @Service | |
| 31 | public class LoginAdministratorUseCaseService implements LoginAdministratorUseCaseInterface { | |
| 32 | ||
| 33 | final SelectAdministratorByEmailQueryInterface selectAdministratorByEmailQueryInterface; | |
| 34 | final SaveLastAdministratorLoginDateCommandInterface saveLastAdministratorLoginDateCommandInterface; | |
| 35 | ||
| 36 | @Value("${jwt.secret}") | |
| 37 |
1
1. getSecret : replaced return value with "" for org/hplr/user/core/usecases/service/LoginAdministratorUseCaseService::getSecret → KILLED |
@Getter |
| 38 | private String secret; | |
| 39 | ||
| 40 | @Override | |
| 41 | public GetTokenResponseDto loginAdministrator(AdministratorLoginDto administratorLoginDto) throws NoSuchElementException, HPLRValidationException, HPLRIllegalStateException { | |
| 42 |
1
1. loginAdministrator : removed call to org/hplr/library/core/model/StringValidator::validateString → KILLED |
StringValidator.validateString(secret); |
| 43 | BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); | |
| 44 | Optional<AdministratorSelectDto> administratorSelectDtoOptional = | |
| 45 | selectAdministratorByEmailQueryInterface.selectAdministratorByEmail(administratorLoginDto.email()); | |
| 46 | ||
| 47 | Administrator administrator = Administrator.fromSelectDto(administratorSelectDtoOptional.orElseThrow(NoSuchElementException::new)); | |
| 48 | ||
| 49 | ||
| 50 |
1
1. loginAdministrator : negated conditional → KILLED |
if(bCryptPasswordEncoder.matches(administratorLoginDto.passwordPlain(), administrator.getAdministratorSecurity().pwHash())){ |
| 51 |
1
1. loginAdministrator : removed call to org/hplr/user/core/usecases/port/out/command/SaveLastAdministratorLoginDateCommandInterface::saveLastLoginDate → KILLED |
saveLastAdministratorLoginDateCommandInterface.saveLastLoginDate(LocalDateTime.now(), administrator.getUserId().id()); |
| 52 | Date currentDate = Date.from(Instant.now()); | |
| 53 |
1
1. loginAdministrator : replaced return value with null for org/hplr/user/core/usecases/service/LoginAdministratorUseCaseService::loginAdministrator → KILLED |
return new GetTokenResponseDto(Jwts.builder() |
| 54 | .claim("id", administrator.getUserId().id()) | |
| 55 | .claim("role", "ADMINISTRATOR") | |
| 56 | .claim("permissions", administrator.getAdministratorSecurity() | |
| 57 | .roleList() | |
| 58 | .stream() | |
| 59 |
1
1. lambda$loginAdministrator$0 : replaced return value with "" for org/hplr/user/core/usecases/service/LoginAdministratorUseCaseService::lambda$loginAdministrator$0 → KILLED |
.map(role->role.name()+",") |
| 60 | .collect(Collectors.joining())) | |
| 61 | .setSubject("hplr") | |
| 62 | .setIssuedAt(currentDate) | |
| 63 | .setExpiration(Date.from(Instant.now().plus(ConstValues.TOKEN_DURATION, ChronoUnit.MINUTES))) | |
| 64 | .signWith(Keys.hmacShaKeyFor(secret.getBytes()), SignatureAlgorithm.HS256) | |
| 65 | .compact()); | |
| 66 | } | |
| 67 | else throw new HPLRValidationException("Wrong password!"); | |
| 68 | ||
| 69 | } | |
| 70 | } | |
Mutations | ||
| 37 |
1.1 |
|
| 42 |
1.1 |
|
| 50 |
1.1 |
|
| 51 |
1.1 |
|
| 53 |
1.1 |
|
| 59 |
1.1 |