1 /*
2  * Copyright (c) 2002, 2016, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.jndi.ldap;
27 
28 import java.util.Arrays;  // JDK1.2
29 import java.io.OutputStream;
30 import javax.naming.ldap.Control;
31 
32 /**
33  * Represents the identity of a 'simple' authenticated LDAP connection.
34  * In addition to ClientId information, this class contains also the
35  * username and password.
36  *
37  * @author Rosanna Lee
38  */
39 class SimpleClientId extends ClientId {
40     final private String username;
41     final private Object passwd;
42     final private int myHash;
43 
SimpleClientId(int version, String hostname, int port, String protocol, Control[] bindCtls, OutputStream trace, String socketFactory, String username, Object passwd)44     SimpleClientId(int version, String hostname, int port,
45         String protocol, Control[] bindCtls, OutputStream trace,
46         String socketFactory, String username, Object passwd) {
47 
48         super(version, hostname, port, protocol, bindCtls, trace,
49                 socketFactory);
50 
51         this.username = username;
52         int pwdHashCode = 0;
53         if (passwd == null) {
54             this.passwd = null;
55         } else if (passwd instanceof byte[]) {
56             this.passwd = ((byte[])passwd).clone();
57             pwdHashCode = Arrays.hashCode((byte[])passwd);
58         } else if (passwd instanceof char[]) {
59             this.passwd = ((char[])passwd).clone();
60             pwdHashCode = Arrays.hashCode((char[])passwd);
61         } else {
62             this.passwd = passwd;
63             pwdHashCode = passwd.hashCode();
64         }
65 
66         myHash = super.hashCode()
67             ^ (username != null ? username.hashCode() : 0)
68             ^ pwdHashCode;
69     }
70 
equals(Object obj)71     public boolean equals(Object obj) {
72         if (obj == null || !(obj instanceof SimpleClientId)) {
73             return false;
74         }
75 
76         SimpleClientId other = (SimpleClientId)obj;
77 
78         return super.equals(obj)
79             && (username == other.username // null OK
80                 || (username != null && username.equals(other.username)))
81             && ((passwd == other.passwd)  // null OK
82                 || (passwd != null && other.passwd != null
83                     && (((passwd instanceof String) && passwd.equals(other.passwd))
84                         || ((passwd instanceof byte[])
85                             && (other.passwd instanceof byte[])
86                             && Arrays.equals((byte[])passwd, (byte[])other.passwd))
87                         || ((passwd instanceof char[])
88                             && (other.passwd instanceof char[])
89                             && Arrays.equals((char[])passwd, (char[])other.passwd)))));
90 
91     }
92 
hashCode()93     public int hashCode() {
94         return myHash;
95     }
96 
toString()97     public String toString() {
98         return super.toString() + ":" + username; // omit password for security
99     }
100 }
101