Skip to content

Commit 2a86d49

Browse files
Merge branch 'develop' into iglezruiz/cargo-migration
2 parents 766abb3 + 6a1cec3 commit 2a86d49

File tree

9 files changed

+171
-225
lines changed

9 files changed

+171
-225
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858

5959
# Initializes the CodeQL tools for scanning.
6060
- name: Initialize CodeQL
61-
uses: github/codeql-action/init@v3
61+
uses: github/codeql-action/init@v4
6262
with:
6363
languages: ${{ matrix.language }}
6464
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -69,7 +69,7 @@ jobs:
6969
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
7070
# If this step fails, then you should remove it and run the build manually (see below)
7171
- name: Autobuild
72-
uses: github/codeql-action/autobuild@v3
72+
uses: github/codeql-action/autobuild@v4
7373

7474
# ℹ️ Command-line programs to run using the OS shell.
7575
# 📚 https://git.io/JvXDl
@@ -83,4 +83,4 @@ jobs:
8383
# make release
8484

8585
- name: Perform CodeQL Analysis
86-
uses: github/codeql-action/analyze@v3
86+
uses: github/codeql-action/analyze@v4

server/src/main/java/org/cloudfoundry/identity/uaa/authentication/manager/ExternalLoginAuthenticationManager.java

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.cloudfoundry.identity.uaa.authentication.manager;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
36
import lombok.Getter;
47
import lombok.Setter;
58
import org.apache.commons.lang3.StringUtils;
@@ -54,7 +57,7 @@
5457

5558
import 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
}

server/src/main/java/org/cloudfoundry/identity/uaa/authentication/manager/LdapLoginAuthenticationManager.java

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
package org.cloudfoundry.identity.uaa.authentication.manager;
1717

18+
import com.google.common.annotations.VisibleForTesting;
1819
import org.cloudfoundry.identity.uaa.authentication.UaaAuthentication;
20+
import org.cloudfoundry.identity.uaa.authentication.manager.ExternalLoginAuthenticationManager.ExternalAuthenticationDetails;
1921
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
2022
import org.cloudfoundry.identity.uaa.provider.IdentityProvider;
2123
import org.cloudfoundry.identity.uaa.provider.IdentityProviderProvisioning;
@@ -46,44 +48,28 @@
4648
import static java.util.Collections.emptyList;
4749
import static org.cloudfoundry.identity.uaa.util.UaaStringUtils.retainAllMatches;
4850

49-
public class LdapLoginAuthenticationManager extends ExternalLoginAuthenticationManager<Object> {
51+
public class LdapLoginAuthenticationManager extends ExternalLoginAuthenticationManager<ExternalAuthenticationDetails> {
5052

5153
protected static Logger logger = LoggerFactory.getLogger(LdapLoginAuthenticationManager.class);
5254

53-
public LdapLoginAuthenticationManager(final @Qualifier("identityProviderProvisioning") IdentityProviderProvisioning providerProvisioning) {
54-
super(providerProvisioning);
55-
}
56-
5755
private String origin = OriginKeys.LDAP;
5856

59-
@Override
60-
public String getOrigin() {
61-
return origin;
62-
}
63-
64-
@Override
65-
public void setOrigin(String origin) {
66-
// only used in LdapLoginAuthenticationManagerTests
67-
this.origin = origin;
57+
public LdapLoginAuthenticationManager(final @Qualifier("identityProviderProvisioning") IdentityProviderProvisioning providerProvisioning) {
58+
super(providerProvisioning);
6859
}
6960

7061
@Override
71-
protected void populateAuthenticationAttributes(UaaAuthentication authentication, Authentication request, Object authenticationData) {
62+
protected void populateAuthenticationAttributes(UaaAuthentication authentication, Authentication request, ExternalAuthenticationDetails authenticationData) {
7263
super.populateAuthenticationAttributes(authentication, request, authenticationData);
7364
authentication.getAuthenticationMethods().add("pwd");
7465
}
7566

7667
@Override
77-
protected Object getExternalAuthenticationDetails(Authentication authentication) throws AuthenticationException {
78-
return null;
79-
}
80-
81-
@Override
82-
protected MultiValueMap<String, String> getUserAttributes(UserDetails request) {
83-
MultiValueMap<String, String> result = super.getUserAttributes(request);
84-
logger.debug("Mapping custom attributes for origin:{} and zone:{}", getOrigin(), IdentityZoneHolder.get().getId());
68+
protected MultiValueMap<String, String> getUserAttributes(UserDetails request, ExternalAuthenticationDetails authenticationData) {
69+
MultiValueMap<String, String> result = super.getUserAttributes(request, authenticationData);
70+
logger.debug("Mapping custom attributes for origin:{} and zone:{}", authenticationData.getOrigin(), IdentityZoneHolder.get().getId());
8571
if (getProviderProvisioning() != null) {
86-
IdentityProvider provider = getProviderProvisioning().retrieveByOrigin(getOrigin(), IdentityZoneHolder.get().getId());
72+
IdentityProvider provider = getProviderProvisioning().retrieveByOrigin(authenticationData.getOrigin(), IdentityZoneHolder.get().getId());
8773
if (request instanceof ExtendedLdapUserDetails ldapDetails) {
8874
LdapIdentityProviderDefinition ldapIdentityProviderDefinition = ObjectUtils.castInstance(provider.getConfig(), LdapIdentityProviderDefinition.class);
8975
Map<String, Object> providerMappings = ldapIdentityProviderDefinition.getAttributeMappings();
@@ -99,16 +85,16 @@ protected MultiValueMap<String, String> getUserAttributes(UserDetails request) {
9985
}
10086
}
10187
} else {
102-
logger.debug("Did not find custom attribute configuration for origin:{} and zone:{}", getOrigin(), IdentityZoneHolder.get().getId());
88+
logger.debug("Did not find custom attribute configuration for origin:{} and zone:{}", authenticationData.getOrigin(), IdentityZoneHolder.get().getId());
10389
}
10490
return result;
10591
}
10692

10793
@Override
108-
protected List<String> getExternalUserAuthorities(UserDetails request) {
94+
protected List<String> getExternalUserAuthorities(UserDetails request, ExternalAuthenticationDetails authenticationData) {
10995
List<String> result = new LinkedList<>();
11096
if (getProviderProvisioning() != null) {
111-
IdentityProvider provider = getProviderProvisioning().retrieveByOrigin(getOrigin(), IdentityZoneHolder.get().getId());
97+
IdentityProvider provider = getProviderProvisioning().retrieveByOrigin(authenticationData.getOrigin(), IdentityZoneHolder.get().getId());
11298
LdapIdentityProviderDefinition ldapIdentityProviderDefinition = ObjectUtils.castInstance(provider.getConfig(), LdapIdentityProviderDefinition.class);
11399
List<String> externalWhiteList = ldapIdentityProviderDefinition.getExternalGroupsWhitelist();
114100
result = new ArrayList<>(retainAllMatches(getAuthoritiesAsNames(request.getAuthorities()), externalWhiteList));
@@ -131,7 +117,7 @@ protected Set<String> getAuthoritiesAsNames(Collection<? extends GrantedAuthorit
131117
}
132118

133119
@Override
134-
protected UaaUser userAuthenticated(Authentication request, UaaUser userFromRequest, UaaUser userFromDb) {
120+
protected UaaUser userAuthenticated(Authentication request, UaaUser userFromRequest, UaaUser userFromDb, ExternalAuthenticationDetails authenticationData) {
135121
boolean userModified = false;
136122
//we must check and see if the email address has changed between authentications
137123
if (request.getPrincipal() != null && request.getPrincipal() instanceof ExtendedLdapUserDetails) {
@@ -146,15 +132,15 @@ protected UaaUser userAuthenticated(Authentication request, UaaUser userFromRequ
146132
userModified = true;
147133
}
148134
}
149-
ExternalGroupAuthorizationEvent event = new ExternalGroupAuthorizationEvent(userFromDb, userModified, request.getAuthorities(), isAutoAddAuthorities());
135+
ExternalGroupAuthorizationEvent event = new ExternalGroupAuthorizationEvent(userFromDb, userModified, request.getAuthorities(), isAutoAddAuthorities(authenticationData.getOrigin()));
150136
publish(event);
151137
return getUserDatabase().retrieveUserById(userFromDb.getId());
152138
}
153139

154-
protected boolean isAutoAddAuthorities() {
140+
protected boolean isAutoAddAuthorities(final String origin) {
155141
Boolean result = true;
156142
if (getProviderProvisioning() != null) {
157-
IdentityProvider provider = getProviderProvisioning().retrieveByOrigin(getOrigin(), IdentityZoneHolder.get().getId());
143+
IdentityProvider provider = getProviderProvisioning().retrieveByOrigin(origin, IdentityZoneHolder.get().getId());
158144
LdapIdentityProviderDefinition ldapIdentityProviderDefinition = ObjectUtils.castInstance(provider.getConfig(), LdapIdentityProviderDefinition.class);
159145
if (ldapIdentityProviderDefinition != null) {
160146
result = ldapIdentityProviderDefinition.isAutoAddGroups();
@@ -164,15 +150,25 @@ protected boolean isAutoAddAuthorities() {
164150
}
165151

166152
@Override
167-
protected boolean isAddNewShadowUser() {
153+
protected boolean isAddNewShadowUser(final String origin) {
168154
boolean result = true;
169155
if (getProviderProvisioning() != null) {
170-
IdentityProvider provider = getProviderProvisioning().retrieveByOrigin(getOrigin(), IdentityZoneHolder.get().getId());
156+
IdentityProvider provider = getProviderProvisioning().retrieveByOrigin(origin, IdentityZoneHolder.get().getId());
171157
LdapIdentityProviderDefinition ldapIdentityProviderDefinition = ObjectUtils.castInstance(provider.getConfig(), LdapIdentityProviderDefinition.class);
172158
if (ldapIdentityProviderDefinition != null) {
173159
result = ldapIdentityProviderDefinition.isAddShadowUserOnLogin();
174160
}
175161
}
176162
return result;
177163
}
164+
165+
@VisibleForTesting
166+
public void setOrigin(final String origin) {
167+
this.origin = origin;
168+
}
169+
170+
@Override
171+
protected ExternalAuthenticationDetails getExternalAuthenticationDetails(Authentication authentication) throws AuthenticationException {
172+
return ExternalAuthenticationDetails.builder().origin(origin).build();
173+
}
178174
}

0 commit comments

Comments
 (0)