11package nl.myndocs.oauth2
22
33import nl.myndocs.oauth2.client.ClientService
4- import nl.myndocs.oauth2.exception.InvalidClientException
5- import nl.myndocs.oauth2.exception.InvalidGrantException
6- import nl.myndocs.oauth2.exception.InvalidIdentityException
7- import nl.myndocs.oauth2.exception.InvalidScopeException
4+ import nl.myndocs.oauth2.exception.*
85import nl.myndocs.oauth2.identity.IdentityService
96import nl.myndocs.oauth2.request.*
107import nl.myndocs.oauth2.response.TokenResponse
@@ -24,6 +21,7 @@ class TokenService(
2421 private val refreshTokenConverter : RefreshTokenConverter ,
2522 private val codeTokenConverter : CodeTokenConverter
2623) {
24+ private val INVALID_REQUEST_FIELD_MESSAGE = " '%s' field is missing"
2725 /* *
2826 * @throws InvalidIdentityException
2927 * @throws InvalidClientException
@@ -32,28 +30,40 @@ class TokenService(
3230 fun authorize (passwordGrantRequest : PasswordGrantRequest ): TokenResponse {
3331 throwExceptionIfUnverifiedClient(passwordGrantRequest)
3432
33+ if (passwordGrantRequest.username == null ) {
34+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" username" ))
35+ }
36+
37+ if (passwordGrantRequest.password == null ) {
38+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" password" ))
39+ }
40+
3541 val requestedClient = clientService.clientOf(
36- passwordGrantRequest.clientId
42+ passwordGrantRequest.clientId!!
3743 )!!
3844 val requestedIdentity = identityService.identityOf(
3945 requestedClient, passwordGrantRequest.username
4046 )
4147
42- if (requestedIdentity == null || ! identityService.validIdentity (requestedClient, requestedIdentity, passwordGrantRequest.password)) {
48+ if (requestedIdentity == null || ! identityService.validCredentials (requestedClient, requestedIdentity, passwordGrantRequest.password)) {
4349 throw InvalidIdentityException ()
4450 }
4551
4652 var requestedScopes = ScopeParser .parseScopes(passwordGrantRequest.scope)
4753 .toSet()
4854
49- if (requestedScopes.isEmpty() ) {
55+ if (passwordGrantRequest.scope == null ) {
5056 requestedScopes = requestedClient.clientScopes
5157 }
5258
53- val clientDiffScopes = diffScopes (requestedClient.clientScopes, requestedScopes)
59+ val scopesAllowed = scopesAllowed (requestedClient.clientScopes, requestedScopes)
5460
55- if (clientDiffScopes.isNotEmpty()) {
56- throw InvalidScopeException (clientDiffScopes)
61+ if (! scopesAllowed) {
62+ throw InvalidScopeException (requestedScopes.minus(requestedClient.clientScopes))
63+ }
64+
65+ if (! identityService.validScopes(requestedClient, requestedIdentity, requestedScopes)) {
66+ throw InvalidScopeException (requestedScopes)
5767 }
5868
5969 val accessToken = accessTokenConverter.convertToToken(
@@ -75,6 +85,14 @@ class TokenService(
7585 fun authorize (authorizationCodeRequest : AuthorizationCodeRequest ): TokenResponse {
7686 throwExceptionIfUnverifiedClient(authorizationCodeRequest)
7787
88+ if (authorizationCodeRequest.code == null ) {
89+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" code" ))
90+ }
91+
92+ if (authorizationCodeRequest.redirectUri == null ) {
93+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" redirect_uri" ))
94+ }
95+
7896 val consumeCodeToken = tokenStore.consumeCodeToken(authorizationCodeRequest.code)
7997 ? : throw InvalidGrantException ()
8098
@@ -102,6 +120,10 @@ class TokenService(
102120 fun refresh (refreshTokenRequest : RefreshTokenRequest ): TokenResponse {
103121 throwExceptionIfUnverifiedClient(refreshTokenRequest)
104122
123+ if (refreshTokenRequest.refreshToken == null ) {
124+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" refresh_token" ))
125+ }
126+
105127 val refreshToken = tokenStore.refreshToken(refreshTokenRequest.refreshToken) ? : throw InvalidGrantException ()
106128
107129 val accessToken = accessTokenConverter.convertToToken(
@@ -117,20 +139,40 @@ class TokenService(
117139 }
118140
119141 fun redirect (redirect : RedirectAuthorizationCodeRequest ): CodeToken {
142+ if (redirect.clientId == null ) {
143+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" client_id" ))
144+ }
145+
146+ if (redirect.username == null ) {
147+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" username" ))
148+ }
149+
150+ if (redirect.password == null ) {
151+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" password" ))
152+ }
153+ if (redirect.redirectUri == null ) {
154+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" redirect_uri" ))
155+ }
156+
120157 val clientOf = clientService.clientOf(redirect.clientId) ? : throw InvalidClientException ()
158+
159+ if (! clientOf.redirectUris.contains(redirect.redirectUri)) {
160+ throw InvalidGrantException (" invalid 'redirect_uri'" )
161+ }
162+
121163 val identityOf = identityService.identityOf(clientOf, redirect.username) ? : throw InvalidIdentityException ()
122164
123- var validIdentity = identityService.validIdentity (clientOf, identityOf, redirect.password)
165+ var validIdentity = identityService.validCredentials (clientOf, identityOf, redirect.password)
124166
125167 if (! validIdentity) {
126168 throw InvalidIdentityException ()
127169 }
128170
129171 val requestedScopes = ScopeParser .parseScopes(redirect.scope)
130172
131- val diffScopes = diffScopes (clientOf.clientScopes, requestedScopes)
132- if (diffScopes.isNotEmpty() ) {
133- throw InvalidScopeException (diffScopes )
173+ val scopesAllowed = scopesAllowed (clientOf.clientScopes, requestedScopes)
174+ if (! scopesAllowed ) {
175+ throw InvalidScopeException (requestedScopes.minus(clientOf.clientScopes) )
134176 }
135177
136178 val codeToken = codeTokenConverter.convertToToken(
@@ -147,19 +189,23 @@ class TokenService(
147189 }
148190
149191 private fun throwExceptionIfUnverifiedClient (clientRequest : ClientRequest ) {
150- val client = clientService.clientOf(clientRequest.clientId) ? : throw InvalidClientException ()
192+ if (clientRequest.clientId == null ) {
193+ throw InvalidRequestException (INVALID_REQUEST_FIELD_MESSAGE .format(" client_id" ))
194+ }
151195
152- if (! clientService.validClient(client, clientRequest.clientSecret) ) {
153- throw InvalidClientException ( )
196+ if (clientRequest.clientSecret == null ) {
197+ throw InvalidRequestException ( INVALID_REQUEST_FIELD_MESSAGE .format( " client_secret " ) )
154198 }
155- }
156199
157- private fun diffScopes (allowedScopes : Set <String >, validationScopes : Set <String >): Set <String > {
158- if (allowedScopes.containsAll(validationScopes)) {
159- return validationScopes.minus(allowedScopes)
200+ val client = clientService.clientOf(clientRequest.clientId!! ) ? : throw InvalidClientException ()
201+
202+ if (! clientService.validClient(client, clientRequest.clientSecret!! )) {
203+ throw InvalidClientException ()
160204 }
205+ }
161206
162- return setOf ()
207+ private fun scopesAllowed (clientScopes : Set <String >, requestedScopes : Set <String >): Boolean {
208+ return clientScopes.containsAll(requestedScopes)
163209 }
164210
165211 private fun AccessToken.toTokenResponse () = TokenResponse (
0 commit comments