KerberosPrincipal.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package javax.security.auth.kerberos;
  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.security.Principal;
  23. import org.apache.harmony.auth.internal.kerberos.v5.KerberosException;
  24. import org.apache.harmony.auth.internal.kerberos.v5.KrbClient;
  25. import org.apache.harmony.auth.internal.kerberos.v5.PrincipalName;
  26. import org.apache.harmony.auth.internal.nls.Messages;
  27. import org.apache.harmony.security.asn1.ASN1StringType;
  28. public final class KerberosPrincipal implements Principal, Serializable {
  29. private static final long serialVersionUID = -7374788026156829911L;
  30. public static final int KRB_NT_UNKNOWN = 0;
  31. public static final int KRB_NT_PRINCIPAL = 1;
  32. public static final int KRB_NT_SRV_INST = 2;
  33. public static final int KRB_NT_SRV_HST = 3;
  34. public static final int KRB_NT_SRV_XHST = 4;
  35. public static final int KRB_NT_UID = 5;
  36. // the full name of principal
  37. private transient PrincipalName name;
  38. // the realm
  39. private transient String realm;
  40. // "principal" @ "realm"
  41. private transient String strName;
  42. private void init(int type, String name) {
  43. // FIXME: correctly implement parsing name according to RFC 1964
  44. // http://www.ietf.org/rfc/rfc1964.txt
  45. if (name == null || name.trim().length() == 0) {
  46. throw new IllegalArgumentException(Messages.getString("auth.23")); //$NON-NLS-1$
  47. }
  48. int pos = name.indexOf('@');
  49. if (pos != -1) {
  50. realm = name.substring(pos + 1, name.length());
  51. // verify realm name according to RFC 1964(2.1.1 (2))
  52. // check invalid chars '/', ':' and null
  53. if (realm.indexOf('/') != -1 || realm.indexOf(':') != -1
  54. || realm.indexOf(0) != -1) {
  55. throw new IllegalArgumentException(Messages
  56. .getString("auth.24")); //$NON-NLS-1$
  57. }
  58. name = name.substring(0, pos);
  59. } else {
  60. // look for default realm name
  61. try {
  62. realm = KrbClient.getRealm();
  63. } catch (KerberosException e) {
  64. throw new IllegalArgumentException(e);
  65. }
  66. }
  67. this.name = new PrincipalName(type, name);
  68. }
  69. public KerberosPrincipal(String name) {
  70. init(KRB_NT_PRINCIPAL, name);
  71. }
  72. public KerberosPrincipal(String name, int type) {
  73. init(type, name);
  74. if (type < 0 || type > KRB_NT_UID) {
  75. throw new IllegalArgumentException(Messages.getString("auth.25")); //$NON-NLS-1$
  76. }
  77. }
  78. public String getName() {
  79. if (strName == null) {
  80. if (realm == null) {
  81. strName = name.getCanonicalName();
  82. } else {
  83. strName = name.getCanonicalName() + '@' + realm;
  84. }
  85. }
  86. return strName;
  87. }
  88. public String getRealm() {
  89. return realm;
  90. }
  91. public int getNameType() {
  92. return name.getType();
  93. }
  94. @Override
  95. public int hashCode() {
  96. return getName().hashCode();
  97. }
  98. @Override
  99. public boolean equals(Object obj) {
  100. if (obj == this) {
  101. return true;
  102. }
  103. if (!(obj instanceof KerberosPrincipal)) {
  104. return false;
  105. }
  106. KerberosPrincipal that = (KerberosPrincipal) obj;
  107. if (realm == null) {
  108. return that.realm == null;
  109. } else if (!realm.equals(that.realm)) {
  110. return false;
  111. }
  112. return name.equals(that.name);
  113. }
  114. @Override
  115. public String toString() {
  116. return getName();
  117. }
  118. private void readObject(ObjectInputStream s) throws IOException,
  119. ClassNotFoundException {
  120. s.defaultReadObject();
  121. name = PrincipalName.instanceOf((byte[]) s.readObject());
  122. realm = (String) ASN1StringType.GENERALSTRING.decode((byte[]) s
  123. .readObject());
  124. //FIXME: verify serialized values
  125. }
  126. private void writeObject(ObjectOutputStream s) throws IOException {
  127. s.defaultWriteObject();
  128. s.writeObject(name.getEncoded());
  129. s.writeObject(ASN1StringType.GENERALSTRING.encode(realm));
  130. }
  131. }