1 /*
   2  * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.security.auth;
  27 
  28 import java.security.Principal;
  29 import javax.naming.InvalidNameException;
  30 import javax.naming.ldap.LdapName;
  31 
  32 /**
  33  * A principal identified by a distinguished name as specified by
  34  * <a href="http://ietf.org//rfc/rfc2253.txt">RFC 2253</a>.
  35  *
  36  * <p>
  37  * After successful authentication, a user {@link java.security.Principal}
  38  * can be associated with a particular {@link javax.security.auth.Subject}
  39  * to augment that <code>Subject</code> with an additional identity.
  40  * Authorization decisions can then be based upon the
  41  * <code>Principal</code>s that are associated with a <code>Subject</code>.
  42  *
  43  * <p>
  44  * This class is immutable.
  45  *
  46  * @since 1.6
  47  */
  48 public final class LdapPrincipal implements Principal, java.io.Serializable {
  49 
  50     private static final long serialVersionUID = 6820120005580754861L;
  51 
  52     /**
  53      * The principal's string name
  54      *
  55      * @serial
  56      */
  57     private final String nameString;
  58 
  59     /**
  60      * The principal's name
  61      *
  62      * @serial
  63      */
  64     private final LdapName name;
  65 
  66     /**
  67      * Creates an LDAP principal.
  68      *
  69      * @param name The principal's string distinguished name.
  70      * @throws InvalidNameException If a syntax violation is detected.
  71      * @exception NullPointerException If the <code>name</code> is
  72      * <code>null</code>.
  73      */
  74     public LdapPrincipal(String name) throws InvalidNameException {
  75         if (name == null) {
  76             throw new NullPointerException("null name is illegal");
  77         }
  78         this.name = getLdapName(name);
  79         nameString = name;
  80     }
  81 
  82     /**
  83      * Compares this principal to the specified object.
  84      *
  85      * @param object The object to compare this principal against.
  86      * @return true if they are equal; false otherwise.
  87      */
  88     public boolean equals(Object object) {
  89         if (this == object) {
  90             return true;
  91         }
  92         if (object instanceof LdapPrincipal) {
  93             try {
  94 
  95                 return
  96                     name.equals(getLdapName(((LdapPrincipal)object).getName()));
  97 
  98             } catch (InvalidNameException e) {
  99                 return false;
 100             }
 101         }
 102         return false;
 103     }
 104 
 105     /**
 106      * Computes the hash code for this principal.
 107      *
 108      * @return The principal's hash code.
 109      */
 110     public int hashCode() {
 111         return name.hashCode();
 112     }
 113 
 114     /**
 115      * Returns the name originally used to create this principal.
 116      *
 117      * @return The principal's string name.
 118      */
 119     public String getName() {
 120         return nameString;
 121     }
 122 
 123     /**
 124      * Creates a string representation of this principal's name in the format
 125      * defined by <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
 126      * If the name has zero components an empty string is returned.
 127      *
 128      * @return The principal's string name.
 129      */
 130     public String toString() {
 131         return name.toString();
 132     }
 133 
 134     // Create an LdapName object from a string distinguished name.
 135     private LdapName getLdapName(String name) throws InvalidNameException {
 136         return new LdapName(name);
 137     }
 138 }