~juju/pyjuju/zookeeper-vendor

« back to all changes in this revision

Viewing changes to src/java/main/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java

  • Committer: Gustavo Niemeyer
  • Date: 2011-05-24 20:53:37 UTC
  • Revision ID: gustavo@niemeyer.net-20110524205337-i11yow5biap5xapo
Importing stock Zookeeper 3.3.3 without jars.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
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
 
9
 *
 
10
 *     http://www.apache.org/licenses/LICENSE-2.0
 
11
 *
 
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.
 
17
 */
 
18
 
 
19
package org.apache.zookeeper.server.auth;
 
20
 
 
21
import java.security.MessageDigest;
 
22
import java.security.NoSuchAlgorithmException;
 
23
 
 
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;
 
28
 
 
29
public class DigestAuthenticationProvider implements AuthenticationProvider {
 
30
    private static final Logger LOG =
 
31
        Logger.getLogger(DigestAuthenticationProvider.class);
 
32
 
 
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)
 
37
     */
 
38
    private final static String superDigest = System.getProperty(
 
39
        "zookeeper.DigestAuthenticationProvider.superDigest");
 
40
 
 
41
    public String getScheme() {
 
42
        return "digest";
 
43
    }
 
44
 
 
45
    static final private String base64Encode(byte b[]) {
 
46
        StringBuilder sb = new StringBuilder();
 
47
        for (int i = 0; i < b.length;) {
 
48
            int pad = 0;
 
49
            int v = (b[i++] & 0xff) << 16;
 
50
            if (i < b.length) {
 
51
                v |= (b[i++] & 0xff) << 8;
 
52
            } else {
 
53
                pad++;
 
54
            }
 
55
            if (i < b.length) {
 
56
                v |= (b[i++] & 0xff);
 
57
            } else {
 
58
                pad++;
 
59
            }
 
60
            sb.append(encode(v >> 18));
 
61
            sb.append(encode(v >> 12));
 
62
            if (pad < 2) {
 
63
                sb.append(encode(v >> 6));
 
64
            } else {
 
65
                sb.append('=');
 
66
            }
 
67
            if (pad < 1) {
 
68
                sb.append(encode(v));
 
69
            } else {
 
70
                sb.append('=');
 
71
            }
 
72
        }
 
73
        return sb.toString();
 
74
    }
 
75
 
 
76
    static final private char encode(int i) {
 
77
        i &= 0x3f;
 
78
        if (i < 26) {
 
79
            return (char) ('A' + i);
 
80
        }
 
81
        if (i < 52) {
 
82
            return (char) ('a' + i - 26);
 
83
        }
 
84
        if (i < 62) {
 
85
            return (char) ('0' + i - 52);
 
86
        }
 
87
        return i == 62 ? '+' : '/';
 
88
    }
 
89
 
 
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);
 
96
    }
 
97
 
 
98
    public KeeperException.Code 
 
99
        handleAuthentication(ServerCnxn cnxn, byte[] authData)
 
100
    {
 
101
        String id = new String(authData);
 
102
        try {
 
103
            String digest = generateDigest(id);
 
104
            if (digest.equals(superDigest)) {
 
105
                cnxn.getAuthInfo().add(new Id("super", ""));
 
106
            }
 
107
            cnxn.getAuthInfo().add(new Id(getScheme(), digest));
 
108
            return KeeperException.Code.OK;
 
109
        } catch (NoSuchAlgorithmException e) {
 
110
            LOG.error("Missing algorithm",e);
 
111
        }
 
112
        return KeeperException.Code.AUTHFAILED;
 
113
    }
 
114
 
 
115
    public boolean isAuthenticated() {
 
116
        return true;
 
117
    }
 
118
 
 
119
    public boolean isValid(String id) {
 
120
        String parts[] = id.split(":");
 
121
        return parts.length == 2;
 
122
    }
 
123
 
 
124
    public boolean matches(String id, String aclExpr) {
 
125
        return id.equals(aclExpr);
 
126
    }
 
127
 
 
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
 
132
     */
 
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]));
 
136
        }
 
137
    }
 
138
}