@@ -12,41 +12,27 @@ import nl.myndocs.oauth2.token.AccessToken
1212import nl.myndocs.oauth2.token.CodeToken
1313
1414fun GrantingCall.redirect (redirect : RedirectAuthorizationCodeRequest ): CodeToken {
15- if (redirect.clientId == null ) {
16- throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" client_id" ))
17- }
18-
19- if (redirect.username == null ) {
20- throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" username" ))
21- }
22-
23- if (redirect.password == null ) {
24- throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" password" ))
25- }
26- if (redirect.redirectUri == null ) {
27- throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" redirect_uri" ))
28- }
29-
30- val clientOf = clientService.clientOf(redirect.clientId) ? : throw InvalidClientException ()
15+ checkMissingFields(redirect)
3116
17+ val clientOf = clientService.clientOf(redirect.clientId!! ) ? : throw InvalidClientException ()
3218 if (! clientOf.redirectUris.contains(redirect.redirectUri)) {
3319 throw InvalidGrantException (" invalid 'redirect_uri'" )
3420 }
3521
36- val authorizedGrantType = AuthorizedGrantType .AUTHORIZATION_CODE
37- if (! clientOf.authorizedGrantTypes.contains(authorizedGrantType)) {
38- throw InvalidGrantException (" Authorize not allowed: '$authorizedGrantType '" )
22+ with (AuthorizedGrantType .AUTHORIZATION_CODE ) {
23+ if (! clientOf.authorizedGrantTypes.contains(this )) {
24+ throw InvalidGrantException (" Authorize not allowed: '$this '" )
25+ }
3926 }
4027
41- val identityOf = identityService.identityOf(clientOf, redirect.username) ? : throw InvalidIdentityException ()
42- val validIdentity = identityService.validCredentials(clientOf, identityOf, redirect.password)
28+ val identityOf = identityService.identityOf(clientOf, redirect.username!! ) ? : throw InvalidIdentityException ()
4329
30+ val validIdentity = identityService.validCredentials(clientOf, identityOf, redirect.password!! )
4431 if (! validIdentity) {
4532 throw InvalidIdentityException ()
4633 }
4734
4835 var requestedScopes = ScopeParser .parseScopes(redirect.scope)
49-
5036 if (redirect.scope == null ) {
5137 requestedScopes = clientOf.clientScopes
5238 }
@@ -56,7 +42,7 @@ fun GrantingCall.redirect(redirect: RedirectAuthorizationCodeRequest): CodeToken
5642 val codeToken = converters.codeTokenConverter.convertToToken(
5743 identityOf,
5844 clientOf.clientId,
59- redirect.redirectUri,
45+ redirect.redirectUri!! ,
6046 requestedScopes
6147 )
6248
@@ -66,42 +52,27 @@ fun GrantingCall.redirect(redirect: RedirectAuthorizationCodeRequest): CodeToken
6652}
6753
6854fun GrantingCall.redirect (redirect : RedirectTokenRequest ): AccessToken {
69- if (redirect.clientId == null ) {
70- throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" client_id" ))
71- }
72-
73- if (redirect.username == null ) {
74- throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" username" ))
75- }
76-
77- if (redirect.password == null ) {
78- throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" password" ))
79- }
80- if (redirect.redirectUri == null ) {
81- throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" redirect_uri" ))
82- }
83-
84- val clientOf = clientService.clientOf(redirect.clientId) ? : throw InvalidClientException ()
55+ checkMissingFields(redirect)
8556
57+ val clientOf = clientService.clientOf(redirect.clientId!! ) ? : throw InvalidClientException ()
8658 if (! clientOf.redirectUris.contains(redirect.redirectUri)) {
8759 throw InvalidGrantException (" invalid 'redirect_uri'" )
8860 }
8961
90- val authorizedGrantType = AuthorizedGrantType .IMPLICIT
91- if (! clientOf.authorizedGrantTypes.contains(authorizedGrantType)) {
92- throw InvalidGrantException (" Authorize not allowed: '$authorizedGrantType '" )
62+ with (AuthorizedGrantType .IMPLICIT ) {
63+ if (! clientOf.authorizedGrantTypes.contains(this )) {
64+ throw InvalidGrantException (" Authorize not allowed: '$this '" )
65+ }
9366 }
9467
95- val identityOf = identityService.identityOf(clientOf, redirect.username) ? : throw InvalidIdentityException ()
96-
97- val validIdentity = identityService.validCredentials(clientOf, identityOf, redirect.password)
68+ val identityOf = identityService.identityOf(clientOf, redirect.username!! ) ? : throw InvalidIdentityException ()
9869
70+ val validIdentity = identityService.validCredentials(clientOf, identityOf, redirect.password!! )
9971 if (! validIdentity) {
10072 throw InvalidIdentityException ()
10173 }
10274
10375 var requestedScopes = ScopeParser .parseScopes(redirect.scope)
104-
10576 if (redirect.scope == null ) {
10677 // @TODO: This behavior is not in the spec and should be configurable https://tools.ietf.org/html/rfc6749#section-3.3
10778 requestedScopes = clientOf.clientScopes
@@ -120,3 +91,26 @@ fun GrantingCall.redirect(redirect: RedirectTokenRequest): AccessToken {
12091
12192 return accessToken
12293}
94+
95+ private fun throwMissingField (field : String ): Nothing =
96+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(field))
97+
98+ private fun checkMissingFields (redirect : RedirectTokenRequest ) = with (redirect) {
99+ when {
100+ clientId == null -> throwMissingField(" client_id" )
101+ username == null -> throwMissingField(" username" )
102+ password == null -> throwMissingField(" password" )
103+ redirectUri == null -> throwMissingField(" redirect_uri" )
104+ else -> this
105+ }
106+ }
107+
108+ private fun checkMissingFields (redirect : RedirectAuthorizationCodeRequest ) = with (redirect) {
109+ when {
110+ clientId == null -> throwMissingField(" client_id" )
111+ username == null -> throwMissingField(" username" )
112+ password == null -> throwMissingField(" password" )
113+ redirectUri == null -> throwMissingField(" redirect_uri" )
114+ else -> this
115+ }
116+ }
0 commit comments