Skip to content

Commit 32c7e8a

Browse files
committed
Use Saml2Error Static Factories
1 parent 3de7312 commit 32c7e8a

File tree

8 files changed

+15
-31
lines changed

8 files changed

+15
-31
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/authentication/BaseOpenSamlAuthenticationProvider.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
302302
throw ex;
303303
}
304304
catch (Exception ex) {
305-
throw createAuthenticationException(Saml2ErrorCodes.INTERNAL_VALIDATION_ERROR, ex.getMessage(), ex);
305+
throw new Saml2AuthenticationException(Saml2Error.internalValidationError(ex.getMessage()), ex);
306306
}
307307
}
308308

@@ -316,7 +316,7 @@ private Response parseResponse(String response) throws Saml2Exception, Saml2Auth
316316
return this.saml.deserialize(response);
317317
}
318318
catch (Exception ex) {
319-
throw createAuthenticationException(Saml2ErrorCodes.MALFORMED_RESPONSE_DATA, ex.getMessage(), ex);
319+
throw new Saml2AuthenticationException(Saml2Error.malformedResponseData(ex.getMessage()), ex);
320320
}
321321
}
322322

@@ -375,7 +375,7 @@ else if (this.logger.isDebugEnabled()) {
375375
.debug("Found " + errors.size() + " validation errors in SAML response [" + response.getID() + "]");
376376
}
377377
Saml2Error first = errors.iterator().next();
378-
throw createAuthenticationException(first.getErrorCode(), first.getDescription(), null);
378+
throw new Saml2AuthenticationException(first);
379379
}
380380
else {
381381
if (this.logger.isDebugEnabled()) {
@@ -408,7 +408,7 @@ private Consumer<ResponseToken> createDefaultResponseElementsDecrypter() {
408408
this.saml.withDecryptionKeys(registration.getDecryptionX509Credentials()).decrypt(response);
409409
}
410410
catch (Exception ex) {
411-
throw createAuthenticationException(Saml2ErrorCodes.DECRYPTION_ERROR, ex.getMessage(), ex);
411+
throw new Saml2AuthenticationException(Saml2Error.decryptionError(ex.getMessage()), ex);
412412
}
413413
};
414414
}
@@ -437,7 +437,7 @@ private Consumer<AssertionToken> createDefaultAssertionElementsDecrypter() {
437437
this.saml.withDecryptionKeys(registration.getDecryptionX509Credentials()).decrypt(assertion);
438438
}
439439
catch (Exception ex) {
440-
throw createAuthenticationException(Saml2ErrorCodes.DECRYPTION_ERROR, ex.getMessage(), ex);
440+
throw new Saml2AuthenticationException(Saml2Error.decryptionError(ex.getMessage()), ex);
441441
}
442442
};
443443
}
@@ -503,11 +503,6 @@ private static Object getXmlObjectValue(XMLObject xmlObject) {
503503
return xmlObject;
504504
}
505505

506-
private static Saml2AuthenticationException createAuthenticationException(String code, String message,
507-
Exception cause) {
508-
return new Saml2AuthenticationException(new Saml2Error(code, message), cause);
509-
}
510-
511506
private static Converter<AssertionToken, Saml2ResponseValidatorResult> createAssertionValidator(String errorCode,
512507
Converter<AssertionToken, SAML20AssertionValidator> validatorConverter,
513508
Converter<AssertionToken, ValidationContext> contextConverter) {

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/BaseOpenSamlAuthenticationTokenConverter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.http.HttpMethod;
2323
import org.springframework.security.saml2.core.OpenSamlInitializationService;
2424
import org.springframework.security.saml2.core.Saml2Error;
25-
import org.springframework.security.saml2.core.Saml2ErrorCodes;
2625
import org.springframework.security.saml2.core.Saml2ParameterNames;
2726
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
2827
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
@@ -182,8 +181,7 @@ private String decode(HttpServletRequest request) {
182181
.decode();
183182
}
184183
catch (Exception ex) {
185-
throw new Saml2AuthenticationException(new Saml2Error(Saml2ErrorCodes.INVALID_RESPONSE, ex.getMessage()),
186-
ex);
184+
throw new Saml2AuthenticationException(Saml2Error.invalidResponse(ex.getMessage()), ex);
187185
}
188186
}
189187

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/Saml2AuthenticationTokenConverter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.springframework.http.HttpMethod;
2222
import org.springframework.security.saml2.core.Saml2Error;
23-
import org.springframework.security.saml2.core.Saml2ErrorCodes;
2423
import org.springframework.security.saml2.core.Saml2ParameterNames;
2524
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
2625
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
@@ -107,12 +106,12 @@ private String decode(HttpServletRequest request) {
107106
if (!this.shouldConvertGetRequests && isGet) {
108107
return null;
109108
}
109+
Saml2Utils.DecodingConfigurer decoding = Saml2Utils.withEncoded(encoded).requireBase64(true).inflate(isGet);
110110
try {
111-
return Saml2Utils.withEncoded(encoded).requireBase64(true).inflate(isGet).decode();
111+
return decoding.decode();
112112
}
113113
catch (Exception ex) {
114-
throw new Saml2AuthenticationException(new Saml2Error(Saml2ErrorCodes.INVALID_RESPONSE, ex.getMessage()),
115-
ex);
114+
throw new Saml2AuthenticationException(Saml2Error.invalidResponse(ex.getMessage()), ex);
116115
}
117116
}
118117

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/authentication/logout/BaseOpenSamlLogoutRequestValidatorParametersResolver.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.springframework.security.core.Authentication;
2424
import org.springframework.security.saml2.core.OpenSamlInitializationService;
2525
import org.springframework.security.saml2.core.Saml2Error;
26-
import org.springframework.security.saml2.core.Saml2ErrorCodes;
2726
import org.springframework.security.saml2.core.Saml2ParameterNames;
2827
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
2928
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
@@ -145,8 +144,7 @@ private Saml2LogoutRequestValidatorParameters logoutRequestById(HttpServletReque
145144
RelyingPartyRegistration registration = this.registrations.findByRegistrationId(registrationId);
146145
if (registration == null) {
147146
throw new Saml2AuthenticationException(
148-
new Saml2Error(Saml2ErrorCodes.RELYING_PARTY_REGISTRATION_NOT_FOUND, "registration not found"),
149-
"registration not found");
147+
Saml2Error.relyingPartyRegistrationNotFound("registration not found"));
150148
}
151149
return logoutRequestByRegistration(request, registration, authentication);
152150
}

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/authentication/logout/Saml2LogoutRequestFilter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.springframework.security.core.context.SecurityContextHolder;
3232
import org.springframework.security.core.context.SecurityContextHolderStrategy;
3333
import org.springframework.security.saml2.core.Saml2Error;
34-
import org.springframework.security.saml2.core.Saml2ErrorCodes;
3534
import org.springframework.security.saml2.core.Saml2ParameterNames;
3635
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
3736
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
@@ -268,8 +267,7 @@ public Saml2LogoutRequestValidatorParameters resolve(HttpServletRequest request,
268267
registrationId);
269268
if (registration == null) {
270269
throw new Saml2AuthenticationException(
271-
new Saml2Error(Saml2ErrorCodes.RELYING_PARTY_REGISTRATION_NOT_FOUND, "registration not found"),
272-
"registration not found");
270+
Saml2Error.relyingPartyRegistrationNotFound("registration not found"));
273271
}
274272
UriResolver uriResolver = RelyingPartyRegistrationPlaceholderResolvers.uriResolver(request, registration);
275273
String entityId = uriResolver.resolve(registration.getEntityId());

saml2/saml2-service-provider/src/opensaml4Main/java/org/springframework/security/saml2/provider/service/web/OpenSamlAuthenticationTokenConverter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.http.HttpMethod;
2525
import org.springframework.security.saml2.core.OpenSamlInitializationService;
2626
import org.springframework.security.saml2.core.Saml2Error;
27-
import org.springframework.security.saml2.core.Saml2ErrorCodes;
2827
import org.springframework.security.saml2.core.Saml2ParameterNames;
2928
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
3029
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
@@ -197,8 +196,7 @@ private String decode(HttpServletRequest request) {
197196
.decode();
198197
}
199198
catch (Exception ex) {
200-
throw new Saml2AuthenticationException(new Saml2Error(Saml2ErrorCodes.INVALID_RESPONSE, ex.getMessage()),
201-
ex);
199+
throw new Saml2AuthenticationException(Saml2Error.invalidResponse(ex.getMessage()), ex);
202200
}
203201
}
204202

saml2/saml2-service-provider/src/opensaml4Main/java/org/springframework/security/saml2/provider/service/web/authentication/logout/OpenSamlLogoutRequestValidatorParametersResolver.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.security.core.Authentication;
2828
import org.springframework.security.saml2.core.OpenSamlInitializationService;
2929
import org.springframework.security.saml2.core.Saml2Error;
30-
import org.springframework.security.saml2.core.Saml2ErrorCodes;
3130
import org.springframework.security.saml2.core.Saml2ParameterNames;
3231
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
3332
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
@@ -159,8 +158,7 @@ private Saml2LogoutRequestValidatorParameters logoutRequestById(HttpServletReque
159158
RelyingPartyRegistration registration = this.registrations.findByRegistrationId(registrationId);
160159
if (registration == null) {
161160
throw new Saml2AuthenticationException(
162-
new Saml2Error(Saml2ErrorCodes.RELYING_PARTY_REGISTRATION_NOT_FOUND, "registration not found"),
163-
"registration not found");
161+
Saml2Error.relyingPartyRegistrationNotFound("registration not found"));
164162
}
165163
return logoutRequestByRegistration(request, registration, authentication);
166164
}

saml2/saml2-service-provider/src/opensaml5Main/java/org/springframework/security/saml2/provider/service/authentication/OpenSaml5AuthenticationProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,8 +935,8 @@ public void setGrantedAuthoritiesConverter(
935935

936936
private static String authenticatedPrincipal(Assertion assertion) {
937937
if (!BaseOpenSamlAuthenticationProvider.hasName(assertion)) {
938-
throw new Saml2AuthenticationException(new Saml2Error(Saml2ErrorCodes.SUBJECT_NOT_FOUND,
939-
"Assertion [" + assertion.getID() + "] is missing a subject"));
938+
throw new Saml2AuthenticationException(
939+
Saml2Error.subjectNotFound("Assertion [" + assertion.getID() + "] is missing a subject"));
940940
}
941941
return assertion.getSubject().getNameID().getValue();
942942
}

0 commit comments

Comments
 (0)