diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/config/JwtUtil.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/config/JwtUtil.kt index d09a17c..4904285 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/config/JwtUtil.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/config/JwtUtil.kt @@ -27,9 +27,10 @@ class JwtUtil { } // Generate JWT token - fun generateToken(email: String): String { + fun generateToken(email: String, isAdmin: Boolean): String { return Jwts.builder() .subject(email) + .claim("isAdmin", isAdmin) .issuedAt(Date()) .expiration(Date(System.currentTimeMillis() + jwtExpirationMs)) .signWith(key) diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/AuthController.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/AuthController.kt index fd9d838..c73ebef 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/AuthController.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/AuthController.kt @@ -24,7 +24,7 @@ class AuthController(private val loginService: LoginService, private val jwtUtil @PostMapping("/login") fun login(@Valid @RequestBody body: LoginRequest): ResponseEntity { val result = loginService.login(body.toDomain()) - val accessToken = jwtUtils.generateToken(result.email) + val accessToken = jwtUtils.generateToken(result.email, result.isAdmin) val refreshToken = refreshTokenService.generateRefreshToken(result.email) val loginResponse = LoginResponse(accessToken, refreshToken, result.name) return ResponseEntity.ok(loginResponse) diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/RefreshTokenService.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/RefreshTokenService.kt index 5897a14..e55f7fe 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/RefreshTokenService.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/RefreshTokenService.kt @@ -5,6 +5,7 @@ import net.halfbinary.scavengerhuntapi.error.exception.ExpiredRefreshTokenExcept import net.halfbinary.scavengerhuntapi.error.exception.InvalidRefreshTokenException import net.halfbinary.scavengerhuntapi.model.RefreshId import net.halfbinary.scavengerhuntapi.model.record.RefreshTokenRecord +import net.halfbinary.scavengerhuntapi.repository.HunterRepository import net.halfbinary.scavengerhuntapi.repository.RefreshTokenRepository import org.slf4j.LoggerFactory import org.springframework.data.repository.findByIdOrNull @@ -13,7 +14,7 @@ import java.time.LocalDateTime import java.time.temporal.ChronoUnit @Service -class RefreshTokenService(private val refreshTokenRepository: RefreshTokenRepository, private val jwtUtil: JwtUtil) { +class RefreshTokenService(private val refreshTokenRepository: RefreshTokenRepository, private val jwtUtil: JwtUtil, private val hunterRepository: HunterRepository) { companion object { private val log = LoggerFactory.getLogger(RefreshTokenService::class.java) @@ -25,7 +26,8 @@ class RefreshTokenService(private val refreshTokenRepository: RefreshTokenReposi removeToken(tokenId) throw ExpiredRefreshTokenException(tokenId) } else { - jwtUtil.generateToken(refreshToken.email) + val isAdmin = hunterRepository.findByEmail(refreshToken.email)?.isAdmin ?: false + jwtUtil.generateToken(refreshToken.email, isAdmin) } }?: throw InvalidRefreshTokenException(tokenId) }