| 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.user.core.model.Player; | |
| 10 | import org.hplr.user.core.usecases.port.dto.GetTokenResponseDto; | |
| 11 | import org.hplr.user.core.usecases.port.dto.PlayerLoginDto; | |
| 12 | import org.hplr.user.core.usecases.port.dto.PlayerSelectDto; | |
| 13 | import org.hplr.user.core.usecases.port.in.LoginPlayerUseCaseInterface; | |
| 14 | import org.hplr.user.core.usecases.port.out.command.SaveLastLoginDateCommandInterface; | |
| 15 | import org.hplr.user.core.usecases.port.out.query.SelectPlayerByEmailQueryInterface; | |
| 16 | import org.hplr.library.core.util.ConstValues; | |
| 17 | import org.hplr.library.exception.HPLRIllegalStateException; | |
| 18 | import org.hplr.library.exception.HPLRValidationException; | |
| 19 | import org.springframework.beans.factory.annotation.Value; | |
| 20 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |
| 21 | import org.springframework.stereotype.Service; | |
| 22 | ||
| 23 | import java.time.Instant; | |
| 24 | import java.time.LocalDateTime; | |
| 25 | import java.time.temporal.ChronoUnit; | |
| 26 | import java.util.Date; | |
| 27 | import java.util.NoSuchElementException; | |
| 28 | import java.util.Optional; | |
| 29 | ||
| 30 | @RequiredArgsConstructor | |
| 31 | @Service | |
| 32 | public class LoginPlayerUseCaseService implements LoginPlayerUseCaseInterface { | |
| 33 | ||
| 34 | final SelectPlayerByEmailQueryInterface selectPlayerByEmailQueryInterface; | |
| 35 | final SaveLastLoginDateCommandInterface saveLastLoginDateCommandInterface; | |
| 36 | ||
| 37 | @Value("${jwt.secret}") | |
| 38 |
1
1. getSecret : replaced return value with "" for org/hplr/user/core/usecases/service/LoginPlayerUseCaseService::getSecret → KILLED |
@Getter |
| 39 | private String secret; | |
| 40 | ||
| 41 | @Override | |
| 42 | public GetTokenResponseDto loginPlayer(PlayerLoginDto playerLoginDto) throws NoSuchElementException, HPLRValidationException, HPLRIllegalStateException { | |
| 43 |
1
1. loginPlayer : removed call to org/hplr/library/core/model/StringValidator::validateString → KILLED |
StringValidator.validateString(secret); |
| 44 | BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); | |
| 45 | Optional<PlayerSelectDto> playerSelectDtoOptional = | |
| 46 | selectPlayerByEmailQueryInterface.selectPlayerByEmail(playerLoginDto.email()); | |
| 47 | ||
| 48 | Player player = Player.fromDto(playerSelectDtoOptional.orElseThrow(NoSuchElementException::new)); | |
| 49 | ||
| 50 | ||
| 51 |
1
1. loginPlayer : negated conditional → KILLED |
if(bCryptPasswordEncoder.matches(playerLoginDto.passwordPlain(), player.getSecurity().pwHash())){ |
| 52 |
1
1. loginPlayer : removed call to org/hplr/user/core/usecases/port/out/command/SaveLastLoginDateCommandInterface::saveLastLoginDate → KILLED |
saveLastLoginDateCommandInterface.saveLastLoginDate(LocalDateTime.now(), player.getUserId().id()); |
| 53 | Date currentDate = Date.from(Instant.now()); | |
| 54 |
1
1. loginPlayer : replaced return value with null for org/hplr/user/core/usecases/service/LoginPlayerUseCaseService::loginPlayer → KILLED |
return new GetTokenResponseDto(Jwts.builder() |
| 55 | .claim("id", player.getUserId().id()) | |
| 56 | .claim("role", "PLAYER") | |
| 57 | .setSubject("hplr") | |
| 58 | .setIssuedAt(currentDate) | |
| 59 | .setExpiration(Date.from(Instant.now().plus(ConstValues.TOKEN_DURATION, ChronoUnit.MINUTES))) | |
| 60 | .signWith(Keys.hmacShaKeyFor(secret.getBytes()), SignatureAlgorithm.HS256) | |
| 61 | .compact()); | |
| 62 | } | |
| 63 | else throw new HPLRValidationException("Wrong password!"); | |
| 64 | ||
| 65 | } | |
| 66 | } | |
Mutations | ||
| 38 |
1.1 |
|
| 43 |
1.1 |
|
| 51 |
1.1 |
|
| 52 |
1.1 |
|
| 54 |
1.1 |