001/** 
002 * Copyright (C) 2009 "Darwin V. Felix" <darwinfelix@users.sourceforge.net>
003 * 
004 * This library is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License as published by the Free Software Foundation; either
007 * version 2.1 of the License, or (at your option) any later version.
008 * 
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012 * Lesser General Public License for more details.
013 * 
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this library; if not, write to the Free Software
016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017 */
018
019package net.sourceforge.spnego;
020
021import java.security.Principal;
022
023import javax.security.auth.kerberos.KerberosPrincipal;
024
025import org.ietf.jgss.GSSCredential;
026
027/**
028 * This class encapsulates a KerberosPrincipal.
029 * 
030 * <p>This class also has a reference to the client's/requester's 
031 * delegated credential (if any). See the {@link DelegateServletRequest} 
032 * documentation for more details.</p>
033 * 
034 * <p>Also, see the delegation examples at 
035 * <a href="http://spnego.sourceforge.net" target="_blank">http://spnego.sourceforge.net</a>
036 * </p>
037 * 
038 * @author Darwin V. Felix
039 *
040 */
041public final class SpnegoPrincipal implements Principal {
042
043    private final transient KerberosPrincipal kerberosPrincipal;
044    
045    private final transient GSSCredential delegatedCred;
046    
047    /**
048     * Constructs a SpnegoPrincipal from the provided String input.
049     * 
050     * @param name the principal name
051     */
052    public SpnegoPrincipal(final String name) {
053        this.kerberosPrincipal = new KerberosPrincipal(name);
054        this.delegatedCred = null;
055    }
056    
057    /**
058     * Constructs a SpnegoPrincipal from the provided String input 
059     * and name type input.
060     * 
061     * @param name the principal name
062     * @param nameType the name type of the principal
063     */
064    public SpnegoPrincipal(final String name, final int nameType) {
065        this.kerberosPrincipal = new KerberosPrincipal(name, nameType);
066        this.delegatedCred = null;
067    }
068
069    /**
070     * Constructs a SpnegoPrincipal from the provided String input 
071     * and name type input.
072     * 
073     * @param name the principal name
074     * @param nameType the name type of the principal
075     * @param delegCred this principal's delegated credential (if any)
076     */
077    public SpnegoPrincipal(final String name, final int nameType
078        , final GSSCredential delegCred) {
079        
080        this.kerberosPrincipal = new KerberosPrincipal(name, nameType);
081        this.delegatedCred = delegCred;
082    }
083    
084    /**
085     * Returns this Principal's delegated credential or null.
086     * 
087     * @return Principal's delegated credential or null.
088     */
089    public GSSCredential getDelegatedCredential() {
090        return this.delegatedCred;
091    }
092    
093    @Override
094    public String getName() {
095        return this.kerberosPrincipal.getName();
096    }
097    
098    /**
099     * Returns the name type of the KerberosPrincipal.
100     * 
101     * @return name type of the KerberosPrincipal
102     */
103    public int getNameType() {
104        return this.kerberosPrincipal.getNameType();
105    }
106    
107    /**
108     * Returns the realm component of this Kerberos principal.
109     * 
110     * @return realm component of this Kerberos principal
111     */
112    public String getRealm() {
113        return this.kerberosPrincipal.getRealm();
114    }
115    
116    @Override
117    public int hashCode() {
118        int result = 31;
119        result = 31 * result + this.kerberosPrincipal.hashCode();
120        result = 31 * result + this.delegatedCred.hashCode();
121        
122        return result;
123    }
124    
125    @Override
126    public boolean equals(final Object object) {
127        if (object == this) {
128            return true;
129        }
130        
131        if (!(object instanceof SpnegoPrincipal)) {
132            return false;
133        }
134        
135        final SpnegoPrincipal obj = (SpnegoPrincipal) object;
136        if (!this.kerberosPrincipal.equals(obj.kerberosPrincipal)
137                || !this.delegatedCred.equals(obj.delegatedCred)) {
138            
139            return false;
140        }
141        
142        return this.hashCode() == obj.hashCode();
143    }
144    
145    @Override
146    public String toString() {
147        return this.kerberosPrincipal.toString();
148    }
149}