Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HelloWorld |
|
| 2.8;2.8 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.examples.simple; | |
11 | ||
12 | import static org.openpermis.examples.simple.HelloWorldUtilities.fatalError; | |
13 | import static org.openpermis.examples.simple.HelloWorldUtilities.validateInput; | |
14 | ||
15 | import java.io.IOException; | |
16 | import java.net.URL; | |
17 | import java.security.cert.CertificateException; | |
18 | import java.security.cert.CertificateFactory; | |
19 | import java.security.cert.X509Certificate; | |
20 | ||
21 | import org.openpermis.AuthorizationService; | |
22 | import org.openpermis.PolicyDecisionPoint; | |
23 | import org.openpermis.builder.AuthorizationServiceBuilder; | |
24 | import org.openpermis.cert.AttributeCertificateExtractorUtility; | |
25 | import org.openpermis.cert.BasicCertificateVerifier; | |
26 | import org.openpermis.cert.CertificateVerifier; | |
27 | import org.openpermis.repository.SubjectRepository; | |
28 | import org.openpermis.repository.basic.UrlSubjectRepository; | |
29 | ||
30 | ||
31 | /** | |
32 | * A sample HelloWorld application that can be invoked from the command line. | |
33 | * <p>For the sake of simplicity error handling is very simple | |
34 | * @since 0.1.0 | |
35 | */ | |
36 | public final class HelloWorld { | |
37 | ||
38 | //---- Static | |
39 | ||
40 | /** | |
41 | * Reads the SoA certificate from a classpath URL. | |
42 | * @return the SoA certificate requested. | |
43 | * @since 0.3.0 | |
44 | */ | |
45 | private static final X509Certificate readSoaCertificate () { | |
46 | try { | |
47 | 0 | final URL soaCertificateUrl = HelloWorld.class.getResource("soa.cer"); |
48 | 0 | final CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
49 | 0 | return (X509Certificate) cf.generateCertificate(soaCertificateUrl.openStream()); |
50 | 0 | } catch (CertificateException e) { |
51 | 0 | fatalError("Failed to parse SoA certificate.", e); |
52 | 0 | } catch (IOException e) { |
53 | 0 | fatalError("Failed to load SoA certificate.", e); |
54 | 0 | } |
55 | 0 | return null; |
56 | } | |
57 | ||
58 | /** | |
59 | * Creates a {@link PolicyDecisionPoint} from an attribute certificate located on the classpath. | |
60 | * @param certificateVerifier the certificate verifier used to verify the attribute | |
61 | * certificate containing the policy. | |
62 | * @return the {@link PolicyDecisionPoint} requested or {@code null} if it could not be read. | |
63 | * @since 0.3.0 | |
64 | */ | |
65 | private static final PolicyDecisionPoint createPolicyDecisionPoint ( | |
66 | CertificateVerifier certificateVerifier | |
67 | ) { | |
68 | try { | |
69 | 0 | final URL policyCertificateUrl = HelloWorld.class.getResource("policy.ace"); |
70 | 0 | return AttributeCertificateExtractorUtility. |
71 | createPolicyDecisionPoint(policyCertificateUrl, certificateVerifier); | |
72 | 0 | } catch (Exception e) { |
73 | 0 | fatalError("Failed to create policy decision point.", e); |
74 | } | |
75 | 0 | return null; |
76 | } | |
77 | ||
78 | /** | |
79 | * Creates a subject repository with the specified certificate verifier. | |
80 | * @param certificateVerifier the certificate verifier used to verify the attribute | |
81 | * certificate of the subjects. | |
82 | * @return the subject repository requested, {@code null} in case of a failure. | |
83 | * @since 0.3.0 | |
84 | */ | |
85 | private static final SubjectRepository readSubjectRepository ( | |
86 | CertificateVerifier certificateVerifier | |
87 | ) { | |
88 | try { | |
89 | 0 | final URL[] subjectUrls = new URL[] { |
90 | HelloWorld.class.getResource("john.ace"), | |
91 | HelloWorld.class.getResource("sara.ace") | |
92 | }; | |
93 | 0 | return new UrlSubjectRepository(certificateVerifier, subjectUrls); |
94 | 0 | } catch (Exception e) { |
95 | 0 | fatalError("Failed to load role certificates.", e); |
96 | } | |
97 | 0 | return null; |
98 | } | |
99 | ||
100 | /** | |
101 | * This main method creates an authorized HelloWorld service and tries to | |
102 | * get greeting messages for all user names that it finds in its argument list. | |
103 | * @param args a set of user names for whom the service will be invoked. | |
104 | * E.g. <code>"cn=john,o=post,c=ch"</code> or | |
105 | * <code>"cn=sara,o=post,c=ch"</code>. | |
106 | * @since 0.1.0 | |
107 | */ | |
108 | public static void main (String[] args) { | |
109 | 0 | final String[] input = validateInput(args); |
110 | ||
111 | // An authorization service needs a trusted public key of the source of authority (SoA) to | |
112 | // validate the attribute certificates (AC), including policies and roles. | |
113 | // Future implementations will allow a advanced public key infrastructure (PKI) | |
114 | 0 | final X509Certificate soaCertificate = readSoaCertificate(); |
115 | ||
116 | // The certificate verifier is used to verify attribute certificates. | |
117 | // In this example, a simple certificate verifier is used: It only knows one trusted SoA | |
118 | // certificate and does not allow chains. Both the attribute certificates with the roles | |
119 | // and the certificate with the policy use the same verifier. You can also use different | |
120 | // certificate verifiers. | |
121 | 0 | final CertificateVerifier certificateVerifier = |
122 | new BasicCertificateVerifier(soaCertificate); | |
123 | ||
124 | // Every authorization service needs a policy decision point. For this example we load the | |
125 | // attribute certificate, containing the policy from a classpath URL. | |
126 | 0 | final PolicyDecisionPoint pdp = createPolicyDecisionPoint(certificateVerifier); |
127 | ||
128 | // Our application needs a subject repository from which subjects | |
129 | // (i.e. authenticated persons with their roles) can be retrieved. | |
130 | // For this example we load the attribute certificates from a fixed list of URLs | |
131 | // retrieved from the classpath. | |
132 | 0 | final SubjectRepository repository = readSubjectRepository(certificateVerifier); |
133 | ||
134 | // The main service every PERMIS application needs is an authorization service. | |
135 | // An authorization service allows an application to retrieve subjects for authenticated | |
136 | // users and to decide whether users should be given access to protected resources. | |
137 | // The easiest way to create an authorization service is to use an | |
138 | // AuthorizationServiceBuilder | |
139 | // and configure the builder with the objects we want to use. The builder | |
140 | // will choose sensible conventions for anything not explicitly configured. | |
141 | 0 | final AuthorizationService authorizationService = new AuthorizationServiceBuilder(). |
142 | withSubjectsFrom(repository).forPolicyDecisionPoint(pdp).build(); | |
143 | ||
144 | // Our example uses an authorizing service wrapper. The wrapper implements the same | |
145 | // interface as the actual service implementation. We expect that we might even | |
146 | // provide a generic authorizing service proxy for arbitrary service interfaces | |
147 | // in the future. | |
148 | 0 | final HelloWorldService service = |
149 | new AuthorizedHelloWorldService(authorizationService, new BasicHelloWorldService()); | |
150 | ||
151 | // The actual example: try to get the service's greeting message for each | |
152 | // user name that was given on the command line. | |
153 | 0 | System.out.println("Getting greetings."); |
154 | 0 | for (String name : input) { |
155 | 0 | System.out.print(" " + name + ": "); |
156 | try { | |
157 | 0 | System.out.println(service.getHelloMessage(name)); |
158 | 0 | } catch (HelloWorldException e) { |
159 | 0 | System.out.println("<" + e.getMessage() + ">"); |
160 | 0 | } |
161 | } | |
162 | 0 | } |
163 | ||
164 | //---- Constructors | |
165 | ||
166 | /** | |
167 | * Objects of this class cannot be instantiated. | |
168 | * @since 0.1.0 | |
169 | */ | |
170 | private HelloWorld () { | |
171 | 0 | super(); |
172 | 0 | } |
173 | ||
174 | } |