@@ -17,7 +17,21 @@ class Utils
1717 redirect : "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" . freeze } . freeze
1818 DSIG = "http://www.w3.org/2000/09/xmldsig#" . freeze
1919 XENC = "http://www.w3.org/2001/04/xmlenc#" . freeze
20- DURATION_FORMAT = %r(^(-?)P(?:(?:(?:(\d +)Y)?(?:(\d +)M)?(?:(\d +)D)?(?:T(?:(\d +)H)?(?:(\d +)M)?(?:(\d +)S)?)?)|(?:(\d +)W))$) . freeze
20+ DURATION_FORMAT = %r(^
21+ (-?)P # 1: Duration sign
22+ (?:
23+ (?:(\d +)Y)? # 2: Years
24+ (?:(\d +)M)? # 3: Months
25+ (?:(\d +)D)? # 4: Days
26+ (?:T
27+ (?:(\d +)H)? # 5: Hours
28+ (?:(\d +)M)? # 6: Minutes
29+ (?:(\d +(?:[.,]\d +)?)S)? # 7: Seconds
30+ )?
31+ |
32+ (\d +)W # 8: Weeks
33+ )
34+ $)x . freeze
2135
2236 # Checks if the x509 cert provided is expired
2337 #
@@ -39,31 +53,18 @@ def self.is_cert_expired(cert)
3953 # current time.
4054 #
4155 # @return [Integer] The new timestamp, after the duration is applied.
42- #
56+ #
4357 def self . parse_duration ( duration , timestamp = Time . now . utc )
4458 matches = duration . match ( DURATION_FORMAT )
45-
59+
4660 if matches . nil?
4761 raise Exception . new ( "Invalid ISO 8601 duration" )
4862 end
4963
50- durYears = matches [ 2 ] . to_i
51- durMonths = matches [ 3 ] . to_i
52- durDays = matches [ 4 ] . to_i
53- durHours = matches [ 5 ] . to_i
54- durMinutes = matches [ 6 ] . to_i
55- durSeconds = matches [ 7 ] . to_f
56- durWeeks = matches [ 8 ] . to_i
57-
58- if matches [ 1 ] == "-"
59- durYears = -durYears
60- durMonths = -durMonths
61- durDays = -durDays
62- durHours = -durHours
63- durMinutes = -durMinutes
64- durSeconds = -durSeconds
65- durWeeks = -durWeeks
66- end
64+ sign = matches [ 1 ] == '-' ? -1 : 1
65+
66+ durYears , durMonths , durDays , durHours , durMinutes , durSeconds , durWeeks =
67+ matches [ 2 ..8 ] . map { |match | match ? sign * match . tr ( ',' , '.' ) . to_f : 0.0 }
6768
6869 initial_datetime = Time . at ( timestamp ) . utc . to_datetime
6970 final_datetime = initial_datetime . next_year ( durYears )
0 commit comments