001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.security.auth.kerberos;
019:
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.io.Serializable;
024: import java.security.Principal;
025:
026: import org.apache.harmony.auth.internal.kerberos.v5.KerberosException;
027: import org.apache.harmony.auth.internal.kerberos.v5.KrbClient;
028: import org.apache.harmony.auth.internal.kerberos.v5.PrincipalName;
029: import org.apache.harmony.auth.internal.nls.Messages;
030: import org.apache.harmony.security.asn1.ASN1StringType;
031:
032: public final class KerberosPrincipal implements Principal, Serializable {
033:
034: private static final long serialVersionUID = -7374788026156829911L;
035:
036: public static final int KRB_NT_UNKNOWN = 0;
037:
038: public static final int KRB_NT_PRINCIPAL = 1;
039:
040: public static final int KRB_NT_SRV_INST = 2;
041:
042: public static final int KRB_NT_SRV_HST = 3;
043:
044: public static final int KRB_NT_SRV_XHST = 4;
045:
046: public static final int KRB_NT_UID = 5;
047:
048: // the full name of principal
049: private transient PrincipalName name;
050:
051: // the realm
052: private transient String realm;
053:
054: // "principal" @ "realm"
055: private transient String strName;
056:
057: private void init(int type, String name) {
058:
059: // FIXME: correctly implement parsing name according to RFC 1964
060: // http://www.ietf.org/rfc/rfc1964.txt
061: if (name == null || name.trim().length() == 0) {
062: throw new IllegalArgumentException(Messages
063: .getString("auth.23")); //$NON-NLS-1$
064: }
065:
066: int pos = name.indexOf('@');
067: if (pos != -1) {
068: realm = name.substring(pos + 1, name.length());
069:
070: // verify realm name according to RFC 1964(2.1.1 (2))
071: // check invalid chars '/', ':' and null
072: if (realm.indexOf('/') != -1 || realm.indexOf(':') != -1
073: || realm.indexOf(0) != -1) {
074: throw new IllegalArgumentException(Messages
075: .getString("auth.24")); //$NON-NLS-1$
076: }
077:
078: name = name.substring(0, pos);
079: } else {
080: // look for default realm name
081: try {
082: realm = KrbClient.getRealm();
083: } catch (KerberosException e) {
084: throw new IllegalArgumentException(e);
085: }
086: }
087: this .name = new PrincipalName(type, name);
088: }
089:
090: public KerberosPrincipal(String name) {
091: init(KRB_NT_PRINCIPAL, name);
092: }
093:
094: public KerberosPrincipal(String name, int type) {
095: init(type, name);
096: if (type < 0 || type > KRB_NT_UID) {
097: throw new IllegalArgumentException(Messages
098: .getString("auth.25")); //$NON-NLS-1$
099: }
100: }
101:
102: public String getName() {
103: if (strName == null) {
104: if (realm == null) {
105: strName = name.getCanonicalName();
106: } else {
107: strName = name.getCanonicalName() + '@' + realm;
108: }
109: }
110: return strName;
111: }
112:
113: public String getRealm() {
114: return realm;
115: }
116:
117: public int getNameType() {
118: return name.getType();
119: }
120:
121: @Override
122: public int hashCode() {
123: return getName().hashCode();
124: }
125:
126: @Override
127: public boolean equals(Object obj) {
128: if (obj == this ) {
129: return true;
130: }
131: if (!(obj instanceof KerberosPrincipal)) {
132: return false;
133: }
134:
135: KerberosPrincipal that = (KerberosPrincipal) obj;
136:
137: if (realm == null) {
138: return that.realm == null;
139: } else if (!realm.equals(that.realm)) {
140: return false;
141: }
142: return name.equals(that.name);
143: }
144:
145: @Override
146: public String toString() {
147: return getName();
148: }
149:
150: private void readObject(ObjectInputStream s) throws IOException,
151: ClassNotFoundException {
152:
153: s.defaultReadObject();
154:
155: name = PrincipalName.instanceOf((byte[]) s.readObject());
156: realm = (String) ASN1StringType.GENERALSTRING.decode((byte[]) s
157: .readObject());
158:
159: //FIXME: verify serialized values
160: }
161:
162: private void writeObject(ObjectOutputStream s) throws IOException {
163:
164: s.defaultWriteObject();
165:
166: s.writeObject(name.getEncoded());
167: s.writeObject(ASN1StringType.GENERALSTRING.encode(realm));
168: }
169: }
|