1
1
package nl.myndocs.oauth2
2
2
3
3
import nl.myndocs.oauth2.authenticator.Authorizer
4
+ import nl.myndocs.oauth2.client.AuthorizedGrantType.AUTHORIZATION_CODE
5
+ import nl.myndocs.oauth2.client.AuthorizedGrantType.CLIENT_CREDENTIALS
6
+ import nl.myndocs.oauth2.client.AuthorizedGrantType.PASSWORD
7
+ import nl.myndocs.oauth2.client.AuthorizedGrantType.REFRESH_TOKEN
4
8
import nl.myndocs.oauth2.exception.*
5
- import nl.myndocs.oauth2.identity.UserInfo
9
+ import nl.myndocs.oauth2.identity.TokenInfo
6
10
import nl.myndocs.oauth2.request.*
7
11
import nl.myndocs.oauth2.token.toMap
8
12
9
13
class CallRouter (
10
14
private val tokenService : TokenService ,
11
15
val tokenEndpoint : String ,
12
16
val authorizeEndpoint : String ,
13
- val userInfoEndpoint : String ,
14
- private val userInfoCallback : (UserInfo ) -> Map <String , Any ?>
17
+ val tokenInfoEndpoint : String ,
18
+ private val tokenInfoCallback : (TokenInfo ) -> Map <String , Any ?>
15
19
) {
16
20
companion object {
17
21
const val METHOD_POST = " post"
@@ -28,7 +32,7 @@ class CallRouter(
28
32
when (callContext.path) {
29
33
tokenEndpoint -> routeTokenEndpoint(callContext)
30
34
authorizeEndpoint -> routeAuthorizeEndpoint(callContext, authorizer)
31
- userInfoEndpoint -> routeUserInfoEndpoint (callContext)
35
+ tokenInfoEndpoint -> routeTokenInfoEndpoint (callContext)
32
36
}
33
37
}
34
38
@@ -38,7 +42,7 @@ class CallRouter(
38
42
}
39
43
40
44
try {
41
- val allowedGrantTypes = setOf (" password " , " authorization_code " , " refresh_token " )
45
+ val allowedGrantTypes = setOf (PASSWORD , AUTHORIZATION_CODE , REFRESH_TOKEN , CLIENT_CREDENTIALS )
42
46
val grantType = callContext.formParameters[" grant_type" ]
43
47
? : throw InvalidRequestException (" 'grant_type' not given" )
44
48
@@ -50,6 +54,7 @@ class CallRouter(
50
54
" password" -> routePasswordGrant(callContext, tokenService)
51
55
" authorization_code" -> routeAuthorizationCodeGrant(callContext, tokenService)
52
56
" refresh_token" -> routeRefreshTokenGrant(callContext, tokenService)
57
+ " client_credentials" -> routeClientCredentialsGrant(callContext, tokenService)
53
58
}
54
59
} catch (oauthException: OauthException ) {
55
60
callContext.respondStatus(STATUS_BAD_REQUEST )
@@ -71,6 +76,16 @@ class CallRouter(
71
76
callContext.respondJson(tokenResponse.toMap())
72
77
}
73
78
79
+ fun routeClientCredentialsGrant (callContext : CallContext , tokenService : TokenService ) {
80
+ val tokenResponse = tokenService.authorize(ClientCredentialsRequest (
81
+ callContext.formParameters[" client_id" ],
82
+ callContext.formParameters[" client_secret" ],
83
+ callContext.formParameters[" scope" ]
84
+ ))
85
+
86
+ callContext.respondJson(tokenResponse.toMap())
87
+ }
88
+
74
89
fun routeRefreshTokenGrant (callContext : CallContext , tokenService : TokenService ) {
75
90
val accessToken = tokenService.refresh(
76
91
RefreshTokenRequest (
@@ -193,7 +208,7 @@ class CallRouter(
193
208
}
194
209
}
195
210
196
- private fun routeUserInfoEndpoint (callContext : CallContext ) {
211
+ private fun routeTokenInfoEndpoint (callContext : CallContext ) {
197
212
if (callContext.method.toLowerCase() != METHOD_GET ) {
198
213
return
199
214
}
@@ -207,8 +222,8 @@ class CallRouter(
207
222
208
223
val token = authorization.substring(7 )
209
224
210
- val userInfoCallback = userInfoCallback (tokenService.userInfo (token))
225
+ val tokenInfoCallback = tokenInfoCallback (tokenService.tokenInfo (token))
211
226
212
- callContext.respondJson(userInfoCallback )
227
+ callContext.respondJson(tokenInfoCallback )
213
228
}
214
229
}
0 commit comments