Class NonceManager


  • final class NonceManager
    extends java.lang.Object
    Tracks the nonces for a given consumer key and/or token. Automagically ensures timestamp is monotonically increasing and tracks all nonces for a given timestamp.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int gcCounter
      Counts number of verification requests performed to schedule garbage collection.
      private int gcPeriod
      Verifications to perform on average before performing garbage collection.
      private long mapSize  
      private long maxAge
      The maximum valid age of a nonce timestamp, in milliseconds.
      private long maximumMapSize  
      private java.util.concurrent.TimeUnit timestampUnit  
      private java.util.SortedMap<java.lang.Long,​java.util.Map<java.lang.String,​java.util.Set<java.lang.String>>> tsToKeyNoncePairs
      Maps timestamps to key-nonce pairs.
    • Constructor Summary

      Constructors 
      Constructor Description
      NonceManager​(long maxAge, int gcPeriod, java.util.concurrent.TimeUnit timestampUnit, long maximumCacheSize)
      Create a new nonce manager configured with maximum age, old nonce cleaning period and a time unit of timestamps.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) long checkAndGetSize()
      Returns number of currently tracked timestamp-key-nonce tuples.
      (package private) void gc​(long now)
      Deletes all nonces older than maxAge.
      private static long longValue​(java.lang.String value)  
      boolean verify​(java.lang.String key, java.lang.String timestamp, java.lang.String nonce)
      Evaluates the timestamp/nonce combination for validity, storing and/or clearing nonces as required.
      (package private) boolean verify​(java.lang.String key, java.lang.String timestamp, java.lang.String nonce, long now)
      Evaluates the timestamp/nonce combination for validity, storing and/or clearing nonces as required.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • maxAge

        private final long maxAge
        The maximum valid age of a nonce timestamp, in milliseconds.
      • gcPeriod

        private final int gcPeriod
        Verifications to perform on average before performing garbage collection.
      • gcCounter

        private int gcCounter
        Counts number of verification requests performed to schedule garbage collection.
      • timestampUnit

        private final java.util.concurrent.TimeUnit timestampUnit
      • maximumMapSize

        private final long maximumMapSize
      • tsToKeyNoncePairs

        private final java.util.SortedMap<java.lang.Long,​java.util.Map<java.lang.String,​java.util.Set<java.lang.String>>> tsToKeyNoncePairs
        Maps timestamps to key-nonce pairs.
      • mapSize

        private volatile long mapSize
    • Constructor Detail

      • NonceManager

        public NonceManager​(long maxAge,
                            int gcPeriod,
                            java.util.concurrent.TimeUnit timestampUnit,
                            long maximumCacheSize)
        Create a new nonce manager configured with maximum age, old nonce cleaning period and a time unit of timestamps.
        Parameters:
        maxAge - the maximum valid age of a nonce timestamp, in milliseconds.
        gcPeriod - number of verifications to be performed on average before performing garbage collection of old nonces.
        timestampUnit - unit in which timestamps are passed to verify(String, String, String) method.
        maximumCacheSize - maximum size of the cache that keeps nonces. If the cache exceeds the method verify(String, String, String) will return false.
    • Method Detail

      • verify

        boolean verify​(java.lang.String key,
                       java.lang.String timestamp,
                       java.lang.String nonce,
                       long now)
        Evaluates the timestamp/nonce combination for validity, storing and/or clearing nonces as required.

        The method is package private in order to be used in unit tests only.

        Parameters:
        key - the oauth_consumer_key value for a given consumer request
        timestamp - the oauth_timestamp value for a given consumer request (in milliseconds).
        nonce - the oauth_nonce value for a given consumer request.
        now - current time in milliseconds
        Returns:
        true if the timestamp/nonce are valid.
      • verify

        public boolean verify​(java.lang.String key,
                              java.lang.String timestamp,
                              java.lang.String nonce)
        Evaluates the timestamp/nonce combination for validity, storing and/or clearing nonces as required.
        Parameters:
        key - the oauth_consumer_key value for a given consumer request
        timestamp - the oauth_timestamp value for a given consumer request (in milliseconds).
        nonce - the oauth_nonce value for a given consumer request.
        Returns:
        true if the timestamp/nonce are valid.
      • gc

        void gc​(long now)
        Deletes all nonces older than maxAge. This method is package private (instead of private) for testability purposes.
        Parameters:
        now - milliseconds since epoch representing "now"
      • checkAndGetSize

        long checkAndGetSize()
        Returns number of currently tracked timestamp-key-nonce tuples. The method should be used by tests only.
        Returns:
        number of currently tracked timestamp-key-nonce tuples.
      • longValue

        private static long longValue​(java.lang.String value)