-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from NTF-marketplace/feature/valid-refreshToken
feat: reissue accessToken
- Loading branch information
Showing
7 changed files
with
153 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
package com.api.auth.config | ||
|
||
import com.api.auth.security.RefreshTokenValidationFilter | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity | ||
import org.springframework.security.config.annotation.web.builders.WebSecurity | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer | ||
import org.springframework.security.config.http.SessionCreationPolicy | ||
import org.springframework.security.web.SecurityFilterChain | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter | ||
|
||
|
||
@Configuration | ||
class SecurityConfig { | ||
@EnableReactiveMethodSecurity | ||
class SecurityConfig( | ||
private val refreshTokenValidationFilter: RefreshTokenValidationFilter | ||
) { | ||
|
||
|
||
@Bean | ||
fun webSecurityCustomizer(): WebSecurityCustomizer { | ||
return WebSecurityCustomizer { web: WebSecurity -> web.ignoring().requestMatchers("/v1/auth") } | ||
} | ||
|
||
@Bean | ||
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { | ||
http | ||
.csrf { it.disable() } | ||
.authorizeHttpRequests { | ||
it.requestMatchers("/v1/auth").permitAll() | ||
it.requestMatchers("/v1/auth/reissue").authenticated() | ||
it.anyRequest().authenticated() | ||
} | ||
.addFilterBefore(refreshTokenValidationFilter, UsernamePasswordAuthenticationFilter::class.java) | ||
|
||
|
||
return http.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.api.auth.jwtUtil | ||
|
||
|
||
import com.nimbusds.jose.JWSAlgorithm | ||
import com.nimbusds.jose.crypto.RSASSAVerifier | ||
import com.nimbusds.jwt.JWTClaimsSet | ||
import com.nimbusds.jwt.SignedJWT | ||
import org.springframework.stereotype.Component | ||
import java.util.* | ||
|
||
@Component | ||
class JwtProvider( | ||
private val jwtBuilder: JwtBuilder, | ||
private val rsaKeyBuilder: RsaKeyBuilder | ||
) { | ||
|
||
fun validateToken(token: String): Boolean { | ||
try { | ||
val rsaKey = rsaKeyBuilder.loadRsaKeyFromFile() | ||
val publicKey = rsaKey.toRSAPublicKey() | ||
|
||
val signedJWT = SignedJWT.parse(token) | ||
|
||
val verifier = RSASSAVerifier(publicKey) | ||
|
||
if (!signedJWT.verify(verifier)) { | ||
return false | ||
} | ||
|
||
val claims = signedJWT.jwtClaimsSet | ||
return Date().before(claims.expirationTime) | ||
|
||
} catch (e: Exception) { | ||
println("Error during token validation: ${e.message}") | ||
return false | ||
} | ||
} | ||
|
||
fun getClaimsFromToken(token: String): JWTClaimsSet? { | ||
try { | ||
val rsaKey = rsaKeyBuilder.loadRsaKeyFromFile() | ||
val publicKey = rsaKey.toRSAPublicKey() | ||
|
||
val signedJWT = SignedJWT.parse(token) | ||
val verifier = RSASSAVerifier(publicKey) | ||
|
||
if (signedJWT.verify(verifier)) { | ||
return signedJWT.jwtClaimsSet | ||
} | ||
} catch (e: Exception) { | ||
println("Error during token decoding: ${e.message}") | ||
} | ||
return null | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/api/auth/security/RefreshTokenValidationFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.api.auth.security | ||
|
||
import com.api.auth.jwtUtil.JwtProvider | ||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
|
||
@Component | ||
class RefreshTokenValidationFilter( | ||
private val jwtProvider: JwtProvider | ||
): OncePerRequestFilter() { | ||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain | ||
) { | ||
val refreshToken = request.getHeader("Authorization")?.substring("Bearer ".length) | ||
if (refreshToken != null && jwtProvider.validateToken(refreshToken)) { | ||
val address = jwtProvider.getClaimsFromToken(refreshToken)?.getClaim("address") | ||
val authentication = UsernamePasswordAuthenticationToken( | ||
address, null, null | ||
) | ||
SecurityContextHolder.getContext().setAuthentication(authentication) | ||
filterChain.doFilter(request, response) | ||
} else { | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid Refresh Token") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
### GET request to example server | ||
GET https://examples.http-client.intellij.net/get | ||
?generated-in=IntelliJ IDEA | ||
|
||
### | ||
POST http://localhost:8081/v1/auth/reissue | ||
Content-Type: application/json | ||
Authorization: Bearer eyJraWQiOiIxOWJkMDRmOC1jNGRkLTQwZGYtYTc4NC1iY2Y5NTU5NmU2OTYiLCJhbGciOiJSUzI1NiJ9.eyJuYmYiOjE3MTU2MDg1OTAsImFkZHJlc3MiOiIweDAxYjcyYjRhYTNmNjZmMjEzZDYyZDUzZTgyOWJjMTcyYTZhNzI4NjciLCJleHAiOjE3MTYwNDA1OTAsImlhdCI6MTcxNTYwODU5MH0.MwcA2pAjk5l1M7-5mkYtwPX7ZlB4a6sCSqPsROZ9cwo-7hYKFK3fdcolkm9EoUfTZQWf8KdNTUm31-FRwoNIyolmk7bAhfB2N64DRrgYlX7RSIvDqDJvWpru4y82q41_vFCJE3MYfeAaj8duRdYx-BBDtYGLBJJAqe9ayJ5gIrYf-OyM2PGzvVRV2hr41vFNVYfaP3tY-4eJ93Cksz1M4bLBaUL5nGtYPrZvKDolxbHOD1H9ygDWpNyZVQmVpudz0ArjNumLi7mn_HXrbfDGZw8768L8E3lp4GE9QbIZHDF77JjkbcO_BBJnqJMMBg9jD4ixHK4TlRslXFabSap_Fw |