1 package org.eclipse.jetty.policy.entry;
2 //========================================================================
3 //Copyright (c) Webtide LLC
4 //------------------------------------------------------------------------
5 //All rights reserved. This program and the accompanying materials
6 //are made available under the terms of the Eclipse Public License v1.0
7 //and Apache License v2.0 which accompanies this distribution.
8 //
9 //The Eclipse Public License is available at
10 //http://www.eclipse.org/legal/epl-v10.html
11 //
12 //The Apache License v2.0 is available at
13 //http://www.apache.org/licenses/LICENSE-2.0.txt
14 //
15 //You may elect to redistribute this code under either of these licenses.
16 //========================================================================
17
18 import java.security.KeyStoreException;
19 import java.security.Principal;
20 import java.security.cert.Certificate;
21 import java.security.cert.X509Certificate;
22
23 import org.eclipse.jetty.policy.PolicyContext;
24 import org.eclipse.jetty.policy.PolicyException;
25
26 public class PrincipalEntry extends AbstractEntry
27 {
28 /**
29 * Wildcard value denotes any class and/or any name. Must be asterisk, for proper general expansion and
30 * PrivateCredentialsPermission wildcarding
31 */
32 public static final String WILDCARD = "*"; //$NON-NLS-1$
33
34 /**
35 * The classname part of principal clause.
36 */
37 private String klass;
38
39 /**
40 * The name part of principal clause.
41 */
42 private String name;
43
44 /**
45 * cached principal if already computed
46 */
47 private Principal principal;
48
49 public Principal toPrincipal( PolicyContext context ) throws PolicyException
50 {
51 if ( principal != null && !isDirty() )
52 {
53 return principal;
54 }
55
56 // if there is no keystore, there is no way to obtain a principal object
57 // TODO validate we need this check
58 if ( context.getKeystore() == null )
59 {
60 return null;
61 }
62
63 try
64 {
65 Certificate certificate = context.getKeystore().getCertificate( name );
66
67 if ( certificate instanceof X509Certificate )
68 {
69 principal = ( (X509Certificate) certificate ).getSubjectX500Principal();
70 return principal;
71 }
72 else
73 {
74 throw new PolicyException( "Unknown Certificate, unable to obtain Principal: " + certificate.getType() );
75 }
76 }
77 catch ( KeyStoreException kse )
78 {
79 throw new PolicyException( kse );
80 }
81 }
82
83 public void expand( PolicyContext context )
84 throws PolicyException
85 {
86 name = context.evaluate( name );
87
88 setExpanded(true);
89 }
90
91 public String getKlass()
92 {
93 return klass;
94 }
95
96 public void setKlass( String klass )
97 {
98 this.klass = klass;
99 }
100
101 public String getName()
102 {
103 return name;
104 }
105
106 public void setName( String name )
107 {
108 this.name = name;
109 }
110
111
112 }