Skip to content

Commit

Permalink
0.1.19-SNAPSHOT dynamic claims support
Browse files Browse the repository at this point in the history
  • Loading branch information
ESchouten committed Apr 15, 2019
1 parent 301350b commit bd9e505
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<groupId>com.erikschouten</groupId>
<artifactId>SpringJWTAuthenticator</artifactId>
<version>0.1.18</version>
<version>0.1.19-SNAPSHOT</version>

<!--mvn versions:update-properties-->
<!--mvn versions:commit-->
<properties>
<kotlin.version>1.3.21</kotlin.version>
<spring.version>5.1.4.RELEASE</spring.version>
<kotlin.version>1.3.30</kotlin.version>
<spring.version>5.1.5.RELEASE</spring.version>

<jjwt.version>0.10.6</jjwt.version>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ private const val HEADER_START = "Bearer "
class JWTSecurityContextRepository(
private val userDetailsService: UserDetailsService,
private val tokenTtlMs: Int = 30 * 60 * 1000,
private val key: SecretKey = Keys.secretKeyFor(SignatureAlgorithm.HS512))
private val key: SecretKey = Keys.secretKeyFor(SignatureAlgorithm.HS512),
private val claimFunctions: List<(String) -> Pair<String, Any>> = emptyList())
: SecurityContextRepository {

private val logger = LoggerFactory.getLogger(JWTSecurityContextRepository::class.java)
Expand Down Expand Up @@ -84,16 +85,23 @@ class JWTSecurityContextRepository(
}
}

private fun createJWT(auth: Authentication) =
Jwts.builder()
.setSubject(auth.name)
.claim("roles", auth.authorities.map { it.authority })
.signWith(key)
.apply {
if (tokenTtlMs != -1) {
setExpiration(Date(System.currentTimeMillis().plus(tokenTtlMs)))
}
}.compact()
private fun createJWT(auth: Authentication): String {
val jwtBuilder = Jwts.builder()
.setSubject(auth.name)
.claim("roles", auth.authorities.map { it.authority })
.signWith(key).apply {
if (tokenTtlMs != -1) {
setExpiration(Date(System.currentTimeMillis().plus(tokenTtlMs)))
}
}

claimFunctions.forEach {
val pair = it(auth.name)
jwtBuilder.claim(pair.first, pair.second)
}

return jwtBuilder.compact()
}

fun validateTokenAndExtractEmail(header: String) =
Jwts.parser()
Expand Down

0 comments on commit bd9e505

Please sign in to comment.