Class ScramScheme
java.lang.Object
org.apache.hc.client5.http.impl.auth.ScramScheme
- All Implemented Interfaces:
AuthScheme
@Contract(threading=UNSAFE)
@Experimental
public final class ScramScheme
extends Object
implements AuthScheme
Strict HTTP SCRAM client implementing
SCRAM-SHA-256 per RFC 7804
with SCRAM core per RFC 5802/7677.
HTTP SCRAM uses no channel binding (GS2 header "n,,"; c=biws).
Experimental: This API is work in progress and may change without notice in a future release.
- Since:
- 5.6
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classprivate static enum -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Base64.Encoderprivate static final Base64.Decoderprivate static final Stringprivate Stringprivate Stringprivate booleanprivate static final intprivate static final intprivate byte[]private static final Stringprivate intprivate static final org.slf4j.Loggerprivate final intprivate final intprivate char[]private Principalprivate Stringprivate byte[]private final SecureRandomprivate Stringprivate Stringprivate Stringprivate ScramScheme.Stateprivate Stringprivate final int -
Constructor Summary
ConstructorsConstructorDescriptionDefault policy: warn ifi < 4096, no hard enforcement; SHA-256 only.ScramScheme(int warnMinIterations, int minIterationsRequired, int maxIterationsAllowed, SecureRandom rnd) Constructor with custom iteration policy.ScramScheme(int warnMinIterations, int minIterationsRequired, SecureRandom rnd) Constructor with custom iteration policy. -
Method Summary
Modifier and TypeMethodDescriptionprivate static Stringb64ToString(String b64) private Stringprivate Stringprivate static StringescapeUser(String user) 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.private StringgenNonce()getName()Returns textual designation of the scheme.ReturnsPrincipalwhose credentials are used.getRealm()Returns authentication realm.private static byte[]hiPBKDF2(char[] password, byte[] salt, int iterations, int dkLen) private static byte[]booleanAuthentication process may involve a series of challenge-response exchanges.booleanSCRAM must inspect final responses to verifyv=inAuthentication-Info.booleanSCRAM is per-request (no connection binding).booleanisResponseReady(org.apache.hc.core5.http.HttpHost host, CredentialsProvider credentialsProvider, org.apache.hc.core5.http.protocol.HttpContext context) Allow response when: - INIT (preemptive client-first) — only if creds have been prepared - ANNOUNCED (401 without data) - SERVER_FIRST_RCVD (ready to send client-final)parseAttrs(String s) voidprocessChallenge(AuthChallenge authChallenge, org.apache.hc.core5.http.protocol.HttpContext context) Legacy entry point: wrapsAuthenticationExceptionasMalformedChallengeException.voidprocessChallenge(org.apache.hc.core5.http.HttpHost host, boolean challenged, AuthChallenge authChallenge, org.apache.hc.core5.http.protocol.HttpContext context) Handles 401 challenges (with/withoutdata) and final responses carryingAuthentication-Info(any status code).private static StringquoteParam(String v) private static byte[]sha256(byte[] in) private static StringtoParamMap(List<org.apache.hc.core5.http.NameValuePair> pairs) private static byte[]xor(byte[] a, byte[] b) private static voidzero(byte[] a) private void
-
Field Details
-
LOG
private static final org.slf4j.Logger LOG -
DEFAULT_WARN_MIN_ITERATIONS
private static final int DEFAULT_WARN_MIN_ITERATIONS- See Also:
-
DEFAULT_MAX_ITERATIONS_ALLOWED
private static final int DEFAULT_MAX_ITERATIONS_ALLOWED- See Also:
-
GS2_HEADER
- See Also:
-
C_BIND_B64
- See Also:
-
B64
-
B64D
-
secureRandom
-
warnMinIterations
private final int warnMinIterations -
minIterationsRequired
private final int minIterationsRequired -
maxIterationsAllowed
private final int maxIterationsAllowed -
state
-
complete
private boolean complete -
realm
-
sid
-
username
-
password
private char[] password -
principal
-
clientNonce
-
clientFirstBare
-
serverFirstRaw
-
serverNonce
-
salt
private byte[] salt -
iterations
private int iterations -
expectedV
private byte[] expectedV
-
-
Constructor Details
-
ScramScheme
public ScramScheme()Default policy: warn ifi < 4096, no hard enforcement; SHA-256 only.- Since:
- 5.6
-
ScramScheme
Constructor with custom iteration policy.- Parameters:
warnMinIterations- warn if iteration count is lower than this (0 disables warnings)minIterationsRequired- fail if iteration count is lower than this (0 disables enforcement)rnd- optional secure random source (null uses system default)- Since:
- 5.6
-
ScramScheme
public ScramScheme(int warnMinIterations, int minIterationsRequired, int maxIterationsAllowed, SecureRandom rnd) Constructor with custom iteration policy.- Parameters:
warnMinIterations- warn if iteration count is lower than this (0 disables warnings)minIterationsRequired- fail if iteration count is lower than this (0 disables enforcement)maxIterationsAllowed- fail if iteration count is greater than this (must be positive)rnd- optional secure random source (null uses system default)- Since:
- 5.6
-
-
Method Details
-
getName
Returns textual designation of the scheme.- Specified by:
getNamein interfaceAuthScheme- Returns:
- the name of the given authentication scheme
- Since:
- 5.6
-
isConnectionBased
public boolean isConnectionBased()SCRAM is per-request (no connection binding).- Specified by:
isConnectionBasedin interfaceAuthScheme- Returns:
trueif the scheme is connection based,falseif the scheme is request based.- Since:
- 5.6
-
isChallengeExpected
public boolean isChallengeExpected()SCRAM must inspect final responses to verifyv=inAuthentication-Info.- Specified by:
isChallengeExpectedin interfaceAuthScheme- Returns:
- true if responses with non 401/407 response codes must be processed by the scheme.
- Since:
- 5.6
-
processChallenge
public void processChallenge(AuthChallenge authChallenge, org.apache.hc.core5.http.protocol.HttpContext context) throws MalformedChallengeException Legacy entry point: wrapsAuthenticationExceptionasMalformedChallengeException.- Specified by:
processChallengein interfaceAuthScheme- Parameters:
authChallenge- the auth challengecontext- HTTP context- Throws:
MalformedChallengeException- in case the auth challenge is incomplete, malformed or otherwise invalid.- Since:
- 5.6
- See Also:
-
processChallenge
public void processChallenge(org.apache.hc.core5.http.HttpHost host, boolean challenged, AuthChallenge authChallenge, org.apache.hc.core5.http.protocol.HttpContext context) throws MalformedChallengeException, AuthenticationException Handles 401 challenges (with/withoutdata) and final responses carryingAuthentication-Info(any status code).- Specified by:
processChallengein interfaceAuthScheme- Parameters:
host- HTTP hostchallenged- true if the response was unauthorised (401/407)authChallenge- the auth challenge or null if no challenge was receivedcontext- HTTP context- Throws:
MalformedChallengeException- in case the auth challenge is incomplete,AuthenticationException- in case the authentication process is unsuccessful.- Since:
- 5.6
-
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.- Since:
- 5.6
-
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
- Since:
- 5.6
-
isResponseReady
public boolean isResponseReady(org.apache.hc.core5.http.HttpHost host, CredentialsProvider credentialsProvider, org.apache.hc.core5.http.protocol.HttpContext context) throws AuthenticationException Allow response when: - INIT (preemptive client-first) — only if creds have been prepared - ANNOUNCED (401 without data) - SERVER_FIRST_RCVD (ready to send client-final)- 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- Since:
- 5.6
-
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- Since:
- 5.6
- See Also:
-
getPrincipal
ReturnsPrincipalwhose credentials are used.- Specified by:
getPrincipalin interfaceAuthScheme- Returns:
- user principal
- Since:
- 5.6
- See Also:
-
buildClientFirst
-
buildClientFinalAndExpectV
- Throws:
AuthenticationException
-
zero
private static void zero(byte[] a) -
zeroAndClearExpectedV
private void zeroAndClearExpectedV() -
toParamMap
-
parseAttrs
- Throws:
MalformedChallengeException
-
genNonce
-
escapeUser
-
quoteParam
-
hiPBKDF2
private static byte[] hiPBKDF2(char[] password, byte[] salt, int iterations, int dkLen) throws GeneralSecurityException - Throws:
GeneralSecurityException
-
hmac
- Throws:
GeneralSecurityException
-
sha256
- Throws:
GeneralSecurityException
-
xor
private static byte[] xor(byte[] a, byte[] b) -
stringToB64
-
b64ToString
- Throws:
MalformedChallengeException
-