Skip to content

Commit c777ebf

Browse files
committed
added universal OAuth exception handling
1 parent 48b857e commit c777ebf

File tree

13 files changed

+161
-1
lines changed

13 files changed

+161
-1
lines changed

openid-connect-server-webapp/src/main/webapp/WEB-INF/application-context.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@
162162

163163
<import resource="authz-config.xml" />
164164

165+
<bean id="oauth2ExceptionTranslator" class="org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator" />
166+
165167
<bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
166168
<property name="authenticationManager" ref="clientAuthenticationManager" />
167169
<property name="filterProcessesUrl" value="/token"/>

openid-connect-server/src/main/java/org/mitre/oauth2/web/IntrospectionEndpoint.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@
3535
import org.slf4j.LoggerFactory;
3636
import org.springframework.beans.factory.annotation.Autowired;
3737
import org.springframework.http.HttpStatus;
38+
import org.springframework.http.ResponseEntity;
3839
import org.springframework.security.access.prepost.PreAuthorize;
3940
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
41+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
42+
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
43+
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
4044
import org.springframework.stereotype.Controller;
4145
import org.springframework.ui.Model;
46+
import org.springframework.web.bind.annotation.ExceptionHandler;
4247
import org.springframework.web.bind.annotation.RequestMapping;
4348
import org.springframework.web.bind.annotation.RequestParam;
4449

@@ -63,6 +68,9 @@ public class IntrospectionEndpoint {
6368
@Autowired
6469
private UserInfoService userInfoService;
6570

71+
@Autowired
72+
private WebResponseExceptionTranslator providerExceptionHandler;
73+
6674
/**
6775
* Logger for this class
6876
*/
@@ -150,5 +158,12 @@ public String verify(@RequestParam("token") String tokenValue,
150158
}
151159

152160
}
161+
162+
@ExceptionHandler(OAuth2Exception.class)
163+
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
164+
logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
165+
return providerExceptionHandler.translate(e);
166+
}
167+
153168

154169
}

openid-connect-server/src/main/java/org/mitre/oauth2/web/ScopeAPI.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030
import org.slf4j.LoggerFactory;
3131
import org.springframework.beans.factory.annotation.Autowired;
3232
import org.springframework.http.HttpStatus;
33+
import org.springframework.http.ResponseEntity;
3334
import org.springframework.security.access.prepost.PreAuthorize;
35+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
36+
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
3437
import org.springframework.stereotype.Controller;
3538
import org.springframework.ui.ModelMap;
39+
import org.springframework.web.bind.annotation.ExceptionHandler;
3640
import org.springframework.web.bind.annotation.PathVariable;
3741
import org.springframework.web.bind.annotation.RequestBody;
3842
import org.springframework.web.bind.annotation.RequestMapping;
@@ -52,6 +56,9 @@ public class ScopeAPI {
5256
@Autowired
5357
private SystemScopeService scopeService;
5458

59+
@Autowired
60+
private WebResponseExceptionTranslator providerExceptionHandler;
61+
5562
/**
5663
* Logger for this class
5764
*/
@@ -177,4 +184,9 @@ public String deleteScope(@PathVariable("id") Long id, ModelMap m) {
177184
}
178185
}
179186

187+
@ExceptionHandler(OAuth2Exception.class)
188+
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
189+
logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
190+
return providerExceptionHandler.translate(e);
191+
}
180192
}

openid-connect-server/src/main/java/org/mitre/oauth2/web/TokenAPI.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@
3333
import org.slf4j.LoggerFactory;
3434
import org.springframework.beans.factory.annotation.Autowired;
3535
import org.springframework.http.HttpStatus;
36+
import org.springframework.http.ResponseEntity;
3637
import org.springframework.security.access.prepost.PreAuthorize;
38+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
39+
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
3740
import org.springframework.stereotype.Controller;
3841
import org.springframework.ui.ModelMap;
42+
import org.springframework.web.bind.annotation.ExceptionHandler;
3943
import org.springframework.web.bind.annotation.PathVariable;
4044
import org.springframework.web.bind.annotation.RequestMapping;
4145
import org.springframework.web.bind.annotation.RequestMethod;
@@ -59,6 +63,9 @@ public class TokenAPI {
5963
@Autowired
6064
private OIDCTokenService oidcTokenService;
6165

66+
@Autowired
67+
private WebResponseExceptionTranslator providerExceptionHandler;
68+
6269
/**
6370
* Logger for this class
6471
*/
@@ -238,4 +245,9 @@ public String deleteRefreshTokenById(@PathVariable("id") Long id, ModelMap m, Pr
238245
}
239246
}
240247

248+
@ExceptionHandler(OAuth2Exception.class)
249+
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
250+
logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
251+
return providerExceptionHandler.translate(e);
252+
}
241253
}

openid-connect-server/src/main/java/org/mitre/openid/connect/web/ApprovedSiteAPI.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@
3232
import org.slf4j.LoggerFactory;
3333
import org.springframework.beans.factory.annotation.Autowired;
3434
import org.springframework.http.HttpStatus;
35+
import org.springframework.http.ResponseEntity;
3536
import org.springframework.security.access.prepost.PreAuthorize;
37+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
38+
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
3639
import org.springframework.stereotype.Controller;
3740
import org.springframework.ui.ModelMap;
41+
import org.springframework.web.bind.annotation.ExceptionHandler;
3842
import org.springframework.web.bind.annotation.PathVariable;
3943
import org.springframework.web.bind.annotation.RequestMapping;
4044
import org.springframework.web.bind.annotation.RequestMethod;
@@ -52,7 +56,10 @@ public class ApprovedSiteAPI {
5256
private ApprovedSiteService approvedSiteService;
5357

5458
@Autowired
55-
OAuth2TokenEntityService tokenServices;
59+
private OAuth2TokenEntityService tokenServices;
60+
61+
@Autowired
62+
private WebResponseExceptionTranslator providerExceptionHandler;
5663

5764
/**
5865
* Logger for this class
@@ -124,4 +131,10 @@ public String getApprovedSite(@PathVariable("id") Long id, ModelMap m, Principal
124131
}
125132

126133
}
134+
135+
@ExceptionHandler(OAuth2Exception.class)
136+
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
137+
logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
138+
return providerExceptionHandler.translate(e);
139+
}
127140
}

openid-connect-server/src/main/java/org/mitre/openid/connect/web/BlacklistAPI.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@
3131
import org.slf4j.LoggerFactory;
3232
import org.springframework.beans.factory.annotation.Autowired;
3333
import org.springframework.http.HttpStatus;
34+
import org.springframework.http.ResponseEntity;
3435
import org.springframework.security.access.prepost.PreAuthorize;
36+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
37+
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
3538
import org.springframework.stereotype.Controller;
3639
import org.springframework.ui.ModelMap;
40+
import org.springframework.web.bind.annotation.ExceptionHandler;
3741
import org.springframework.web.bind.annotation.PathVariable;
3842
import org.springframework.web.bind.annotation.RequestBody;
3943
import org.springframework.web.bind.annotation.RequestMapping;
@@ -61,6 +65,9 @@ public class BlacklistAPI {
6165
*/
6266
private static final Logger logger = LoggerFactory.getLogger(BlacklistAPI.class);
6367

68+
@Autowired
69+
private WebResponseExceptionTranslator providerExceptionHandler;
70+
6471
private Gson gson = new Gson();
6572
private JsonParser parser = new JsonParser();
6673

@@ -202,4 +209,10 @@ public String getBlacklistedSite(@PathVariable("id") Long id, ModelMap m) {
202209
}
203210

204211
}
212+
213+
@ExceptionHandler(OAuth2Exception.class)
214+
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
215+
logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
216+
return providerExceptionHandler.translate(e);
217+
}
205218
}

openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@
3434
import org.slf4j.LoggerFactory;
3535
import org.springframework.beans.factory.annotation.Autowired;
3636
import org.springframework.http.HttpStatus;
37+
import org.springframework.http.ResponseEntity;
3738
import org.springframework.security.access.prepost.PreAuthorize;
3839
import org.springframework.security.core.Authentication;
3940
import org.springframework.security.core.GrantedAuthority;
41+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
42+
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
4043
import org.springframework.stereotype.Controller;
4144
import org.springframework.ui.Model;
45+
import org.springframework.web.bind.annotation.ExceptionHandler;
4246
import org.springframework.web.bind.annotation.PathVariable;
4347
import org.springframework.web.bind.annotation.RequestBody;
4448
import org.springframework.web.bind.annotation.RequestMapping;
@@ -71,6 +75,9 @@ public class ClientAPI {
7175
@Autowired
7276
private UserInfoService userInfoService;
7377

78+
@Autowired
79+
private WebResponseExceptionTranslator providerExceptionHandler;
80+
7481
private JsonParser parser = new JsonParser();
7582

7683
private Gson gson = new GsonBuilder()
@@ -376,4 +383,10 @@ private boolean isAdmin(Authentication auth) {
376383
}
377384
return false;
378385
}
386+
387+
@ExceptionHandler(OAuth2Exception.class)
388+
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
389+
logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
390+
return providerExceptionHandler.translate(e);
391+
}
379392
}

openid-connect-server/src/main/java/org/mitre/openid/connect/web/DataAPI.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
3333
import org.springframework.beans.factory.annotation.Autowired;
34+
import org.springframework.http.ResponseEntity;
3435
import org.springframework.security.access.prepost.PreAuthorize;
36+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
37+
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
3538
import org.springframework.stereotype.Controller;
3639
import org.springframework.ui.Model;
40+
import org.springframework.web.bind.annotation.ExceptionHandler;
3741
import org.springframework.web.bind.annotation.RequestMapping;
3842
import org.springframework.web.bind.annotation.RequestMethod;
3943

@@ -72,6 +76,9 @@ public class DataAPI {
7276
@Autowired
7377
private MITREidDataService_1_1 dataService_1_2;
7478

79+
@Autowired
80+
private WebResponseExceptionTranslator providerExceptionHandler;
81+
7582
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
7683
public String importData(Reader in, Model m) throws IOException {
7784

@@ -140,5 +147,10 @@ public void exportData(HttpServletResponse resp, Principal prin) throws IOExcept
140147
}
141148
}
142149

150+
@ExceptionHandler(OAuth2Exception.class)
151+
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
152+
logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
153+
return providerExceptionHandler.translate(e);
154+
}
143155

144156
}

openid-connect-server/src/main/java/org/mitre/openid/connect/web/DynamicClientRegistrationEndpoint.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,15 @@
4444
import org.slf4j.LoggerFactory;
4545
import org.springframework.beans.factory.annotation.Autowired;
4646
import org.springframework.http.HttpStatus;
47+
import org.springframework.http.ResponseEntity;
4748
import org.springframework.security.access.prepost.PreAuthorize;
49+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
4850
import org.springframework.security.oauth2.provider.OAuth2Authentication;
4951
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
52+
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
5053
import org.springframework.stereotype.Controller;
5154
import org.springframework.ui.Model;
55+
import org.springframework.web.bind.annotation.ExceptionHandler;
5256
import org.springframework.web.bind.annotation.PathVariable;
5357
import org.springframework.web.bind.annotation.RequestBody;
5458
import org.springframework.web.bind.annotation.RequestMapping;
@@ -85,6 +89,9 @@ public class DynamicClientRegistrationEndpoint {
8589
@Autowired
8690
private OIDCTokenService connectTokenService;
8791

92+
@Autowired
93+
private WebResponseExceptionTranslator providerExceptionHandler;
94+
8895
/**
8996
* Logger for this class
9097
*/
@@ -559,4 +566,10 @@ private OAuth2AccessTokenEntity fetchValidRegistrationToken(OAuth2Authentication
559566
return token;
560567
}
561568
}
569+
570+
@ExceptionHandler(OAuth2Exception.class)
571+
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
572+
logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
573+
return providerExceptionHandler.translate(e);
574+
}
562575
}

openid-connect-server/src/main/java/org/mitre/openid/connect/web/ProtectedResourceRegistrationEndpoint.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@
4343
import org.slf4j.LoggerFactory;
4444
import org.springframework.beans.factory.annotation.Autowired;
4545
import org.springframework.http.HttpStatus;
46+
import org.springframework.http.ResponseEntity;
4647
import org.springframework.security.access.prepost.PreAuthorize;
48+
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
4749
import org.springframework.security.oauth2.provider.OAuth2Authentication;
4850
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
51+
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
4952
import org.springframework.stereotype.Controller;
5053
import org.springframework.ui.Model;
54+
import org.springframework.web.bind.annotation.ExceptionHandler;
5155
import org.springframework.web.bind.annotation.PathVariable;
5256
import org.springframework.web.bind.annotation.RequestBody;
5357
import org.springframework.web.bind.annotation.RequestMapping;
@@ -82,6 +86,9 @@ public class ProtectedResourceRegistrationEndpoint {
8286
@Autowired
8387
private OIDCTokenService connectTokenService;
8488

89+
@Autowired
90+
private WebResponseExceptionTranslator providerExceptionHandler;
91+
8592
/**
8693
* Logger for this class
8794
*/
@@ -469,4 +476,10 @@ private OAuth2AccessTokenEntity fetchValidRegistrationToken(OAuth2Authentication
469476
return token;
470477
}
471478
}
479+
480+
@ExceptionHandler(OAuth2Exception.class)
481+
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
482+
logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
483+
return providerExceptionHandler.translate(e);
484+
}
472485
}

0 commit comments

Comments
 (0)