001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.util;
019
020
021import java.util.Date;
022
023
024/**
025 * Date utilities. Use {@link com.nimbusds.jwt.util.DateUtils} instead.
026 */
027@Deprecated
028public class DateUtils {
029
030
031        /**
032         * Converts the specified date object to a Unix epoch time in seconds.
033         *
034         * @param date The date. Must not be {@code null}.
035         *
036         * @return The Unix epoch time, in seconds.
037         */
038        public static long toSecondsSinceEpoch(final Date date) {
039
040                return date.getTime() / 1000L;
041        }
042
043
044        /**
045         * Converts the specified Unix epoch time in seconds to a date object.
046         *
047         * @param time The Unix epoch time, in seconds. Must not be negative.
048         *
049         * @return The date.
050         */
051        public static Date fromSecondsSinceEpoch(final long time) {
052
053                return new Date(time * 1000L);
054        }
055
056
057        /**
058         * Check if the specified date is after the specified reference, given
059         * the maximum accepted negative clock skew.
060         *
061         * <p>Formula:
062         *
063         * <pre>
064         * return date + clock_skew &gt; reference
065         * </pre>
066         *
067         * Example: Ensure a JWT expiration (exp) timestamp is after the
068         * current time, with a minute of acceptable clock skew.
069         *
070         * <pre>
071         * boolean valid = DateUtils.isAfter(exp, new Date(), 60);
072         * </pre>
073         *
074         * @param date                The date to check. Must not be
075         *                            {@code null}.
076         * @param reference           The reference date (e.g. the current
077         *                            time). Must not be {@code null}.
078         * @param maxClockSkewSeconds The maximum acceptable negative clock
079         *                            skew of the date value to check, in
080         *                            seconds.
081         *
082         * @return {@code true} if the date is before the reference, plus the
083         *         maximum accepted clock skew, else {@code false}.
084         */
085        public static boolean isAfter(final Date date,
086                                      final Date reference,
087                                      final long maxClockSkewSeconds) {
088
089                return new Date(date.getTime() + maxClockSkewSeconds*1000L).after(reference);
090        }
091
092
093        /**
094         * Checks if the specified date is before the specified reference,
095         * given the maximum accepted positive clock skew.
096         *
097         * <p>Formula:
098         *
099         * <pre>
100         * return date - clock_skew &lt; reference
101         * </pre>
102         *
103         * Example: Ensure a JWT issued-at (iat) timestamp is before the
104         * current time, with a minute of acceptable clock skew.
105         *
106         * <pre>
107         * boolean valid = DateUtils.isBefore(iat, new Date(), 60);
108         * </pre>
109         *
110         * @param date                The date to check. Must not be
111         *                            {@code null}.
112         * @param reference           The reference date (e.g. the current
113         *                            time). Must not be {@code null}.
114         * @param maxClockSkewSeconds The maximum acceptable clock skew of the
115         *                            date value to check, in seconds.
116         *
117         * @return {@code true} if the date is before the reference, minus the
118         *         maximum accepted clock skew, else {@code false}.
119         */
120        public static boolean isBefore(final Date date,
121                                       final Date reference,
122                                       final long maxClockSkewSeconds) {
123
124                return new Date(date.getTime() - maxClockSkewSeconds*1000L).before(reference);
125        }
126
127
128        /**
129         * Prevents instantiation.
130         */
131        private DateUtils() { }
132}