Skip to content

Commit 4c317cc

Browse files
committed
Add hooks to retrieve last-sent and last-received requests and responses
1 parent 09d1e6a commit 4c317cc

10 files changed

Lines changed: 346 additions & 19 deletions

File tree

core/src/main/java/com/onelogin/saml2/authn/AuthnRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public String getEncodedAuthnRequest() throws IOException {
131131
/**
132132
* @return unsigned plain-text AuthnRequest.
133133
*/
134-
protected String getAuthnRequestXml() {
134+
public String getAuthnRequestXml() {
135135
return authnRequestString;
136136
}
137137

core/src/main/java/com/onelogin/saml2/authn/SamlResponse.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public class SamlResponse {
5959
* A DOMDocument object loaded from the SAML Response (Decrypted).
6060
*/
6161
private Document decryptedDocument;
62+
63+
/**
64+
* A DOMDocument class loaded from the original decrypted assertion.
65+
* used in order to avoid signature validation conflicts due namespace issues
66+
*/
67+
private Document originalDecryptedAssertion;
6268

6369
/**
6470
* URL of the current host + current view
@@ -1001,4 +1007,32 @@ private Document decryptAssertion(Document dom) throws Exception {
10011007
// LOGGER.debug("Decrypted SAMLResponse --> " + xmlStr);
10021008
return doc;
10031009
}
1010+
1011+
/**
1012+
* @return the SAMLResponse XML, If the Assertion of the SAMLResponse was encrypted,
1013+
* returns the XML with the assertion decrypted
1014+
*/
1015+
public String getSAMLResponseXml() {
1016+
String xml;
1017+
if (encrypted) {
1018+
xml = Util.convertDocumentToString(decryptedDocument);
1019+
} else {
1020+
xml = samlResponseString;
1021+
}
1022+
return xml;
1023+
}
1024+
1025+
/**
1026+
* @return the SAMLResponse Document, If the Assertion of the SAMLResponse was encrypted,
1027+
* returns the Document with the assertion decrypted
1028+
*/
1029+
protected Document getSAMLResponseDocument() {
1030+
Document doc;
1031+
if (encrypted) {
1032+
doc = decryptedDocument;
1033+
} else {
1034+
doc = samlResponseDocument;
1035+
}
1036+
return doc;
1037+
}
10041038
}

core/src/main/java/com/onelogin/saml2/logout/LogoutRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public String getEncodedLogoutRequest() throws IOException {
181181
/**
182182
* @return the plain XML Logout Request
183183
*/
184-
protected String getLogoutRequestXml() {
184+
public String getLogoutRequestXml() {
185185
return logoutRequestString;
186186
}
187187

core/src/main/java/com/onelogin/saml2/logout/LogoutResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public String getEncodedLogoutResponse() throws IOException {
139139
/**
140140
* @return the plain XML Logout Response
141141
*/
142-
protected String getLogoutResponseXml() {
142+
public String getLogoutResponseXml() {
143143
return logoutResponseString;
144144
}
145145

core/src/test/java/com/onelogin/saml2/test/authn/AuthnRequestTest.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void testGetEncodedAuthnRequestSimulated() throws Exception {
3232
final String authnRequestString = Util.getFileAsString("data/requests/authn_request.xml");
3333
AuthnRequest authnRequest = new AuthnRequest(settings) {
3434
@Override
35-
protected String getAuthnRequestXml() {
35+
public String getAuthnRequestXml() {
3636
return authnRequestString;
3737
}
3838
};
@@ -56,7 +56,7 @@ protected String getAuthnRequestXml() {
5656
settings.setCompressRequest(true);
5757
authnRequest = new AuthnRequest(settings) {
5858
@Override
59-
protected String getAuthnRequestXml() {
59+
public String getAuthnRequestXml() {
6060
return authnRequestString;
6161
}
6262
};
@@ -66,7 +66,7 @@ protected String getAuthnRequestXml() {
6666
settings.setCompressRequest(false);
6767
authnRequest = new AuthnRequest(settings) {
6868
@Override
69-
protected String getAuthnRequestXml() {
69+
public String getAuthnRequestXml() {
7070
return authnRequestString;
7171
}
7272
};
@@ -100,6 +100,21 @@ public void testGetEncodedAuthnRequestOnlySettings() throws Exception {
100100
assertThat(authnRequestStr, containsString("ProviderName=\"SP Java Example\""));
101101
}
102102

103+
/**
104+
* Tests the getAuthnRequestXml method of AuthnRequest
105+
*
106+
* @throws Exception
107+
*
108+
* @see com.onelogin.saml2.authn.getAuthnRequestXml
109+
*/
110+
@Test
111+
public void testGetAuthnRequestXml() throws Exception {
112+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
113+
AuthnRequest authnRequest = new AuthnRequest(settings);
114+
String authnRequestXML = authnRequest.getAuthnRequestXml();
115+
assertThat(authnRequestXML, containsString("<samlp:AuthnRequest"));
116+
}
117+
103118
/**
104119
* Tests the AuthnRequest Constructor
105120
* The creation of a deflated SAML Request with the different values of ForceAuthn
@@ -257,8 +272,15 @@ public void testAuthNContext() throws Exception {
257272
assertThat(authnRequestStr, containsString("<saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:X509</saml:AuthnContextClassRef>"));
258273
}
259274

275+
/**
276+
* Tests the getId method of AuthnRequest
277+
*
278+
* @throws Exception
279+
*
280+
* @see com.onelogin.saml2.authn.getId
281+
*/
260282
@Test
261-
public void testAuthNId() throws Exception
283+
public void testGetId() throws Exception
262284
{
263285
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
264286

core/src/test/java/com/onelogin/saml2/test/authn/AuthnResponseTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.onelogin.saml2.authn.SamlResponse;
44
import com.onelogin.saml2.http.HttpRequest;
5+
import com.onelogin.saml2.logout.LogoutResponse;
56
import com.onelogin.saml2.model.SamlResponseStatus;
67
import com.onelogin.saml2.settings.Saml2Settings;
78
import com.onelogin.saml2.settings.SettingsBuilder;
@@ -103,6 +104,31 @@ public void testNamespaces() throws Exception {
103104
assertEquals(expectedLastName, attributes.get("LastName"));
104105
}
105106

107+
/**
108+
* Tests the getSAMLResponseXml method of SamlResponse
109+
*
110+
* @throws Exception
111+
*
112+
* @see com.onelogin.saml2.authn.SamlResponse#getSAMLResponseXml
113+
*/
114+
@Test
115+
public void testGetSAMLResponseXml() throws Exception {
116+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
117+
118+
final String requestURL = "/";
119+
String samlResponseEncoded = Util.getFileAsString("data/responses/response1.xml.base64");
120+
121+
SamlResponse samlResponse = new SamlResponse(settings, newHttpRequest(requestURL, samlResponseEncoded));
122+
String samlResponseXML = samlResponse.getSAMLResponseXml();
123+
assertThat(samlResponseXML, containsString("<samlp:Response"));
124+
125+
samlResponseEncoded = Util.getFileAsString("data/responses/valid_encrypted_assertion.xml.base64");
126+
samlResponse = new SamlResponse(settings, newHttpRequest(requestURL, samlResponseEncoded));
127+
samlResponseXML = samlResponse.getSAMLResponseXml();
128+
assertThat(samlResponseXML, containsString("<samlp:Response"));
129+
assertThat(samlResponseXML, containsString("<saml:Assertion"));
130+
}
131+
106132
/**
107133
* Tests the getNameId method of SamlResponse
108134
*

core/src/test/java/com/onelogin/saml2/test/logout/LogoutRequestTest.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.onelogin.saml2.logout.LogoutRequest;
2626
import com.onelogin.saml2.http.HttpRequest;
27+
import com.onelogin.saml2.authn.AuthnRequest;
2728
import com.onelogin.saml2.exception.XMLEntityException;
2829
import com.onelogin.saml2.settings.Saml2Settings;
2930
import com.onelogin.saml2.settings.SettingsBuilder;
@@ -45,7 +46,7 @@ public void testGetEncodedLogoutRequestSimulated() throws Exception {
4546
final String logoutRequestString = Util.getFileAsString("data/logout_requests/logout_request.xml");
4647
LogoutRequest logoutRequest = new LogoutRequest(settings) {
4748
@Override
48-
protected String getLogoutRequestXml() {
49+
public String getLogoutRequestXml() {
4950
return logoutRequestString;
5051
}
5152
};
@@ -69,7 +70,7 @@ protected String getLogoutRequestXml() {
6970
settings.setCompressRequest(true);
7071
logoutRequest = new LogoutRequest(settings) {
7172
@Override
72-
protected String getLogoutRequestXml() {
73+
public String getLogoutRequestXml() {
7374
return logoutRequestString;
7475
}
7576
};
@@ -79,7 +80,7 @@ protected String getLogoutRequestXml() {
7980
settings.setCompressRequest(false);
8081
logoutRequest = new LogoutRequest(settings) {
8182
@Override
82-
protected String getLogoutRequestXml() {
83+
public String getLogoutRequestXml() {
8384
return logoutRequestString;
8485
}
8586
};
@@ -168,7 +169,7 @@ public void testConstructorWithSessionIndex() throws Exception {
168169
@Test
169170
public void testConstructorWithEncryptedNameID() throws Exception {
170171
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.all.properties").build();
171-
LogoutRequest logoutRequest = new LogoutRequest(settings);
172+
LogoutRequest logoutRequest = new LogoutRequest(settings);
172173
String logoutRequestStringBase64 = logoutRequest.getEncodedLogoutRequest();
173174
String logoutRequestStr = Util.base64decodedInflated(logoutRequestStringBase64);
174175
assertThat(logoutRequestStr, containsString("<saml:EncryptedID><xenc:EncryptedData"));
@@ -179,7 +180,30 @@ public void testConstructorWithEncryptedNameID() throws Exception {
179180
logoutRequestStr = Util.base64decodedInflated(logoutRequestStringBase64);
180181
assertThat(logoutRequestStr, not(containsString("<saml:EncryptedID><xenc:EncryptedData")));
181182
}
182-
183+
184+
/**
185+
* Tests the getLogoutRequestXml method of LogoutRequest
186+
*
187+
* @throws Exception
188+
*
189+
* @see com.onelogin.saml2.logout.getLogoutRequestXml
190+
*/
191+
@Test
192+
public void testGetLogoutRequestXml() throws Exception {
193+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
194+
LogoutRequest logoutRequest = new LogoutRequest(settings);
195+
String logoutRequestXML = logoutRequest.getLogoutRequestXml();
196+
assertThat(logoutRequestXML, containsString("<samlp:LogoutRequest"));
197+
198+
String samlRequestEncoded = Util.getFileAsString("data/logout_requests/logout_request.xml.base64");
199+
String requestURL = "/";
200+
HttpRequest httpRequest = newHttpRequest(requestURL, samlRequestEncoded);
201+
logoutRequest = new LogoutRequest(settings, httpRequest);
202+
logoutRequestXML = logoutRequest.getLogoutRequestXml();
203+
assertThat(logoutRequestXML, containsString("<samlp:LogoutRequest"));
204+
205+
}
206+
183207
/**
184208
* Tests the getNameIdData method of LogoutRequest
185209
* Case: Able to get the NameIdData

core/src/test/java/com/onelogin/saml2/test/logout/LogoutResponseTest.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.onelogin.saml2.exception.XMLEntityException;
2020
import com.onelogin.saml2.http.HttpRequest;
21+
import com.onelogin.saml2.logout.LogoutRequest;
2122
import com.onelogin.saml2.logout.LogoutResponse;
2223
import com.onelogin.saml2.settings.Saml2Settings;
2324
import com.onelogin.saml2.settings.SettingsBuilder;
@@ -42,7 +43,7 @@ public void testGetEncodedLogoutResponseSimulated() throws Exception {
4243

4344
LogoutResponse logoutResponseBuilder = new LogoutResponse(settings, httpRequest) {
4445
@Override
45-
protected String getLogoutResponseXml() {
46+
public String getLogoutResponseXml() {
4647
return logoutResponseString;
4748
}
4849
};
@@ -57,7 +58,7 @@ protected String getLogoutResponseXml() {
5758

5859
LogoutResponse logoutResponse = new LogoutResponse(settings, httpRequest) {
5960
@Override
60-
protected String getLogoutResponseXml() {
61+
public String getLogoutResponseXml() {
6162
return logoutResponseString;
6263
}
6364
};
@@ -77,7 +78,7 @@ protected String getLogoutResponseXml() {
7778
settings.setCompressResponse(true);
7879
logoutResponse = new LogoutResponse(settings, httpRequest) {
7980
@Override
80-
protected String getLogoutResponseXml() {
81+
public String getLogoutResponseXml() {
8182
return logoutResponseString;
8283
}
8384
};
@@ -87,7 +88,7 @@ protected String getLogoutResponseXml() {
8788
settings.setCompressResponse(false);
8889
logoutResponse = new LogoutResponse(settings, httpRequest) {
8990
@Override
90-
protected String getLogoutResponseXml() {
91+
public String getLogoutResponseXml() {
9192
return logoutResponseString;
9293
}
9394
};
@@ -153,6 +154,30 @@ public void testBuild() throws IOException, XMLEntityException, URISyntaxExcepti
153154
assertThat(logoutRequestStr, containsString("InResponseTo=\"inResponseValue\""));
154155
}
155156

157+
/**
158+
* Tests the getLogoutResponseXml method of LogoutResponse
159+
*
160+
* @throws Exception
161+
*
162+
* @see com.onelogin.saml2.logout.getLogoutResponseXml
163+
*/
164+
@Test
165+
public void testGetLogoutRequestXml() throws Exception {
166+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
167+
LogoutResponse logoutResponse = new LogoutResponse(settings, null);
168+
logoutResponse.build();
169+
String logoutResponseXML = logoutResponse.getLogoutResponseXml();
170+
assertThat(logoutResponseXML, containsString("<samlp:LogoutResponse"));
171+
172+
String samlResponseEncoded = Util.getFileAsString("data/logout_responses/logout_response.xml.base64");
173+
String requestURL = "/";
174+
HttpRequest httpRequest = newHttpRequest(requestURL, samlResponseEncoded);
175+
logoutResponse = new LogoutResponse(settings, httpRequest);
176+
logoutResponseXML = logoutResponse.getLogoutResponseXml();
177+
assertThat(logoutResponseXML, containsString("<samlp:LogoutResponse"));
178+
179+
}
180+
156181
/**
157182
* Tests the getStatus method of LogoutResponse
158183
*

0 commit comments

Comments
 (0)