Class NonceManager

java.lang.Object
org.glassfish.jersey.server.oauth1.NonceManager

final class NonceManager extends 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
    Counts number of verification requests performed to schedule garbage collection.
    private final int
    Verifications to perform on average before performing garbage collection.
    private long
     
    private final long
    The maximum valid age of a nonce timestamp, in milliseconds.
    private final long
     
    private final TimeUnit
     
    private final SortedMap<Long,Map<String,Set<String>>>
    Maps timestamps to key-nonce pairs.
  • Constructor Summary

    Constructors
    Constructor
    Description
    NonceManager(long maxAge, int gcPeriod, 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

    Modifier and Type
    Method
    Description
    (package private) long
    Returns number of currently tracked timestamp-key-nonce tuples.
    (package private) void
    gc(long now)
    Deletes all nonces older than maxAge.
    private static long
     
    boolean
    verify(String key, String timestamp, String nonce)
    Evaluates the timestamp/nonce combination for validity, storing and/or clearing nonces as required.
    (package private) boolean
    verify(String key, String timestamp, 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 Details

    • 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 TimeUnit timestampUnit
    • maximumMapSize

      private final long maximumMapSize
    • tsToKeyNoncePairs

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

      private volatile long mapSize
  • Constructor Details

    • NonceManager

      public NonceManager(long maxAge, int gcPeriod, 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 Details

    • verify

      boolean verify(String key, String timestamp, 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(String key, String timestamp, 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(String value)