|
12 | 12 | import com.onelogin.saml2.util.Util; |
13 | 13 |
|
14 | 14 | import org.hamcrest.Matchers; |
| 15 | +import org.joda.time.DateTime; |
| 16 | +import org.joda.time.DateTimeUtils; |
15 | 17 | import org.joda.time.Instant; |
| 18 | +import org.joda.time.format.ISODateTimeFormat; |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Before; |
16 | 21 | import org.junit.Rule; |
17 | 22 | import org.junit.Test; |
18 | 23 | import org.junit.rules.ExpectedException; |
|
23 | 28 |
|
24 | 29 | import java.io.IOException; |
25 | 30 | import java.util.ArrayList; |
| 31 | +import java.util.Date; |
26 | 32 | import java.util.HashMap; |
27 | 33 | import java.util.List; |
28 | 34 | import java.util.Map; |
@@ -51,6 +57,18 @@ public class AuthnResponseTest { |
51 | 57 | @Rule |
52 | 58 | public ExpectedException expectedEx = ExpectedException.none(); |
53 | 59 |
|
| 60 | + @Before |
| 61 | + public void setDateTime() { |
| 62 | + //All calls to Joda time check will use this timestamp as "now" value : |
| 63 | + setDateTime("2020-06-01T00:00:00Z"); |
| 64 | + } |
| 65 | + |
| 66 | + @After |
| 67 | + public void goBackToNormal() { |
| 68 | + DateTimeUtils.setCurrentMillisSystem(); |
| 69 | + } |
| 70 | + |
| 71 | + |
54 | 72 | /** |
55 | 73 | * Tests the constructor of SamlResponse |
56 | 74 | * |
@@ -1923,6 +1941,33 @@ public void testDatetimeWithMiliseconds() throws IOException, Error, XPathExpres |
1923 | 1941 | assertEquals("No Signature found. SAML Response rejected", samlResponse.getError()); |
1924 | 1942 | } |
1925 | 1943 |
|
| 1944 | + @Test |
| 1945 | + public void testParseAzureB2CTimestamp() throws IOException, Error, XPathExpressionException, ParserConfigurationException, SAXException, SettingsException, ValidationError { |
| 1946 | + Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build(); |
| 1947 | + String samlResponseEncoded = Util.getFileAsString("data/responses/invalids/redacted_azure_b2c.xml.base64"); |
| 1948 | + |
| 1949 | + settings.setStrict(false); |
| 1950 | + SamlResponse samlResponse = new SamlResponse(settings, newHttpRequest(samlResponseEncoded)); |
| 1951 | + assertFalse(samlResponse.isValid()); |
| 1952 | + assertEquals("No Signature found. SAML Response rejected", samlResponse.getError()); |
| 1953 | + |
| 1954 | + settings.setStrict(true); |
| 1955 | + setDateTime("2020-07-16T07:57:00Z"); |
| 1956 | + samlResponse = new SamlResponse(settings, newHttpRequest(samlResponseEncoded)); |
| 1957 | + assertFalse(samlResponse.isValid()); |
| 1958 | + assertEquals("A valid SubjectConfirmation was not found on this Response: SubjectConfirmationData doesn't match a valid Recipient", samlResponse.getError()); |
| 1959 | + |
| 1960 | + setDateTime("2020-07-01T00:00:00Z"); |
| 1961 | + samlResponse = new SamlResponse(settings, newHttpRequest(samlResponseEncoded)); |
| 1962 | + assertFalse(samlResponse.isValid()); |
| 1963 | + assertEquals("Could not validate timestamp: not yet valid. Check system clock.", samlResponse.getError()); |
| 1964 | + |
| 1965 | + setDateTime("2020-08-01T00:00:00Z"); |
| 1966 | + samlResponse = new SamlResponse(settings, newHttpRequest(samlResponseEncoded)); |
| 1967 | + assertFalse(samlResponse.isValid()); |
| 1968 | + assertEquals("Could not validate timestamp: expired. Check system clock.", samlResponse.getError()); |
| 1969 | + } |
| 1970 | + |
1926 | 1971 | /** |
1927 | 1972 | * Tests the isValid method of SamlResponse |
1928 | 1973 | * Case: invalid requestId |
@@ -2881,5 +2926,10 @@ private static HttpRequest newHttpRequest(String samlResponseEncoded) { |
2881 | 2926 | private static HttpRequest newHttpRequest(String requestURL, String samlResponseEncoded) { |
2882 | 2927 | return new HttpRequest(requestURL, (String)null).addParameter("SAMLResponse", samlResponseEncoded); |
2883 | 2928 | } |
| 2929 | + |
| 2930 | + private void setDateTime(String ISOTimeStamp) { |
| 2931 | + DateTime dateTime = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC().parseDateTime(ISOTimeStamp); |
| 2932 | + DateTimeUtils.setCurrentMillisFixed(dateTime.toDate().getTime()); |
| 2933 | + } |
2884 | 2934 | } |
2885 | 2935 |
|
0 commit comments