11package nl.myndocs.oauth2
22
3- import nl.myndocs.oauth2.authenticator.Authorizer
3+ import nl.myndocs.oauth2.authenticator.Credentials
44import nl.myndocs.oauth2.exception.*
55import nl.myndocs.oauth2.grant.Granter
66import nl.myndocs.oauth2.grant.GrantingCall
@@ -11,6 +11,8 @@ import nl.myndocs.oauth2.request.CallContext
1111import nl.myndocs.oauth2.request.RedirectAuthorizationCodeRequest
1212import nl.myndocs.oauth2.request.RedirectTokenRequest
1313import nl.myndocs.oauth2.request.headerCaseInsensitive
14+ import nl.myndocs.oauth2.router.RedirectRouter
15+ import nl.myndocs.oauth2.router.RedirectRouterResponse
1416
1517class CallRouter (
1618 val tokenEndpoint : String ,
@@ -19,7 +21,7 @@ class CallRouter(
1921 private val tokenInfoCallback : (TokenInfo ) -> Map <String , Any ?>,
2022 private val granters : List <GrantingCall .() - > Granter >,
2123 private val grantingCallFactory : (CallContext ) -> GrantingCall
22- ) {
24+ ) : RedirectRouter {
2325 companion object {
2426 const val METHOD_POST = " post"
2527 const val METHOD_GET = " get"
@@ -29,16 +31,21 @@ class CallRouter(
2931
3032 }
3133
32- fun route (
33- callContext : CallContext ,
34- authorizer : Authorizer ) {
34+ fun route (callContext : CallContext ) {
3535 when (callContext.path) {
3636 tokenEndpoint -> routeTokenEndpoint(callContext)
37- authorizeEndpoint -> routeAuthorizeEndpoint(callContext, authorizer)
3837 tokenInfoEndpoint -> routeTokenInfoEndpoint(callContext)
3938 }
4039 }
4140
41+ override fun route (callContext : CallContext , credentials : Credentials ? ): RedirectRouterResponse {
42+ return when (callContext.path) {
43+ authorizeEndpoint -> routeAuthorizeEndpoint(callContext, credentials)
44+ else -> throw NoRoutesFoundException (" Route '${callContext.path} ' not found" )
45+ }
46+ }
47+
48+
4249 private fun routeTokenEndpoint (callContext : CallContext ) {
4350 if (callContext.method.toLowerCase() != METHOD_POST ) {
4451 return
@@ -72,21 +79,18 @@ class CallRouter(
7279
7380 fun routeAuthorizationCodeRedirect (
7481 callContext : CallContext ,
75- authorizer : Authorizer
76- ) {
82+ credentials : Credentials ?
83+ ): RedirectRouterResponse {
7784 val queryParameters = callContext.queryParameters
78- val credentials = authorizer.extractCredentials()
7985 try {
8086 val redirect = grantingCallFactory(callContext).redirect(
8187 RedirectAuthorizationCodeRequest (
8288 queryParameters[" client_id" ],
8389 queryParameters[" redirect_uri" ],
84- credentials?.username ? : " " ,
85- credentials?.password ? : " " ,
90+ credentials?.username,
91+ credentials?.password,
8692 queryParameters[" scope" ]
87- ),
88- authorizer.authenticator(),
89- authorizer.scopesVerifier()
93+ )
9094 )
9195
9296 var stateQueryParameter = " "
@@ -96,31 +100,31 @@ class CallRouter(
96100 }
97101
98102 callContext.redirect(queryParameters[" redirect_uri" ] + " ?code=${redirect.codeToken}$stateQueryParameter " )
103+
104+ return RedirectRouterResponse (true )
99105 } catch (unverifiedIdentityException: InvalidIdentityException ) {
100106 callContext.respondStatus(STATUS_UNAUTHORIZED )
101- authorizer.failedAuthentication()
107+
108+ return RedirectRouterResponse (false )
102109 }
103110 }
104111
105112
106113 fun routeAccessTokenRedirect (
107114 callContext : CallContext ,
108- authorizer : Authorizer
109- ) {
115+ credentials : Credentials ?
116+ ): RedirectRouterResponse {
110117 val queryParameters = callContext.queryParameters
111- val credentials = authorizer.extractCredentials()
112118
113119 try {
114120 val redirect = grantingCallFactory(callContext).redirect(
115121 RedirectTokenRequest (
116122 queryParameters[" client_id" ],
117123 queryParameters[" redirect_uri" ],
118- credentials?.username ? : " " ,
119- credentials?.password ? : " " ,
124+ credentials?.username,
125+ credentials?.password,
120126 queryParameters[" scope" ]
121- ),
122- authorizer.authenticator(),
123- authorizer.scopesVerifier()
127+ )
124128 )
125129
126130 var stateQueryParameter = " "
@@ -134,33 +138,33 @@ class CallRouter(
134138 " &token_type=bearer&expires_in=${redirect.expiresIn()}$stateQueryParameter "
135139 )
136140
141+ return RedirectRouterResponse (true )
137142 } catch (unverifiedIdentityException: InvalidIdentityException ) {
138- authorizer.failedAuthentication()
139143 callContext.respondStatus(STATUS_UNAUTHORIZED )
144+
145+ return RedirectRouterResponse (false )
140146 }
141147 }
142148
143- private fun routeAuthorizeEndpoint (callContext : CallContext , authorizer : Authorizer ) {
149+ private fun routeAuthorizeEndpoint (callContext : CallContext , credentials : Credentials ? ): RedirectRouterResponse {
144150 try {
145- if (callContext.method.toLowerCase() != METHOD_GET ) {
146- return
151+ if (! arrayOf( METHOD_GET , METHOD_POST ).contains( callContext.method.toLowerCase()) ) {
152+ return RedirectRouterResponse ( false )
147153 }
148154
149- val allowedResponseTypes = setOf (" code" , " token" )
150155 val responseType = callContext.queryParameters[" response_type" ]
151156 ? : throw InvalidRequestException (" 'response_type' not given" )
152157
153- if (! allowedResponseTypes.contains(responseType)) {
154- throw InvalidGrantException (" 'grant_type' with value '$responseType ' not allowed" )
155- }
156-
157- when (responseType) {
158- " code" -> routeAuthorizationCodeRedirect(callContext, authorizer)
159- " token" -> routeAccessTokenRedirect(callContext, authorizer)
158+ return when (responseType) {
159+ " code" -> routeAuthorizationCodeRedirect(callContext, credentials)
160+ " token" -> routeAccessTokenRedirect(callContext, credentials)
161+ else -> throw InvalidGrantException (" 'grant_type' with value '$responseType ' not allowed" )
160162 }
161163 } catch (oauthException: OauthException ) {
162164 callContext.respondStatus(STATUS_BAD_REQUEST )
163165 callContext.respondJson(oauthException.toMap())
166+
167+ return RedirectRouterResponse (false )
164168 }
165169 }
166170
0 commit comments