KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > certs > SubjectCNMapping


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.auth.certs;
8
9 import java.security.Principal JavaDoc;
10 import java.security.cert.X509Certificate JavaDoc;
11 import org.jboss.security.CertificatePrincipal;
12 import org.jboss.security.SimplePrincipal;
13
14 /** A CertificatePrincipal implementation that uses the client cert
15  * SubjectDN CN='...' element as the principal.
16  *
17  * @author Scott.Stark@jboss.org
18  * @version $Revision: 1.2.4.1 $
19  */

20 public class SubjectCNMapping
21    implements CertificatePrincipal
22 {
23    /** Returns the client cert common name portion (cn=...) of the SubjectDN
24     * as the principal.
25     *
26     * @param certs Array of client certificates, with the first one in
27     * the array being the certificate of the client itself.
28     */

29    public Principal JavaDoc toPrinicipal(X509Certificate JavaDoc[] certs)
30    {
31       Principal JavaDoc cn = null;
32       Principal JavaDoc subject = certs[0].getSubjectDN();
33       // Look for a cn=... entry in the subject DN
34
String JavaDoc dn = subject.getName().toLowerCase();
35       int index = dn.indexOf("cn=");
36       if( index >= 0 )
37       {
38          int comma = dn.indexOf(',', index);
39          if( comma < 0 )
40             comma = dn.length();
41          String JavaDoc name = dn.substring(index+3, comma);
42          cn = new SimplePrincipal(name);
43       }
44       else
45       {
46          // Fallback to the DN
47
cn = subject;
48       }
49       return cn;
50    }
51 }
52
Popular Tags