Skip to content

Commit b3ff7f2

Browse files
authored
Merge pull request #84 from YektaDev/develop
Simplifications and cleaning up unused imports
2 parents 8a1d026 + 04602c9 commit b3ff7f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+327
-351
lines changed

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

Lines changed: 32 additions & 39 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,24 +44,21 @@ 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

60-
val granterMap = granters
61-
.map {
62-
val granter = grantingCall.it()
63-
granter.grantType to granter
64-
}
65-
.toMap()
58+
val granterMap = granters.associate {
59+
val granter = grantingCall.it()
60+
granter.grantType to granter
61+
}
6662

6763
val allowedGrantTypes = granterMap.keys
6864

@@ -78,19 +74,19 @@ class CallRouter(
7874
}
7975

8076
fun routeAuthorizationCodeRedirect(
81-
callContext: CallContext,
82-
credentials: Credentials?
77+
callContext: CallContext,
78+
credentials: Credentials?
8379
): RedirectRouterResponse {
8480
val queryParameters = callContext.queryParameters
8581
try {
8682
val redirect = grantingCallFactory(callContext).redirect(
87-
RedirectAuthorizationCodeRequest(
88-
queryParameters["client_id"],
89-
queryParameters["redirect_uri"],
90-
credentials?.username,
91-
credentials?.password,
92-
queryParameters["scope"]
93-
)
83+
RedirectAuthorizationCodeRequest(
84+
queryParameters["client_id"],
85+
queryParameters["redirect_uri"],
86+
credentials?.username,
87+
credentials?.password,
88+
queryParameters["scope"]
89+
)
9490
)
9591

9692
var stateQueryParameter = ""
@@ -109,33 +105,31 @@ class CallRouter(
109105
}
110106
}
111107

112-
113108
fun routeAccessTokenRedirect(
114-
callContext: CallContext,
115-
credentials: Credentials?
109+
callContext: CallContext,
110+
credentials: Credentials?
116111
): RedirectRouterResponse {
117112
val queryParameters = callContext.queryParameters
118113

119114
try {
120115
val redirect = grantingCallFactory(callContext).redirect(
121-
RedirectTokenRequest(
122-
queryParameters["client_id"],
123-
queryParameters["redirect_uri"],
124-
credentials?.username,
125-
credentials?.password,
126-
queryParameters["scope"]
127-
)
116+
RedirectTokenRequest(
117+
queryParameters["client_id"],
118+
queryParameters["redirect_uri"],
119+
credentials?.username,
120+
credentials?.password,
121+
queryParameters["scope"]
122+
)
128123
)
129124

130125
var stateQueryParameter = ""
131-
132126
if (queryParameters["state"] != null) {
133127
stateQueryParameter = "&state=" + queryParameters["state"]
134128
}
135129

136130
callContext.redirect(
137-
queryParameters["redirect_uri"] + "#access_token=${redirect.accessToken}" +
138-
"&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"
139133
)
140134

141135
return RedirectRouterResponse(true)
@@ -153,7 +147,7 @@ class CallRouter(
153147
}
154148

155149
val responseType = callContext.queryParameters["response_type"]
156-
?: throw InvalidRequestException("'response_type' not given")
150+
?: throw InvalidRequestException("'response_type' not given")
157151

158152
return when (responseType) {
159153
"code" -> routeAuthorizationCodeRedirect(callContext, credentials)
@@ -185,7 +179,6 @@ class CallRouter(
185179
}
186180

187181
val token = authorization.substring(7)
188-
189182
val tokenInfoCallback = tokenInfoCallback(grantingCallFactory(callContext).tokenInfo(token))
190183

191184
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")
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
package nl.myndocs.oauth2.exception
22

3-
fun OauthException.toMap(): Map<String, String> {
4-
5-
val mutableMapOf = mutableMapOf<String, String>(
6-
"error" to this.error.errorName
7-
)
8-
9-
if (this.errorDescription != null) {
10-
mutableMapOf["error_description"] = this.errorDescription
3+
fun OauthException.toMap(): Map<String, String> = with(mutableMapOf("error" to error.errorName)) {
4+
if (errorDescription != null) {
5+
this["error_description"] = errorDescription
116
}
12-
13-
return mutableMapOf.toMap()
7+
toMap()
148
}

0 commit comments

Comments
 (0)