Class DigestScheme
- All Implemented Interfaces:
Serializable, AuthScheme
Since the digest username is included as clear text in the generated Authentication header, the charset of the username must be compatible with the HTTP element charset used by the connection.
- Since:
- 4.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enumEnum representing supported digest algorithms for HTTP Digest Authentication, including session-based variants.private static enumRepresent the possible values of quality of protection. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]private byte[]private ByteArrayBuilderprivate Stringprivate booleanprivate UsernamePasswordCredentialsprivate Charsetprivate static final char[]Hexa values used when creating 32 character long digest in HTTP DigestScheme in case of authentication.private Stringprivate static final org.slf4j.Loggerprivate longprivate static final longprivate booleanFlag indicating whether username hashing is supported. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate booleancontainsInvalidABNFChars(String value) Checks if a given string contains characters that are not allowed in an ABNF quoted-string as per standard specifications.(package private) static byte[]createCnonce(DigestScheme.DigestAlgorithm algorithm) Creates a random cnonce value based on the specified algorithm's expected entropy.private StringcreateDigestResponse(org.apache.hc.core5.http.HttpRequest request) private static MessageDigestcreateMessageDigest(String digAlg) (package private) static StringformatHex(byte[] binaryData) Encodes a byte array digest into a hexadecimal string.generateAuthResponse(org.apache.hc.core5.http.HttpHost host, org.apache.hc.core5.http.HttpRequest request, org.apache.hc.core5.http.protocol.HttpContext context) Generates an authorization response based on the current state.(package private) StringgetA1()(package private) StringgetA2()getName()Returns textual designation of the given authentication scheme.getNonce()longReturnsPrincipalwhose credentials are used to generate an authentication response.getRealm()Returns authentication realm.voidinitPreemptive(Credentials credentials, String cnonce, String realm) booleanAuthentication process may involve a series of challenge-response exchanges.booleanDetermines if the authentication scheme is expected to provide an authorization response on a per connection basis instead of the standard per request basisbooleanisResponseReady(org.apache.hc.core5.http.HttpHost host, CredentialsProvider credentialsProvider, org.apache.hc.core5.http.protocol.HttpContext context) Determines whether or not an authorization response can be generated based on the actual authentication state.voidprocessChallenge(AuthChallenge authChallenge, org.apache.hc.core5.http.protocol.HttpContext context) Processes the given auth challenge.private voidtoString()private voidMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface AuthScheme
isChallengeExpected, processChallenge
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
LOG
private static final org.slf4j.Logger LOG -
HEXADECIMAL
private static final char[] HEXADECIMALHexa values used when creating 32 character long digest in HTTP DigestScheme in case of authentication.- See Also:
-
defaultCharset
-
paramMap
-
complete
private boolean complete -
buffer
-
userhashSupported
private boolean userhashSupportedFlag indicating whether username hashing is supported.This flag is used to determine if the server supports hashing of the username as part of the Digest Access Authentication process. When set to
true, the client is expected to hash the username using the same algorithm used for hashing the credentials. This is in accordance with Section 3.4.4 of RFC 7616.The default value is
false, indicating that username hashing is not supported. If the server requires username hashing (indicated by theuserhashparameter in the a header set totrue), this flag should be set totrueto comply with the server's requirements. -
lastNonce
-
nounceCount
private long nounceCount -
cnonce
-
a1
private byte[] a1 -
a2
private byte[] a2 -
credentials
-
-
Constructor Details
-
DigestScheme
public DigestScheme() -
DigestScheme
Deprecated.This constructor is deprecated to enforce the use ofStandardCharsets.UTF_8encoding in compliance with RFC 7616 for HTTP Digest Access Authentication. Use the default constructorDigestScheme()instead.- Parameters:
charset- theCharsetset to be used for encoding credentials. This parameter is ignored as UTF-8 is always used.
-
-
Method Details
-
initPreemptive
-
getName
Description copied from interface:AuthSchemeReturns textual designation of the given authentication scheme.- Specified by:
getNamein interfaceAuthScheme- Returns:
- the name of the given authentication scheme
-
isConnectionBased
public boolean isConnectionBased()Description copied from interface:AuthSchemeDetermines if the authentication scheme is expected to provide an authorization response on a per connection basis instead of the standard per request basis- Specified by:
isConnectionBasedin interfaceAuthScheme- Returns:
trueif the scheme is connection based,falseif the scheme is request based.
-
getRealm
Description copied from interface:AuthSchemeReturns authentication realm. If the concept of an authentication realm is not applicable to the given authentication scheme, returnsnull.- Specified by:
getRealmin interfaceAuthScheme- Returns:
- the authentication realm
-
processChallenge
public void processChallenge(AuthChallenge authChallenge, org.apache.hc.core5.http.protocol.HttpContext context) throws MalformedChallengeException Description copied from interface:AuthSchemeProcesses the given auth challenge. Some authentication schemes may involve multiple challenge-response exchanges. Such schemes must be able to maintain internal state when dealing with sequential challengesPlease note auth schemes that perform mutual authentication must implement
AuthScheme.processChallenge(HttpHost, boolean, AuthChallenge, HttpContext)andAuthScheme.isChallengeExpected()instead.- Specified by:
processChallengein interfaceAuthScheme- Parameters:
authChallenge- the auth challengecontext- HTTP context- Throws:
MalformedChallengeException- in case the auth challenge is incomplete, malformed or otherwise invalid.- See Also:
-
isChallengeComplete
public boolean isChallengeComplete()Description copied from interface:AuthSchemeAuthentication process may involve a series of challenge-response exchanges. This method tests if the authorization process has been fully completed (either successfully or unsuccessfully), that is, all the required authorization challenges have been processed in their entirety.Please note if the scheme returns
truefrom this method in response to a challenge, it effectively implies a failure to respond to this challenge and termination of the authentication process.- Specified by:
isChallengeCompletein interfaceAuthScheme- Returns:
trueif the authentication process has been completed,falseotherwise.
-
isResponseReady
public boolean isResponseReady(org.apache.hc.core5.http.HttpHost host, CredentialsProvider credentialsProvider, org.apache.hc.core5.http.protocol.HttpContext context) throws AuthenticationException Description copied from interface:AuthSchemeDetermines whether or not an authorization response can be generated based on the actual authentication state. Generally the outcome of this method will depend upon availability of user credentials necessary to produce an authorization response.- Specified by:
isResponseReadyin interfaceAuthScheme- Parameters:
credentialsProvider- The credentials to be used for authenticationcontext- HTTP context- Returns:
trueif an authorization response can be generated and the authentication handshake can proceed,falseotherwise.- Throws:
AuthenticationException- if authorization string cannot be generated due to an authentication failure
-
getPrincipal
Description copied from interface:AuthSchemeReturnsPrincipalwhose credentials are used to generate an authentication response. Connection based schemes are required to return a userPrincipalif authorization applies to for the entire life span of connection.- Specified by:
getPrincipalin interfaceAuthScheme- Returns:
- user principal
- See Also:
-
generateAuthResponse
public String generateAuthResponse(org.apache.hc.core5.http.HttpHost host, org.apache.hc.core5.http.HttpRequest request, org.apache.hc.core5.http.protocol.HttpContext context) throws AuthenticationException Description copied from interface:AuthSchemeGenerates an authorization response based on the current state. Some authentication schemes may need to load user credentials required to generate an authorization response from aCredentialsProviderprior to this method call.- Specified by:
generateAuthResponsein interfaceAuthScheme- Parameters:
request- The request being authenticatedcontext- HTTP context- Returns:
- authorization header
- Throws:
AuthenticationException- if authorization string cannot be generated due to an authentication failure- See Also:
-
createMessageDigest
private static MessageDigest createMessageDigest(String digAlg) throws UnsupportedDigestAlgorithmException -
createDigestResponse
private String createDigestResponse(org.apache.hc.core5.http.HttpRequest request) throws AuthenticationException - Throws:
AuthenticationException
-
getNonce
-
getNounceCount
@Internal public long getNounceCount() -
getCnonce
-
getA1
String getA1() -
getA2
String getA2() -
formatHex
Encodes a byte array digest into a hexadecimal string.This method supports digests of various lengths, such as 16 bytes (128-bit) for MD5, 32 bytes (256-bit) for SHA-256, and SHA-512/256. Each byte is converted to two hexadecimal characters, so the resulting string length is twice the byte array length.
- Parameters:
binaryData- the array containing the digest bytes- Returns:
- encoded hexadecimal string, or
nullif encoding failed
-
createCnonce
Creates a random cnonce value based on the specified algorithm's expected entropy. Adjusts the length of the byte array based on the algorithm to ensure sufficient entropy.- Parameters:
algorithm- the algorithm for which the cnonce is being generated (e.g., "MD5", "SHA-256", "SHA-512-256").- Returns:
- The cnonce value as a byte array.
- Since:
- 5.5
-
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOExceptionClassNotFoundException
-
toString
-
containsInvalidABNFChars
Checks if a given string contains characters that are not allowed in an ABNF quoted-string as per standard specifications.The method checks for: - Control characters (ASCII 0x00 to 0x1F and 0x7F). - Characters outside the printable ASCII range (above 0x7E). - Double quotes (") and backslashes (\), which are not allowed.
- Parameters:
value- The string to be checked for invalid ABNF characters.- Returns:
trueif invalid characters are found,falseotherwise.- Throws:
IllegalArgumentException- if the input string is null.
-
StandardCharsets.UTF_8encoding in compliance with RFC 7616 for HTTP Digest Access Authentication.