Skip to content

Commit d08c5ec

Browse files
authored
Merge pull request #912 from aka4rKO/main-username
[OB4] Fixes username resolving issue for secondary user store
2 parents 52288bc + 1f3cfd2 commit d08c5ec

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

financial-services-accelerator/components/org.wso2.financial.services.accelerator.common/src/main/java/org/wso2/financial/services/accelerator/common/util/FinancialServicesUtils.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,35 +145,54 @@ public static String reduceStringLength(String input, int maxLength) {
145145
@Generated(message = "Ignoring because OAuth2Util cannot be mocked with no constructors")
146146
public static String resolveUsernameFromUserId(String userID) {
147147

148-
if (!startsWithUUID(userID)) {
149-
// If the user ID is not starting with a UUID that means request has sent the username,
148+
if (!containsUUID(userID)) {
149+
// If the user ID doesn't have a UUID that means request has sent the username,
150150
// return the same user ID as the username.
151151
return userID;
152152
}
153153

154-
String username = null;
154+
String extractedUUID = extractUUID(userID);
155155
try {
156-
if (userID.contains(FinancialServicesConstants.TENANT_DOMAIN)) {
157-
username = OAuth2Util.resolveUsernameFromUserId(FinancialServicesConstants.TENANT_DOMAIN,
158-
userID.split("@" + FinancialServicesConstants.TENANT_DOMAIN)[0]);
159-
} else {
160-
username = OAuth2Util.resolveUsernameFromUserId(FinancialServicesConstants.TENANT_DOMAIN, userID);
161-
}
156+
return OAuth2Util.resolveUsernameFromUserId(FinancialServicesConstants.TENANT_DOMAIN, extractedUUID);
162157
} catch (UserStoreException e) {
163158
log.debug("Returning null since user ID is not found in the database", e);
164159
return null;
165160
}
166-
return username;
167161
}
168162

169163
/**
170-
* Method to check whether the input string starts with a UUID.
164+
* Method to extract the UUID from the input string.
165+
*
166+
* @param input Input string
167+
* @return Extracted UUID if found, else null
168+
*/
169+
public static String extractUUID(String input) {
170+
171+
if (input == null) {
172+
return null;
173+
}
174+
175+
// Use your existing constant or regex string
176+
Pattern uuidPattern = Pattern.compile(FinancialServicesConstants.UUID_REGEX);
177+
Matcher matcher = uuidPattern.matcher(input);
178+
179+
// 1. Search for the pattern
180+
if (matcher.find()) {
181+
// 2. Return the specific substring that matched
182+
return matcher.group();
183+
}
184+
185+
return null;
186+
}
187+
188+
/**
189+
* Method to check whether the input string has a UUID.
171190
* @param input Input string
172-
* @return true if the input string starts with a UUID
191+
* @return true if the input string has a UUID
173192
*/
174-
public static boolean startsWithUUID(String input) {
175-
Pattern uuidPattern = Pattern.compile("^" + FinancialServicesConstants.UUID_REGEX + ".*$");
176-
return uuidPattern.matcher(input).matches();
193+
public static boolean containsUUID(String input) {
194+
Pattern uuidPattern = Pattern.compile(FinancialServicesConstants.UUID_REGEX);
195+
return uuidPattern.matcher(input).find();
177196
}
178197

179198
/**

financial-services-accelerator/components/org.wso2.financial.services.accelerator.common/src/test/java/org/wso2/financial/services/accelerator/common/test/util/FinancialServicesUtilsTest.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,32 @@ public void testReducingStringLength() throws Exception {
7474
}
7575

7676
@Test
77-
public void testStartsWithUUID() {
77+
public void testContainsUUID() {
7878

79-
Assert.assertFalse(FinancialServicesUtils.startsWithUUID("String Body"));
79+
Assert.assertFalse(FinancialServicesUtils.containsUUID("String Body"));
8080

81-
Assert.assertTrue(FinancialServicesUtils.startsWithUUID(UUID.randomUUID().toString()));
81+
Assert.assertTrue(FinancialServicesUtils.containsUUID(UUID.randomUUID().toString()));
82+
Assert.assertTrue(FinancialServicesUtils.containsUUID("abc" + UUID.randomUUID()));
83+
Assert.assertTrue(FinancialServicesUtils.containsUUID(UUID.randomUUID() + "abc"));
84+
Assert.assertTrue(FinancialServicesUtils.containsUUID("abc" + UUID.randomUUID() + "abc"));
85+
Assert.assertTrue(FinancialServicesUtils.containsUUID("abc " + UUID.randomUUID() + " abc"));
86+
}
87+
88+
@Test
89+
public void testExtractUUID() {
90+
91+
String uuid = UUID.randomUUID().toString();
92+
93+
Assert.assertEquals(FinancialServicesUtils
94+
.extractUUID(uuid + "@carbon.super"), uuid);
95+
Assert.assertEquals(FinancialServicesUtils
96+
.extractUUID(uuid + "@carbon.super@carbon.super"), uuid);
97+
Assert.assertEquals(FinancialServicesUtils
98+
.extractUUID("SECONDARY/" + uuid + "@carbon.super@carbon.super"), uuid);
99+
Assert.assertNull(FinancialServicesUtils
100+
.extractUUID("SECONDARY/@carbon.super@carbon.super"));
101+
Assert.assertNull(FinancialServicesUtils
102+
.extractUUID("psu@gold.com"));
82103
}
83104

84105
@Test

0 commit comments

Comments
 (0)