71
71
import java .security .Principal ;
72
72
import java .sql .Timestamp ;
73
73
import java .text .SimpleDateFormat ;
74
- import java .util .ArrayList ;
75
74
import java .util .Arrays ;
76
75
import java .util .Comparator ;
77
76
import java .util .Date ;
85
84
import java .util .Properties ;
86
85
import java .util .regex .Matcher ;
87
86
import java .util .regex .Pattern ;
88
- import java .util .stream .Stream ;
89
87
90
88
import static java .nio .charset .StandardCharsets .UTF_8 ;
91
89
import static java .util .Base64 .getDecoder ;
@@ -289,8 +287,8 @@ private String login(Model model, Principal principal, List<String> excludedProm
289
287
String loginHintParam = extractLoginHintParam (session , request );
290
288
UaaLoginHint uaaLoginHint = UaaLoginHint .parseRequestParameter (loginHintParam );
291
289
292
- Map <String , SamlIdentityProviderDefinition > samlIdentityProviders ;
293
- Map <String , AbstractExternalOAuthIdentityProviderDefinition > oauthIdentityProviders ;
290
+ Map <String , SamlIdentityProviderDefinition > samlIdentityProviders = null ;
291
+ Map <String , AbstractExternalOAuthIdentityProviderDefinition > oauthIdentityProviders = null ;
294
292
Map <String , AbstractIdentityProviderDefinition > allIdentityProviders = Map .of ();
295
293
Map .Entry <String , AbstractIdentityProviderDefinition > loginHintProvider = null ;
296
294
@@ -304,32 +302,34 @@ private String login(Model model, Principal principal, List<String> excludedProm
304
302
);
305
303
if (idp != null ) {
306
304
loginHintProvider = Map .entry (idp .getOriginKey (), idp .getConfig ());
305
+ oauthIdentityProviders = new HashMap <>();
306
+ oauthIdentityProviders .put (idp .getOriginKey (), (AbstractExternalOAuthIdentityProviderDefinition ) idp .getConfig ());
307
307
}
308
308
} catch (EmptyResultDataAccessException ignored ) {
309
309
// ignore
310
310
}
311
311
}
312
312
if (loginHintProvider != null ) {
313
- oauthIdentityProviders = Map . of ( );
314
- samlIdentityProviders = Map . of ( );
313
+ oauthIdentityProviders = addDefaultOauthMap ( oauthIdentityProviders , allowedIdentityProviderKeys , defaultIdentityProviderName );
314
+ samlIdentityProviders = addDefaultSamlMap ( samlIdentityProviders , allowedIdentityProviderKeys , defaultIdentityProviderName );
315
315
} else {
316
316
accountChooserNeeded = false ;
317
317
samlIdentityProviders = getSamlIdentityProviderDefinitions (allowedIdentityProviderKeys );
318
318
oauthIdentityProviders = getOauthIdentityProviderDefinitions (allowedIdentityProviderKeys );
319
319
allIdentityProviders = concatenateMaps (samlIdentityProviders , oauthIdentityProviders );
320
320
}
321
- } else if (!jsonResponse && (accountChooserNeeded || (accountChooserEnabled && !discoveryEnabled && !discoveryPerformed ))) {
321
+ } else if (!jsonResponse && (accountChooserNeeded || (accountChooserEnabled && !discoveryEnabled && !discoveryPerformed ))) {
322
322
// when `/login` is requested to return html response (as opposed to json response)
323
323
//Account and origin chooser do not need idp information
324
- oauthIdentityProviders = Map . of ( );
325
- samlIdentityProviders = Map . of ( );
324
+ oauthIdentityProviders = addDefaultOauthMap ( oauthIdentityProviders , allowedIdentityProviderKeys , defaultIdentityProviderName );
325
+ samlIdentityProviders = addDefaultSamlMap ( samlIdentityProviders , allowedIdentityProviderKeys , defaultIdentityProviderName );
326
326
} else {
327
327
samlIdentityProviders = getSamlIdentityProviderDefinitions (allowedIdentityProviderKeys );
328
328
329
329
if (jsonResponse ) {
330
330
/* the OAuth IdPs and all IdPs are used for determining the redirect; if jsonResponse is true, the
331
331
* redirect is ignored anyway */
332
- oauthIdentityProviders = Map . of ( );
332
+ oauthIdentityProviders = addDefaultOauthMap ( oauthIdentityProviders , allowedIdentityProviderKeys , defaultIdentityProviderName );
333
333
} else {
334
334
oauthIdentityProviders = getOauthIdentityProviderDefinitions (allowedIdentityProviderKeys );
335
335
}
@@ -652,6 +652,36 @@ private Map<String, SamlIdentityProviderDefinition> getSamlIdentityProviderDefin
652
652
return filteredIdps .stream ().collect (new MapCollector <>(SamlIdentityProviderDefinition ::getIdpEntityAlias , idp -> idp ));
653
653
}
654
654
655
+ private Map <String , SamlIdentityProviderDefinition > addDefaultSamlMap (Map <String , SamlIdentityProviderDefinition > list , List <String > allowedIdps , String defaultIdp ) {
656
+ Map <String , SamlIdentityProviderDefinition > defaultList = list == null ? new HashMap <>() : list ;
657
+ IdentityProvider samlIdP = getIdentityProviderByOrigin (allowedIdps , defaultIdp );
658
+ if (samlIdP != null && samlIdP .getConfig () instanceof SamlIdentityProviderDefinition samlDefinition ) {
659
+ defaultList .putIfAbsent (samlDefinition .getIdpEntityAlias (), samlDefinition );
660
+ }
661
+ return defaultList ;
662
+ }
663
+
664
+ private Map <String , AbstractExternalOAuthIdentityProviderDefinition > addDefaultOauthMap (Map <String , AbstractExternalOAuthIdentityProviderDefinition > list , List <String > allowedIdps , String defaultIdp ) {
665
+ Map <String , AbstractExternalOAuthIdentityProviderDefinition > defaultList = list == null ? new HashMap <>() : list ;
666
+ IdentityProvider oauthIdP = getIdentityProviderByOrigin (allowedIdps , defaultIdp );
667
+ if (oauthIdP != null && oauthIdP .getConfig () instanceof AbstractExternalOAuthIdentityProviderDefinition oDefinition ) {
668
+ defaultList .putIfAbsent (oauthIdP .getOriginKey (), oDefinition );
669
+ }
670
+ return defaultList ;
671
+ }
672
+
673
+ private IdentityProvider getIdentityProviderByOrigin (List <String > allowedIdps , String originKey ) {
674
+ IdentityProvider identityProvider = null ;
675
+ try {
676
+ if (originKey != null && (allowedIdps == null || allowedIdps .contains (originKey ))) {
677
+ identityProvider = providerProvisioning .retrieveByOrigin (originKey , IdentityZoneHolder .get ().getId ());
678
+ }
679
+ } catch (EmptyResultDataAccessException ignored ) {
680
+ // ignore
681
+ }
682
+ return identityProvider ;
683
+ }
684
+
655
685
protected Map <String , AbstractExternalOAuthIdentityProviderDefinition > getOauthIdentityProviderDefinitions (List <String > allowedIdps ) {
656
686
List <IdentityProvider > identityProviders = externalOAuthProviderConfigurator .retrieveActiveByTypes (
657
687
IdentityZoneHolder .get ().getId (),
@@ -704,22 +734,28 @@ private void populatePrompts(
704
734
Map <String , AbstractExternalOAuthIdentityProviderDefinition > oauthIdentityProviders ,
705
735
boolean returnLoginPrompts
706
736
) {
737
+ boolean noIdpsPresent = true ;
707
738
for (SamlIdentityProviderDefinition idp : samlIdentityProviders .values ()) {
708
739
if (idp .isShowSamlLink ()) {
709
740
model .addAttribute (SHOW_LOGIN_LINKS , true );
741
+ noIdpsPresent = false ;
710
742
break ;
711
743
}
712
744
}
713
745
for (AbstractExternalOAuthIdentityProviderDefinition oauthIdp : oauthIdentityProviders .values ()) {
714
746
if (oauthIdp .isShowLinkText ()) {
715
747
model .addAttribute (SHOW_LOGIN_LINKS , true );
748
+ noIdpsPresent = false ;
716
749
break ;
717
750
}
718
751
}
719
752
720
753
//make the list writeable
721
754
final List <String > excludedPrompts = new LinkedList <>(exclude );
722
755
756
+ if (noIdpsPresent ) {
757
+ excludedPrompts .add (PASSCODE );
758
+ }
723
759
if (!returnLoginPrompts ) {
724
760
excludedPrompts .add (USERNAME_PARAMETER );
725
761
excludedPrompts .add ("password" );
0 commit comments