Skip to content

Commit 36b819d

Browse files
committed
Reformat files
1 parent e27646f commit 36b819d

36 files changed

+250
-233
lines changed

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

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@ import nl.myndocs.oauth2.router.RedirectRouter
1515
import nl.myndocs.oauth2.router.RedirectRouterResponse
1616

1717
class CallRouter(
18-
val tokenEndpoint: String,
19-
val authorizeEndpoint: String,
20-
val tokenInfoEndpoint: String,
21-
private val tokenInfoCallback: (TokenInfo) -> Map<String, Any?>,
22-
private val granters: List<GrantingCall.() -> Granter>,
23-
private val grantingCallFactory: (CallContext) -> GrantingCall
18+
val tokenEndpoint: String,
19+
val authorizeEndpoint: String,
20+
val tokenInfoEndpoint: String,
21+
private val tokenInfoCallback: (TokenInfo) -> Map<String, Any?>,
22+
private val granters: List<GrantingCall.() -> Granter>,
23+
private val grantingCallFactory: (CallContext) -> GrantingCall
2424
) : RedirectRouter {
2525
companion object {
2626
const val METHOD_POST = "post"
2727
const val METHOD_GET = "get"
2828

2929
const val STATUS_BAD_REQUEST = 400
3030
const val STATUS_UNAUTHORIZED = 401
31-
3231
}
3332

3433
fun route(callContext: CallContext) {
@@ -45,15 +44,14 @@ class CallRouter(
4544
}
4645
}
4746

48-
4947
private fun routeTokenEndpoint(callContext: CallContext) {
5048
if (callContext.method.toLowerCase() != METHOD_POST) {
5149
return
5250
}
5351

5452
try {
5553
val grantType = callContext.formParameters["grant_type"]
56-
?: throw InvalidRequestException("'grant_type' not given")
54+
?: throw InvalidRequestException("'grant_type' not given")
5755

5856
val grantingCall = grantingCallFactory(callContext)
5957

@@ -76,19 +74,19 @@ class CallRouter(
7674
}
7775

7876
fun routeAuthorizationCodeRedirect(
79-
callContext: CallContext,
80-
credentials: Credentials?
77+
callContext: CallContext,
78+
credentials: Credentials?
8179
): RedirectRouterResponse {
8280
val queryParameters = callContext.queryParameters
8381
try {
8482
val redirect = grantingCallFactory(callContext).redirect(
85-
RedirectAuthorizationCodeRequest(
86-
queryParameters["client_id"],
87-
queryParameters["redirect_uri"],
88-
credentials?.username,
89-
credentials?.password,
90-
queryParameters["scope"]
91-
)
83+
RedirectAuthorizationCodeRequest(
84+
queryParameters["client_id"],
85+
queryParameters["redirect_uri"],
86+
credentials?.username,
87+
credentials?.password,
88+
queryParameters["scope"]
89+
)
9290
)
9391

9492
var stateQueryParameter = ""
@@ -107,33 +105,31 @@ class CallRouter(
107105
}
108106
}
109107

110-
111108
fun routeAccessTokenRedirect(
112-
callContext: CallContext,
113-
credentials: Credentials?
109+
callContext: CallContext,
110+
credentials: Credentials?
114111
): RedirectRouterResponse {
115112
val queryParameters = callContext.queryParameters
116113

117114
try {
118115
val redirect = grantingCallFactory(callContext).redirect(
119-
RedirectTokenRequest(
120-
queryParameters["client_id"],
121-
queryParameters["redirect_uri"],
122-
credentials?.username,
123-
credentials?.password,
124-
queryParameters["scope"]
125-
)
116+
RedirectTokenRequest(
117+
queryParameters["client_id"],
118+
queryParameters["redirect_uri"],
119+
credentials?.username,
120+
credentials?.password,
121+
queryParameters["scope"]
122+
)
126123
)
127124

128125
var stateQueryParameter = ""
129-
130126
if (queryParameters["state"] != null) {
131127
stateQueryParameter = "&state=" + queryParameters["state"]
132128
}
133129

134130
callContext.redirect(
135-
queryParameters["redirect_uri"] + "#access_token=${redirect.accessToken}" +
136-
"&token_type=bearer&expires_in=${redirect.expiresIn()}$stateQueryParameter"
131+
queryParameters["redirect_uri"] + "#access_token=${redirect.accessToken}" +
132+
"&token_type=bearer&expires_in=${redirect.expiresIn()}$stateQueryParameter"
137133
)
138134

139135
return RedirectRouterResponse(true)
@@ -151,7 +147,7 @@ class CallRouter(
151147
}
152148

153149
val responseType = callContext.queryParameters["response_type"]
154-
?: throw InvalidRequestException("'response_type' not given")
150+
?: throw InvalidRequestException("'response_type' not given")
155151

156152
return when (responseType) {
157153
"code" -> routeAuthorizationCodeRedirect(callContext, credentials)
@@ -183,7 +179,6 @@ class CallRouter(
183179
}
184180

185181
val token = authorization.substring(7)
186-
187182
val tokenInfoCallback = tokenInfoCallback(grantingCallFactory(callContext).tokenInfo(token))
188183

189184
callContext.respondJson(tokenInfoCallback)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package nl.myndocs.oauth2.client
22

33
data class Client(
4-
val clientId: String,
5-
val clientScopes: Set<String>,
6-
val redirectUris: Set<String>,
7-
val authorizedGrantTypes: Set<String>
4+
val clientId: String,
5+
val clientScopes: Set<String>,
6+
val redirectUris: Set<String>,
7+
val authorizedGrantTypes: Set<String>
88
)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ package nl.myndocs.oauth2.client
22

33
interface ClientService {
44
fun clientOf(clientId: String): Client?
5-
65
fun validClient(client: Client, clientSecret: String): Boolean
76
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ internal object CallRouterBuilder {
1212
var tokenInfoEndpoint: String = "/oauth/tokeninfo"
1313
var tokenInfoCallback: (TokenInfo) -> Map<String, Any?> = { tokenInfo ->
1414
mapOf(
15-
"username" to tokenInfo.identity?.username,
16-
"scopes" to tokenInfo.scopes
15+
"username" to tokenInfo.identity?.username,
16+
"scopes" to tokenInfo.scopes
1717
).filterValues { it != null }
1818
}
1919
var granters: List<GrantingCall.() -> Granter> = listOf()
2020
}
2121

2222
fun build(configuration: Configuration, grantingCallFactory: (CallContext) -> GrantingCall) = CallRouter(
23-
configuration.tokenEndpoint,
24-
configuration.authorizeEndpoint,
25-
configuration.tokenInfoEndpoint,
26-
configuration.tokenInfoCallback,
27-
listOf<GrantingCall.() -> Granter>(
28-
{ grantPassword() },
29-
{ grantAuthorizationCode() },
30-
{ grantClientCredentials() },
31-
{ grantRefreshToken() }
32-
) + configuration.granters,
33-
grantingCallFactory
23+
configuration.tokenEndpoint,
24+
configuration.authorizeEndpoint,
25+
configuration.tokenInfoEndpoint,
26+
configuration.tokenInfoCallback,
27+
listOf<GrantingCall.() -> Granter>(
28+
{ grantPassword() },
29+
{ grantAuthorizationCode() },
30+
{ grantClientCredentials() },
31+
{ grantRefreshToken() }
32+
) + configuration.granters,
33+
grantingCallFactory
3434
)
3535
}

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

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

33
import nl.myndocs.oauth2.CallRouter
44

5-
data class Configuration(
6-
val callRouter: CallRouter
7-
)
5+
data class Configuration(val callRouter: CallRouter)

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ object ConfigurationBuilder {
5454
var accessTokenResponder: AccessTokenResponder = DefaultAccessTokenResponder
5555
}
5656

57-
fun build(configurer: Configuration.() -> Unit, configuration: Configuration): nl.myndocs.oauth2.config.Configuration {
57+
fun build(
58+
configurer: Configuration.() -> Unit,
59+
configuration: Configuration
60+
): nl.myndocs.oauth2.config.Configuration {
5861
configurer(configuration)
5962

6063
val grantingCallFactory: (CallContext) -> GrantingCall = { callContext ->
@@ -64,23 +67,23 @@ object ConfigurationBuilder {
6467
override val clientService = configuration.clientService!!
6568
override val tokenStore = configuration.tokenStore!!
6669
override val converters = Converters(
67-
configuration.accessTokenConverter,
68-
configuration.refreshTokenConverter,
69-
configuration.codeTokenConverter
70+
configuration.accessTokenConverter,
71+
configuration.refreshTokenConverter,
72+
configuration.codeTokenConverter
7073
)
7174
override val accessTokenResponder = configuration.accessTokenResponder
7275
}
7376
}
7477
return Configuration(
75-
CallRouterBuilder.build(
76-
configuration.callRouterConfiguration,
77-
grantingCallFactory
78-
)
78+
CallRouterBuilder.build(
79+
configuration.callRouterConfiguration,
80+
grantingCallFactory
81+
)
7982
)
8083
}
84+
8185
fun build(configurer: Configuration.() -> Unit): nl.myndocs.oauth2.config.Configuration {
8286
val configuration = Configuration()
83-
8487
return build(configurer, configuration)
8588
}
8689
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package nl.myndocs.oauth2.exception
22

3-
class InvalidScopeException(val notAllowedScopes: Set<String>) : OauthException(OauthError.INVALID_SCOPE, "Scopes not allowed: $notAllowedScopes")
3+
class InvalidScopeException(val notAllowedScopes: Set<String>) :
4+
OauthException(OauthError.INVALID_SCOPE, "Scopes not allowed: $notAllowedScopes")

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import nl.myndocs.oauth2.request.PasswordGrantRequest
88
import nl.myndocs.oauth2.scope.ScopeParser
99
import nl.myndocs.oauth2.token.AccessToken
1010

11-
1211
/**
1312
* @throws InvalidIdentityException
1413
* @throws InvalidClientException
@@ -33,15 +32,20 @@ fun GrantingCall.authorize(passwordGrantRequest: PasswordGrantRequest): AccessTo
3332
}
3433

3534
val requestedIdentity = identityService.identityOf(
36-
requestedClient, passwordGrantRequest.username
35+
requestedClient, passwordGrantRequest.username
3736
)
3837

39-
if (requestedIdentity == null || !identityService.validCredentials(requestedClient, requestedIdentity, passwordGrantRequest.password)) {
38+
if (requestedIdentity == null || !identityService.validCredentials(
39+
requestedClient,
40+
requestedIdentity,
41+
passwordGrantRequest.password
42+
)
43+
) {
4044
throw InvalidIdentityException()
4145
}
4246

4347
var requestedScopes = ScopeParser.parseScopes(passwordGrantRequest.scope)
44-
.toSet()
48+
.toSet()
4549

4650
if (passwordGrantRequest.scope == null) {
4751
requestedScopes = requestedClient.clientScopes
@@ -50,14 +54,14 @@ fun GrantingCall.authorize(passwordGrantRequest: PasswordGrantRequest): AccessTo
5054
validateScopes(requestedClient, requestedIdentity, requestedScopes)
5155

5256
val accessToken = converters.accessTokenConverter.convertToToken(
57+
requestedIdentity,
58+
requestedClient.clientId,
59+
requestedScopes,
60+
converters.refreshTokenConverter.convertToToken(
5361
requestedIdentity,
5462
requestedClient.clientId,
55-
requestedScopes,
56-
converters.refreshTokenConverter.convertToToken(
57-
requestedIdentity,
58-
requestedClient.clientId,
59-
requestedScopes
60-
)
63+
requestedScopes
64+
)
6165
)
6266

6367
tokenStore.storeAccessToken(accessToken)
@@ -77,22 +81,22 @@ fun GrantingCall.authorize(authorizationCodeRequest: AuthorizationCodeRequest):
7781
}
7882

7983
val consumeCodeToken = tokenStore.consumeCodeToken(authorizationCodeRequest.code)
80-
?: throw InvalidGrantException()
84+
?: throw InvalidGrantException()
8185

8286

8387
if (consumeCodeToken.redirectUri != authorizationCodeRequest.redirectUri || consumeCodeToken.clientId != authorizationCodeRequest.clientId) {
8488
throw InvalidGrantException()
8589
}
8690

8791
val accessToken = converters.accessTokenConverter.convertToToken(
92+
consumeCodeToken.identity,
93+
consumeCodeToken.clientId,
94+
consumeCodeToken.scopes,
95+
converters.refreshTokenConverter.convertToToken(
8896
consumeCodeToken.identity,
8997
consumeCodeToken.clientId,
90-
consumeCodeToken.scopes,
91-
converters.refreshTokenConverter.convertToToken(
92-
consumeCodeToken.identity,
93-
consumeCodeToken.clientId,
94-
consumeCodeToken.scopes
95-
)
98+
consumeCodeToken.scopes
99+
)
96100
)
97101

98102
tokenStore.storeAccessToken(accessToken)
@@ -106,18 +110,18 @@ fun GrantingCall.authorize(clientCredentialsRequest: ClientCredentialsRequest):
106110
val requestedClient = clientService.clientOf(clientCredentialsRequest.clientId!!) ?: throw InvalidClientException()
107111

108112
val scopes = clientCredentialsRequest.scope
109-
?.let { ScopeParser.parseScopes(it).toSet() }
110-
?: requestedClient.clientScopes
113+
?.let { ScopeParser.parseScopes(it).toSet() }
114+
?: requestedClient.clientScopes
111115

112116
val accessToken = converters.accessTokenConverter.convertToToken(
117+
identity = null,
118+
clientId = clientCredentialsRequest.clientId,
119+
requestedScopes = scopes,
120+
refreshToken = converters.refreshTokenConverter.convertToToken(
113121
identity = null,
114122
clientId = clientCredentialsRequest.clientId,
115-
requestedScopes = scopes,
116-
refreshToken = converters.refreshTokenConverter.convertToToken(
117-
identity = null,
118-
clientId = clientCredentialsRequest.clientId,
119-
requestedScopes = scopes
120-
)
123+
requestedScopes = scopes
124+
)
121125
)
122126

123127
tokenStore.storeAccessToken(accessToken)

0 commit comments

Comments
 (0)