2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the
7
* "License"); you may not use this file except in compliance
8
* with the License. You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
package org.apache.zookeeper.server.auth;
21
import java.security.MessageDigest;
22
import java.security.NoSuchAlgorithmException;
24
import org.apache.log4j.Logger;
25
import org.apache.zookeeper.KeeperException;
26
import org.apache.zookeeper.data.Id;
27
import org.apache.zookeeper.server.ServerCnxn;
29
public class DigestAuthenticationProvider implements AuthenticationProvider {
30
private static final Logger LOG =
31
Logger.getLogger(DigestAuthenticationProvider.class);
33
/** specify a command line property with key of
34
* "zookeeper.DigestAuthenticationProvider.superDigest"
35
* and value of "super:<base64encoded(SHA1(password))>" to enable
36
* super user access (i.e. acls disabled)
38
private final static String superDigest = System.getProperty(
39
"zookeeper.DigestAuthenticationProvider.superDigest");
41
public String getScheme() {
45
static final private String base64Encode(byte b[]) {
46
StringBuilder sb = new StringBuilder();
47
for (int i = 0; i < b.length;) {
49
int v = (b[i++] & 0xff) << 16;
51
v |= (b[i++] & 0xff) << 8;
60
sb.append(encode(v >> 18));
61
sb.append(encode(v >> 12));
63
sb.append(encode(v >> 6));
76
static final private char encode(int i) {
79
return (char) ('A' + i);
82
return (char) ('a' + i - 26);
85
return (char) ('0' + i - 52);
87
return i == 62 ? '+' : '/';
90
static public String generateDigest(String idPassword)
91
throws NoSuchAlgorithmException {
92
String parts[] = idPassword.split(":", 2);
93
byte digest[] = MessageDigest.getInstance("SHA1").digest(
94
idPassword.getBytes());
95
return parts[0] + ":" + base64Encode(digest);
98
public KeeperException.Code
99
handleAuthentication(ServerCnxn cnxn, byte[] authData)
101
String id = new String(authData);
103
String digest = generateDigest(id);
104
if (digest.equals(superDigest)) {
105
cnxn.getAuthInfo().add(new Id("super", ""));
107
cnxn.getAuthInfo().add(new Id(getScheme(), digest));
108
return KeeperException.Code.OK;
109
} catch (NoSuchAlgorithmException e) {
110
LOG.error("Missing algorithm",e);
112
return KeeperException.Code.AUTHFAILED;
115
public boolean isAuthenticated() {
119
public boolean isValid(String id) {
120
String parts[] = id.split(":");
121
return parts.length == 2;
124
public boolean matches(String id, String aclExpr) {
125
return id.equals(aclExpr);
128
/** Call with a single argument of user:pass to generate authdata.
129
* Authdata output can be used when setting superDigest for example.
130
* @param args single argument of user:pass
131
* @throws NoSuchAlgorithmException
133
public static void main(String args[]) throws NoSuchAlgorithmException {
134
for (int i = 0; i < args.length; i++) {
135
System.out.println(args[i] + "->" + generateDigest(args[i]));