forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClarinShibAuthentication.java
1383 lines (1227 loc) · 61.4 KB
/
ClarinShibAuthentication.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.authenticate.clarin;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.authenticate.AuthenticationMethod;
import org.dspace.authenticate.factory.AuthenticateServiceFactory;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.MetadataField;
import org.dspace.content.MetadataFieldName;
import org.dspace.content.MetadataSchema;
import org.dspace.content.MetadataSchemaEnum;
import org.dspace.content.NonUniqueMetadataException;
import org.dspace.content.clarin.ClarinUserRegistration;
import org.dspace.content.clarin.ClarinVerificationToken;
import org.dspace.content.factory.ClarinServiceFactory;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.MetadataFieldService;
import org.dspace.content.service.MetadataSchemaService;
import org.dspace.content.service.clarin.ClarinUserRegistrationService;
import org.dspace.content.service.clarin.ClarinVerificationTokenService;
import org.dspace.core.Context;
import org.dspace.core.Utils;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.EPersonService;
import org.dspace.eperson.service.GroupService;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
/**
* Shibboleth authentication for CLARIN-DSpace
*
* This class is customized ShibAuthentication class.
*
* Shibboleth is a distributed authentication system for securely authenticating
* users and passing attributes about the user from one or more identity
* providers. In the Shibboleth terminology DSpace is a Service Provider which
* receives authentication information and then based upon that provides a
* service to the user. With Shibboleth DSpace will require that you use
* Apache installed with the mod_shib module acting as a proxy for all HTTP
* requests for your servlet container (typically Tomcat). DSpace will receive
* authentication information from the mod_shib module through HTTP headers.
*
* See for more information on installing and configuring a Shibboleth
* Service Provider:
* https://wiki.shibboleth.net/confluence/display/SHIB2/Installation
*
* See the DSpace.cfg or DSpace manual for information on how to configure
* this authentication module.
*
* @author <a href="mailto:bliong@melcoe.mq.edu.au">Bruc Liong, MELCOE</a>
* @author <a href="mailto:kli@melcoe.mq.edu.au">Xiang Kevin Li, MELCOE</a>
* @author <a href="http://www.scottphillips.com">Scott Phillips</a>
* @author Milan Majchrak (milan.majchrak at dataquest.sk)
*/
public class ClarinShibAuthentication implements AuthenticationMethod {
/**
* log4j category
*/
private static final Logger log = LogManager.getLogger(ClarinShibAuthentication.class);
// If the user which are in the login process has email already associated with a different users email.
private boolean isDuplicateUser = false;
/**
* Additional metadata mappings
**/
protected Map<String, String> metadataHeaderMap = null;
/**
* Shibboleth headers retrieved from the request headers (standard auth) or request attribute (verification token).
*/
ShibHeaders shibheaders;
/**
* The class with user email and shib headers.
*/
ClarinVerificationToken clarinVerificationToken;
/**
* Maximum length for eperson metadata fields
**/
protected final int NAME_MAX_SIZE = 64;
protected final int PHONE_MAX_SIZE = 32;
/**
* Maximum length for eperson additional metadata fields
**/
protected final int METADATA_MAX_SIZE = 1024;
protected EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
protected GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
protected MetadataFieldService metadataFieldService = ContentServiceFactory.getInstance().getMetadataFieldService();
protected MetadataSchemaService metadataSchemaService = ContentServiceFactory.getInstance()
.getMetadataSchemaService();
protected ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
protected ClarinUserRegistrationService clarinUserRegistrationService =
ClarinServiceFactory.getInstance().getClarinUserRegistration();
protected ClarinVerificationTokenService clarinVerificationTokenService = ClarinServiceFactory.getInstance()
.getClarinVerificationTokenService();
/**
* Authenticate the given or implicit credentials. This is the heart of the
* authentication method: test the credentials for authenticity, and if
* accepted, attempt to match (or optionally, create) an
* <code>EPerson</code>. If an <code>EPerson</code> is found it is set in
* the <code>Context</code> that was passed.
*
* DSpace supports authentication using NetID, or email address. A user's NetID
* is a unique identifier from the IdP that identifies a particular user. The
* NetID can be of almost any form such as a unique integer, string, or with
* Shibboleth 2.0 you can use "targeted ids". You will need to coordinate with
* your Shibboleth federation or identity provider. There are three ways to
* supply identity information to DSpace:
*
* 1) NetID from Shibboleth Header (best)
*
* The NetID-based method is superior because users may change their email
* address with the identity provider. When this happens DSpace will not be
* able to associate their new address with their old account.
*
* 2) Email address from Shibboleth Header (okay)
*
* In the case where a NetID header is not available or not found DSpace
* will fall back to identifying a user based-upon their email address.
*
* 3) Tomcat's Remote User (worst)
*
* In the event that neither Shibboleth headers are found then as a last
* resort DSpace will look at Tomcat's remote user field. This is the least
* attractive option because Tomcat has no way to supply additional
* attributes about a user. Because of this the autoregister option is not
* supported if this method is used.
*
* Identity Scheme Migration Strategies:
*
* If you are currently using Email based authentication (either 1 or 2) and
* want to upgrade to NetID based authentication then there is an easy path.
* Simply enable Shibboleth to pass the NetID attribute and set the netid-header
* below to the correct value. When a user attempts to log in to DSpace first
* DSpace will look for an EPerson with the passed NetID, however when this
* fails DSpace will fall back to email based authentication. Then DSpace will
* update the user's EPerson account record to set their netid so all future
* authentications for this user will be based upon netid. One thing to note
* is that DSpace will prevent an account from switching NetIDs. If an account
* already has a NetID set and then they try and authenticate with a
* different NetID the authentication will fail.
*
* @param context DSpace context, will be modified (ePerson set) upon success.
* @param username Username (or email address) when method is explicit. Use null
* for implicit method.
* @param password Password for explicit auth, or null for implicit method.
* @param realm Not used by Shibboleth-based authentication
* @param request The HTTP request that started this operation, or null if not
* applicable.
* @return One of: SUCCESS, BAD_CREDENTIALS, CERT_REQUIRED, NO_SUCH_USER,
* BAD_ARGS
* <p>
* Meaning: <br>
* SUCCESS - authenticated OK. <br>
* BAD_CREDENTIALS - user exists, but credentials (e.g. passwd)
* don't match <br>
* CERT_REQUIRED - not allowed to login this way without X.509 cert.
* <br>
* NO_SUCH_USER - user not found using this method. <br>
* BAD_ARGS - user/pw not appropriate for this method
* @throws SQLException if database error
*/
@Override
public int authenticate(Context context, String username, String password,
String realm, HttpServletRequest request) throws SQLException {
// Check if sword compatibility is allowed, and if so see if we can
// authenticate based upon a username and password. This is really helpful
// if your repo uses Shibboleth but you want some accounts to be able use
// sword. This allows this compatibility without installing the password-based
// authentication method which has side effects such as allowing users to login
// with a username and password from the webui.
boolean swordCompatibility = configurationService
.getBooleanProperty("authentication-shibboleth.sword.compatibility", true);
if (swordCompatibility &&
username != null && username.length() > 0 &&
password != null && password.length() > 0) {
return swordCompatibility(context, username, password, request);
}
if (request == null) {
log.warn("Unable to authenticate using Shibboleth because the request object is null.");
return BAD_ARGS;
}
// CLARIN
// Log all headers received if debugging is turned on. This is enormously
// helpful when debugging shibboleth related problems.
if (log.isDebugEnabled()) {
log.debug("Starting Shibboleth Authentication");
}
// Shib headers could be loaded from the request header or request attribute. The shib headers are in the
// request attribute only if the user is trying to authenticate by `verification token`.
String shibHeadersAttr = (String) request.getAttribute("shib.headers");
if (StringUtils.isNotEmpty(shibHeadersAttr)) {
shibheaders = new ShibHeaders(shibHeadersAttr);
} else {
shibheaders = new ShibHeaders(request);
}
shibheaders.log_headers();
String organization = shibheaders.get_idp();
if (organization == null) {
log.info("Exiting shibboleth authenticate because no idp set");
return BAD_ARGS;
}
// The user e-mail is not stored in the `shibheaders` but in the `clarinVerificationToken`.
// The email was added to the `clarinVerificationToken` in the ClarinShibbolethFilter.
String[] netidHeaders = configurationService.getArrayProperty("authentication-shibboleth.netid-header");
// Load the verification token from the request header or from the request parameter.
// This is only set if the user is trying to authenticate with the `verification-token`.
String VERIFICATION_TOKEN = "verification-token";
String verificationTokenFromRequest = StringUtils.defaultIfBlank(request.getHeader(VERIFICATION_TOKEN),
request.getParameter(VERIFICATION_TOKEN));
if (StringUtils.isNotEmpty(verificationTokenFromRequest)) {
log.info("Verification token from request header `{}`: {}", VERIFICATION_TOKEN,
verificationTokenFromRequest);
clarinVerificationToken = clarinVerificationTokenService.findByToken(context, verificationTokenFromRequest);
}
// CLARIN
// Initialize the additional EPerson metadata.
initialize(context);
// Should we auto register new users.
boolean autoRegister = configurationService.getBooleanProperty("authentication-shibboleth.autoregister", true);
// Four steps to authenticate a user
try {
// Step 1: Identify User
EPerson eperson = findEPerson(context, request, netidHeaders);
// Step 2: Register New User, if necessary
if (eperson == null && autoRegister && !isDuplicateUser) {
eperson = registerNewEPerson(context, request, netidHeaders);
}
if (eperson == null) {
return AuthenticationMethod.NO_SUCH_USER;
}
// Step 3: Update User's Metadata
updateEPerson(context, request, eperson, netidHeaders);
// Step 4: Log the user in.
context.setCurrentUser(eperson);
request.getSession().setAttribute("shib.authenticated", true);
AuthenticateServiceFactory.getInstance().getAuthenticationService().initEPerson(context, request, eperson);
log.info(eperson.getEmail() + " has been authenticated via shibboleth.");
return AuthenticationMethod.SUCCESS;
} catch (Throwable t) {
// Log the error, and undo the authentication before returning a failure.
log.error("Unable to successfully authenticate using shibboleth for user because of " +
"an exception.", t);
context.setCurrentUser(null);
return AuthenticationMethod.NO_SUCH_USER;
}
}
/**
* Get list of extra groups that user implicitly belongs to. Note that this
* method will be invoked regardless of the authentication status of the
* user (logged-in or not) e.g. a group that depends on the client
* network-address.
*
* DSpace is able to place users into pre-defined groups based upon values
* received from Shibboleth. Using this option you can place all faculty members
* into a DSpace group when the correct affiliation's attribute is provided.
* When DSpace does this they are considered 'special groups', these are really
* groups but the user's membership within these groups is not recorded in the
* database. Each time a user authenticates they are automatically placed within
* the pre-defined DSpace group, so if the user loses their affiliation then the
* next time they login they will no longer be in the group.
*
* Depending upon the shibboleth attributed use in the role-header, it may be
* scoped. Scoped is shibboleth terminology for identifying where an attribute
* originated from. For example a students affiliation may be encoded as
* "student@tamu.edu". The part after the @ sign is the scope, and the preceding
* value is the value. You may use the whole value or only the value or scope.
* Using this you could generate a role for students and one institution
* different than students at another institution. Or if you turn on
* ignore-scope you could ignore the institution and place all students into
* one group.
*
* The values extracted (a user may have multiple roles) will be used to look
* up which groups to place the user into. The groups are defined as
* {@code authentication.shib.role.<role-name>} which is a comma separated list of
* DSpace groups.
*
* @param context A valid DSpace context.
* @param request The request that started this operation, or null if not
* applicable.
* @return array of EPerson-group IDs, possibly 0-length, but never
* <code>null</code>.
*/
@Override
public List<Group> getSpecialGroups(Context context, HttpServletRequest request) {
try {
// User has not successfuly authenticated via shibboleth.
if (request == null ||
context.getCurrentUser() == null ||
request.getSession().getAttribute("shib.authenticated") == null) {
return Collections.EMPTY_LIST;
}
// If we have already calculated the special groups then return them.
if (request.getSession().getAttribute("shib.specialgroup") != null) {
log.debug("Returning cached special groups.");
List<UUID> sessionGroupIds = (List<UUID>) request.getSession().getAttribute("shib.specialgroup");
List<Group> result = new ArrayList<>();
for (UUID uuid : sessionGroupIds) {
result.add(groupService.find(context, uuid));
}
return result;
}
List<UUID> groupIds = new ShibGroup(new ShibHeaders(request), context).get();
// Cache the special groups, so we don't have to recalculate them again
// for this session.
request.getSession().setAttribute("shib.specialgroup", groupIds);
List<Group> groups = new ArrayList<>();
for (UUID uuid : groupIds) {
Group foundGroup = groupService.find(context, uuid);
if (Objects.isNull(foundGroup)) {
continue;
}
groups.add(foundGroup);
}
return groups;
} catch (Throwable t) {
log.error("Unable to validate any sepcial groups this user may belong too because of an exception.", t);
return Collections.EMPTY_LIST;
}
}
/**
* Indicate whether or not a particular self-registering user can set
* themselves a password in the profile info form.
*
* @param context DSpace context
* @param request HTTP request, in case anything in that is used to decide
* @param email e-mail address of user attempting to register
* @throws SQLException if database error
*/
@Override
public boolean allowSetPassword(Context context,
HttpServletRequest request, String email) throws SQLException {
// don't use password at all
return false;
}
/**
* Predicate, is this an implicit authentication method. An implicit method
* gets credentials from the environment (such as an HTTP request or even
* Java system properties) rather than the explicit username and password.
* For example, a method that reads the X.509 certificates in an HTTPS
* request is implicit.
*
* @return true if this method uses implicit authentication.
*/
@Override
public boolean isImplicit() {
return false;
}
/**
* Indicate whether or not a particular user can self-register, based on
* e-mail address.
*
* @param context DSpace context
* @param request HTTP request, in case anything in that is used to decide
* @param username e-mail address of user attempting to register
* @throws SQLException if database error
*/
@Override
public boolean canSelfRegister(Context context, HttpServletRequest request,
String username) throws SQLException {
// Shibboleth will auto create accounts if configured to do so, but that is not
// the same as self register. Self register means that the user can sign up for
// an account from the web. This is not supported with shibboleth.
return false;
}
/**
* Initialize a new e-person record for a self-registered new user.
*
* @param context DSpace context
* @param request HTTP request, in case it's needed
* @param eperson newly created EPerson record - email + information from the
* registration form will have been filled out.
* @throws SQLException if database error
*/
@Override
public void initEPerson(Context context, HttpServletRequest request,
EPerson eperson) throws SQLException {
// We don't do anything because all our work is done authenticate and special groups.
}
/**
* Get login page to which to redirect. Returns URL (as string) to which to
* redirect to obtain credentials (either password prompt or e.g. HTTPS port
* for client cert.); null means no redirect.
* <P>
* For Shibboleth, this URL looks like (note 'target' param is URL encoded, but shown as unencoded in this example)
* [shibURL]?target=[dspace.server.url]/api/authn/shibboleth?redirectUrl=[dspace.ui.url]
* <P>
* This URL is used by the client to redirect directly to Shibboleth for authentication. The "target" param
* is then the location (in REST API) where Shibboleth redirects back to. The "redirectUrl" is the path/URL in the
* client (e.g. Angular UI) which the REST API redirects the user to (after capturing/storing any auth info from
* Shibboleth).
* @param context DSpace context, will be modified (ePerson set) upon success.
* @param request The HTTP request that started this operation, or null if not
* applicable.
* @param response The HTTP response from the servlet method.
* @return fully-qualified URL or null
*/
@Override
public String loginPageURL(Context context, HttpServletRequest request, HttpServletResponse response) {
// If this server is configured for lazy sessions then use this to
// login, otherwise default to the protected shibboleth url.
boolean lazySession = configurationService.getBooleanProperty("authentication-shibboleth.lazysession", false);
if ( lazySession ) {
String shibURL = getShibURL(request);
// Determine the client redirect URL, where to redirect after authenticating.
String redirectUrl = null;
if (request.getHeader("Referer") != null && StringUtils.isNotBlank(request.getHeader("Referer"))) {
redirectUrl = request.getHeader("Referer");
} else if (request.getHeader("X-Requested-With") != null
&& StringUtils.isNotBlank(request.getHeader("X-Requested-With"))) {
redirectUrl = request.getHeader("X-Requested-With");
}
// Determine the server return URL, where shib will send the user after authenticating.
// We need it to trigger DSpace's ShibbolethLoginFilter so we will extract the user's information,
// locally authenticate them & then redirect back to the UI.
String returnURL = configurationService.getProperty("dspace.server.url") + "/api/authn/shibboleth"
+ ((redirectUrl != null) ? "?redirectUrl=" + redirectUrl : "");
try {
shibURL += "?target=" + URLEncoder.encode(returnURL, "UTF-8");
} catch (UnsupportedEncodingException uee) {
log.error("Unable to generate lazysession authentication",uee);
}
log.debug("Redirecting user to Shibboleth initiator: " + shibURL);
return response.encodeRedirectURL(shibURL);
} else {
// If we are not using lazy sessions rely on the protected URL.
return response.encodeRedirectURL(request.getContextPath()
+ "/shibboleth-login");
}
}
@Override
public String getName() {
return "shibboleth";
}
/**
* Check if Shibboleth plugin is enabled
* @return true if enabled, false otherwise
*/
public static boolean isEnabled() {
final String shibPluginName = new ClarinShibAuthentication().getName();
boolean shibEnabled = false;
// Loop through all enabled authentication plugins to see if Shibboleth is one of them.
Iterator<AuthenticationMethod> authenticationMethodIterator =
AuthenticateServiceFactory.getInstance().getAuthenticationService().authenticationMethodIterator();
while (authenticationMethodIterator.hasNext()) {
if (shibPluginName.equals(authenticationMethodIterator.next().getName())) {
shibEnabled = true;
break;
}
}
return shibEnabled;
}
/**
* Identify an existing EPerson based upon the shibboleth attributes provided on
* the request object. There are three cases where this can occurr, each as
* a fallback for the previous method.
*
* 1) NetID from Shibboleth Header (best)
* The NetID-based method is superior because users may change their email
* address with the identity provider. When this happens DSpace will not be
* able to associate their new address with their old account.
* CLARIN
* Sometimes if the user with netid exists the epersonService.findByNetid cannot find it. This is happening
* only if the user is authenticated with `verification-token`. This problem is fixed.
* CLARIN
*
* 2) Email address from Shibboleth Header (okay)
* In the case where a NetID header is not available or not found DSpace
* will fall back to identifying a user based upon their email address.
*
* 3) Tomcat's Remote User (worst)
* In the event that neither Shibboleth headers are found then as a last
* resort DSpace will look at Tomcat's remote user field. This is the least
* attractive option because Tomcat has no way to supply additional
* attributes about a user. Because of this the autoregister option is not
* supported if this method is used.
*
* If successful then the identified EPerson will be returned, otherwise null.
*
* @param context The DSpace database context
* @param request The current HTTP Request
* @return The EPerson identified or null.
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
protected EPerson findEPerson(Context context, HttpServletRequest request, String[] netidHeaders)
throws SQLException {
boolean isUsingTomcatUser = configurationService
.getBooleanProperty("authentication-shibboleth.email-use-tomcat-remote-user");
String emailHeader = configurationService.getProperty("authentication-shibboleth.email-header");
EPerson eperson = null;
boolean foundNetID = false;
boolean foundEmail = false;
boolean foundRemoteUser = false;
// 1) First, look for a netid header.
if (netidHeaders != null) {
eperson = findEpersonByNetId(netidHeaders, shibheaders, eperson, ePersonService, context, true);
if (eperson != null) {
foundNetID = true;
}
}
// 2) Second, look for an email header.
if (eperson == null && emailHeader != null) {
String email = getEmailAcceptedOrNull(findSingleAttribute(request, emailHeader));
if (StringUtils.isEmpty(email) && Objects.nonNull(clarinVerificationToken)) {
email = clarinVerificationToken.getEmail();
}
if (email != null) {
foundEmail = true;
email = email.toLowerCase();
eperson = ePersonService.findByEmail(context, email);
if (eperson == null) {
log.info(
"Unable to identify EPerson based upon Shibboleth email header: '" + emailHeader + "'='" +
email + "'.");
} else {
log.info(
"Identified EPerson based upon Shibboleth email header: '" + emailHeader + "'='"
+ email + "'" + ".");
}
// The condition `Objects.isNull(clarinVerificationToken)` was added because ePersonService couldn't
// find the eperson by netid when he exists. Otherwise the service find the user correctly
// but in that case when the clarinVerificationToken is not null it cannot find him. Do not know why.
if (eperson != null && eperson.getNetid() != null && Objects.isNull(clarinVerificationToken)) {
// If the user has a netID it has been locked to that netid, don't let anyone else try and steal
// the account.
log.error(
"The identified EPerson based upon Shibboleth email header, '" + emailHeader + "'='"
+ email + "', is locked to another netid: '" + eperson.getNetid() +
"'. This might be a possible hacking attempt to steal another users " +
"credentials. If the user's netid has changed you will need to manually " +
"change it to the correct value or unset it in the database.");
this.isDuplicateUser = true;
eperson = null;
}
}
}
// 3) Last, check to see if tomcat is passing a user.
if (eperson == null && isUsingTomcatUser) {
String email = request.getRemoteUser();
if (email != null) {
foundRemoteUser = true;
email = email.toLowerCase();
eperson = ePersonService.findByEmail(context, email);
if (eperson == null) {
log.info("Unable to identify EPerson based upon Tomcat's remote user: '" + email + "'.");
} else {
log.info("Identified EPerson based upon Tomcat's remote user: '" + email + "'.");
}
if (eperson != null && eperson.getNetid() != null) {
// If the user has a netID it has been locked to that netid, don't let anyone else try and steal
// the account.
log.error(
"The identified EPerson based upon Tomcat's remote user, '" + email + "', is locked to " +
"another netid: '" + eperson
.getNetid() + "'. This might be a possible hacking attempt to steal another" +
" users credentials. If the user's netid has changed you will need to manually" +
" change it to the correct value or unset it in the database.");
eperson = null;
}
}
}
if (!foundNetID && !foundEmail && !foundRemoteUser) {
log.error(
"Shibboleth authentication was not able to find a NetId, Email, or Tomcat Remote user for " +
"which to indentify a user from.");
}
return eperson;
}
/**
* Register a new eperson object. This method is called when no existing user was
* found for the NetID or Email and autoregister is enabled. When these conditions
* are met this method will create a new eperson object.
*
* In order to create a new eperson object there is a minimal set of metadata
* required: Email, First Name, and Last Name. If we don't have access to these
* three pieces of information then we will be unable to create a new eperson
* object, such as the case when Tomcat's Remote User field is used to identify
* a particular user.
*
* Note, that this method only adds the minimal metadata. Any additional metadata
* will need to be added by the updateEPerson method.
*
* @param context The current DSpace database context
* @param request The current HTTP Request
* @return A new eperson object or null if unable to create a new eperson.
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
protected EPerson registerNewEPerson(Context context, HttpServletRequest request, String[] netidHeaders)
throws SQLException, AuthorizeException {
// Header names
String emailHeader = configurationService.getProperty("authentication-shibboleth.email-header");
String fnameHeader = configurationService.getProperty("authentication-shibboleth.firstname-header");
String lnameHeader = configurationService.getProperty("authentication-shibboleth.lastname-header");
// CLARIN
String org = shibheaders.get_idp();
if ( org == null ) {
return null;
}
// CLARIN
// Header values
String netid = getFirstNetId(netidHeaders);
String email = getEmailAcceptedOrNull(findSingleAttribute(request, emailHeader));
String fname = Headers.updateValueByCharset(findSingleAttribute(request, fnameHeader));
String lname = Headers.updateValueByCharset(findSingleAttribute(request, lnameHeader));
// If the values are not in the request headers try to retrieve it from `shibheaders`.
if (StringUtils.isEmpty(email) && Objects.nonNull(clarinVerificationToken)) {
email = clarinVerificationToken.getEmail();
}
if (StringUtils.isEmpty(fname)) {
fname = shibheaders.get_single(fnameHeader);
}
if (StringUtils.isEmpty(lname)) {
lname = shibheaders.get_single(lnameHeader);
}
if ( email == null ) {
// We require that there be an email, first name, and last name. If we
// don't have at least these three pieces of information then we fail.
String message = "Unable to register new eperson because we are unable to find an email address along " +
"with first and last name for the user.\n";
message += " NetId Header: '" + Arrays.toString(netidHeaders) + "'='" + netid + "' (Optional) \n";
message += " Email Header: '" + emailHeader + "'='" + email + "' \n";
message += " First Name Header: '" + fnameHeader + "'='" + fname + "' \n";
message += " Last Name Header: '" + lnameHeader + "'='" + lname + "'";
log.error( String.format(
"Could not identify a user from [%s] - we have not received enough information " +
"(email, netid, eppn, ...). \n\nDetails:\n%s\n\nHeaders received:\n%s",
org, message, request.getHeaderNames().toString()) );
return null; // TODO should this throw an exception?
}
// Turn off authorizations to create a new user
context.turnOffAuthorisationSystem();
EPerson eperson = ePersonService.create(context);
// Set the minimum attributes for the new eperson
if (netid != null) {
eperson.setNetid(netid);
}
eperson.setEmail(email.toLowerCase());
if (fname != null) {
eperson.setFirstName(context, fname);
}
if (lname != null) {
eperson.setLastName(context, lname);
}
eperson.setCanLogIn(true);
// Commit the new eperson
AuthenticateServiceFactory.getInstance().getAuthenticationService().initEPerson(context, request, eperson);
ePersonService.update(context, eperson);
context.dispatchEvents();
/* CLARIN
*
* Register User in the CLARIN license database
*
*/
// if no email the registration is postponed after entering and confirming mail
if (Objects.nonNull(email)) {
try {
ClarinUserRegistration clarinUserRegistration = new ClarinUserRegistration();
clarinUserRegistration.setConfirmation(true);
clarinUserRegistration.setEmail(email);
clarinUserRegistration.setPersonID(eperson.getID());
clarinUserRegistration.setOrganization(org);
clarinUserRegistrationService.create(context, clarinUserRegistration);
eperson.setCanLogIn(false);
ePersonService.update(context, eperson);
} catch (Exception e) {
throw new AuthorizeException("User has not been added among registred users!") ;
}
}
/* CLARIN */
// Turn authorizations back on.
context.restoreAuthSystemState();
if (log.isInfoEnabled()) {
String message = "Auto registered new eperson using Shibboleth-based attributes:";
if (netid != null) {
message += " NetId: '" + netid + "'\n";
}
message += " Email: '" + email + "' \n";
message += " First Name: '" + fname + "' \n";
message += " Last Name: '" + lname + "'";
log.info(message);
}
return eperson;
}
/**
* After we successfully authenticated a user, this method will update the user's attributes. The
* user's email, name, or other attribute may have been changed since the last time they
* logged into DSpace. This method will update the database with their most recent information.
*
* This method handles the basic DSpace metadata (email, first name, last name) along with
* additional metadata set using the setMetadata() methods on the eperson object. The
* additional metadata are defined by a mapping created in the dspace.cfg.
*
* @param context The current DSpace database context
* @param request The current HTTP Request
* @param eperson The eperson object to update.
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
protected void updateEPerson(Context context, HttpServletRequest request, EPerson eperson, String[] netidHeaders)
throws SQLException, AuthorizeException {
// Header names & values
String emailHeader = configurationService.getProperty("authentication-shibboleth.email-header");
String fnameHeader = configurationService.getProperty("authentication-shibboleth.firstname-header");
String lnameHeader = configurationService.getProperty("authentication-shibboleth.lastname-header");
String netid = getFirstNetId(netidHeaders);
String email = getEmailAcceptedOrNull(findSingleAttribute(request, emailHeader));
String fname = Headers.updateValueByCharset(findSingleAttribute(request, fnameHeader));
String lname = Headers.updateValueByCharset(findSingleAttribute(request, lnameHeader));
// If the values are not in the request headers try to retrieve it from `shibheaders`.
if (StringUtils.isEmpty(email) && Objects.nonNull(clarinVerificationToken)) {
email = clarinVerificationToken.getEmail();
}
if (StringUtils.isEmpty(fname)) {
fname = shibheaders.get_single(fnameHeader);
}
if (StringUtils.isEmpty(lname)) {
lname = shibheaders.get_single(lnameHeader);
}
// Truncate values of parameters that are too big.
if (fname != null && fname.length() > NAME_MAX_SIZE) {
log.warn(
"Truncating eperson's first name because it is longer than " + NAME_MAX_SIZE + ": '" + fname + "'");
fname = fname.substring(0, NAME_MAX_SIZE);
}
if (lname != null && lname.length() > NAME_MAX_SIZE) {
log.warn("Truncating eperson's last name because it is longer than " + NAME_MAX_SIZE + ": '" + lname + "'");
lname = lname.substring(0, NAME_MAX_SIZE);
}
context.turnOffAuthorisationSystem();
// 1) Update the minimum metadata
// Only update the netid if none has been previously set. This can occur when a repo switches
// to netid based authentication. The current users do not have netids and fall back to email-based
// identification but once they login we update their record and lock the account to a particular netid.
if (netid != null && eperson.getNetid() == null) {
eperson.setNetid(netid);
}
// The email could have changed if using netid based lookup.
if (email != null) {
String lowerCaseEmail = email.toLowerCase();
// Check the email is unique
EPerson epersonByEmail = ePersonService.findByEmail(context, lowerCaseEmail);
if (epersonByEmail != null && !epersonByEmail.getID().equals(eperson.getID())) {
log.error("Unable to update the eperson's email metadata because the email '{}' is already in use.",
lowerCaseEmail);
throw new AuthorizeException("The email address is already in use.");
} else {
eperson.setEmail(email.toLowerCase());
}
}
if (fname != null) {
eperson.setFirstName(context, fname);
}
if (lname != null) {
eperson.setLastName(context, lname);
}
if (log.isDebugEnabled()) {
String message = "Updated the eperson's minimal metadata: \n";
message += " Email Header: '" + emailHeader + "' = '" + email + "' \n";
message += " First Name Header: '" + fnameHeader + "' = '" + fname + "' \n";
message += " Last Name Header: '" + fnameHeader + "' = '" + lname + "'";
log.debug(message);
}
// 2) Update additional eperson metadata
for (String header : metadataHeaderMap.keySet()) {
String field = metadataHeaderMap.get(header);
String value = findSingleAttribute(request, header);
if (StringUtils.isEmpty(value)) {
value = shibheaders.get_single(header);
}
// Truncate values
if (value == null) {
log.warn("Unable to update the eperson's '{}' metadata"
+ " because the header '{}' does not exist.", field, header);
continue;
} else if ("phone".equals(field) && value.length() > PHONE_MAX_SIZE) {
log.warn("Truncating eperson phone metadata because it is longer than {}: '{}'",
PHONE_MAX_SIZE, value);
value = value.substring(0, PHONE_MAX_SIZE);
} else if (value.length() > METADATA_MAX_SIZE) {
log.warn("Truncating eperson {} metadata because it is longer than {}: '{}'",
field, METADATA_MAX_SIZE, value);
value = value.substring(0, METADATA_MAX_SIZE);
}
String[] nameParts = MetadataFieldName.parse(field);
ePersonService.setMetadataSingleValue(context, eperson,
nameParts[0], nameParts[1], nameParts[2], value, null);
log.debug("Updated the eperson's '{}' metadata using header: '{}' = '{}'.",
field, header, value);
}
ePersonService.update(context, eperson);
context.dispatchEvents();
context.restoreAuthSystemState();
}
/**
* Provide password-based authentication to enable sword compatibility.
*
* Sword compatibility will allow this authentication method to work when using
* sword. Sword relies on username and password based authentication and is
* entirely incapable of supporting shibboleth. This option allows you to
* authenticate username and passwords for sword sessions without adding
* another authentication method onto the stack. You will need to ensure that
* a user has a password. One way to do that is to create the user via the
* create-administrator command line command and then edit their permissions.
*
* @param context The DSpace database context
* @param username The username
* @param password The password
* @param request The HTTP Request
* @return A valid DSpace Authentication Method status code.
* @throws SQLException if database error
*/
protected int swordCompatibility(Context context, String username, String password, HttpServletRequest request)
throws SQLException {
log.debug("Shibboleth Sword compatibility activated.");
EPerson eperson = ePersonService.findByEmail(context, username.toLowerCase());
if (eperson == null) {
// lookup failed.
log.error(
"Shibboleth-based password authentication failed for user " + username +
" because no such user exists.");
return NO_SUCH_USER;
} else if (!eperson.canLogIn()) {
// cannot login this way
log.error(
"Shibboleth-based password authentication failed for user " + username +
" because the eperson object is not allowed to login.");
return BAD_ARGS;
} else if (eperson.getRequireCertificate()) {
// this user can only login with x.509 certificate
log.error(
"Shibboleth-based password authentication failed for user " + username +
" because the eperson object requires a certificate to authenticate..");
return CERT_REQUIRED;
} else if (ePersonService.checkPassword(context, eperson, password)) {
// Password matched
AuthenticateServiceFactory.getInstance().getAuthenticationService().initEPerson(context, request, eperson);
context.setCurrentUser(eperson);
log.info(eperson
.getEmail() + " has been authenticated via shibboleth using password-based sword " +
"compatibility mode.");
return SUCCESS;
} else {
// Passsword failure
log.error(
"Shibboleth-based password authentication failed for user " + username +
" because a bad password was supplied.");
return BAD_CREDENTIALS;
}
}
/**
* Initialize Shibboleth Authentication.
*
* During initalization the mapping of additional eperson metadata will be loaded from the DSpace.cfg
* and cached. While loading the metadata mapping this method will check the EPerson object to see
* if it supports the metadata field. If the field is not supported and autocreate is turned on then
* the field will be automatically created.
*
* It is safe to call this methods multiple times.
*
* @param context context
* @throws SQLException if database error
*/
protected synchronized void initialize(Context context) throws SQLException {
if (metadataHeaderMap != null) {
return;
}
HashMap<String, String> map = new HashMap<>();
String[] mappingString = configurationService.getArrayProperty("authentication-shibboleth.eperson.metadata");
boolean autoCreate = configurationService
.getBooleanProperty("authentication-shibboleth.eperson.metadata.autocreate", true);
// Bail out if not set, returning an empty map.
if (mappingString == null || mappingString.length == 0) {
log.debug("No additional eperson metadata mapping found: authentication.shib.eperson.metadata");
metadataHeaderMap = map;
return;
}