2727import java .security .cert .CertificateFactory ;
2828import java .security .cert .X509Certificate ;
2929import java .security .spec .PKCS8EncodedKeySpec ;
30+ import java .time .Clock ;
31+ import java .time .Duration ;
32+ import java .time .Instant ;
33+ import java .time .Period ;
34+ import java .time .ZoneOffset ;
35+ import java .time .ZonedDateTime ;
36+ import java .time .format .DateTimeFormatter ;
37+ import java .time .format .DateTimeParseException ;
38+ import java .time .temporal .TemporalAccessor ;
39+ import java .time .temporal .TemporalAmount ;
3040import java .util .Arrays ;
3141import java .util .Calendar ;
3242import java .util .HashMap ;
3343import java .util .HashSet ;
3444import java .util .Iterator ;
3545import java .util .List ;
36- import java .util .Locale ;
3746import java .util .Map ;
3847import java .util .Set ;
3948import java .util .TimeZone ;
7584import org .apache .xml .security .signature .XMLSignature ;
7685import org .apache .xml .security .transforms .Transforms ;
7786import org .apache .xml .security .utils .XMLUtils ;
78- import org .joda .time .DateTime ;
79- import org .joda .time .DateTimeZone ;
80- import org .joda .time .Period ;
81- import org .joda .time .format .DateTimeFormatter ;
82- import org .joda .time .format .ISODateTimeFormat ;
83- import org .joda .time .format .ISOPeriodFormat ;
84- import org .joda .time .format .PeriodFormatter ;
8587import org .slf4j .Logger ;
8688import org .slf4j .LoggerFactory ;
8789import org .w3c .dom .Attr ;
@@ -110,8 +112,7 @@ public final class Util {
110112 */
111113 private static final Logger LOGGER = LoggerFactory .getLogger (Util .class );
112114
113- private static final DateTimeFormatter DATE_TIME_FORMAT = ISODateTimeFormat .dateTimeNoMillis ().withZoneUTC ();
114- private static final DateTimeFormatter DATE_TIME_FORMAT_MILLS = ISODateTimeFormat .dateTime ().withZoneUTC ();
115+ private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter .ISO_DATE_TIME .withZone (ZoneOffset .UTC );
115116 public static final String UNIQUE_ID_PREFIX = "ONELOGIN_" ;
116117 public static final String RESPONSE_SIGNATURE_XPATH = "/samlp:Response/ds:Signature" ;
117118 public static final String ASSERTION_SIGNATURE_XPATH = "/samlp:Response/saml:Assertion/ds:Signature" ;
@@ -1826,10 +1827,10 @@ public static String generateUniqueID() {
18261827 *
18271828 * @return int The new timestamp, after the duration is applied.
18281829 *
1829- * @throws IllegalArgumentException
1830+ * @throws DateTimeParseException
18301831 */
1831- public static long parseDuration (String duration ) throws IllegalArgumentException {
1832- TimeZone timeZone = DateTimeZone . UTC . toTimeZone ( );
1832+ public static long parseDuration (String duration ) throws DateTimeParseException {
1833+ TimeZone timeZone = TimeZone . getTimeZone ( ZoneOffset . UTC );
18331834 return parseDuration (duration , Calendar .getInstance (timeZone ).getTimeInMillis () / 1000 );
18341835 }
18351836
@@ -1843,36 +1844,40 @@ public static long parseDuration(String duration) throws IllegalArgumentExceptio
18431844 *
18441845 * @return the new timestamp, after the duration is applied In Seconds.
18451846 *
1846- * @throws IllegalArgumentException
1847+ * @throwsa DateTimeParseException
18471848 */
1848- public static long parseDuration (String durationString , long timestamp ) throws IllegalArgumentException {
1849+ public static long parseDuration (String durationString , long timestamp ) throws DateTimeParseException {
18491850 boolean haveMinus = false ;
18501851
18511852 if (durationString .startsWith ("-" )) {
18521853 durationString = durationString .substring (1 );
18531854 haveMinus = true ;
18541855 }
18551856
1856- PeriodFormatter periodFormatter = ISOPeriodFormat .standard ().withLocale (new Locale ("UTC" ));
1857- Period period = periodFormatter .parsePeriod (durationString );
1857+ TemporalAmount amount ;
1858+ if (durationString .startsWith ("PT" )) {
1859+ amount = Duration .parse (durationString );
1860+ } else {
1861+ amount = Period .parse (durationString );
1862+ }
18581863
1859- DateTime dt = new DateTime (timestamp * 1000 , DateTimeZone .UTC );
1864+ ZonedDateTime dt = Instant . ofEpochSecond (timestamp ). atZone ( ZoneOffset .UTC );
18601865
1861- DateTime result = null ;
1866+ ZonedDateTime result ;
18621867 if (haveMinus ) {
1863- result = dt .minus (period );
1868+ result = dt .minus (amount );
18641869 } else {
1865- result = dt .plus (period );
1870+ result = dt .plus (amount );
18661871 }
1867- return result .getMillis () / 1000 ;
1872+ return result .toEpochSecond () ;
18681873 }
18691874
18701875 /**
18711876 * @return the unix timestamp that matches the current time.
18721877 */
18731878 public static Long getCurrentTimeStamp () {
1874- DateTime currentDate = new DateTime ( DateTimeZone . UTC );
1875- return currentDate .getMillis () / 1000 ;
1879+ ZonedDateTime currentDate = ZonedDateTime . now ( clock );
1880+ return currentDate .toEpochSecond () ;
18761881 }
18771882
18781883 /**
@@ -1893,8 +1898,8 @@ public static long getExpireTime(String cacheDuration, String validUntil) {
18931898 }
18941899
18951900 if (validUntil != null && !StringUtils .isEmpty (validUntil )) {
1896- DateTime dt = Util .parseDateTime (validUntil );
1897- long validUntilTimeInt = dt .getMillis () / 1000 ;
1901+ Instant dt = Util .parseDateTime (validUntil );
1902+ long validUntilTimeInt = dt .toEpochMilli () / 1000 ;
18981903 if (expireTime == 0 || expireTime > validUntilTimeInt ) {
18991904 expireTime = validUntilTimeInt ;
19001905 }
@@ -1940,25 +1945,7 @@ public static long getExpireTime(String cacheDuration, long validUntil) {
19401945 * @return string with format yyyy-MM-ddTHH:mm:ssZ
19411946 */
19421947 public static String formatDateTime (long timeInMillis ) {
1943- return DATE_TIME_FORMAT .print (timeInMillis );
1944- }
1945-
1946- /**
1947- * Create string form time In Millis with format yyyy-MM-ddTHH:mm:ssZ
1948- *
1949- * @param time
1950- * The time
1951- * @param millis
1952- * Defines if the time is in Millis
1953- *
1954- * @return string with format yyyy-MM-ddTHH:mm:ssZ
1955- */
1956- public static String formatDateTime (long time , boolean millis ) {
1957- if (millis ) {
1958- return DATE_TIME_FORMAT_MILLS .print (time );
1959- } else {
1960- return formatDateTime (time );
1961- }
1948+ return DATE_TIME_FORMAT .format (Instant .ofEpochMilli (timeInMillis ));
19621949 }
19631950
19641951 /**
@@ -1969,15 +1956,9 @@ public static String formatDateTime(long time, boolean millis) {
19691956 *
19701957 * @return datetime
19711958 */
1972- public static DateTime parseDateTime (String dateTime ) {
1973-
1974- DateTime parsedData = null ;
1975- try {
1976- parsedData = DATE_TIME_FORMAT .parseDateTime (dateTime );
1977- } catch (Exception e ) {
1978- return DATE_TIME_FORMAT_MILLS .parseDateTime (dateTime );
1979- }
1980- return parsedData ;
1959+ public static Instant parseDateTime (String dateTime ) {
1960+ TemporalAccessor parsedData = DATE_TIME_FORMAT .parse (dateTime );
1961+ return Instant .from (parsedData );
19811962 }
19821963
19831964 /**
@@ -2007,5 +1988,53 @@ private static byte[] toBytesUtf8(String str) {
20071988 }
20081989 }
20091990
1991+ private static Clock clock = Clock .systemUTC ();
1992+
1993+ /**
1994+ * Get current timestamp milliseconds.
1995+ *
1996+ * @return current timestamp
1997+ */
1998+ public static long getCurrentTimeMillis () {
1999+ return clock .millis ();
2000+ }
2001+
2002+ static void setFixedClock (Clock fixClock ) {
2003+ clock = fixClock ;
2004+ }
2005+
2006+ static void setSystemClock () {
2007+ clock = Clock .systemUTC ();
2008+ }
2009+
2010+ /**
2011+ * Checks if specified instant is equal to now.
2012+ *
2013+ * @param instant the instant to compare to
2014+ * @return true if instant is equal to now
2015+ */
2016+ public static boolean isEqualNow (Instant instant ) {
2017+ return instant .equals (Instant .now (clock ));
2018+ }
2019+
2020+ /**
2021+ * Checks if specified instant is before now.
2022+ *
2023+ * @param instant the instant to compare to
2024+ * @return true if instant is before now
2025+ */
2026+ public static boolean isBeforeNow (Instant instant ) {
2027+ return instant .isBefore (Instant .now (clock ));
2028+ }
2029+
2030+ /**
2031+ * Checks if specified instant is after now.
2032+ *
2033+ * @param instant the instant to compare to
2034+ * @return true if instant is before now
2035+ */
2036+ public static boolean isAfterNow (Instant instant ) {
2037+ return instant .isAfter (Instant .now (clock ));
2038+ }
20102039
20112040}
0 commit comments