Skip to content

Commit 35e03d0

Browse files
jaro-herodGitHub Enterprise
authored andcommitted
MTM-60160: the oauth based authentication fails with newer spring version cause of change : spring-projects/spring-security#10347. The microservice proxy sends basic auth in combination with cookie authentication which now triggers re authentication as username don't match (#90)
1 parent 41df650 commit 35e03d0

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

microservice/security/src/main/java/com/cumulocity/microservice/security/token/JwtTokenAuthentication.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,25 @@ public Object getDetails() {
6565

6666
@Override
6767
public Object getPrincipal() {
68-
return currentUserRepresentation != null ? currentUserRepresentation.getUserName() : null;
68+
return getUserName();
6969
}
7070

7171
@Override
7272
public String getName() {
73+
final String tenantName = getTenantName();
74+
final String userName = getUserName();
75+
if (userName != null && tenantName != null) {
76+
return tenantName + "/" + userName;
77+
} else {
78+
return userName;
79+
}
80+
}
81+
82+
private String getUserName() {
7383
return currentUserRepresentation != null ? currentUserRepresentation.getUserName() : null;
7484
}
7585

76-
String getTenantName(){
86+
String getTenantName() {
7787
return userCredentials == null ? null : userCredentials.getTenant();
7888
}
7989
}

microservice/security/src/test/java/com/cumulocity/microservice/security/token/JwtTokenAuthenticationTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cumulocity.microservice.security.token;
22

33
import com.cumulocity.microservice.context.credentials.UserCredentials;
4+
import com.cumulocity.rest.representation.user.CurrentUserRepresentation;
45
import org.junit.jupiter.api.Test;
56
import org.junit.jupiter.api.extension.ExtendWith;
67
import org.mockito.junit.jupiter.MockitoExtension;
@@ -51,4 +52,21 @@ public void shouldGetNullTenantNameWhenUserCredentialsAreMissing() {
5152
assertThat(tenantName).isNull();
5253
}
5354

55+
// Fix of https://cumulocity.atlassian.net/browse/MTM-60160
56+
@Test
57+
public void shouldReturnNameToIncludeTenantAndUserNameToMatch() {
58+
// given
59+
JwtTokenAuthentication jwtTokenAuthentication = new JwtTokenAuthentication(credentials);
60+
jwtTokenAuthentication.setUserCredentials(UserCredentials.builder().tenant(TENANT_NAME).build());
61+
CurrentUserRepresentation currentUser = new CurrentUserRepresentation();
62+
currentUser.setUserName("user");
63+
jwtTokenAuthentication.setCurrentUserRepresentation(currentUser);
64+
//when
65+
String tenantName = jwtTokenAuthentication.getName();
66+
67+
//then
68+
assertThat(tenantName).isEqualTo(TENANT_NAME + "/" + currentUser.getUserName());
69+
}
70+
71+
5472
}

0 commit comments

Comments
 (0)