Class DigestAuthenticator


  • final class DigestAuthenticator
    extends java.lang.Object
    Implementation of Digest Http Authentication method (RFC 2617).
    • Field Detail

      • HEX_ARRAY

        private static final char[] HEX_ARRAY
      • KEY_VALUE_PAIR_PATTERN

        private static final java.util.regex.Pattern KEY_VALUE_PAIR_PATTERN
      • CLIENT_NONCE_BYTE_COUNT

        private static final int CLIENT_NONCE_BYTE_COUNT
        See Also:
        Constant Field Values
      • randomGenerator

        private final java.security.SecureRandom randomGenerator
    • Constructor Detail

      • DigestAuthenticator

        DigestAuthenticator​(HttpAuthenticationFilter.Credentials credentials,
                            int limit)
        Create a new instance initialized from credentials and configuration.
        Parameters:
        credentials - Credentials. Can be null if there are no default credentials.
        limit - Maximum number of URIs that should be kept in the cache containing URIs and their DigestAuthenticator.DigestScheme.
    • Method Detail

      • filterRequest

        boolean filterRequest​(javax.ws.rs.client.ClientRequestContext request)
                       throws java.io.IOException
        Process request and add authentication information if possible.
        Parameters:
        request - Request context.
        Returns:
        true if authentication information was added.
        Throws:
        java.io.IOException - When error with encryption occurs.
      • filterResponse

        public boolean filterResponse​(javax.ws.rs.client.ClientRequestContext request,
                                      javax.ws.rs.client.ClientResponseContext response)
                               throws java.io.IOException
        Process response and repeat the request if digest authentication is requested. When request is repeated the response will be modified to contain new response information.
        Parameters:
        request - Request context.
        response - Response context (will be updated with newest response data if the request was repeated).
        Returns:
        true if response does not require authentication or if authentication is required, new request was done with digest authentication information and authentication was successful.
        Throws:
        java.io.IOException - When error with encryption occurs.
      • parseAuthHeaders

        private DigestAuthenticator.DigestScheme parseAuthHeaders​(java.util.List<?> headers)
                                                           throws java.io.IOException
        Parse digest header.
        Parameters:
        headers - List of header strings
        Returns:
        DigestScheme or null if no digest header exists.
        Throws:
        java.io.IOException
      • createNextAuthToken

        private java.lang.String createNextAuthToken​(DigestAuthenticator.DigestScheme ds,
                                                     javax.ws.rs.client.ClientRequestContext requestContext,
                                                     HttpAuthenticationFilter.Credentials credentials)
                                              throws java.io.IOException
        Creates digest string including counter.
        Parameters:
        ds - DigestScheme instance
        requestContext - client request context
        Returns:
        digest authentication token string
        Throws:
        java.io.IOException
      • append

        private static void append​(java.lang.StringBuilder sb,
                                   java.lang.String key,
                                   java.lang.String value,
                                   boolean useQuote)
        Append comma separated key=value token
        Parameters:
        sb - string builder instance
        key - key string
        value - value string
        useQuote - true if value needs to be enclosed in quotes
      • append

        private static void append​(java.lang.StringBuilder sb,
                                   java.lang.String key,
                                   java.lang.String value)
        Append comma separated key=value token. The value gets enclosed in quotes.
        Parameters:
        sb - string builder instance
        key - key string
        value - value string
      • bytesToHex

        private static java.lang.String bytesToHex​(byte[] bytes)
        Convert bytes array to hex string.
        Parameters:
        bytes - array of bytes
        Returns:
        hex string
      • md5

        private static java.lang.String md5​(java.lang.String... tokens)
                                     throws java.io.IOException
        Colon separated value MD5 hash.
        Parameters:
        tokens - one or more strings
        Returns:
        M5 hash string
        Throws:
        java.io.IOException
      • randomBytes

        private java.lang.String randomBytes​(int nbBytes)
        Generate a random sequence of bytes and return its hex representation
        Parameters:
        nbBytes - number of bytes to generate
        Returns:
        hex string