Skip to content

Commit e7c8b26

Browse files
authored
Jwt converters (#48)
* JWT converter * Use interface instead of callback to have more control
1 parent a4b597e commit e7c8b26

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

oauth2-server-jwt/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>kotlin-oauth2-server</artifactId>
7+
<groupId>nl.myndocs</groupId>
8+
<version>0.3.2-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>oauth2-server-jwt</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>nl.myndocs</groupId>
17+
<artifactId>oauth2-server-core</artifactId>
18+
<version>${project.version}</version>
19+
<scope>provided</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.auth0</groupId>
23+
<artifactId>java-jwt</artifactId>
24+
<version>3.5.0</version>
25+
</dependency>
26+
</dependencies>
27+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package nl.myndocs.convert
2+
3+
import com.auth0.jwt.JWT
4+
import java.time.Instant
5+
import java.util.*
6+
7+
object DefaultJwtBuilder : JwtBuilder {
8+
override fun buildJwt(username: String?, clientId: String, requestedScopes: Set<String>, expiresInSeconds: Long) =
9+
JWT.create()
10+
.withIssuedAt(Date.from(Instant.now()))
11+
.withExpiresAt(
12+
Date.from(
13+
Instant.now()
14+
.plusSeconds(expiresInSeconds)
15+
)
16+
)
17+
.withClaim("client_id", clientId)
18+
.withArrayClaim("scopes", requestedScopes.toTypedArray())
19+
.let { withBuilder -> if (username != null) withBuilder.withClaim("username", username) else withBuilder }
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package nl.myndocs.convert
2+
3+
import com.auth0.jwt.algorithms.Algorithm
4+
import nl.myndocs.oauth2.token.AccessToken
5+
import nl.myndocs.oauth2.token.RefreshToken
6+
import nl.myndocs.oauth2.token.converter.AccessTokenConverter
7+
import java.time.Instant
8+
9+
class JwtAccessTokenConverter(
10+
private val algorithm: Algorithm,
11+
private val accessTokenExpireInSeconds: Int = 3600,
12+
private val jwtBuilder: JwtBuilder = DefaultJwtBuilder
13+
) : AccessTokenConverter {
14+
override fun convertToToken(username: String?, clientId: String, requestedScopes: Set<String>, refreshToken: RefreshToken?): AccessToken {
15+
val jwtBuilder = jwtBuilder.buildJwt(
16+
username,
17+
clientId,
18+
requestedScopes,
19+
accessTokenExpireInSeconds.toLong()
20+
)
21+
22+
return AccessToken(
23+
jwtBuilder.sign(algorithm),
24+
"bearer",
25+
Instant.now().plusSeconds(accessTokenExpireInSeconds.toLong()),
26+
username,
27+
clientId,
28+
requestedScopes,
29+
refreshToken
30+
)
31+
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package nl.myndocs.convert
2+
3+
import com.auth0.jwt.JWTCreator
4+
5+
interface JwtBuilder {
6+
fun buildJwt(username: String?, clientId: String, requestedScopes: Set<String>, expiresInSeconds: Long): JWTCreator.Builder
7+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package nl.myndocs.convert
2+
3+
import com.auth0.jwt.algorithms.Algorithm
4+
import nl.myndocs.oauth2.token.RefreshToken
5+
import nl.myndocs.oauth2.token.converter.RefreshTokenConverter
6+
import java.time.Instant
7+
8+
class JwtRefreshTokenConverter(
9+
private val algorithm: Algorithm,
10+
private val refreshTokenExpireInSeconds: Int = 86400,
11+
private val jwtBuilder: JwtBuilder = DefaultJwtBuilder
12+
) : RefreshTokenConverter {
13+
override fun convertToToken(username: String?, clientId: String, requestedScopes: Set<String>): RefreshToken {
14+
val jwtBuilder = jwtBuilder.buildJwt(
15+
username,
16+
clientId,
17+
requestedScopes,
18+
refreshTokenExpireInSeconds.toLong()
19+
)
20+
21+
return RefreshToken(
22+
jwtBuilder.sign(algorithm),
23+
Instant.now().plusSeconds(refreshTokenExpireInSeconds.toLong()),
24+
username,
25+
clientId,
26+
requestedScopes
27+
)
28+
}
29+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<module>oauth2-server-javalin</module>
2626
<module>oauth2-server-sparkjava</module>
2727
<module>oauth2-server-http4k</module>
28+
<module>oauth2-server-jwt</module>
2829
</modules>
2930

3031
<dependencies>

0 commit comments

Comments
 (0)