forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShibServiceBean.java
240 lines (216 loc) · 8.7 KB
/
ShibServiceBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package edu.harvard.iq.dataverse.authorization.providers.shib;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import edu.harvard.iq.dataverse.authorization.AuthTestDataServiceBean;
import edu.harvard.iq.dataverse.authorization.AuthenticationRequest;
import edu.harvard.iq.dataverse.authorization.AuthenticationResponse;
import edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean;
import edu.harvard.iq.dataverse.authorization.exceptions.AuthenticationFailedException;
import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinAuthenticationProvider;
import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUser;
import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUserServiceBean;
import edu.harvard.iq.dataverse.authorization.providers.builtin.PasswordEncryption;
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.config.SystemConfig;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
@Named
@Stateless
public class ShibServiceBean {
private static final Logger logger = Logger.getLogger(ShibServiceBean.class.getCanonicalName());
@EJB
AuthenticationServiceBean authSvc;
@EJB
BuiltinUserServiceBean builtinUserService;
@EJB
AuthTestDataServiceBean authTestDataService;
@EJB
SystemConfig systemConfig;
@EJB
SettingsServiceBean settingsService;
/**
* "Production" means "don't mess with the HTTP request".
*/
public enum DevShibAccountType {
PRODUCTION,
RANDOM,
TESTSHIB1,
HARVARD1,
HARVARD2,
TWO_EMAILS,
INVALID_EMAIL,
EMAIL_WITH_LEADING_SPACE,
UID_WITH_LEADING_SPACE,
IDENTIFIER_WITH_LEADING_SPACE,
MISSING_REQUIRED_ATTR,
};
public DevShibAccountType getDevShibAccountType() {
DevShibAccountType saneDefault = DevShibAccountType.PRODUCTION;
String settingReturned = settingsService.getValueForKey(SettingsServiceBean.Key.DebugShibAccountType);
logger.fine("setting returned: " + settingReturned);
if (settingReturned != null) {
try {
DevShibAccountType parsedValue = DevShibAccountType.valueOf(settingReturned);
return parsedValue;
} catch (IllegalArgumentException ex) {
logger.info("Couldn't parse value: " + ex + " - returning a sane default: " + saneDefault);
return saneDefault;
}
} else {
logger.fine("Shibboleth dev mode has not been configured. Returning a sane default: " + saneDefault);
return saneDefault;
}
}
/**
* This method exists so developers don't have to run Shibboleth locally.
* You can populate the request with Shibboleth attributes by changing a
* setting like this:
*
* curl -X PUT -d RANDOM
* http://localhost:8080/api/admin/settings/:DebugShibAccountType
*
* When you're done, feel free to delete the setting:
*
* curl -X DELETE
* http://localhost:8080/api/admin/settings/:DebugShibAccountType
*
* Note that setting ShibUseHeaders to true will make this "dev mode" stop
* working.
*/
public void possiblyMutateRequestInDev(HttpServletRequest request) {
switch (getDevShibAccountType()) {
case PRODUCTION:
logger.fine("Request will not be mutated");
break;
case RANDOM:
mutateRequestForDevRandom(request);
break;
case TESTSHIB1:
ShibUtil.mutateRequestForDevConstantTestShib1(request);
break;
case HARVARD1:
ShibUtil.mutateRequestForDevConstantHarvard1(request);
break;
case HARVARD2:
ShibUtil.mutateRequestForDevConstantHarvard2(request);
break;
case TWO_EMAILS:
ShibUtil.mutateRequestForDevConstantTwoEmails(request);
break;
case INVALID_EMAIL:
ShibUtil.mutateRequestForDevConstantInvalidEmail(request);
break;
case EMAIL_WITH_LEADING_SPACE:
ShibUtil.mutateRequestForDevConstantEmailWithLeadingSpace(request);
break;
case UID_WITH_LEADING_SPACE:
ShibUtil.mutateRequestForDevConstantUidWithLeadingSpace(request);
break;
case IDENTIFIER_WITH_LEADING_SPACE:
ShibUtil.mutateRequestForDevConstantIdentifierWithLeadingSpace(request);
break;
case MISSING_REQUIRED_ATTR:
ShibUtil.mutateRequestForDevConstantMissingRequiredAttributes(request);
break;
default:
logger.info("Should never reach here");
break;
}
}
public AuthenticatedUser findAuthUserByEmail(String emailToFind) {
return authSvc.getAuthenticatedUserByEmail(emailToFind);
}
public BuiltinUser findBuiltInUserByAuthUserIdentifier(String authUserIdentifier) {
return builtinUserService.findByUserName(authUserIdentifier);
}
public String getAffiliation(String shibIdp, DevShibAccountType devShibAccountType) {
JsonArray emptyJsonArray = new JsonArray();
String discoFeedJson = emptyJsonArray.toString();
String discoFeedUrl;
if (devShibAccountType.equals(DevShibAccountType.PRODUCTION)) {
discoFeedUrl = systemConfig.getDataverseSiteUrl() + "/Shibboleth.sso/DiscoFeed";
} else {
String devUrl = "http://localhost:8080/resources/dev/sample-shib-identities.json";
discoFeedUrl = devUrl;
}
logger.fine("Trying to get affiliation from disco feed URL: " + discoFeedUrl);
URL url = null;
try {
url = new URL(discoFeedUrl);
} catch (MalformedURLException ex) {
logger.info(ex.toString());
return null;
}
if (url == null) {
logger.info("url object was null after parsing " + discoFeedUrl);
return null;
}
HttpURLConnection discoFeedRequest = null;
try {
discoFeedRequest = (HttpURLConnection) url.openConnection();
} catch (IOException ex) {
logger.info(ex.toString());
return null;
}
if (discoFeedRequest == null) {
logger.info("disco feed request was null");
return null;
}
try {
discoFeedRequest.connect();
} catch (IOException ex) {
logger.info(ex.toString());
return null;
}
JsonParser jp = new JsonParser();
JsonElement root = null;
try {
root = jp.parse(new InputStreamReader((InputStream) discoFeedRequest.getInputStream()));
} catch (IOException ex) {
logger.info(ex.toString());
return null;
}
if (root == null) {
logger.info("root was null");
return null;
}
JsonArray rootArray = root.getAsJsonArray();
if (rootArray == null) {
logger.info("Couldn't get JSON Array from URL");
return null;
}
discoFeedJson = rootArray.toString();
logger.fine("Dump of disco feed:" + discoFeedJson);
String affiliation = ShibUtil.getDisplayNameFromDiscoFeed(shibIdp, discoFeedJson);
if (affiliation != null) {
return affiliation;
} else {
logger.info("Couldn't find an affiliation from " + shibIdp);
return null;
}
}
private void mutateRequestForDevRandom(HttpServletRequest request) {
Map<String, String> randomUser = authTestDataService.getRandomUser();
request.setAttribute(ShibUtil.lastNameAttribute, randomUser.get("lastName"));
request.setAttribute(ShibUtil.firstNameAttribute, randomUser.get("firstName"));
request.setAttribute(ShibUtil.emailAttribute, randomUser.get("email"));
request.setAttribute(ShibUtil.shibIdpAttribute, randomUser.get("idp"));
// eppn
request.setAttribute(ShibUtil.uniquePersistentIdentifier, UUID.randomUUID().toString().substring(0, 8));
}
}