Skip to content

Commit 5bba700

Browse files
committed
Implement basic, top-level exception handling class. Author: necouchman #206
1 parent 4444fa0 commit 5bba700

File tree

7 files changed

+92
-48
lines changed

7 files changed

+92
-48
lines changed
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
package com.onelogin.saml2.exception;
22

3-
public class Error extends Exception {
3+
public class Error extends SAMLException {
44

55
private static final long serialVersionUID = 1L;
66

7-
public static final int SETTINGS_FILE_NOT_FOUND = 1;
8-
public static final int METADATA_SP_INVALID = 2;
9-
public static final int SAML_RESPONSE_NOT_FOUND = 3;
10-
public static final int SAML_LOGOUTMESSAGE_NOT_FOUND = 4;
11-
public static final int SAML_LOGOUTREQUEST_INVALID = 5;
12-
public static final int SAML_LOGOUTRESPONSE_INVALID = 6;
13-
public static final int SAML_SINGLE_LOGOUT_NOT_SUPPORTED = 7;
14-
7+
public static final int SETTINGS_FILE_NOT_FOUND = 1;
8+
public static final int METADATA_SP_INVALID = 2;
9+
public static final int SAML_RESPONSE_NOT_FOUND = 3;
10+
public static final int SAML_LOGOUTMESSAGE_NOT_FOUND = 4;
11+
public static final int SAML_LOGOUTREQUEST_INVALID = 5;
12+
public static final int SAML_LOGOUTRESPONSE_INVALID = 6;
13+
public static final int SAML_SINGLE_LOGOUT_NOT_SUPPORTED = 7;
14+
1515
private int errorCode;
16-
16+
1717
public Error(String message, int errorCode) {
1818
super(message);
1919
this.errorCode = errorCode;
2020
}
21-
22-
public int getErrorCode() {
23-
return errorCode;
24-
}
21+
22+
public int getErrorCode() {
23+
return errorCode;
24+
}
2525

2626
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.onelogin.saml2.exception;
2+
3+
/**
4+
* Top-level exception class for the OneLogin SAML client.
5+
*/
6+
public class SAMLException extends Exception {
7+
8+
private static final long serialVersionUID = 1L;
9+
10+
/**
11+
* Construct a SAMLException with the provided error message.
12+
*
13+
* @param message
14+
* The human-readable error message associated with this exception.
15+
*/
16+
public SAMLException(String message) {
17+
super(message);
18+
}
19+
20+
/**
21+
* Construct a SAMLException with the provided cause for the exception.
22+
*
23+
* @param cause
24+
* The upstream cause of this exception.
25+
*/
26+
public SAMLException(Throwable cause) {
27+
super(cause);
28+
}
29+
30+
/**
31+
* Construct a SAMLException with the provided human-readable error message
32+
* and upstream cause.
33+
*
34+
* @param message
35+
* The human-readable error message associated with this exception.
36+
*
37+
* @param cause
38+
* The upstream cause associated with this exception.
39+
*/
40+
public SAMLException(String message, Throwable cause) {
41+
super(message, cause);
42+
}
43+
44+
}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package com.onelogin.saml2.exception;
22

3-
public class SettingsException extends Exception {
3+
public class SettingsException extends SAMLException {
44

55
private static final long serialVersionUID = 1L;
66

7-
public static final int SETTINGS_INVALID_SYNTAX = 1;
8-
public static final int SETTINGS_INVALID = 2;
9-
public static final int CERT_NOT_FOUND = 3;
10-
public static final int PRIVATE_KEY_NOT_FOUND = 4;
11-
public static final int PUBLIC_CERT_FILE_NOT_FOUND = 5;
12-
public static final int PRIVATE_KEY_FILE_NOT_FOUND = 6;
13-
14-
private int errorCode;
15-
7+
public static final int SETTINGS_INVALID_SYNTAX = 1;
8+
public static final int SETTINGS_INVALID = 2;
9+
public static final int CERT_NOT_FOUND = 3;
10+
public static final int PRIVATE_KEY_NOT_FOUND = 4;
11+
public static final int PUBLIC_CERT_FILE_NOT_FOUND = 5;
12+
public static final int PRIVATE_KEY_FILE_NOT_FOUND = 6;
13+
14+
private int errorCode;
15+
1616
public SettingsException(String message, int errorCode) {
1717
super(message);
1818
this.errorCode = errorCode;
1919
}
20-
21-
public int getErrorCode() {
22-
return errorCode;
23-
}
20+
21+
public int getErrorCode() {
22+
return errorCode;
23+
}
2424

2525
}

core/src/main/java/com/onelogin/saml2/exception/ValidationError.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.onelogin.saml2.exception;
22

3-
public class ValidationError extends Exception {
3+
public class ValidationError extends SAMLException {
44

55
private static final long serialVersionUID = 1L;
66

@@ -53,16 +53,16 @@ public class ValidationError extends Exception {
5353
public static final int NOT_SUPPORTED = 46;
5454
public static final int KEY_ALGORITHM_ERROR = 47;
5555
public static final int MISSING_ENCRYPTED_ELEMENT = 48;
56-
57-
private int errorCode;
58-
56+
57+
private int errorCode;
58+
5959
public ValidationError(String message, int errorCode) {
6060
super(message);
6161
this.errorCode = errorCode;
6262
}
6363

64-
public int getErrorCode() {
65-
return errorCode;
66-
}
64+
public int getErrorCode() {
65+
return errorCode;
66+
}
6767

6868
}

core/src/main/java/com/onelogin/saml2/exception/XMLEntityException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.onelogin.saml2.exception;
22

3-
public class XMLEntityException extends Exception {
3+
public class XMLEntityException extends SAMLException {
44

55
private static final long serialVersionUID = 1L;
66

core/src/main/java/com/onelogin/saml2/settings/Metadata.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class Metadata {
4343
private static final int SECONDS_CACHED = 604800; // 1 week
4444

4545
/**
46-
* AttributeConsumingService
46+
* AttributeConsumingService
4747
*/
4848
private AttributeConsumingService attributeConsumingService = null;
4949

@@ -64,7 +64,7 @@ public class Metadata {
6464

6565
/**
6666
* Constructs the Metadata object.
67-
*
67+
*
6868
* @param settings
6969
* Saml2Settings object. Setting data
7070
* @param validUntilTime
@@ -83,7 +83,7 @@ public Metadata(Saml2Settings settings, Calendar validUntilTime, Integer cacheDu
8383
} else {
8484
this.validUntilTime = validUntilTime;
8585
}
86-
86+
8787
this.attributeConsumingService = attributeConsumingService;
8888

8989
if (cacheDuration == null) {
@@ -95,13 +95,13 @@ public Metadata(Saml2Settings settings, Calendar validUntilTime, Integer cacheDu
9595
StrSubstitutor substitutor = generateSubstitutor(settings);
9696
String unsignedMetadataString = substitutor.replace(getMetadataTemplate());
9797

98-
LOGGER.debug("metadata --> " + unsignedMetadataString);
99-
metadataString = unsignedMetadataString;
98+
LOGGER.debug("metadata --> " + unsignedMetadataString);
99+
metadataString = unsignedMetadataString;
100100
}
101101

102102
/**
103103
* Constructs the Metadata object.
104-
*
104+
*
105105
* @param settings
106106
* Saml2Settings object. Setting data
107107
* @param validUntilTime
@@ -114,7 +114,7 @@ public Metadata(Saml2Settings settings, Calendar validUntilTime, Integer cacheDu
114114
public Metadata(Saml2Settings settings, Calendar validUntilTime, Integer cacheDuration) throws CertificateEncodingException {
115115
this(settings, validUntilTime, cacheDuration, null);
116116
}
117-
117+
118118
/**
119119
* Constructs the Metadata object.
120120
*
@@ -126,7 +126,7 @@ public Metadata(Saml2Settings settings, Calendar validUntilTime, Integer cacheDu
126126
public Metadata(Saml2Settings settings) throws CertificateEncodingException {
127127
this(settings, null, null);
128128
}
129-
129+
130130
/**
131131
* Substitutes metadata variables within a string by values.
132132
*
@@ -138,8 +138,8 @@ public Metadata(Saml2Settings settings) throws CertificateEncodingException {
138138
private StrSubstitutor generateSubstitutor(Saml2Settings settings) throws CertificateEncodingException {
139139

140140
Map<String, String> valueMap = new HashMap<String, String>();
141-
Boolean wantsEncrypted = settings.getWantAssertionsEncrypted() || settings.getWantNameIdEncrypted();
142-
141+
Boolean wantsEncrypted = settings.getWantAssertionsEncrypted() || settings.getWantNameIdEncrypted();
142+
143143
valueMap.put("id", Util.generateUniqueID(settings.getUniqueIDPrefix()));
144144
valueMap.put("validUntilTime", Util.formatDateTime(validUntilTime.getTimeInMillis()));
145145
valueMap.put("cacheDuration", String.valueOf(cacheDuration));
@@ -152,7 +152,7 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) throws Certif
152152
valueMap.put("sls", toSLSXml(settings.getSpSingleLogoutServiceUrl(), settings.getSpSingleLogoutServiceBinding()));
153153

154154
valueMap.put("strAttributeConsumingService", getAttributeConsumingServiceXml());
155-
155+
156156
valueMap.put("strKeyDescriptor", toX509KeyDescriptorsXML(settings.getSPcert(), settings.getSPcertNew(), wantsEncrypted));
157157
valueMap.put("strContacts", toContactsXml(settings.getContacts()));
158158
valueMap.put("strOrganization", toOrganizationXml(settings.getOrganization()));

core/src/test/java/com/onelogin/saml2/test/util/UtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ public void testValidateSign() throws URISyntaxException, IOException, Certifica
11141114
String signedAssertionStr = Util.getFileAsString("data/responses/signed_assertion_response.xml.base64");
11151115
String samlSignedAssertionStr = new String(Util.base64decoder(signedAssertionStr));
11161116
Document samlSignedAssertionDocument = Util.loadXML(samlSignedAssertionStr);
1117-
1117+
11181118
assertTrue(Util.validateSign(samlSignedAssertionDocument, cert, null, null, ASSERTION_SIGNATURE_XPATH));
11191119
assertTrue(Util.validateSign(samlSignedAssertionDocument, (X509Certificate) null, fingerprint_sha1, null, ASSERTION_SIGNATURE_XPATH));
11201120
assertTrue(Util.validateSign(samlSignedAssertionDocument, (X509Certificate) null, fingerprint_sha1, "SHA-1", ASSERTION_SIGNATURE_XPATH));

0 commit comments

Comments
 (0)