Skip to content
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
14 changes: 7 additions & 7 deletions oauth2-server-core/src/main/java/nl/myndocs/oauth2/CallRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import nl.myndocs.oauth2.client.AuthorizedGrantType.CLIENT_CREDENTIALS
import nl.myndocs.oauth2.client.AuthorizedGrantType.PASSWORD
import nl.myndocs.oauth2.client.AuthorizedGrantType.REFRESH_TOKEN
import nl.myndocs.oauth2.exception.*
import nl.myndocs.oauth2.identity.UserInfo
import nl.myndocs.oauth2.identity.TokenInfo
import nl.myndocs.oauth2.request.*
import nl.myndocs.oauth2.token.toMap

class CallRouter(
private val tokenService: TokenService,
val tokenEndpoint: String,
val authorizeEndpoint: String,
val userInfoEndpoint: String,
private val userInfoCallback: (UserInfo) -> Map<String, Any?>
val tokenInfoEndpoint: String,
private val tokenInfoCallback: (TokenInfo) -> Map<String, Any?>
) {
companion object {
const val METHOD_POST = "post"
Expand All @@ -32,7 +32,7 @@ class CallRouter(
when (callContext.path) {
tokenEndpoint -> routeTokenEndpoint(callContext)
authorizeEndpoint -> routeAuthorizeEndpoint(callContext, authorizer)
userInfoEndpoint -> routeUserInfoEndpoint(callContext)
tokenInfoEndpoint -> routeTokenInfoEndpoint(callContext)
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ class CallRouter(
}
}

private fun routeUserInfoEndpoint(callContext: CallContext) {
private fun routeTokenInfoEndpoint(callContext: CallContext) {
if (callContext.method.toLowerCase() != METHOD_GET) {
return
}
Expand All @@ -222,8 +222,8 @@ class CallRouter(

val token = authorization.substring(7)

val userInfoCallback = userInfoCallback(tokenService.userInfo(token))
val tokenInfoCallback = tokenInfoCallback(tokenService.tokenInfo(token))

callContext.respondJson(userInfoCallback)
callContext.respondJson(tokenInfoCallback)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import nl.myndocs.oauth2.client.ClientService
import nl.myndocs.oauth2.exception.*
import nl.myndocs.oauth2.identity.Identity
import nl.myndocs.oauth2.identity.IdentityService
import nl.myndocs.oauth2.identity.UserInfo
import nl.myndocs.oauth2.identity.TokenInfo
import nl.myndocs.oauth2.request.*
import nl.myndocs.oauth2.response.TokenResponse
import nl.myndocs.oauth2.scope.ScopeParser
Expand Down Expand Up @@ -315,13 +315,12 @@ class Oauth2TokenService(
}
}

override fun userInfo(accessToken: String): UserInfo {
override fun tokenInfo(accessToken: String): TokenInfo {
val storedAccessToken = tokenStore.accessToken(accessToken) ?: throw InvalidGrantException()
val client = clientService.clientOf(storedAccessToken.clientId) ?: throw InvalidClientException()
val identity = storedAccessToken.username?.let { identityService.identityOf(client, it) }
?: throw InvalidIdentityException()

return UserInfo(
return TokenInfo(
identity,
client,
storedAccessToken.scopes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package nl.myndocs.oauth2

import nl.myndocs.oauth2.authenticator.Authenticator
import nl.myndocs.oauth2.authenticator.IdentityScopeVerifier
import nl.myndocs.oauth2.identity.UserInfo
import nl.myndocs.oauth2.identity.TokenInfo
import nl.myndocs.oauth2.request.*
import nl.myndocs.oauth2.response.TokenResponse
import nl.myndocs.oauth2.token.AccessToken
Expand All @@ -29,5 +29,5 @@ interface TokenService {
identityScopeVerifier: IdentityScopeVerifier?
): AccessToken

fun userInfo(accessToken: String): UserInfo
fun tokenInfo(accessToken: String): TokenInfo
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package nl.myndocs.oauth2.config

import nl.myndocs.oauth2.CallRouter
import nl.myndocs.oauth2.TokenService
import nl.myndocs.oauth2.identity.UserInfo
import nl.myndocs.oauth2.identity.TokenInfo

internal object CallRouterBuilder {
class Configuration {
var tokenEndpoint: String = "/oauth/token"
var authorizeEndpoint: String = "/oauth/authorize"
var userInfoEndpoint: String = "/oauth/userinfo"
var userInfoCallback: (UserInfo) -> Map<String, Any?> = { userInfo ->
var tokenInfoEndpoint: String = "/oauth/tokeninfo"
var tokenInfoCallback: (TokenInfo) -> Map<String, Any?> = { tokenInfo ->
mapOf(
"username" to userInfo.identity.username,
"scopes" to userInfo.scopes
)
"username" to tokenInfo.identity?.username,
"scopes" to tokenInfo.scopes
).filterValues { it != null }
}
var tokenService: TokenService? = null
}
Expand All @@ -29,7 +29,7 @@ internal object CallRouterBuilder {
configuration.tokenService!!,
configuration.tokenEndpoint,
configuration.authorizeEndpoint,
configuration.userInfoEndpoint,
configuration.userInfoCallback
configuration.tokenInfoEndpoint,
configuration.tokenInfoCallback
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package nl.myndocs.oauth2.config

import nl.myndocs.oauth2.TokenService
import nl.myndocs.oauth2.authenticator.Authorizer
import nl.myndocs.oauth2.identity.UserInfo
import nl.myndocs.oauth2.identity.TokenInfo
import nl.myndocs.oauth2.request.CallContext
import nl.myndocs.oauth2.request.auth.BasicAuthorizer

Expand All @@ -28,16 +28,16 @@ object ConfigurationBuilder {
callRouterConfiguration.tokenEndpoint = value
}

var userInfoEndpoint: String
get() = callRouterConfiguration.userInfoEndpoint
var tokenInfoEndpoint: String
get() = callRouterConfiguration.tokenInfoEndpoint
set(value) {
callRouterConfiguration.userInfoEndpoint = value
callRouterConfiguration.tokenInfoEndpoint = value
}

var userInfoCallback: (UserInfo) -> Map<String, Any?>
get() = callRouterConfiguration.userInfoCallback
var tokenInfoCallback: (TokenInfo) -> Map<String, Any?>
get() = callRouterConfiguration.tokenInfoCallback
set(value) {
callRouterConfiguration.userInfoCallback = value
callRouterConfiguration.tokenInfoCallback = value
}

var authorizerFactory: (CallContext) -> Authorizer = ::BasicAuthorizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package nl.myndocs.oauth2.identity

import nl.myndocs.oauth2.client.Client

data class UserInfo(
val identity: Identity,
data class TokenInfo(
val identity: Identity?,
val client: Client,
val scopes: Set<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ infix fun RoutingHttpHandler.`enable oauth2`(configurationCallback: Configuratio

responseBuilder.build()
},
callRouter.userInfoEndpoint bind Method.GET to { request: Request ->
callRouter.tokenInfoEndpoint bind Method.GET to { request: Request ->
val responseBuilder = ResponseBuilder()
val callContext = Http4kCallContext(request, responseBuilder)
callRouter.route(callContext, configuration.authorizerFactory(callContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun Javalin.enableOauthServer(configurationCallback: ConfigurationBuilder.Config
}
}

path(callRouter.userInfoEndpoint) {
path(callRouter.tokenInfoEndpoint) {
get { ctx ->
val javalinCallContext = JavalinCallContext(ctx)
callRouter.route(javalinCallContext, configuration.authorizerFactory(javalinCallContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Oauth2Server {
res.body()
}

get(callRouter.userInfoEndpoint) { req, res ->
get(callRouter.tokenInfoEndpoint) { req, res ->
val sparkjavaCallContext = SparkjavaCallContext(req, res)
callRouter.route(sparkjavaCallContext, configuration.authorizerFactory(sparkjavaCallContext))

Expand Down