KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > auth > login > PasswordCredential


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.security.auth.login;
25
26 /**
27  * This class holds the user password for the shared password realm and the
28  * realm name. This credential is added as a private credential to the
29  * JAAS subject.
30  */

31
32 public class PasswordCredential {
33     private String JavaDoc username;
34     private String JavaDoc password;
35     private String JavaDoc realm;
36     private boolean readOnly = false;
37     // target_name is filled in by the SecSecurityServer interceptor
38
// only when a CSIv2 GSSUP authenticator is received.
39

40     private byte[] target_name = {};
41
42     /**
43      * Construct a credential with the specified password and realm name.
44      * @param the password.
45      * @param the realm name. The only value supported for now is "default".
46      */

47     public PasswordCredential(String JavaDoc user, String JavaDoc password, String JavaDoc realm)
48     {
49     this.username = user;
50     this.password = password;
51     this.realm = realm;
52
53         if (this.username == null ) { this.username = ""; }
54         if (this.password == null ) { this.password = ""; }
55         if (this.realm == null ) { this.realm = ""; }
56     }
57
58     
59     /**
60      * called by SecServerRequestInterceptor
61      * The object if created on the server side is readonly
62      */

63     public PasswordCredential(String JavaDoc user, String JavaDoc password,
64                               String JavaDoc realm, byte[] target_name)
65     {
66         this(user, password, realm);
67     this.target_name = target_name;
68         readOnly = true;
69     }
70
71     
72     /**
73      * Return the realm name.
74      * @return the realm name. Only value supported for now is "default".
75      */

76     public String JavaDoc getRealm() {
77     return realm;
78     }
79
80     
81     /**
82      * Return the username.
83      * @return the user name.
84      */

85     public String JavaDoc getUser() {
86     return username;
87     }
88
89     public void setRealm(String JavaDoc realm){
90         if(!readOnly){
91             this.realm = realm;
92         }
93     }
94     
95     /**
96      * Return the password.
97      * @return the password.
98      */

99     public String JavaDoc getPassword() {
100     return password;
101     }
102
103     
104     /**
105      * Return the target_name
106      * @return the target_name
107      */

108     public byte[] getTargetName() {
109     return this.target_name;
110     }
111
112     /**
113      * Compare two instances of the credential and return true if they are
114      * the same and false otherwise.
115      * @param the object that this instance is being compared to.
116      * @return true if the instances are equal, false otherwise
117      */

118     public boolean equals(Object JavaDoc o) {
119     if(o instanceof PasswordCredential) {
120         PasswordCredential pc = (PasswordCredential) o;
121         if(pc.getUser().equals(username) &&
122         pc.getPassword().equals(password) &&
123         pc.getRealm().equals(realm)) {
124         return true;
125         }
126     }
127     return false;
128     }
129
130     
131     /**
132      * Return the hashCode computed from the password and realm name.
133      * @return the hash code.
134      */

135     public int hashCode() {
136     return username.hashCode() + password.hashCode() + realm.hashCode();
137     }
138
139     
140     /**
141      * The string representation of the credential.
142      */

143     public String JavaDoc toString() {
144     String JavaDoc s = "Realm=" + realm;
145     s = s + " Username=" + username;
146     s = s + " Password=" + "########";
147     s = s + " TargetName = " + target_name;
148     return s;
149     }
150
151 }
152
Popular Tags