Skip to content

Simplifications and cleaning up unused imports #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 32 additions & 39 deletions oauth2-server-core/src/main/java/nl/myndocs/oauth2/CallRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ import nl.myndocs.oauth2.router.RedirectRouter
import nl.myndocs.oauth2.router.RedirectRouterResponse

class CallRouter(
val tokenEndpoint: String,
val authorizeEndpoint: String,
val tokenInfoEndpoint: String,
private val tokenInfoCallback: (TokenInfo) -> Map<String, Any?>,
private val granters: List<GrantingCall.() -> Granter>,
private val grantingCallFactory: (CallContext) -> GrantingCall
val tokenEndpoint: String,
val authorizeEndpoint: String,
val tokenInfoEndpoint: String,
private val tokenInfoCallback: (TokenInfo) -> Map<String, Any?>,
private val granters: List<GrantingCall.() -> Granter>,
private val grantingCallFactory: (CallContext) -> GrantingCall
) : RedirectRouter {
companion object {
const val METHOD_POST = "post"
const val METHOD_GET = "get"

const val STATUS_BAD_REQUEST = 400
const val STATUS_UNAUTHORIZED = 401

}

fun route(callContext: CallContext) {
Expand All @@ -45,24 +44,21 @@ class CallRouter(
}
}


private fun routeTokenEndpoint(callContext: CallContext) {
if (callContext.method.toLowerCase() != METHOD_POST) {
return
}

try {
val grantType = callContext.formParameters["grant_type"]
?: throw InvalidRequestException("'grant_type' not given")
?: throw InvalidRequestException("'grant_type' not given")

val grantingCall = grantingCallFactory(callContext)

val granterMap = granters
.map {
val granter = grantingCall.it()
granter.grantType to granter
}
.toMap()
val granterMap = granters.associate {
val granter = grantingCall.it()
granter.grantType to granter
}

val allowedGrantTypes = granterMap.keys

Expand All @@ -78,19 +74,19 @@ class CallRouter(
}

fun routeAuthorizationCodeRedirect(
callContext: CallContext,
credentials: Credentials?
callContext: CallContext,
credentials: Credentials?
): RedirectRouterResponse {
val queryParameters = callContext.queryParameters
try {
val redirect = grantingCallFactory(callContext).redirect(
RedirectAuthorizationCodeRequest(
queryParameters["client_id"],
queryParameters["redirect_uri"],
credentials?.username,
credentials?.password,
queryParameters["scope"]
)
RedirectAuthorizationCodeRequest(
queryParameters["client_id"],
queryParameters["redirect_uri"],
credentials?.username,
credentials?.password,
queryParameters["scope"]
)
)

var stateQueryParameter = ""
Expand All @@ -109,33 +105,31 @@ class CallRouter(
}
}


fun routeAccessTokenRedirect(
callContext: CallContext,
credentials: Credentials?
callContext: CallContext,
credentials: Credentials?
): RedirectRouterResponse {
val queryParameters = callContext.queryParameters

try {
val redirect = grantingCallFactory(callContext).redirect(
RedirectTokenRequest(
queryParameters["client_id"],
queryParameters["redirect_uri"],
credentials?.username,
credentials?.password,
queryParameters["scope"]
)
RedirectTokenRequest(
queryParameters["client_id"],
queryParameters["redirect_uri"],
credentials?.username,
credentials?.password,
queryParameters["scope"]
)
)

var stateQueryParameter = ""

if (queryParameters["state"] != null) {
stateQueryParameter = "&state=" + queryParameters["state"]
}

callContext.redirect(
queryParameters["redirect_uri"] + "#access_token=${redirect.accessToken}" +
"&token_type=bearer&expires_in=${redirect.expiresIn()}$stateQueryParameter"
queryParameters["redirect_uri"] + "#access_token=${redirect.accessToken}" +
"&token_type=bearer&expires_in=${redirect.expiresIn()}$stateQueryParameter"
)

return RedirectRouterResponse(true)
Expand All @@ -153,7 +147,7 @@ class CallRouter(
}

val responseType = callContext.queryParameters["response_type"]
?: throw InvalidRequestException("'response_type' not given")
?: throw InvalidRequestException("'response_type' not given")

return when (responseType) {
"code" -> routeAuthorizationCodeRedirect(callContext, credentials)
Expand Down Expand Up @@ -185,7 +179,6 @@ class CallRouter(
}

val token = authorization.substring(7)

val tokenInfoCallback = tokenInfoCallback(grantingCallFactory(callContext).tokenInfo(token))

callContext.respondJson(tokenInfoCallback)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package nl.myndocs.oauth2.client

data class Client(
val clientId: String,
val clientScopes: Set<String>,
val redirectUris: Set<String>,
val authorizedGrantTypes: Set<String>
val clientId: String,
val clientScopes: Set<String>,
val redirectUris: Set<String>,
val authorizedGrantTypes: Set<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ package nl.myndocs.oauth2.client

interface ClientService {
fun clientOf(clientId: String): Client?

fun validClient(client: Client, clientSecret: String): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ internal object CallRouterBuilder {
var tokenInfoEndpoint: String = "/oauth/tokeninfo"
var tokenInfoCallback: (TokenInfo) -> Map<String, Any?> = { tokenInfo ->
mapOf(
"username" to tokenInfo.identity?.username,
"scopes" to tokenInfo.scopes
"username" to tokenInfo.identity?.username,
"scopes" to tokenInfo.scopes
).filterValues { it != null }
}
var granters: List<GrantingCall.() -> Granter> = listOf()
}

fun build(configuration: Configuration, grantingCallFactory: (CallContext) -> GrantingCall) = CallRouter(
configuration.tokenEndpoint,
configuration.authorizeEndpoint,
configuration.tokenInfoEndpoint,
configuration.tokenInfoCallback,
listOf<GrantingCall.() -> Granter>(
{ grantPassword() },
{ grantAuthorizationCode() },
{ grantClientCredentials() },
{ grantRefreshToken() }
) + configuration.granters,
grantingCallFactory
configuration.tokenEndpoint,
configuration.authorizeEndpoint,
configuration.tokenInfoEndpoint,
configuration.tokenInfoCallback,
listOf<GrantingCall.() -> Granter>(
{ grantPassword() },
{ grantAuthorizationCode() },
{ grantClientCredentials() },
{ grantRefreshToken() }
) + configuration.granters,
grantingCallFactory
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ package nl.myndocs.oauth2.config

import nl.myndocs.oauth2.CallRouter

data class Configuration(
val callRouter: CallRouter
)
data class Configuration(val callRouter: CallRouter)
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ object ConfigurationBuilder {
var accessTokenResponder: AccessTokenResponder = DefaultAccessTokenResponder
}

fun build(configurer: Configuration.() -> Unit, configuration: Configuration): nl.myndocs.oauth2.config.Configuration {
fun build(
configurer: Configuration.() -> Unit,
configuration: Configuration
): nl.myndocs.oauth2.config.Configuration {
configurer(configuration)

val grantingCallFactory: (CallContext) -> GrantingCall = { callContext ->
Expand All @@ -64,23 +67,23 @@ object ConfigurationBuilder {
override val clientService = configuration.clientService!!
override val tokenStore = configuration.tokenStore!!
override val converters = Converters(
configuration.accessTokenConverter,
configuration.refreshTokenConverter,
configuration.codeTokenConverter
configuration.accessTokenConverter,
configuration.refreshTokenConverter,
configuration.codeTokenConverter
)
override val accessTokenResponder = configuration.accessTokenResponder
}
}
return Configuration(
CallRouterBuilder.build(
configuration.callRouterConfiguration,
grantingCallFactory
)
CallRouterBuilder.build(
configuration.callRouterConfiguration,
grantingCallFactory
)
)
}

fun build(configurer: Configuration.() -> Unit): nl.myndocs.oauth2.config.Configuration {
val configuration = Configuration()

return build(configurer, configuration)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package nl.myndocs.oauth2.exception

class InvalidScopeException(val notAllowedScopes: Set<String>) : OauthException(OauthError.INVALID_SCOPE, "Scopes not allowed: $notAllowedScopes")
class InvalidScopeException(val notAllowedScopes: Set<String>) :
OauthException(OauthError.INVALID_SCOPE, "Scopes not allowed: $notAllowedScopes")
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package nl.myndocs.oauth2.exception

fun OauthException.toMap(): Map<String, String> {

val mutableMapOf = mutableMapOf<String, String>(
"error" to this.error.errorName
)

if (this.errorDescription != null) {
mutableMapOf["error_description"] = this.errorDescription
fun OauthException.toMap(): Map<String, String> = with(mutableMapOf("error" to error.errorName)) {
if (errorDescription != null) {
this["error_description"] = errorDescription
}

return mutableMapOf.toMap()
toMap()
}
Loading