Skip to content

Commit

Permalink
modify :: tokenProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
meltapplee committed Sep 2, 2024
1 parent 74fb48d commit 5b2b8e5
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions src/main/kotlin/org/meogo/global/jwt/JwtTokenProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.meogo.global.jwt.entity.repository.RefreshTokenRepository
import org.meogo.global.jwt.exception.ExpiredTokenException
import org.meogo.global.jwt.exception.InvalidJwtException
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.stereotype.Component
import java.util.Date
import javax.servlet.http.HttpServletRequest
Expand All @@ -29,16 +28,17 @@ class JwtTokenProvider(
}

fun getToken(name: String): TokenResponse {
val accessToken: String = generateAccessToken(name, ACCESS_KEY, jwtProperties.accessExp)
val refreshToken: String = generateRefreshToken(REFRESH_KEY, jwtProperties.refreshExp)
val accessToken = generateAccessToken(name, ACCESS_KEY, jwtProperties.accessExp)
val refreshToken = generateRefreshToken(REFRESH_KEY, jwtProperties.refreshExp)
refreshTokenRepository.save(
RefreshToken(name, refreshToken, jwtProperties.refreshExp)
)
return TokenResponse(accessToken = accessToken, refreshToken = refreshToken)
return TokenResponse(accessToken, refreshToken)
}

private fun generateAccessToken(name: String, type: String, expiration: Long): String {
return Jwts.builder().signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey)
return Jwts.builder()
.signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey)
.setSubject(name)
.setHeaderParam("type", type)
.setIssuedAt(Date())
Expand All @@ -47,48 +47,45 @@ class JwtTokenProvider(
}

private fun generateRefreshToken(type: String, ttl: Long): String {
return Jwts.builder().signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey)
return Jwts.builder()
.signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey)
.setHeaderParam("type", type)
.setIssuedAt(Date())
.setExpiration(Date(System.currentTimeMillis() + ttl * 1000))
.compact()
}

fun resolveToken(request: HttpServletRequest): String? {
val bearer: String? = request.getHeader("Authorization")

val bearer = request.getHeader("Authorization")
return parseToken(bearer)
}

fun parseToken(bearerToken: String?): String? {
return if (bearerToken != null && bearerToken.startsWith("Bearer ")) {
return bearerToken.replace("Bearer ", "")
bearerToken.removePrefix("Bearer ")
} else {
null
}
}

fun authorization(token: String): UsernamePasswordAuthenticationToken {
return token.let {
val userDetails: UserDetails = authDetailsService.loadUserByUsername(getTokenSubject(token))
return UsernamePasswordAuthenticationToken(userDetails, "", userDetails.authorities)
}
val userDetails = authDetailsService.loadUserByUsername(getTokenSubject(token))
return UsernamePasswordAuthenticationToken(userDetails, "", userDetails.authorities)
}

private fun getTokenSubject(subject: String): String {
return getTokenBody(subject).subject
private fun getTokenSubject(token: String): String {
return getTokenBody(token).subject
}

private fun getTokenBody(token: String?): Claims {
return try {
Jwts.parser().setSigningKey(jwtProperties.secretKey)
.parseClaimsJws(token).body
Jwts.parser().setSigningKey(jwtProperties.secretKey).parseClaimsJws(token).body
} catch (e: ExpiredJwtException) {
throw ExpiredTokenException
} catch (e: InvalidClaimException) {
throw InvalidJwtException
} catch (e: Exception) {
when (e) {
is ExpiredJwtException -> throw ExpiredTokenException
is InvalidClaimException -> throw InvalidJwtException
else -> throw InvalidJwtException
}
throw InvalidJwtException
}
}
}

0 comments on commit 5b2b8e5

Please sign in to comment.