| 1 | package com.edtech.service; | |
| 2 | ||
| 3 | import com.edtech.model.User; | |
| 4 | import io.jsonwebtoken.Claims; | |
| 5 | import io.jsonwebtoken.JwtException; | |
| 6 | import io.jsonwebtoken.Jwts; | |
| 7 | import io.jsonwebtoken.security.Keys; | |
| 8 | import java.nio.charset.StandardCharsets; | |
| 9 | import java.time.Duration; | |
| 10 | import java.time.Instant; | |
| 11 | import java.util.Date; | |
| 12 | import java.util.UUID; | |
| 13 | import javax.crypto.SecretKey; | |
| 14 | import org.springframework.beans.factory.annotation.Value; | |
| 15 | import org.springframework.stereotype.Service; | |
| 16 | ||
| 17 | /** Documentação para JwtService. */ | |
| 18 | @Service | |
| 19 | public class JwtService { | |
| 20 | ||
| 21 | private final SecretKey secretKey; | |
| 22 | private final Duration expiration; | |
| 23 | ||
| 24 | /** Documentação. */ | |
| 25 | public JwtService( | |
| 26 | @Value("${jwt.secret:}") String secret, | |
| 27 | @Value("${jwt.expiration-minutes:60}") long expirationMinutes) { | |
| 28 |
1
1. <init> : removed call to org/springframework/util/Assert::hasText → SURVIVED |
org.springframework.util.Assert.hasText( |
| 29 | secret, "JWT_SECRET deve ser definido para autenticação."); | |
| 30 | this.secretKey = Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8)); | |
| 31 | this.expiration = Duration.ofMinutes(expirationMinutes); | |
| 32 | } | |
| 33 | ||
| 34 | /** Documentação para o método generateToken. */ | |
| 35 | public String generateToken(User user) { | |
| 36 |
1
1. generateToken : replaced return value with "" for com/edtech/service/JwtService::generateToken → KILLED |
return generateToken(user, expiration); |
| 37 | } | |
| 38 | ||
| 39 | /** Documentação para o método generateToken. */ | |
| 40 | public String generateToken(User user, Duration tokenTtl) { | |
| 41 | Instant now = Instant.now(); | |
| 42 | Instant expiresAt = now.plus(tokenTtl); | |
| 43 | ||
| 44 |
1
1. generateToken : replaced return value with "" for com/edtech/service/JwtService::generateToken → KILLED |
return Jwts.builder() |
| 45 | .subject(user.getEmail()) | |
| 46 | .claim("uid", user.getId().toString()) | |
| 47 | .claim("role", user.getRole().name()) | |
| 48 | .issuedAt(Date.from(now)) | |
| 49 | .expiration(Date.from(expiresAt)) | |
| 50 | .signWith(secretKey) | |
| 51 | .compact(); | |
| 52 | } | |
| 53 | ||
| 54 | /** Documentação para o método extractSubject. */ | |
| 55 | public String extractSubject(String token) { | |
| 56 |
1
1. extractSubject : replaced return value with "" for com/edtech/service/JwtService::extractSubject → KILLED |
return validateToken(token).getSubject(); |
| 57 | } | |
| 58 | ||
| 59 | /** Documentação para o método getUserIdFromToken. */ | |
| 60 | public UUID getUserIdFromToken(String token) { | |
| 61 | Claims claims = validateToken(token); | |
| 62 | String uidStr = claims.get("uid", String.class); | |
| 63 |
1
1. getUserIdFromToken : replaced return value with null for com/edtech/service/JwtService::getUserIdFromToken → NO_COVERAGE |
return UUID.fromString(uidStr); |
| 64 | } | |
| 65 | ||
| 66 | /** Documentação para o método isValid. */ | |
| 67 | public boolean isValid(String token, User user) { | |
| 68 | try { | |
| 69 | String subject = extractSubject(token); | |
| 70 |
3
1. isValid : negated conditional → KILLED 2. isValid : negated conditional → KILLED 3. isValid : replaced boolean return with true for com/edtech/service/JwtService::isValid → KILLED |
return subject != null && subject.equalsIgnoreCase(user.getEmail()); |
| 71 | } catch (JwtException | IllegalArgumentException exception) { | |
| 72 |
1
1. isValid : replaced boolean return with true for com/edtech/service/JwtService::isValid → KILLED |
return false; |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | /** Documentação para o método validateToken. */ | |
| 77 | public Claims validateToken(String token) { | |
| 78 |
1
1. validateToken : replaced return value with null for com/edtech/service/JwtService::validateToken → KILLED |
return Jwts.parser().verifyWith(secretKey).build().parseSignedClaims(token).getPayload(); |
| 79 | } | |
| 80 | } | |
Mutations | ||
| 28 |
1.1 |
|
| 36 |
1.1 |
|
| 44 |
1.1 |
|
| 56 |
1.1 |
|
| 63 |
1.1 |
|
| 70 |
1.1 2.2 3.3 |
|
| 72 |
1.1 |
|
| 78 |
1.1 |