13
13
import org .keycloak .models .AuthenticationExecutionModel ;
14
14
import org .keycloak .models .IdentityProviderModel ;
15
15
import org .keycloak .models .KeycloakSession ;
16
+ import org .keycloak .models .ModelDuplicateException ;
16
17
import org .keycloak .models .RealmModel ;
17
18
import org .keycloak .models .UserModel ;
19
+ import org .keycloak .models .utils .KeycloakModelUtils ;
18
20
import org .keycloak .services .managers .AuthenticationManager ;
19
21
20
22
import java .util .List ;
21
23
24
+ import static org .keycloak .protocol .oidc .OIDCLoginProtocol .LOGIN_HINT_PARAM ;
22
25
import static org .keycloak .services .validation .Validation .FIELD_USERNAME ;
23
26
24
27
final class HomeIdpDiscoveryAuthenticator extends AbstractUsernameFormAuthenticator {
@@ -106,8 +109,12 @@ public void action(AuthenticationFlowContext authenticationFlowContext) {
106
109
107
110
final List <IdentityProviderModel > homeIdps = context .discoverer (discovererConfig ).discoverForUser (authenticationFlowContext , username );
108
111
if (homeIdps .isEmpty ()) {
109
- authenticationFlowContext .attempted ();
110
- context .loginHint ().setInAuthSession (username );
112
+ if (authenticationFlowContext .getExecution ().getRequirement () == AuthenticationExecutionModel .Requirement .REQUIRED ) {
113
+ authenticationFlowContext .success ();
114
+ } else {
115
+ authenticationFlowContext .attempted ();
116
+ context .loginHint ().setInAuthSession (username );
117
+ }
111
118
} else {
112
119
RememberMe rememberMe = context .rememberMe ();
113
120
rememberMe .handleAction (formData );
@@ -117,6 +124,7 @@ public void action(AuthenticationFlowContext authenticationFlowContext) {
117
124
}
118
125
119
126
private String setUserInContext (AuthenticationFlowContext context , String username ) {
127
+ context .clearUser ();
120
128
username = trimToNull (username );
121
129
122
130
if (username == null ) {
@@ -130,6 +138,19 @@ private String setUserInContext(AuthenticationFlowContext context, String userna
130
138
LOG .debugf ("Found username '%s' in request" , username );
131
139
context .getEvent ().detail (Details .USERNAME , username );
132
140
context .getAuthenticationSession ().setAuthNote (ATTEMPTED_USERNAME , username );
141
+ context .getAuthenticationSession ().setClientNote (LOGIN_HINT_PARAM , username );
142
+
143
+ try {
144
+ UserModel user = KeycloakModelUtils .findUserByNameOrEmail (context .getSession (), context .getRealm (),
145
+ username );
146
+ if (user != null ) {
147
+ LOG .tracef ("Setting user '%s' in context" , user .getId ());
148
+ context .setUser (user );
149
+ }
150
+ } catch (ModelDuplicateException ex ) {
151
+ LOG .warnf (ex , "Could not uniquely identify the user. Multiple users with name or email '%s' found." ,
152
+ username );
153
+ }
133
154
134
155
return username ;
135
156
}
0 commit comments