1 7 8 package javax.security.auth.kerberos; 9 10 import java.io.*; 11 import sun.security.krb5.Asn1Exception; 12 import sun.security.krb5.KrbException; 13 import sun.security.krb5.PrincipalName; 14 import sun.security.krb5.Realm; 15 import sun.security.util.*; 16 17 24 25 public final class KerberosPrincipal 26 implements java.security.Principal , java.io.Serializable { 27 28 private static final long serialVersionUID = -7374788026156829911L; 29 30 32 35 36 public static final int KRB_NT_UNKNOWN = 0; 37 38 41 42 public static final int KRB_NT_PRINCIPAL = 1; 43 44 47 public static final int KRB_NT_SRV_INST = 2; 48 49 52 53 public static final int KRB_NT_SRV_HST = 3; 54 55 58 59 public static final int KRB_NT_SRV_XHST = 4; 60 61 64 65 public static final int KRB_NT_UID = 5; 66 67 68 private transient String fullName; 69 70 private transient String realm; 71 72 private transient int nameType; 73 74 private static final char NAME_REALM_SEPARATOR = '@'; 75 76 100 104 public KerberosPrincipal(String name) { 105 106 PrincipalName krb5Principal = null; 107 108 try { 109 krb5Principal = new PrincipalName(name, KRB_NT_PRINCIPAL); 111 } catch (KrbException e) { 112 throw new IllegalArgumentException (e.getMessage()); 113 } 114 nameType = KRB_NT_PRINCIPAL; fullName = krb5Principal.toString(); 116 realm = krb5Principal.getRealmString(); 117 } 118 119 146 147 public KerberosPrincipal(String name, int nameType) { 148 149 PrincipalName krb5Principal = null; 150 151 try { 152 krb5Principal = new PrincipalName(name,nameType); 154 } catch (KrbException e) { 155 throw new IllegalArgumentException (e.getMessage()); 156 } 157 158 this.nameType = nameType; 159 fullName = krb5Principal.toString(); 160 realm = krb5Principal.getRealmString(); 161 } 162 167 public String getRealm() { 168 return realm; 169 } 170 171 180 public int hashCode() { 181 return getName().hashCode(); 182 } 183 184 197 public boolean equals(Object other) { 198 199 if (other == this) 200 return true; 201 202 if (! (other instanceof KerberosPrincipal )) { 203 return false; 204 } else { 205 String myFullName = getName(); 206 String otherFullName = ((KerberosPrincipal ) other).getName(); 207 if (nameType == ((KerberosPrincipal )other).nameType && 208 myFullName.equals(otherFullName)) { 209 return true; 210 } 211 } 212 return false; 213 } 214 215 223 224 private synchronized void writeObject(ObjectOutputStream oos) 225 throws IOException { 226 227 PrincipalName krb5Principal = null; 228 try { 229 krb5Principal = new PrincipalName(fullName,nameType); 230 oos.writeObject(krb5Principal.asn1Encode()); 231 oos.writeObject(krb5Principal.getRealm().asn1Encode()); 232 } catch (Exception e) { 233 IOException ioe = new IOException(e.getMessage()); 234 ioe.initCause(e); 235 throw ioe; 236 } 237 } 238 239 242 243 private synchronized void readObject(ObjectInputStream ois) 244 throws IOException, ClassNotFoundException { 245 byte[] asn1EncPrincipal = (byte [])ois.readObject(); 246 byte[] encRealm = (byte [])ois.readObject(); 247 try { 248 PrincipalName krb5Principal = new PrincipalName(new 249 DerValue(asn1EncPrincipal)); 250 realm = (new Realm(new DerValue(encRealm))).toString(); 251 fullName = krb5Principal.toString() + NAME_REALM_SEPARATOR + 252 realm.toString(); 253 nameType = krb5Principal.getNameType(); 254 } catch (Exception e) { 255 IOException ioe = new IOException(e.getMessage()); 256 ioe.initCause(e); 257 throw ioe; 258 } 259 } 260 261 268 public String getName() { 269 return fullName; 270 } 271 272 280 281 public int getNameType() { 282 return nameType; 283 } 284 285 public String toString() { 287 return getName(); 288 } 289 } 290 | Popular Tags |