Skip to content

Commit 8c7ea98

Browse files
Thomasludomikula
Thomas
authored andcommitted
add jwt
add maputils
1 parent af3a8f4 commit 8c7ea98

File tree

5 files changed

+91
-8
lines changed

5 files changed

+91
-8
lines changed

server/api-service/lowcoder-domain/src/main/java/org/lowcoder/domain/user/model/AuthToken.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class AuthToken implements Serializable {
1818
private int expireIn;
1919
private String refreshToken;
2020
private int refreshTokenExpireIn;
21+
private String jwt;
2122

2223
private String openId;
2324
private String code;

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/request/oauth2/request/GenericAuthRequest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.lowcoder.api.authentication.request.oauth2.request;
22

3-
import lombok.Setter;
43
import org.lowcoder.api.authentication.request.AuthException;
54
import org.lowcoder.api.authentication.request.oauth2.GenericOAuthProviderSource;
65
import org.lowcoder.api.authentication.request.oauth2.OAuth2RequestContext;
@@ -48,7 +47,7 @@ protected Mono<AuthToken> getAuthToken(OAuth2RequestContext context) {
4847
if (map.containsKey("error") || map.containsKey("error_description")) {
4948
return Mono.error(new AuthException(JsonUtils.toJson(map)));
5049
}
51-
return Mono.just(mapToAuthToken(map));
50+
return Mono.just(mapToAuthToken(map, config.getSourceMappings()));
5251
});
5352
}
5453

@@ -70,7 +69,7 @@ protected Mono<AuthToken> refreshAuthToken(String refreshToken) {
7069
if (map.containsKey("error") || map.containsKey("error_description")) {
7170
return Mono.error(new AuthException(JsonUtils.toJson(map)));
7271
}
73-
return Mono.just(mapToAuthToken(map));
72+
return Mono.just(mapToAuthToken(map, config.getSourceMappings()));
7473
});
7574
}
7675

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.lowcoder.api.authentication.util;
2+
3+
import java.util.Map;
4+
5+
public class AdvancedMapUtils {
6+
7+
/**
8+
* Retrieves a string value from a nested map structure using a key format that supports array indices and nested objects.
9+
*
10+
* @param map The map from which to retrieve the value.
11+
* @param key The key in the format "abc[0].def.hi".
12+
* @return The string value if found, otherwise null.
13+
*/
14+
public static String getString(Map<String, Object> map, String key) {
15+
String[] parts = key.split("\\.");
16+
Object current = map;
17+
18+
for (String part : parts) {
19+
if (current == null) {
20+
return null;
21+
}
22+
23+
if (part.contains("[")) {
24+
int startIdx = part.indexOf('[');
25+
int endIdx = part.indexOf(']');
26+
String arrayKey = part.substring(0, startIdx);
27+
int index = Integer.parseInt(part.substring(startIdx + 1, endIdx));
28+
29+
if (!(current instanceof Map)) {
30+
return null;
31+
}
32+
33+
current = ((Map<String, Object>) current).get(arrayKey);
34+
35+
if (current instanceof java.util.List) {
36+
java.util.List<?> list = (java.util.List<?>) current;
37+
if (index < 0 || index >= list.size()) {
38+
return null;
39+
}
40+
current = list.get(index);
41+
} else {
42+
return null;
43+
}
44+
} else {
45+
if (!(current instanceof Map)) {
46+
return null;
47+
}
48+
current = ((Map<String, Object>) current).get(part);
49+
}
50+
}
51+
52+
return current instanceof String ? (String) current : null;
53+
}
54+
}

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/util/AuthenticationUtils.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,17 @@ public String getName() {
6464

6565
/**
6666
* Utility method to map from Map to AuthToken
67-
* @param map Object
67+
*
68+
* @param map Object
69+
* @param sourceMappings
6870
* @return AuthToken
6971
*/
70-
public static AuthToken mapToAuthToken(Map<String, Object> map) {
72+
public static AuthToken mapToAuthToken(Map<String, Object> map, HashMap<String, String> sourceMappings) {
7173
return AuthToken.builder()
7274
.accessToken(MapUtils.getString(map, "access_token"))
7375
.expireIn(MapUtils.getIntValue(map, "expires_in"))
7476
.refreshToken(MapUtils.getString(map, "refresh_token"))
77+
.jwt(AdvancedMapUtils.getString(map, MapUtils.getString(sourceMappings, "jwt")))
7578
.build();
7679
}
7780

@@ -84,9 +87,9 @@ public static AuthToken mapToAuthToken(Map<String, Object> map) {
8487
*/
8588
public static AuthUser mapToAuthUser(Map<String, Object> map, HashMap<String, String> sourceMappings) {
8689
return AuthUser.builder()
87-
.uid(MapUtils.getString(map, MapUtils.getString(sourceMappings, "uid")))
88-
.username(MapUtils.getString(map, MapUtils.getString(sourceMappings, "username")))
89-
.avatar(MapUtils.getString(map, MapUtils.getString(sourceMappings, "avatar")))
90+
.uid(AdvancedMapUtils.getString(map, MapUtils.getString(sourceMappings, "uid")))
91+
.username(AdvancedMapUtils.getString(map, MapUtils.getString(sourceMappings, "username")))
92+
.avatar(AdvancedMapUtils.getString(map, MapUtils.getString(sourceMappings, "avatar")))
9093
.rawUserInfo(map)
9194
.build();
9295
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.lowcoder.api.authentication;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.lowcoder.api.authentication.util.AdvancedMapUtils;
6+
7+
import java.util.Arrays;
8+
import java.util.Collections;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
public class AdvancedMapUtilsTest {
13+
@Test
14+
public void testGetStringFromMap() throws Exception {
15+
Map<String, Object> nestedMap = new HashMap<>();
16+
nestedMap.put("abc", Arrays.asList(
17+
Collections.singletonMap("def", Collections.singletonMap("hi", "hello world")),
18+
Collections.singletonMap("def", Collections.singletonMap("hi", "another value"))
19+
));
20+
21+
String value0 = AdvancedMapUtils.getString(nestedMap, "abc[0].def.hi");
22+
String value1 = AdvancedMapUtils.getString(nestedMap, "abc[1].def.hi");
23+
Assertions.assertSame("hello world", value0);
24+
Assertions.assertSame("another value", value1);
25+
}
26+
}

0 commit comments

Comments
 (0)