Skip to content

Commit ba57a32

Browse files
authored
Merge pull request #36 from myndocs/token-info
Change user info to token info
2 parents d75b299 + 4eddcba commit ba57a32

File tree

9 files changed

+32
-33
lines changed

9 files changed

+32
-33
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import nl.myndocs.oauth2.client.AuthorizedGrantType.CLIENT_CREDENTIALS
66
import nl.myndocs.oauth2.client.AuthorizedGrantType.PASSWORD
77
import nl.myndocs.oauth2.client.AuthorizedGrantType.REFRESH_TOKEN
88
import nl.myndocs.oauth2.exception.*
9-
import nl.myndocs.oauth2.identity.UserInfo
9+
import nl.myndocs.oauth2.identity.TokenInfo
1010
import nl.myndocs.oauth2.request.*
1111
import nl.myndocs.oauth2.token.toMap
1212

1313
class CallRouter(
1414
private val tokenService: TokenService,
1515
val tokenEndpoint: String,
1616
val authorizeEndpoint: String,
17-
val userInfoEndpoint: String,
18-
private val userInfoCallback: (UserInfo) -> Map<String, Any?>
17+
val tokenInfoEndpoint: String,
18+
private val tokenInfoCallback: (TokenInfo) -> Map<String, Any?>
1919
) {
2020
companion object {
2121
const val METHOD_POST = "post"
@@ -32,7 +32,7 @@ class CallRouter(
3232
when (callContext.path) {
3333
tokenEndpoint -> routeTokenEndpoint(callContext)
3434
authorizeEndpoint -> routeAuthorizeEndpoint(callContext, authorizer)
35-
userInfoEndpoint -> routeUserInfoEndpoint(callContext)
35+
tokenInfoEndpoint -> routeTokenInfoEndpoint(callContext)
3636
}
3737
}
3838

@@ -208,7 +208,7 @@ class CallRouter(
208208
}
209209
}
210210

211-
private fun routeUserInfoEndpoint(callContext: CallContext) {
211+
private fun routeTokenInfoEndpoint(callContext: CallContext) {
212212
if (callContext.method.toLowerCase() != METHOD_GET) {
213213
return
214214
}
@@ -222,8 +222,8 @@ class CallRouter(
222222

223223
val token = authorization.substring(7)
224224

225-
val userInfoCallback = userInfoCallback(tokenService.userInfo(token))
225+
val tokenInfoCallback = tokenInfoCallback(tokenService.tokenInfo(token))
226226

227-
callContext.respondJson(userInfoCallback)
227+
callContext.respondJson(tokenInfoCallback)
228228
}
229229
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import nl.myndocs.oauth2.client.ClientService
88
import nl.myndocs.oauth2.exception.*
99
import nl.myndocs.oauth2.identity.Identity
1010
import nl.myndocs.oauth2.identity.IdentityService
11-
import nl.myndocs.oauth2.identity.UserInfo
11+
import nl.myndocs.oauth2.identity.TokenInfo
1212
import nl.myndocs.oauth2.request.*
1313
import nl.myndocs.oauth2.response.TokenResponse
1414
import nl.myndocs.oauth2.scope.ScopeParser
@@ -315,13 +315,12 @@ class Oauth2TokenService(
315315
}
316316
}
317317

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

324-
return UserInfo(
323+
return TokenInfo(
325324
identity,
326325
client,
327326
storedAccessToken.scopes

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

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

33
import nl.myndocs.oauth2.authenticator.Authenticator
44
import nl.myndocs.oauth2.authenticator.IdentityScopeVerifier
5-
import nl.myndocs.oauth2.identity.UserInfo
5+
import nl.myndocs.oauth2.identity.TokenInfo
66
import nl.myndocs.oauth2.request.*
77
import nl.myndocs.oauth2.response.TokenResponse
88
import nl.myndocs.oauth2.token.AccessToken
@@ -29,5 +29,5 @@ interface TokenService {
2929
identityScopeVerifier: IdentityScopeVerifier?
3030
): AccessToken
3131

32-
fun userInfo(accessToken: String): UserInfo
32+
fun tokenInfo(accessToken: String): TokenInfo
3333
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package nl.myndocs.oauth2.config
22

33
import nl.myndocs.oauth2.CallRouter
44
import nl.myndocs.oauth2.TokenService
5-
import nl.myndocs.oauth2.identity.UserInfo
5+
import nl.myndocs.oauth2.identity.TokenInfo
66

77
internal object CallRouterBuilder {
88
class Configuration {
99
var tokenEndpoint: String = "/oauth/token"
1010
var authorizeEndpoint: String = "/oauth/authorize"
11-
var userInfoEndpoint: String = "/oauth/userinfo"
12-
var userInfoCallback: (UserInfo) -> Map<String, Any?> = { userInfo ->
11+
var tokenInfoEndpoint: String = "/oauth/tokeninfo"
12+
var tokenInfoCallback: (TokenInfo) -> Map<String, Any?> = { tokenInfo ->
1313
mapOf(
14-
"username" to userInfo.identity.username,
15-
"scopes" to userInfo.scopes
16-
)
14+
"username" to tokenInfo.identity?.username,
15+
"scopes" to tokenInfo.scopes
16+
).filterValues { it != null }
1717
}
1818
var tokenService: TokenService? = null
1919
}
@@ -29,7 +29,7 @@ internal object CallRouterBuilder {
2929
configuration.tokenService!!,
3030
configuration.tokenEndpoint,
3131
configuration.authorizeEndpoint,
32-
configuration.userInfoEndpoint,
33-
configuration.userInfoCallback
32+
configuration.tokenInfoEndpoint,
33+
configuration.tokenInfoCallback
3434
)
3535
}

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

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

33
import nl.myndocs.oauth2.TokenService
44
import nl.myndocs.oauth2.authenticator.Authorizer
5-
import nl.myndocs.oauth2.identity.UserInfo
5+
import nl.myndocs.oauth2.identity.TokenInfo
66
import nl.myndocs.oauth2.request.CallContext
77
import nl.myndocs.oauth2.request.auth.BasicAuthorizer
88

@@ -28,16 +28,16 @@ object ConfigurationBuilder {
2828
callRouterConfiguration.tokenEndpoint = value
2929
}
3030

31-
var userInfoEndpoint: String
32-
get() = callRouterConfiguration.userInfoEndpoint
31+
var tokenInfoEndpoint: String
32+
get() = callRouterConfiguration.tokenInfoEndpoint
3333
set(value) {
34-
callRouterConfiguration.userInfoEndpoint = value
34+
callRouterConfiguration.tokenInfoEndpoint = value
3535
}
3636

37-
var userInfoCallback: (UserInfo) -> Map<String, Any?>
38-
get() = callRouterConfiguration.userInfoCallback
37+
var tokenInfoCallback: (TokenInfo) -> Map<String, Any?>
38+
get() = callRouterConfiguration.tokenInfoCallback
3939
set(value) {
40-
callRouterConfiguration.userInfoCallback = value
40+
callRouterConfiguration.tokenInfoCallback = value
4141
}
4242

4343
var authorizerFactory: (CallContext) -> Authorizer = ::BasicAuthorizer

oauth2-server-core/src/main/java/nl/myndocs/oauth2/identity/UserInfo.kt renamed to oauth2-server-core/src/main/java/nl/myndocs/oauth2/identity/TokenInfo.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package nl.myndocs.oauth2.identity
22

33
import nl.myndocs.oauth2.client.Client
44

5-
data class UserInfo(
6-
val identity: Identity,
5+
data class TokenInfo(
6+
val identity: Identity?,
77
val client: Client,
88
val scopes: Set<String>
99
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ infix fun RoutingHttpHandler.`enable oauth2`(configurationCallback: Configuratio
3030

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fun Javalin.enableOauthServer(configurationCallback: ConfigurationBuilder.Config
2626
}
2727
}
2828

29-
path(callRouter.userInfoEndpoint) {
29+
path(callRouter.tokenInfoEndpoint) {
3030
get { ctx ->
3131
val javalinCallContext = JavalinCallContext(ctx)
3232
callRouter.route(javalinCallContext, configuration.authorizerFactory(javalinCallContext))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object Oauth2Server {
2626
res.body()
2727
}
2828

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

0 commit comments

Comments
 (0)