Skip to content

Commit cf461de

Browse files
authored
Sparkjava implementation (#13)
1 parent 95d60be commit cf461de

File tree

6 files changed

+118
-1
lines changed

6 files changed

+118
-1
lines changed

kotlin-oauth2-server-core/src/main/java/nl/myndocs/oauth2/CallRouter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ class CallRouter(
125125

126126
callContext.redirect(queryParameters["redirect_uri"] + "?code=${redirect.codeToken}$stateQueryParameter")
127127
} catch (unverifiedIdentityException: InvalidIdentityException) {
128-
authorizer.failedAuthentication()
129128
callContext.respondStatus(STATUS_UNAUTHORIZED)
129+
authorizer.failedAuthentication()
130130
}
131131
}
132132

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.1.1</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>kotlin-oauth2-server-sparkjava</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.sparkjava</groupId>
17+
<artifactId>spark-core</artifactId>
18+
<version>2.5.4</version>
19+
<scope>provided</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>nl.myndocs</groupId>
23+
<artifactId>kotlin-oauth2-server-core</artifactId>
24+
<version>${project.version}</version>
25+
<scope>provided</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.google.code.gson</groupId>
29+
<artifactId>gson</artifactId>
30+
<version>2.8.5</version>
31+
<scope>provided</scope>
32+
</dependency>
33+
</dependencies>
34+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package nl.myndocs.oauth2.sparkjava
2+
3+
import nl.myndocs.oauth2.config.ConfigurationBuilder
4+
import nl.myndocs.oauth2.sparkjava.request.SparkjavaCallContext
5+
import spark.Spark.get
6+
import spark.Spark.post
7+
8+
object Oauth2Server {
9+
fun configureOauth2Server(configurationCallback: ConfigurationBuilder.Configuration.() -> Unit) {
10+
val configuration = ConfigurationBuilder.build(configurationCallback)
11+
12+
val callRouter = configuration.callRouter
13+
14+
post(callRouter.tokenEndpoint) { req, res ->
15+
val sparkjavaCallContext = SparkjavaCallContext(req, res)
16+
callRouter.route(sparkjavaCallContext, configuration.authorizerFactory(sparkjavaCallContext))
17+
18+
res.body()
19+
}
20+
21+
22+
get(callRouter.authorizeEndpoint) { req, res ->
23+
val sparkjavaCallContext = SparkjavaCallContext(req, res)
24+
callRouter.route(sparkjavaCallContext, configuration.authorizerFactory(sparkjavaCallContext))
25+
26+
res.body()
27+
}
28+
29+
get(callRouter.userInfoEndpoint) { req, res ->
30+
val sparkjavaCallContext = SparkjavaCallContext(req, res)
31+
callRouter.route(sparkjavaCallContext, configuration.authorizerFactory(sparkjavaCallContext))
32+
33+
res.body()
34+
}
35+
}
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package nl.myndocs.oauth2.sparkjava.json
2+
3+
import com.google.gson.Gson
4+
5+
object JsonMapper {
6+
private val gson = Gson()
7+
8+
fun toJson(content: Any) = gson.toJson(content)
9+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package nl.myndocs.oauth2.sparkjava.request
2+
3+
import nl.myndocs.oauth2.request.CallContext
4+
import nl.myndocs.oauth2.sparkjava.json.JsonMapper
5+
import spark.Request
6+
import spark.Response
7+
8+
class SparkjavaCallContext(val request: Request, val response: Response) : CallContext {
9+
override val path: String = request.pathInfo()
10+
override val method: String = request.requestMethod()
11+
override val headers: Map<String, String> = request.headers()
12+
.map { it.toLowerCase() to request.headers(it) }
13+
.toMap()
14+
15+
override val queryParameters: Map<String, String> = request.queryParams()
16+
.map { it.toLowerCase() to request.queryParams(it) }
17+
.toMap()
18+
19+
override val formParameters: Map<String, String> = queryParameters
20+
21+
override fun respondStatus(statusCode: Int) {
22+
response.status(statusCode)
23+
response.body("")
24+
}
25+
26+
override fun respondHeader(name: String, value: String) {
27+
response.header(name, value)
28+
}
29+
30+
override fun respondJson(content: Any) {
31+
response.body(JsonMapper.toJson(content))
32+
}
33+
34+
override fun redirect(uri: String) {
35+
response.redirect(uri)
36+
}
37+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<module>kotlin-oauth2-server-identity-inmemory</module>
2323
<module>kotlin-oauth2-server-token-store-inmemory</module>
2424
<module>kotlin-oauth2-server-javalin</module>
25+
<module>kotlin-oauth2-server-sparkjava</module>
2526
</modules>
2627

2728
<dependencies>

0 commit comments

Comments
 (0)