Started to implement config settings based on Relying Party.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / common / ServiceProviderMapper.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation
3  * for Advanced Internet Development, Inc. All rights reserved
4  * 
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 
9  * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 
12  * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution, if any, must include
15  * the following acknowledgment: "This product includes software developed by
16  * the University Corporation for Advanced Internet Development
17  * <http://www.ucaid.edu> Internet2 Project. Alternately, this acknowledegement
18  * may appear in the software itself, if and wherever such third-party
19  * acknowledgments normally appear.
20  * 
21  * Neither the name of Shibboleth nor the names of its contributors, nor
22  * Internet2, nor the University Corporation for Advanced Internet Development,
23  * Inc., nor UCAID may be used to endorse or promote products derived from this
24  * software without specific prior written permission. For written permission,
25  * please contact shibboleth@shibboleth.org
26  * 
27  * Products derived from this software may not be called Shibboleth, Internet2,
28  * UCAID, or the University Corporation for Advanced Internet Development, nor
29  * may Shibboleth appear in their name, without prior written permission of the
30  * University Corporation for Advanced Internet Development.
31  * 
32  * 
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
36  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
37  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
38  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
39  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY
40  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  */
47 package edu.internet2.middleware.shibboleth.common;
48
49 import java.util.HashMap;
50 import java.util.Map;
51 import java.util.Properties;
52
53 import org.w3c.dom.Element;
54
55 /**
56  * @author Walter Hoehn
57  *  
58  */
59 public class ServiceProviderMapper {
60
61         ShibbolethOriginConfig configuration;
62         Credentials credentials;
63         Map relyingParties = new HashMap();
64         Map relyingPartyGroups = new HashMap();
65
66         public ServiceProviderMapper(ShibbolethOriginConfig configuration, Credentials credentials) {
67                 this.configuration = configuration;
68                 this.credentials = credentials;
69         }
70
71         public void addRelyingParty(Element e) {
72                 if (e.getLocalName().equals("RelyingParty")) {
73                         RelyingParty party = new RelyingPartyImpl(e, configuration, credentials);
74                         relyingParties.put(party.getName(), party);
75                 } else if (e.getLocalName().equals("RelyingPartyGroup")) {
76                         RelyingParty party = new RelyingPartyImpl(e, configuration, credentials);
77                         relyingPartyGroups.put(party.getName(), party);
78                 } else {
79                         //TODO throw exception here
80                 }
81         }
82
83         public RelyingParty getRelyingParty(String providerIdFromTarget) {
84
85                 //If the target did not send a Provider Id, then assume it is a Shib
86                 // 1.1 or older target
87                 if (providerIdFromTarget == null || providerIdFromTarget.equals("")) {
88                         return new LegacyWrapper(getDefaultRelyingPatry());
89                 }
90
91                 if (!relyingParties.containsKey(providerIdFromTarget)) {
92                         return getDefaultRelyingPatry();
93                 }
94                 //TODO do secondary lookup for groups (metadata)
95                 return (RelyingParty) relyingParties.get(providerIdFromTarget);
96         }
97
98         private RelyingParty getDefaultRelyingPatry() {
99
100                 return (RelyingParty) relyingParties.get(
101                         configuration.getConfigProperty(
102                                 "edu.internet2.middleware.shibboleth.common.RelyingParty.defaultRelyingParty"));
103                 // TODO look for groups too, probably first
104         }
105         class RelyingPartyImpl implements RelyingParty {
106
107                 protected ShibbolethOriginConfig originConfig;
108                 protected Properties partyOverrides = new Properties();
109                 protected RelyingPartyIdentityProvider identityProvider;
110                 protected String id = "test:id";
111
112                 public RelyingPartyImpl(Element partyConfig, ShibbolethOriginConfig globalConfig, Credentials credentials) {
113
114                         this.originConfig = globalConfig;
115                         
116                         
117                         //TODO this is just a stub... has to come from configuration
118                         partyOverrides.setProperty(
119                                 "edu.internet2.middleware.shibboleth.hs.HandleServlet.responseSigningCredential",
120                                 "foo");
121
122                         identityProvider =
123                                 new RelyingPartyIdentityProvider(
124                                         getConfigProperty("edu.internet2.middleware.shibboleth.hs.HandleServlet.providerId"),
125                                         credentials.getCredential(
126                                                 getConfigProperty("edu.internet2.middleware.shibboleth.hs.HandleServlet.responseSigningCredential")));
127                         //TODO stub
128
129                 }
130
131                 public String getProviderId() {
132                         return id;
133                 }
134
135                 public String getName() {
136                         return id;
137                 }
138
139                 public boolean isLegacyProvider() {
140                         return false;
141                 }
142
143                 public String getConfigProperty(String key) {
144                         if (partyOverrides.containsKey(key)) {
145                                 return partyOverrides.getProperty(key);
146                         }
147                         return originConfig.getConfigProperty(key);
148                 }
149
150                 public String getHSNameFormatId() {
151                         return null;
152                 }
153                 public IdentityProvider getIdentityProvider() {
154                         return identityProvider;
155                 }
156                 class RelyingPartyIdentityProvider implements IdentityProvider {
157
158                         private String providerId;
159                         private Credential responseSigningCredential;
160
161                         RelyingPartyIdentityProvider(String providerId, Credential responseSigningCred) {
162                                 this.providerId = providerId;
163                                 this.responseSigningCredential = responseSigningCred;
164                         }
165                         
166                         public String getProviderId() {
167                                 return providerId;
168                         }
169
170                         public Credential getResponseSigningCredential() {
171                                 return responseSigningCredential;
172                         }
173
174                         public Credential getAssertionSigningCredential() {
175                                 return null;
176                         }
177
178                 }
179         }
180         class RelyingPartyGroupWrapper implements RelyingParty {
181
182                 private RelyingParty wrapped;
183                 private String providerId;
184
185                 RelyingPartyGroupWrapper(RelyingPartyImpl wrapped, String providerId) {
186                         this.wrapped = wrapped;
187                         this.providerId = providerId;
188                 }
189
190                 public String getName() {
191                         return wrapped.getName();
192                 }
193
194                 public String getConfigProperty(String key) {
195                         return wrapped.getConfigProperty(key);
196                 }
197
198                 public boolean isLegacyProvider() {
199                         return true;
200                 }
201
202                 public String getHSNameFormatId() {
203                         return wrapped.getHSNameFormatId();
204                 }
205
206                 public IdentityProvider getIdentityProvider() {
207                         return wrapped.getIdentityProvider();
208                 }
209
210                 public String getProviderId() {
211                         return providerId;
212                 }
213         }
214
215         class LegacyWrapper implements RelyingParty {
216                 private RelyingParty wrapped;
217
218                 LegacyWrapper(RelyingParty wrapped) {
219                         this.wrapped = wrapped;
220                 }
221
222                 public String getName() {
223                         return wrapped.getName();
224                 }
225
226                 public String getConfigProperty(String key) {
227                         return wrapped.getConfigProperty(key);
228                 }
229
230                 public boolean isLegacyProvider() {
231                         return true;
232                 }
233
234                 public String getHSNameFormatId() {
235                         return wrapped.getHSNameFormatId();
236                 }
237
238                 public IdentityProvider getIdentityProvider() {
239                         return wrapped.getIdentityProvider();
240                 }
241
242                 public String getProviderId() {
243                         return wrapped.getProviderId();
244                 }
245         }
246 }