Skip to content

Commit 89ef5aa

Browse files
authored
Simplify authorizer factory configuration (#64)
* Simplify authorizer factory configuration
1 parent bf64ca8 commit 89ef5aa

File tree

8 files changed

+25
-26
lines changed

8 files changed

+25
-26
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class CallRouter(
1818
val tokenInfoEndpoint: String,
1919
private val tokenInfoCallback: (TokenInfo) -> Map<String, Any?>,
2020
private val granters: List<GrantingCall.() -> Granter>,
21-
private val grantingCallFactory: (CallContext) -> GrantingCall
21+
private val grantingCallFactory: (CallContext) -> GrantingCall,
22+
private val authorizerFactory: (CallContext) -> Authorizer
2223
) {
2324
companion object {
2425
const val METHOD_POST = "post"
@@ -29,12 +30,10 @@ class CallRouter(
2930

3031
}
3132

32-
fun route(
33-
callContext: CallContext,
34-
authorizer: Authorizer) {
33+
fun route(callContext: CallContext) {
3534
when (callContext.path) {
3635
tokenEndpoint -> routeTokenEndpoint(callContext)
37-
authorizeEndpoint -> routeAuthorizeEndpoint(callContext, authorizer)
36+
authorizeEndpoint -> routeAuthorizeEndpoint(callContext, authorizerFactory(callContext))
3837
tokenInfoEndpoint -> routeTokenInfoEndpoint(callContext)
3938
}
4039
}

oauth2-server-core/src/main/java/nl/myndocs/oauth2/config/CallRouterBuilder.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nl.myndocs.oauth2.config
22

33
import nl.myndocs.oauth2.CallRouter
4+
import nl.myndocs.oauth2.authenticator.Authorizer
45
import nl.myndocs.oauth2.grant.*
56
import nl.myndocs.oauth2.identity.TokenInfo
67
import nl.myndocs.oauth2.request.CallContext
@@ -19,7 +20,7 @@ internal object CallRouterBuilder {
1920
var granters: List<GrantingCall.() -> Granter> = listOf()
2021
}
2122

22-
fun build(configuration: Configuration, grantingCallFactory: (CallContext) -> GrantingCall) = CallRouter(
23+
fun build(configuration: Configuration, grantingCallFactory: (CallContext) -> GrantingCall, authorizerFactory: (CallContext) -> Authorizer) = CallRouter(
2324
configuration.tokenEndpoint,
2425
configuration.authorizeEndpoint,
2526
configuration.tokenInfoEndpoint,
@@ -30,6 +31,7 @@ internal object CallRouterBuilder {
3031
{ grantClientCredentials() },
3132
{ grantRefreshToken() }
3233
) + configuration.granters,
33-
grantingCallFactory
34+
grantingCallFactory,
35+
authorizerFactory
3436
)
3537
}

oauth2-server-core/src/main/java/nl/myndocs/oauth2/config/Configuration.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ import nl.myndocs.oauth2.authenticator.Authorizer
55
import nl.myndocs.oauth2.request.CallContext
66

77
data class Configuration(
8-
val callRouter: CallRouter,
9-
val authorizerFactory: (CallContext) -> Authorizer
8+
val callRouter: CallRouter
109
)

oauth2-server-core/src/main/java/nl/myndocs/oauth2/config/ConfigurationBuilder.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,12 @@ object ConfigurationBuilder {
7676
override val accessTokenResponder = configuration.accessTokenResponder
7777
}
7878
}
79-
return nl.myndocs.oauth2.config.Configuration(
80-
CallRouterBuilder.build(configuration.callRouterConfiguration, grantingCallFactory),
81-
configuration.authorizerFactory
79+
return Configuration(
80+
CallRouterBuilder.build(
81+
configuration.callRouterConfiguration,
82+
grantingCallFactory,
83+
configuration.authorizerFactory
84+
)
8285
)
8386
}
8487
}

oauth2-server-http4k/src/main/java/nl/myndocs/oauth2/http4k/Oauth2Server.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ infix fun RoutingHttpHandler.`enable oauth2`(configurationCallback: Configuratio
1919
callRouter.tokenEndpoint bind Method.POST to { request: Request ->
2020
val responseBuilder = ResponseBuilder()
2121
val callContext = Http4kCallContext(request, responseBuilder)
22-
callRouter.route(callContext, configuration.authorizerFactory(callContext))
22+
callRouter.route(callContext)
2323

2424
responseBuilder.build()
2525
},
2626
callRouter.authorizeEndpoint bind Method.GET to { request: Request ->
2727
val responseBuilder = ResponseBuilder()
2828
val callContext = Http4kCallContext(request, responseBuilder)
29-
callRouter.route(callContext, configuration.authorizerFactory(callContext))
29+
callRouter.route(callContext)
3030

3131
responseBuilder.build()
3232
},
3333
callRouter.tokenInfoEndpoint bind Method.GET to { request: Request ->
3434
val responseBuilder = ResponseBuilder()
3535
val callContext = Http4kCallContext(request, responseBuilder)
36-
callRouter.route(callContext, configuration.authorizerFactory(callContext))
36+
callRouter.route(callContext)
3737

3838
responseBuilder.build()
3939
}

oauth2-server-javalin/src/main/java/nl/myndocs/oauth2/javalin/Oauth2Server.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ fun Javalin.enableOauthServer(configurationCallback: ConfigurationBuilder.Config
1515
path(callRouter.tokenEndpoint) {
1616
post { ctx ->
1717
val javalinCallContext = JavalinCallContext(ctx)
18-
callRouter.route(javalinCallContext, configuration.authorizerFactory(javalinCallContext))
18+
callRouter.route(javalinCallContext)
1919
}
2020
}
2121

2222
path(callRouter.authorizeEndpoint) {
2323
get { ctx ->
2424
val javalinCallContext = JavalinCallContext(ctx)
25-
callRouter.route(javalinCallContext, configuration.authorizerFactory(javalinCallContext))
25+
callRouter.route(javalinCallContext)
2626
}
2727
}
2828

2929
path(callRouter.tokenInfoEndpoint) {
3030
get { ctx ->
3131
val javalinCallContext = JavalinCallContext(ctx)
32-
callRouter.route(javalinCallContext, configuration.authorizerFactory(javalinCallContext))
32+
callRouter.route(javalinCallContext)
3333
}
3434
}
3535
}

oauth2-server-ktor/src/main/java/nl/myndocs/oauth2/ktor/feature/Oauth2ServerFeature.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import nl.myndocs.oauth2.request.CallContext
1313
class Oauth2ServerFeature(configuration: Configuration) {
1414
val callRouter = configuration.callRouter
1515

16-
val authorizerFactory: (CallContext) -> Authorizer = configuration.authorizerFactory
17-
18-
1916
companion object Feature : ApplicationFeature<ApplicationCallPipeline, ConfigurationBuilder.Configuration, Oauth2ServerFeature> {
2017
override val key = AttributeKey<Oauth2ServerFeature>("Oauth2ServerFeature")
2118

@@ -26,9 +23,8 @@ class Oauth2ServerFeature(configuration: Configuration) {
2623

2724
pipeline.intercept(ApplicationCallPipeline.Features) {
2825
val ktorCallContext = KtorCallContext(call)
29-
val authorizer = feature.authorizerFactory(ktorCallContext)
3026

31-
feature.callRouter.route(ktorCallContext, authorizer)
27+
feature.callRouter.route(ktorCallContext)
3228
}
3329

3430
return feature

oauth2-server-sparkjava/src/main/java/nl/myndocs/oauth2/sparkjava/Oauth2Server.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ object Oauth2Server {
1313

1414
post(callRouter.tokenEndpoint) { req, res ->
1515
val sparkjavaCallContext = SparkjavaCallContext(req, res)
16-
callRouter.route(sparkjavaCallContext, configuration.authorizerFactory(sparkjavaCallContext))
16+
callRouter.route(sparkjavaCallContext)
1717

1818
res.body()
1919
}
2020

2121

2222
get(callRouter.authorizeEndpoint) { req, res ->
2323
val sparkjavaCallContext = SparkjavaCallContext(req, res)
24-
callRouter.route(sparkjavaCallContext, configuration.authorizerFactory(sparkjavaCallContext))
24+
callRouter.route(sparkjavaCallContext)
2525

2626
res.body()
2727
}
2828

2929
get(callRouter.tokenInfoEndpoint) { req, res ->
3030
val sparkjavaCallContext = SparkjavaCallContext(req, res)
31-
callRouter.route(sparkjavaCallContext, configuration.authorizerFactory(sparkjavaCallContext))
31+
callRouter.route(sparkjavaCallContext)
3232

3333
res.body()
3434
}

0 commit comments

Comments
 (0)