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 org.apache.harmony.auth.jgss.kerberos;
019:
020: import org.apache.harmony.auth.jgss.GSSNameImpl;
021: import org.apache.harmony.auth.jgss.GSSUtils;
022: import org.ietf.jgss.GSSException;
023: import org.ietf.jgss.GSSName;
024: import org.ietf.jgss.Oid;
025:
026: /**
027: * Current the org.apache.harmony.auth.jgss.GSSNameImpl just supports kerberos
028: * related GSSName.
029: */
030: public class KerberosName extends GSSNameImpl {
031:
032: private String name;
033:
034: private Oid nameType;
035:
036: public KerberosName(String name, Oid nameType) throws GSSException {
037: if (null == name) {
038: throw new GSSException(
039: KerberosUtils.DEFAULT_GSSEXCEPTION_MAJOR_CODE,
040: KerberosUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE,
041: "Cannot import null GSSName");
042: }
043:
044: if (null == nameType) {
045: nameType = KerberosUtils.KRB5_PRINCIPAL_NAMETYPE;
046: }
047:
048: if (nameType.equals(GSSName.NT_HOSTBASED_SERVICE)) {
049: name = name.replaceAll("@", "/");
050: }
051:
052: if (!(nameType.equals(GSSName.NT_HOSTBASED_SERVICE)
053: || nameType.equals(GSSName.NT_USER_NAME) || nameType
054: .equals(KerberosUtils.KRB5_PRINCIPAL_NAMETYPE))) {
055: throw new GSSException(
056: KerberosUtils.DEFAULT_GSSEXCEPTION_MAJOR_CODE,
057: KerberosUtils.DEFAULT_GSSEXCEPTION_MINOR_CODE,
058: "Unsupported OID");
059: }
060: this .name = name;
061: this .nameType = nameType;
062: }
063:
064: String getName() {
065: return name;
066: }
067:
068: public GSSName canonicalize(Oid mech) throws GSSException {
069: return new KerberosName(getName(), mech);
070: }
071:
072: public boolean equals(GSSName another) throws GSSException {
073: if (isAnonymous() && another.isAnonymous()) {
074: return true;
075: }
076:
077: if (!(another instanceof KerberosName)) {
078:
079: return false;
080: }
081:
082: KerberosName anotherNameImpl = (KerberosName) another;
083: String this Name = getName();
084: String anotherName = anotherNameImpl.getName();
085:
086: if (!this Name.equals(anotherName)) {
087: return false;
088: }
089:
090: Oid this Oid = getStringNameType();
091: Oid anotherOid = anotherNameImpl.getStringNameType();
092:
093: if (this Oid.equals(KerberosUtils.KRB5_PRINCIPAL_NAMETYPE)
094: || anotherOid
095: .equals(KerberosUtils.KRB5_PRINCIPAL_NAMETYPE)) {
096: return true;
097: }
098: return this Oid.equals(anotherOid);
099: }
100:
101: public Oid getStringNameType() throws GSSException {
102: return nameType;
103: }
104:
105: // org.apache.harmony.auth.jgss.GSSNameImpl actually does not support
106: // GSSNAME.NT_ANONYMOUS, so it always returns false.
107: public boolean isAnonymous() {
108: return nameType.equals(GSSName.NT_ANONYMOUS);
109: }
110:
111: // org.apache.harmony.auth.jgss.GSSNameImpl only supports MN GSSNAME.
112: public boolean isMN() {
113: return true;
114: }
115:
116: public String toString() {
117: return name;
118: }
119:
120: @Override
121: protected byte[] exportMechDependent() throws GSSException {
122: return GSSUtils.getBytes(name);
123: }
124:
125: @Override
126: protected Oid getMech() {
127: return KerberosUtils.KRB5_MECH;
128: }
129: }
|