Authenticator supporting the DIGEST authentication method.
| 30 | * Authenticator supporting the DIGEST authentication method. |
| 31 | */ |
| 32 | public class DigestAuthenticator extends Authenticator { |
| 33 | |
| 34 | /** |
| 35 | * Construct a new DigestAuthenticator. |
| 36 | */ |
| 37 | public DigestAuthenticator() { |
| 38 | super(); |
| 39 | } |
| 40 | |
| 41 | private static final StringManager sm = StringManager.getManager(DigestAuthenticator.class); |
| 42 | |
| 43 | /** |
| 44 | * Name of the DIGEST authentication scheme. |
| 45 | */ |
| 46 | public static final String schemeName = "digest"; |
| 47 | private static final Object cnonceGeneratorLock = new Object(); |
| 48 | private static volatile SecureRandom cnonceGenerator; |
| 49 | private int nonceCount = 0; |
| 50 | private long cNonce; |
| 51 | |
| 52 | @Override |
| 53 | public String getAuthorization(String method, String requestUri, String authenticateHeader, String userName, |
| 54 | String userPassword, String userRealm) throws AuthenticationException { |
| 55 | |
| 56 | validateUsername(userName); |
| 57 | validatePassword(userPassword); |
| 58 | |
| 59 | Map<String,String> parameterMap = parseAuthenticateHeader(authenticateHeader); |
| 60 | String realm = parameterMap.get("realm"); |
| 61 | |
| 62 | validateRealm(userRealm, realm); |
| 63 | |
| 64 | String nonce = parameterMap.get("nonce"); |
| 65 | String messageQop = parameterMap.get("qop"); |
| 66 | if (messageQop == null || messageQop.isEmpty()) { |
| 67 | throw new AuthenticationException(sm.getString("digestAuthenticator.noQop")); |
| 68 | } |
| 69 | String algorithm = parameterMap.get("algorithm") == null ? "MD5" : parameterMap.get("algorithm"); |
| 70 | String opaque = parameterMap.get("opaque"); |
| 71 | if (cnonceGenerator == null) { |
| 72 | synchronized (cnonceGeneratorLock) { |
| 73 | if (cnonceGenerator == null) { |
| 74 | cnonceGenerator = new SecureRandom(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | cNonce = cnonceGenerator.nextLong(); |
| 79 | nonceCount++; |
| 80 | |
| 81 | StringBuilder challenge = new StringBuilder(); |
| 82 | |
| 83 | challenge.append("Digest "); |
| 84 | challenge.append("username=\"").append(userName).append("\","); |
| 85 | challenge.append("realm=\"").append(realm).append("\","); |
| 86 | challenge.append("nonce=\"").append(nonce).append("\","); |
| 87 | challenge.append("uri=\"").append(requestUri).append("\","); |
| 88 | |
| 89 | try { |
nothing calls this directly
no test coverage detected