blob: e7a8aa242a6127c11f9ef575974c66695a9a324b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.jndi.ldap;
27
28import java.util.Arrays; // JDK 1.2
29import java.util.Hashtable;
30
31import java.io.OutputStream;
32import javax.naming.ldap.Control;
33
34/**
35 * Extends SimpleClientId to add property values specific for Digest-MD5.
36 * This includes:
37 * realm, authzid, qop, strength, maxbuffer, mutual-auth, reuse,
38 * all policy-related selection properties.
39 * Two DigestClientIds are identical iff they pass the SimpleClientId
40 * equals() test and that all of these property values are the same.
41 *
42 * @author Rosanna Lee
43 */
44class DigestClientId extends SimpleClientId {
45 private static final String[] SASL_PROPS = {
46 "java.naming.security.sasl.authorizationId",
47 "java.naming.security.sasl.realm",
48 "javax.security.sasl.qop",
49 "javax.security.sasl.strength",
50 "javax.security.sasl.reuse",
51 "javax.security.sasl.server.authentication",
52 "javax.security.sasl.maxbuffer",
53 "javax.security.sasl.policy.noplaintext",
54 "javax.security.sasl.policy.noactive",
55 "javax.security.sasl.policy.nodictionary",
56 "javax.security.sasl.policy.noanonymous",
57 "javax.security.sasl.policy.forward",
58 "javax.security.sasl.policy.credentials",
59 };
60
61 final private String[] propvals;
62 final private int myHash;
63 private int pHash = 0;
64
65 DigestClientId(int version, String hostname, int port,
66 String protocol, Control[] bindCtls, OutputStream trace,
67 String socketFactory, String username,
68 Object passwd, Hashtable env) {
69
70 super(version, hostname, port, protocol, bindCtls, trace,
71 socketFactory, username, passwd);
72
73 if (env == null) {
74 propvals = null;
75 } else {
76 // Could be smarter and apply default values for props
77 // but for now, we just record and check exact matches
78 propvals = new String[SASL_PROPS.length];
79 for (int i = 0; i < SASL_PROPS.length; i++) {
80 propvals[i] = (String) env.get(SASL_PROPS[i]);
81 if (propvals[i] != null) {
82 pHash = pHash * 31 + propvals[i].hashCode();
83 }
84 }
85 }
86 myHash = super.hashCode() + pHash;
87 }
88
89 public boolean equals(Object obj) {
90 if (!(obj instanceof DigestClientId)) {
91 return false;
92 }
93 DigestClientId other = (DigestClientId)obj;
94 return myHash == other.myHash
95 && pHash == other.pHash
96 && super.equals(obj)
97 && Arrays.equals(propvals, other.propvals);
98 }
99
100 public int hashCode() {
101 return myHash;
102 }
103
104 public String toString() {
105 if (propvals != null) {
106 StringBuffer buf = new StringBuffer();
107 for (int i = 0; i < propvals.length; i++) {
108 buf.append(':');
109 if (propvals[i] != null) {
110 buf.append(propvals[i]);
111 }
112 }
113 return super.toString() + buf.toString();
114 } else {
115 return super.toString();
116 }
117 }
118}