@@ -2,8 +2,10 @@ package nl.myndocs.oauth2
22
33import nl.myndocs.oauth2.authenticator.Authenticator
44import nl.myndocs.oauth2.authenticator.IdentityScopeVerifier
5+ import nl.myndocs.oauth2.client.Client
56import nl.myndocs.oauth2.client.ClientService
67import nl.myndocs.oauth2.exception.*
8+ import nl.myndocs.oauth2.identity.Identity
79import nl.myndocs.oauth2.identity.IdentityService
810import nl.myndocs.oauth2.identity.UserInfo
911import nl.myndocs.oauth2.request.*
@@ -59,15 +61,7 @@ class Oauth2TokenService(
5961 requestedScopes = requestedClient.clientScopes
6062 }
6163
62- val scopesAllowed = scopesAllowed(requestedClient.clientScopes, requestedScopes)
63-
64- if (! scopesAllowed) {
65- throw InvalidScopeException (requestedScopes.minus(requestedClient.clientScopes))
66- }
67-
68- if (! identityService.validScopes(requestedClient, requestedIdentity, requestedScopes)) {
69- throw InvalidScopeException (requestedScopes)
70- }
64+ validateScopes(requestedClient, requestedIdentity, requestedScopes)
7165
7266 val accessToken = accessTokenConverter.convertToToken(
7367 requestedIdentity.username,
@@ -186,16 +180,7 @@ class Oauth2TokenService(
186180 requestedScopes = clientOf.clientScopes
187181 }
188182
189- val scopesAllowed = identityScopeVerifier?.validScopes(clientOf, identityOf, requestedScopes)
190- ? : scopesAllowed(clientOf.clientScopes, requestedScopes)
191-
192- if (! scopesAllowed) {
193- throw InvalidScopeException (requestedScopes.minus(clientOf.clientScopes))
194- }
195-
196- if (! identityService.validScopes(clientOf, identityOf, requestedScopes)) {
197- throw InvalidScopeException (requestedScopes)
198- }
183+ validateScopes(clientOf, identityOf, requestedScopes, identityScopeVerifier)
199184
200185 val codeToken = codeTokenConverter.convertToToken(
201186 identityOf.username,
@@ -250,15 +235,7 @@ class Oauth2TokenService(
250235 requestedScopes = clientOf.clientScopes
251236 }
252237
253- val scopesAllowed = identityScopeVerifier?.validScopes(clientOf, identityOf, requestedScopes)
254- ? : scopesAllowed(clientOf.clientScopes, requestedScopes)
255- if (! scopesAllowed) {
256- throw InvalidScopeException (requestedScopes.minus(clientOf.clientScopes))
257- }
258-
259- if (! identityService.validScopes(clientOf, identityOf, requestedScopes)) {
260- throw InvalidScopeException (requestedScopes)
261- }
238+ validateScopes(clientOf, identityOf, requestedScopes, identityScopeVerifier)
262239
263240 val accessToken = accessTokenConverter.convertToToken(
264241 identityOf.username,
@@ -272,6 +249,25 @@ class Oauth2TokenService(
272249 return accessToken
273250 }
274251
252+ private fun validateScopes (
253+ client : Client ,
254+ identity : Identity ,
255+ requestedScopes : Set <String >,
256+ identityScopeVerifier : IdentityScopeVerifier ? = null) {
257+ val scopesAllowed = scopesAllowed(client.clientScopes, requestedScopes)
258+ if (! scopesAllowed) {
259+ throw InvalidScopeException (requestedScopes.minus(client.clientScopes))
260+ }
261+
262+ val allowedScopes = identityScopeVerifier?.allowedScopes(client, identity, requestedScopes)
263+ ? : identityService.allowedScopes(client, identity, requestedScopes)
264+
265+ val ivalidScopes = requestedScopes.minus(allowedScopes)
266+ if (ivalidScopes.isNotEmpty()) {
267+ throw InvalidScopeException (ivalidScopes)
268+ }
269+ }
270+
275271 override fun userInfo (accessToken : String ): UserInfo {
276272 val storedAccessToken = tokenStore.accessToken(accessToken)!!
277273 val client = clientService.clientOf(storedAccessToken.clientId)!!
0 commit comments