forked from benmccann/jenkins-saml-plugin
-
Notifications
You must be signed in to change notification settings - Fork 102
/
SamlSecurityRealm.java
784 lines (695 loc) · 32.6 KB
/
SamlSecurityRealm.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
/* Licensed to Jenkins CI under one or more contributor license
agreements. See the NOTICE file distributed with this work
for additional information regarding copyright ownership.
Jenkins CI licenses this file to you under the Apache License,
Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License. */
package org.jenkinsci.plugins.saml;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpSession;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.acegisecurity.Authentication;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.saml.conf.Attribute;
import org.jenkinsci.plugins.saml.conf.AttributeEntry;
import org.jenkinsci.plugins.saml.user.SamlCustomProperty;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.Header;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.pac4j.core.exception.http.FoundAction;
import org.pac4j.core.exception.http.WithLocationAction;
import org.pac4j.core.exception.http.OkAction;
import org.pac4j.core.exception.http.RedirectionAction;
import org.pac4j.core.exception.http.SeeOtherAction;
import org.springframework.dao.DataAccessException;
import org.pac4j.saml.profile.SAML2Profile;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.GroupDetails;
import hudson.security.SecurityRealm;
import hudson.security.UserMayOrMayNotExistException2;
import hudson.tasks.Mailer.UserProperty;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;
import jenkins.security.SecurityListener;
import static org.apache.commons.codec.binary.Base64.decodeBase64;
import static org.apache.commons.codec.binary.Base64.isBase64;
import static org.opensaml.saml.common.xml.SAMLConstants.SAML2_REDIRECT_BINDING_URI;
/**
* Authenticates the user via SAML.
* This class is the main entry point to the plugin.
* Uses Stapler (stapler.kohsuke.org) to bind methods to URLs.
*
* @see SecurityRealm
*/
public class SamlSecurityRealm extends SecurityRealm {
public static final String DEFAULT_DISPLAY_NAME_ATTRIBUTE_NAME = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
public static final String DEFAULT_GROUPS_ATTRIBUTE_NAME = "http://schemas.xmlsoap.org/claims/Group";
public static final int DEFAULT_MAXIMUM_AUTHENTICATION_LIFETIME = 24 * 60 * 60; // 24h
public static final String DEFAULT_USERNAME_CASE_CONVERSION = "none";
public static final String SP_METADATA_FILE_NAME = "saml-sp-metadata.xml";
public static final String IDP_METADATA_FILE_NAME = "saml-idp-metadata.xml";
/**
* form validation messages.
*/
public static final String ERROR_ONLY_SPACES_FIELD_VALUE = "The field should have a value different than spaces";
public static final String ERROR_NOT_VALID_NUMBER = "The field should be a number greater than 0 and lower than " + Integer.MAX_VALUE + ".";
public static final String ERROR_MALFORMED_URL = "The url is malformed.";
public static final String ERROR_IDP_METADATA_EMPTY = "The IdP Metadata can not be empty.";
public static final String WARN_RECOMMENDED_TO_SET_THE_GROUPS_ATTRIBUTE = "It is recommended to set the groups attribute.";
public static final String WARN_RECOMMENDED_TO_SET_THE_USERNAME_ATTRIBUTE = "It is recommended to set the username attribute.";
public static final String WARN_RECOMMENDED_TO_SET_THE_EMAIL_ATTRIBUTE = "It is recommended to set the email attribute.";
public static final String ERROR_NOT_POSSIBLE_TO_READ_KS_FILE = "It is not possible to read the keystore file.";
public static final String ERROR_CERTIFICATES_COULD_NOT_BE_LOADED = "Any of the certificates in the keystore could not be loaded";
public static final String ERROR_ALGORITHM_CANNOT_BE_FOUND = "the algorithm used to check the integrity of the keystore cannot be found";
public static final String ERROR_NO_PROVIDER_SUPPORTS_A_KS_SPI_IMPL = "No Provider supports a KeyStoreSpi implementation for the specified type.";
public static final String ERROR_WRONG_INFO_OR_PASSWORD = "The entry is a PrivateKeyEntry or SecretKeyEntry and the specified protParam does not contain the information needed to recover the key (e.g. wrong password)";
public static final String ERROR_INSUFFICIENT_OR_INVALID_INFO = "The specified protParam were insufficient or invalid";
/**
* URL to process the SAML answers
*/
public static final String CONSUMER_SERVICE_URL_PATH = "securityRealm/finishLogin";
private static final Logger LOG = Logger.getLogger(SamlSecurityRealm.class.getName());
private static final String REFERER_ATTRIBUTE = SamlSecurityRealm.class.getName() + ".referer";
public static final String WARN_THERE_IS_NOT_KEY_STORE = "There is not keyStore to validate";
public static final String ERROR_NOT_KEY_FOUND = "Not key found";
public static final String SUCCESS = "Success";
public static final String NOT_POSSIBLE_TO_GET_THE_METADATA = "Was not possible to get the Metadata from the URL ";
public static final String CHECK_TROUBLESHOOTING_GUIDE = "\nIf you have issues check the troubleshoting guide at https://github.com/jenkinsci/saml-plugin/blob/master/doc/TROUBLESHOOTING.md";
public static final String CHECK_MAX_AUTH_LIFETIME = "\nFor more info check 'Maximum Authentication Lifetime' at https://github.com/jenkinsci/saml-plugin/blob/master/doc/CONFIGURE.md#configuring-plugin-settings";
public static final String WARN_KEYSTORE_NOT_SET = "Keystore is not set";
public static final String WARN_PRIVATE_KEY_ALIAS_NOT_SET = "Key alias is not set";
public static final String WARN_PRIVATE_KEYSTORE_PASS_NOT_SET = "Keystore password is not set";
public static final String WARN_PRIVATE_KEY_PASS_NOT_SET = "Key password is not set";
/**
* configuration settings.
*/
private String displayNameAttributeName;
private String groupsAttributeName;
private int maximumAuthenticationLifetime;
private String emailAttributeName;
private final String usernameCaseConversion;
private final String usernameAttributeName;
private final String logoutUrl;
private String binding;
private final SamlEncryptionData encryptionData;
private final SamlAdvancedConfiguration advancedConfiguration;
private final IdpMetadataConfiguration idpMetadataConfiguration;
private List<AttributeEntry> samlCustomAttributes;
/**
* Jenkins passes these parameters in when you update the settings.
* It does this because of the @DataBoundConstructor.
*
* @param idpMetadataConfiguration How to obtain the IdP Metadata configuration.
* @param displayNameAttributeName attribute that has the displayname
* @param groupsAttributeName attribute that has the groups
* @param maximumAuthenticationLifetime maximum time that an identification it is valid
* @param usernameAttributeName attribute that has the username
* @param emailAttributeName attribute that has the email
* @param logoutUrl optional URL to redirect on logout
* @param advancedConfiguration advanced configuration settings
* @param encryptionData encryption configuration settings
* @param usernameCaseConversion username case sensitive settings
* @param binding SAML binding method.
* @param samlCustomAttributes Custom Attributes to read from the SAML Responsse.
* @throws IOException if it is not possible to write the IdP metadata file.
*/
@DataBoundConstructor
public SamlSecurityRealm(
IdpMetadataConfiguration idpMetadataConfiguration,
String displayNameAttributeName,
String groupsAttributeName,
Integer maximumAuthenticationLifetime,
String usernameAttributeName,
String emailAttributeName,
String logoutUrl,
SamlAdvancedConfiguration advancedConfiguration,
SamlEncryptionData encryptionData,
String usernameCaseConversion,
String binding,
List<AttributeEntry> samlCustomAttributes) throws IOException {
super();
this.idpMetadataConfiguration = idpMetadataConfiguration;
this.usernameAttributeName = hudson.Util.fixEmptyAndTrim(usernameAttributeName);
this.usernameCaseConversion = org.apache.commons.lang.StringUtils.defaultIfBlank(usernameCaseConversion, DEFAULT_USERNAME_CASE_CONVERSION);
this.logoutUrl = hudson.Util.fixEmptyAndTrim(logoutUrl);
this.displayNameAttributeName = DEFAULT_DISPLAY_NAME_ATTRIBUTE_NAME;
this.groupsAttributeName = DEFAULT_GROUPS_ATTRIBUTE_NAME;
this.maximumAuthenticationLifetime = DEFAULT_MAXIMUM_AUTHENTICATION_LIFETIME;
if (displayNameAttributeName != null && !displayNameAttributeName.isEmpty()) {
this.displayNameAttributeName = displayNameAttributeName;
}
if (groupsAttributeName != null && !groupsAttributeName.isEmpty()) {
this.groupsAttributeName = groupsAttributeName;
}
if (maximumAuthenticationLifetime != null && maximumAuthenticationLifetime > 0) {
this.maximumAuthenticationLifetime = maximumAuthenticationLifetime;
}
if (org.apache.commons.lang.StringUtils.isNotBlank(emailAttributeName)) {
this.emailAttributeName = hudson.Util.fixEmptyAndTrim(emailAttributeName);
}
this.advancedConfiguration = advancedConfiguration;
this.encryptionData = encryptionData;
this.binding = binding;
this.samlCustomAttributes = samlCustomAttributes;
this.idpMetadataConfiguration.createIdPMetadataFile();
LOG.finer(this.toString());
}
// migration code for the new IdP metadata file
@SuppressWarnings("unused")
public Object readResolve() {
File idpMetadataFile = new File(getIDPMetadataFilePath());
if (!idpMetadataFile.exists() && idpMetadataConfiguration != null && idpMetadataConfiguration.getXml() != null){
try {
idpMetadataConfiguration.createIdPMetadataFile();
} catch (IOException e) {
LOG.log(Level.SEVERE, e.getMessage(), e);
}
}
if(StringUtils.isEmpty(getBinding())){
binding = SAML2_REDIRECT_BINDING_URI;
}
return this;
}
@Override
public boolean allowsSignup() {
return false;
}
@Override
public SecurityComponents createSecurityComponents() {
LOG.finer("createSecurityComponents");
return new SecurityComponents(authentication -> {
if (authentication instanceof SamlAuthenticationToken) {
return authentication;
}
throw new BadCredentialsException("Unexpected authentication type: " + authentication);
}, new SamlUserDetailsService());
}
@Override
public String getLoginUrl() {
return "securityRealm/commenceLogin";
}
/**
* /securityRealm/commenceLogin
*
* @param request http request.
* @param response http response.
* @param referer referer.
* @param from http request "from" parameter.
* @return the http response.
*/
@SuppressWarnings("unused")
public HttpResponse doCommenceLogin(final StaplerRequest request, final StaplerResponse response, @QueryParameter
String from, @Header("Referer") final String referer) {
LOG.fine("SamlSecurityRealm.doCommenceLogin called. Using consumerServiceUrl " + getSamlPluginConfig().getConsumerServiceUrl());
String redirectOnFinish = calculateSafeRedirect(from, referer);
request.getSession().setAttribute(REFERER_ATTRIBUTE, redirectOnFinish);
RedirectionAction action = new SamlRedirectActionWrapper(getSamlPluginConfig(), request, response).get();
if (action instanceof SeeOtherAction || action instanceof FoundAction) {
LOG.fine("REDIRECT : " + ((WithLocationAction)action).getLocation());
return HttpResponses.redirectTo(((WithLocationAction)action).getLocation());
} else if (action instanceof OkAction) {
LOG.fine("SUCCESS : " + ((OkAction) action).getContent());
return HttpResponses.literalHtml(((OkAction) action).getContent());
} else {
throw new IllegalStateException("Received unexpected response type " + action.getCode());
}
}
/**
* Check parameters "from" and "referer" to decide where is the safe URL to be redirected.
* @param from http request "from" parameter.
* @param referer referer header.
* @return a safe URL to be redirected.
*/
private String calculateSafeRedirect(String from, String referer) {
String redirectURL;
String rootUrl = baseUrl();
//noinspection PointlessNullCheck
if (from != null && Util.isSafeToRedirectTo(from)) {
redirectURL = from;
} else {
if (referer != null && (referer.startsWith(rootUrl) || Util.isSafeToRedirectTo(referer))) {
redirectURL = referer;
} else {
redirectURL = rootUrl;
}
}
LOG.fine("Safe URL redirection: " + redirectURL);
return redirectURL;
}
/**
* /securityRealm/finishLogin
*
* @param request http request.
* @param response http response.
* @return the http response.
*/
@SuppressWarnings("unused")
@RequirePOST
public HttpResponse doFinishLogin(final StaplerRequest request, final StaplerResponse response) {
LOG.finer("SamlSecurityRealm.doFinishLogin called");
String referer = (String) request.getSession().getAttribute(REFERER_ATTRIBUTE);
// redirect back to original page
String redirectUrl = referer != null ? referer : baseUrl();
recreateSession(request);
logSamlResponse(request);
boolean saveUser = false;
SAML2Profile saml2Profile;
try {
saml2Profile = new SamlProfileWrapper(getSamlPluginConfig(), request, response).get();
} catch (BadCredentialsException e){
LOG.log(Level.WARNING, "Unable to validate the SAML Response: " + e.getMessage()
+ CHECK_MAX_AUTH_LIFETIME
+ CHECK_TROUBLESHOOTING_GUIDE, e);
return HttpResponses.redirectTo(getEffectiveLogoutUrl());
}
// getId and possibly convert, based on settings
String username = loadUserName(saml2Profile);
List<GrantedAuthority> authorities = loadGrantedAuthorities(saml2Profile);
// create user data
SamlUserDetails userDetails = new SamlUserDetails(username, authorities);
SamlAuthenticationToken samlAuthToken = new SamlAuthenticationToken(userDetails);
ACL.as2(samlAuthToken);
SecurityListener.fireAuthenticated2(userDetails);
User user = User.current();
saveUser |= modifyUserFullName(user, saml2Profile);
//retrieve user email
List<String> emails = getListOfValues(saml2Profile.getAttribute(getEmailAttributeName()));
saveUser |= modifyUserEmail(user, emails);
saveUser |= modifyUserSamlCustomAttributes(user, saml2Profile);
try {
if (user != null && saveUser) {
user.save();
}
} catch (IOException e) {
// even if it fails, nothing critical
LOG.log(Level.WARNING, "Unable to save updated user data", e);
}
SecurityListener.fireLoggedIn(userDetails.getUsername());
return HttpResponses.redirectTo(redirectUrl);
}
/**
* retrieve the value of an attribute in a list for consistence with the reset of attributes manage.
* @return the values of the attribute in a list.
*/
@NonNull
private List<String> getListOfValues(Object attributeValue) {
@SuppressWarnings("unchecked")
List<String> listOfValues = Collections.emptyList();
if(attributeValue instanceof List) {
listOfValues = (List<String>) attributeValue;
} else if (attributeValue instanceof String){
listOfValues = Collections.singletonList((String) attributeValue);
}
return listOfValues;
}
private String getEffectiveLogoutUrl() {
return StringUtils.isNotBlank(getLogoutUrl()) ? getLogoutUrl() : Jenkins.get().getRootUrl() + SamlLogoutAction.POST_LOGOUT_URL;
}
/**
* check if a request contains a session, if so, it invalidates the session and create new one to avoid session
* fixation.
* @param request request.
*/
private void recreateSession(StaplerRequest request) {
HttpSession session = request.getSession(false);
if(session != null){
LOG.finest("Invalidate previous session");
// avoid session fixation
session.invalidate();
}
request.getSession(true);
}
private boolean modifyUserSamlCustomAttributes(User user, SAML2Profile profile) {
boolean saveUser = false;
if(!getSamlCustomAttributes().isEmpty() && user != null){
SamlCustomProperty userProperty = new SamlCustomProperty(new ArrayList<>());
for (AttributeEntry attributeEntry : getSamlCustomAttributes()) {
if(attributeEntry instanceof Attribute){
Attribute attr = (Attribute) attributeEntry;
SamlCustomProperty.Attribute item = new SamlCustomProperty.Attribute(attr.getName(),attr.getDisplayName());
item.setValue(profile.getAttribute(attr.getName()).toString());
userProperty.getAttributes().add(item);
}
}
try {
user.addProperty(userProperty);
} catch (IOException e) {
LOG.log(Level.SEVERE, "Could not update user SAML custom attributes", e);
}
saveUser = true;
}
return saveUser;
}
/**
* Tries to log the content of the SAMLResponse even it is not valid.
* @param request Request received in doFinishLogin, it should be a SAMLResponse.
*/
private void logSamlResponse(StaplerRequest request) {
if(LOG.isLoggable(Level.FINEST)){
try {
String samlResponse = request.getParameter("SAMLResponse");
if(isBase64(samlResponse)) {
LOG.finest("SAMLResponse XML:" + new String(decodeBase64(samlResponse), request.getCharacterEncoding()));
} else {
LOG.finest("SAMLResponse XML:" + samlResponse);
}
} catch (Exception e) {
LOG.finest("No UTF-8 SAMLResponse XML");
try(InputStream in = request.getInputStream()) {
LOG.finest(IOUtils.toString(in, request.getCharacterEncoding()));
} catch (IOException e1) {
LOG.finest("Was not possible to read the request");
}
}
}
}
private String baseUrl() {
return Jenkins.get().getRootUrl();
}
/**
* load the username from the profile and set the correct characters case.
*
* @param saml2Profile SAML Profile.
* @return the user name.
*/
private String loadUserName(SAML2Profile saml2Profile) {
String username = getUsernameFromProfile(saml2Profile);
if ("lowercase".compareTo(getUsernameCaseConversion()) == 0) {
username = username.toLowerCase();
} else if ("uppercase".compareTo(getUsernameCaseConversion()) == 0) {
username = username.toUpperCase();
}
return username;
}
/**
* modify the fullname in the current user taken it from the SAML Profile.
*
* @param user current user.
* @param saml2Profile SAML Profile.
* @return true if the current user is modified.
*/
private boolean modifyUserFullName(User user, SAML2Profile saml2Profile) {
boolean saveUser = false;
// retrieve user display name
String userFullName = null;
List<String> names = getListOfValues(saml2Profile.getAttribute(getDisplayNameAttributeName()));
if (!names.isEmpty()) {
userFullName = names.get(0);
}
// update user full name if necessary
if (user != null && StringUtils.isNotBlank(userFullName)) {
if (userFullName.compareTo(user.getFullName()) != 0) {
user.setFullName(userFullName);
saveUser = true;
}
}
return saveUser;
}
/**
* load the granted authorities from the SAML Profile.
*
* @param saml2Profile SAML Profile.
* @return granted authorities.
*/
@Restricted(NoExternalUse.class) // Visible for testing
List<GrantedAuthority> loadGrantedAuthorities(SAML2Profile saml2Profile) {
// prepare list of groups
List<String> groups = getListOfValues(saml2Profile.getAttribute(getGroupsAttributeName())) ;
// build list of authorities
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(AUTHENTICATED_AUTHORITY2);
int countEmptyGroups = 0;
for (String group : groups) {
if (StringUtils.isNotBlank(group)) {
authorities.add(new SimpleGrantedAuthority(group));
} else {
countEmptyGroups++;
}
}
if (countEmptyGroups > 0) {
LOG.log(Level.WARNING, String.format("Found %d empty groups in the saml profile for %s. Please check the SAML backend configuration.", countEmptyGroups, getUsernameFromProfile(saml2Profile)));
}
return authorities;
}
/**
* set the user email. It will take the first not empty value on the list of email.
*
* @param user current user.
* @param emails user emails.
* @return true if the current user is modified.
*/
private boolean modifyUserEmail(User user, @NonNull List<String> emails) {
String userEmail = null;
boolean saveUser = false;
if (emails.isEmpty()) {
LOG.warning("There is not Email attribute '" + getEmailAttributeName() + "' for user : " + user.getId());
return false;
}
for (String item : emails) {
if (StringUtils.isNotEmpty(item)) {
userEmail = item;
break;
}
}
if (StringUtils.isBlank(userEmail)) {
LOG.warning("The Email is blank for user : " + user.getId());
}
try {
if (user != null && StringUtils.isNotBlank(userEmail)) {
UserProperty currentUserEmailProperty = user.getProperty(UserProperty.class);
if (currentUserEmailProperty == null ||
userEmail.compareTo(StringUtils.defaultIfBlank(currentUserEmailProperty.getAddress(), "")) != 0) {
// email address
UserProperty emailProperty = new UserProperty(userEmail);
user.addProperty(emailProperty);
saveUser = true;
}
}
} catch (IOException e) {
LOG.log(Level.SEVERE, "Could not update user email", e);
}
return saveUser;
}
/**
* Extract a usable Username from the samlProfile object.
*
* @param saml2Profile user profile
* @return the username or if it is not possible to get the attribute the profile ID
*/
private String getUsernameFromProfile(SAML2Profile saml2Profile) {
if (getUsernameAttributeName() != null) {
List<String> attributes = getListOfValues(saml2Profile.getAttribute(getUsernameAttributeName()));
if (!attributes.isEmpty()) {
return attributes.get(0);
}
LOG.log(Level.SEVERE, "Unable to get username from attribute {0} value {1}, Saml Profile {2}",
new Object[]{getUsernameAttributeName(), attributes.toString(), saml2Profile});
LOG.log(Level.SEVERE, "Falling back to NameId {0}", saml2Profile.getId());
}
return saml2Profile.getId();
}
static String getIDPMetadataFilePath() {
return Jenkins.get().getRootDir().getAbsolutePath() + File.separator + IDP_METADATA_FILE_NAME;
}
static String getSPMetadataFilePath() {
return Jenkins.get().getRootDir().getAbsolutePath() + File.separator + SP_METADATA_FILE_NAME;
}
/**
* /securityRealm/metadata
* <p>
* URL request service method to expose the SP metadata to the user so that
* they can configure their IdP.
*
* @param request http request.
* @param response http response.
* @return the http response.
*/
@SuppressWarnings("unused")
@RequirePOST
public HttpResponse doMetadata(StaplerRequest request, StaplerResponse response) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return new SamlSPMetadataWrapper(getSamlPluginConfig(), request, response).get();
}
/**
* @see SecurityRealm#getPostLogOutUrl
* Note: It does not call the logout service on SAML because the library does not implement it on this version,
* it will be done when we upgrade the library.
*/
@SuppressWarnings("deprecation")
@Override
protected String getPostLogOutUrl(StaplerRequest req, @Nonnull Authentication auth) {
LOG.log(Level.FINE, "Doing Logout {}", auth.getPrincipal());
// if we just redirect to the root and anonymous does not have Overall read then we will start a login all over again.
// we are actually anonymous here as the security context has been cleared
if (Jenkins.get().hasPermission(Jenkins.READ) && StringUtils.isBlank(getLogoutUrl())) {
return super.getPostLogOutUrl(req, auth);
}
return getEffectiveLogoutUrl();
}
@Override
@RequirePOST
public void doLogout(StaplerRequest req, StaplerResponse rsp) throws IOException, javax.servlet.ServletException {
Jenkins.get().checkPermission(Jenkins.READ);
super.doLogout(req, rsp);
LOG.log(Level.FINEST, "Here we could do the SAML Single Logout");
}
/**
* This method is overwritten due to SAML has no way to retrieve the members of a Group and this cause issues on
* some Authorization plugins. Because of that we have to implement SamlGroupDetails
*/
@SuppressWarnings("deprecation")
@Override
public GroupDetails loadGroupByGroupname(String groupname) throws UsernameNotFoundException, DataAccessException {
GroupDetails dg = new SamlGroupDetails(groupname);
if (dg.getMembers().isEmpty()) {
throw new UserMayOrMayNotExistException2(groupname);
}
return dg;
}
/**
* This method is overwritten due to SAML has no way to retrieve the members of a Group and this cause issues on
* some Authorization plugins. Because of that we have to implement SamlGroupDetails
*/
@SuppressWarnings("deprecation")
@Override
public GroupDetails loadGroupByGroupname(String groupname, boolean fetchMembers)
throws UsernameNotFoundException, DataAccessException {
GroupDetails dg = loadGroupByGroupname(groupname);
if (fetchMembers) {
dg.getMembers();
}
return dg;
}
/**
* @return plugin configuration parameters.
*/
public SamlPluginConfig getSamlPluginConfig() {
return new SamlPluginConfig(displayNameAttributeName, groupsAttributeName,
maximumAuthenticationLifetime, emailAttributeName, idpMetadataConfiguration, usernameCaseConversion,
usernameAttributeName, logoutUrl, binding, encryptionData, advancedConfiguration);
}
@SuppressWarnings("unused")
@Extension
public static final class DescriptorImpl extends Descriptor<SecurityRealm> {
public DescriptorImpl() {
super();
}
public DescriptorImpl(Class<? extends SecurityRealm> clazz) {
super(clazz);
}
@NonNull
@Override
public String getDisplayName() {
return "SAML 2.0";
}
@RequirePOST
public FormValidation doCheckLogoutUrl(@QueryParameter String logoutUrl) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return SamlFormValidation.checkUrlFormat(logoutUrl);
}
@RequirePOST
public FormValidation doCheckDisplayNameAttributeName(@QueryParameter String displayNameAttributeName) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return SamlFormValidation.checkStringFormat(displayNameAttributeName);
}
@RequirePOST
public FormValidation doCheckGroupsAttributeName(@QueryParameter String groupsAttributeName) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return SamlFormValidation.checkStringAttributeFormat(groupsAttributeName, SamlSecurityRealm.WARN_RECOMMENDED_TO_SET_THE_GROUPS_ATTRIBUTE, true);
}
@RequirePOST
public FormValidation doCheckUsernameAttributeName(@QueryParameter String usernameAttributeName) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return SamlFormValidation.checkStringAttributeFormat(usernameAttributeName, SamlSecurityRealm.WARN_RECOMMENDED_TO_SET_THE_USERNAME_ATTRIBUTE, true);
}
@RequirePOST
public FormValidation doCheckEmailAttributeName(@QueryParameter String emailAttributeName) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return SamlFormValidation.checkStringAttributeFormat(emailAttributeName, SamlSecurityRealm.WARN_RECOMMENDED_TO_SET_THE_EMAIL_ATTRIBUTE, true);
}
@RequirePOST
public FormValidation doCheckMaximumAuthenticationLifetime(@QueryParameter String maximumAuthenticationLifetime) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return SamlFormValidation.checkIntegerFormat(maximumAuthenticationLifetime);
}
}
public String getUsernameAttributeName() {
return usernameAttributeName;
}
public String getDisplayNameAttributeName() {
return displayNameAttributeName;
}
public String getGroupsAttributeName() {
return groupsAttributeName;
}
public Integer getMaximumAuthenticationLifetime() {
return maximumAuthenticationLifetime;
}
public SamlAdvancedConfiguration getAdvancedConfiguration() {
return advancedConfiguration;
}
public String getBinding() {
return binding;
}
public SamlEncryptionData getEncryptionData() {
return encryptionData;
}
public String getUsernameCaseConversion() {
return usernameCaseConversion;
}
public String getEmailAttributeName() {
return emailAttributeName;
}
public String getLogoutUrl() {
return logoutUrl;
}
public IdpMetadataConfiguration getIdpMetadataConfiguration() {
return idpMetadataConfiguration;
}
@NonNull
public List<AttributeEntry> getSamlCustomAttributes() {
if(samlCustomAttributes == null){
return java.util.Collections.emptyList();
}
return samlCustomAttributes;
}
public void setSamlCustomAttribute(List<AttributeEntry> samlCustomAttributes) {
this.samlCustomAttributes = samlCustomAttributes;
}
@Override
public String toString() {
return "SamlSecurityRealm{" + getSamlPluginConfig().toString() + '}';
}
}