11package org .cloudfoundry .identity .uaa .authentication .manager ;
22
3+ import lombok .AllArgsConstructor ;
4+ import lombok .Builder ;
5+ import lombok .Data ;
36import lombok .Getter ;
47import lombok .Setter ;
58import org .apache .commons .lang3 .StringUtils ;
5457
5558import static java .util .Collections .emptySet ;
5659
57- public abstract class ExternalLoginAuthenticationManager <ExternalAuthenticationDetails > implements AuthenticationManager , ApplicationEventPublisherAware , BeanNameAware {
60+ public abstract class ExternalLoginAuthenticationManager <EAD extends ExternalLoginAuthenticationManager . ExternalAuthenticationDetails > implements AuthenticationManager , ApplicationEventPublisherAware , BeanNameAware {
5861
5962 public static final String USER_ATTRIBUTE_PREFIX = "user.attribute." ;
6063 private static final String FALLBACK_EMAIL_DOMAIN_TEMPLATE = "user.from.%s.cf" ;
@@ -86,16 +89,18 @@ public final void setApplicationEventPublisher(@NonNull ApplicationEventPublishe
8689 this .eventPublisher = eventPublisher ;
8790 }
8891
89- public abstract String getOrigin ();
90-
91- public abstract void setOrigin (String origin );
92-
9392 @ Override
9493 public Authentication authenticate (Authentication request ) throws AuthenticationException {
9594 if (logger .isDebugEnabled ()) {
9695 logger .debug ("Starting external authentication for:{}" , UaaStringUtils .getCleanedUserControlString (request .toString ()));
9796 }
98- ExternalAuthenticationDetails authenticationData = getExternalAuthenticationDetails (request );
97+
98+ EAD authenticationData = getExternalAuthenticationDetails (request );
99+ if (authenticationData == null ) {
100+ return null ;
101+ }
102+ final String origin = authenticationData .getOrigin ();
103+
99104 UaaUser userFromRequest = getUser (request , authenticationData );
100105 if (userFromRequest == null ) {
101106 return null ;
@@ -104,28 +109,28 @@ public Authentication authenticate(Authentication request) throws Authentication
104109 UaaUser userFromDb ;
105110
106111 try {
107- logger .debug ("Searching for user by (username:{} , origin:{})" , userFromRequest .getUsername (), getOrigin () );
108- userFromDb = userDatabase .retrieveUserByName (userFromRequest .getUsername (), getOrigin () );
112+ logger .debug ("Searching for user by (username:{} , origin:{})" , userFromRequest .getUsername (), origin );
113+ userFromDb = userDatabase .retrieveUserByName (userFromRequest .getUsername (), origin );
109114 } catch (UsernameNotFoundException e ) {
110- logger .debug ("Searching for user by (email:{} , origin:{})" , userFromRequest .getEmail (), getOrigin () );
111- userFromDb = userDatabase .retrieveUserByEmail (userFromRequest .getEmail (), getOrigin () );
115+ logger .debug ("Searching for user by (email:{} , origin:{})" , userFromRequest .getEmail (), origin );
116+ userFromDb = userDatabase .retrieveUserByEmail (userFromRequest .getEmail (), origin );
112117 }
113118
114119 // Register new users automatically
115120 if (userFromDb == null ) {
116- if (!isAddNewShadowUser ()) {
121+ if (!isAddNewShadowUser (origin )) {
117122 throw new AccountNotPreCreatedException ("The user account must be pre-created. Please contact your system administrator." );
118123 }
119124 publish (new NewUserAuthenticatedEvent (userFromRequest .authorities (List .of ())));
120125 try {
121- userFromDb = userDatabase .retrieveUserByName (userFromRequest .getUsername (), getOrigin () );
126+ userFromDb = userDatabase .retrieveUserByName (userFromRequest .getUsername (), origin );
122127 } catch (UsernameNotFoundException ex ) {
123128 throw new BadCredentialsException ("Unable to register user in internal UAA store." );
124129 }
125130 }
126131
127132 //user is authenticated and exists in UAA
128- UaaUser user = userAuthenticated (request , userFromRequest , userFromDb );
133+ UaaUser user = userAuthenticated (request , userFromRequest , userFromDb , authenticationData );
129134
130135 UaaAuthenticationDetails uaaAuthenticationDetails ;
131136 if (request .getDetails () instanceof UaaAuthenticationDetails ) {
@@ -139,10 +144,10 @@ public Authentication authenticate(Authentication request) throws Authentication
139144 return success ;
140145 }
141146
142- protected void populateAuthenticationAttributes (UaaAuthentication authentication , Authentication request , ExternalAuthenticationDetails authenticationData ) {
147+ protected void populateAuthenticationAttributes (UaaAuthentication authentication , Authentication request , EAD authenticationData ) {
143148 if (request .getPrincipal () instanceof UserDetails userDetails ) {
144- authentication .setUserAttributes (getUserAttributes (userDetails ));
145- authentication .setExternalGroups (new HashSet <>(getExternalUserAuthorities (userDetails )));
149+ authentication .setUserAttributes (getUserAttributes (userDetails , authenticationData ));
150+ authentication .setExternalGroups (new HashSet <>(getExternalUserAuthorities (userDetails , authenticationData )));
146151 }
147152
148153 if (authentication .getAuthenticationMethods () == null ) {
@@ -153,7 +158,7 @@ protected void populateAuthenticationAttributes(UaaAuthentication authentication
153158
154159 // persist the user attributes and external groups in the user info table if configured in the IdP
155160 if ((hasUserAttributes (authentication ) || hasExternalGroups (authentication )) && getProviderProvisioning () != null ) {
156- IdentityProvider <ExternalIdentityProviderDefinition > provider = getProviderProvisioning ().retrieveByOrigin (getOrigin (), IdentityZoneHolder .get ().getId ());
161+ IdentityProvider <ExternalIdentityProviderDefinition > provider = getProviderProvisioning ().retrieveByOrigin (authenticationData . getOrigin (), IdentityZoneHolder .get ().getId ());
157162 if (provider .getConfig () != null && provider .getConfig ().isStoreCustomAttributes ()) {
158163 logger .debug ("Storing custom attributes for user_id:{}" , authentication .getPrincipal ().getId ());
159164 UserInfo userInfo = new UserInfo ()
@@ -172,25 +177,25 @@ private boolean hasUserAttributes(UaaAuthentication authentication) {
172177 return authentication .getUserAttributes () != null && !authentication .getUserAttributes ().isEmpty ();
173178 }
174179
175- protected abstract ExternalAuthenticationDetails getExternalAuthenticationDetails (Authentication authentication ) throws AuthenticationException ;
180+ protected abstract EAD getExternalAuthenticationDetails (Authentication authentication ) throws AuthenticationException ;
176181
177- protected abstract boolean isAddNewShadowUser ();
182+ protected abstract boolean isAddNewShadowUser (final String origin );
178183
179- protected MultiValueMap <String , String > getUserAttributes (UserDetails request ) {
184+ protected MultiValueMap <String , String > getUserAttributes (UserDetails request , EAD authenticationData ) {
180185 return new LinkedMultiValueMap <>();
181186 }
182187
183- protected abstract List <String > getExternalUserAuthorities (UserDetails request );
188+ protected abstract List <String > getExternalUserAuthorities (UserDetails request , EAD authenticationData );
184189
185190 protected final void publish (ApplicationEvent event ) {
186191 if (eventPublisher != null ) {
187192 eventPublisher .publishEvent (event );
188193 }
189194 }
190195
191- protected abstract UaaUser userAuthenticated (Authentication request , UaaUser userFromRequest , UaaUser userFromDb );
196+ protected abstract UaaUser userAuthenticated (Authentication request , UaaUser userFromRequest , UaaUser userFromDb , EAD authenticationData );
192197
193- protected UaaUser getUser (Authentication request , ExternalAuthenticationDetails authDetails ) {
198+ protected UaaUser getUser (Authentication request , EAD authDetails ) {
194199 UserDetails userDetails ;
195200 if (request .getPrincipal () instanceof UserDetails ) {
196201 userDetails = (UserDetails ) request .getPrincipal ();
@@ -219,7 +224,7 @@ protected UaaUser getUser(Authentication request, ExternalAuthenticationDetails
219224 }
220225
221226 if (UaaStringUtils .isEmpty (email )) {
222- email = generateEmailIfNullOrEmpty (name );
227+ email = generateEmailIfNullOrEmpty (name , authDetails . getOrigin () );
223228 }
224229
225230 String givenName = null ;
@@ -242,20 +247,20 @@ protected UaaUser getUser(Authentication request, ExternalAuthenticationDetails
242247 .withFamilyName (familyName )
243248 .withCreated (new Date ())
244249 .withModified (new Date ())
245- .withOrigin (getOrigin ())
250+ .withOrigin (authDetails . getOrigin ())
246251 .withExternalId (externalId )
247252 .withZoneId (IdentityZoneHolder .get ().getId ())
248253 .withPhoneNumber (phoneNumber );
249254
250255 return new UaaUser (userPrototype );
251256 }
252257
253- protected final String generateEmailIfNullOrEmpty (String name ) {
258+ protected static String generateEmailIfNullOrEmpty (final String name , final String origin ) {
254259 if (name == null ) {
255260 throw new BadCredentialsException ("Cannot determine username from credentials supplied" );
256261 }
257262
258- final String fallbackEmailDomain = FALLBACK_EMAIL_DOMAIN_TEMPLATE .formatted (getOrigin () );
263+ final String fallbackEmailDomain = FALLBACK_EMAIL_DOMAIN_TEMPLATE .formatted (origin );
259264
260265 // use fallback domain if no '@' is present
261266 if (!name .contains ("@" )) {
@@ -310,4 +315,23 @@ protected final List<SimpleGrantedAuthority> evaluateExternalGroupMappings(Strin
310315 public void setBeanName (@ NonNull String name ) {
311316 this .name = name ;
312317 }
318+
319+ @ Data
320+ @ Builder
321+ @ AllArgsConstructor
322+ public static class ExternalAuthenticationDetails {
323+ private String origin ;
324+
325+ public ExternalAuthenticationDetails () {
326+ this .origin = "unknown" ;
327+ }
328+
329+ public final String getOrigin () {
330+ return origin ;
331+ }
332+
333+ public final void setOrigin (final String origin ) {
334+ this .origin = origin ;
335+ }
336+ }
313337}
0 commit comments