Skip to content

Commit 6d59b28

Browse files
Thomasrludomikula
Thomasr
authored andcommitted
wrap legacy oauth provider with genericoauth
1 parent 29d4f2b commit 6d59b28

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

server/api-service/lowcoder-sdk/src/main/java/org/lowcoder/sdk/auth/constants/Oauth2Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Oauth2Constants {
1717
+ "&client_id=" + CLIENT_ID_PLACEHOLDER
1818
+ "&redirect_uri=" + REDIRECT_URL_PLACEHOLDER
1919
+ "&state=" + STATE_PLACEHOLDER
20-
+ "&scope=";
20+
+ "&scope=user";
2121

2222
public static final String GOOGLE_AUTHORIZE_URL = "https://accounts.google.com/o/oauth2/v2/auth"
2323
+ "?response_type=code"

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

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

3+
import java.util.HashMap;
34
import java.util.Set;
45

56
import org.lowcoder.api.authentication.request.AuthRequest;
@@ -9,6 +10,7 @@
910
import org.lowcoder.sdk.auth.Oauth2KeycloakAuthConfig;
1011
import org.lowcoder.sdk.auth.Oauth2OryAuthConfig;
1112
import org.lowcoder.sdk.auth.Oauth2SimpleAuthConfig;
13+
import org.lowcoder.sdk.auth.constants.AuthTypeConstants;
1214
import org.springframework.stereotype.Component;
1315

1416
import reactor.core.publisher.Mono;
@@ -25,10 +27,98 @@ public Mono<AuthRequest> build(OAuth2RequestContext context) {
2527

2628
private AbstractOauth2Request<? extends Oauth2SimpleAuthConfig> buildRequest(OAuth2RequestContext context) {
2729
return switch (context.getAuthConfig().getAuthType()) {
28-
case GITHUB -> new GithubRequest((Oauth2SimpleAuthConfig) context.getAuthConfig());
29-
case GOOGLE -> new GoogleRequest((Oauth2SimpleAuthConfig) context.getAuthConfig());
30-
case ORY -> new OryRequest((Oauth2OryAuthConfig) context.getAuthConfig());
31-
case KEYCLOAK -> new KeycloakRequest((Oauth2KeycloakAuthConfig)context.getAuthConfig());
30+
case GITHUB -> {
31+
HashMap<String, String> sourceMappings = new HashMap<>();
32+
sourceMappings.put("uid", "id");
33+
sourceMappings.put("email", "email");
34+
sourceMappings.put("username", "login");
35+
sourceMappings.put("avatar", "avatar_url");
36+
Oauth2SimpleAuthConfig config = (Oauth2SimpleAuthConfig) context.getAuthConfig();
37+
yield new GenericAuthRequest(Oauth2GenericAuthConfig.builder()
38+
.tokenEndpoint(Oauth2DefaultSource.GITHUB.accessToken())
39+
.userInfoEndpoint(Oauth2DefaultSource.GITHUB.userInfo())
40+
.userInfoIntrospection(true)
41+
.source(config.getSource())
42+
.sourceName(config.getSourceName())
43+
.enableRegister(config.isEnableRegister())
44+
.enable(config.isEnable())
45+
.scope("read:email read:user")
46+
.userCanSelectAccounts(true)
47+
.sourceMappings(sourceMappings)
48+
.clientSecret(config.getClientSecret())
49+
.clientId(config.getClientId())
50+
.authType(GENERIC)
51+
.build());
52+
}
53+
case GOOGLE -> {
54+
HashMap<String, String> sourceMappings = new HashMap<>();
55+
sourceMappings.put("uid", "sub");
56+
sourceMappings.put("email", "email");
57+
sourceMappings.put("username", "email");
58+
sourceMappings.put("avatar", "picture");
59+
Oauth2SimpleAuthConfig config = (Oauth2SimpleAuthConfig) context.getAuthConfig();
60+
yield new GenericAuthRequest(Oauth2GenericAuthConfig.builder()
61+
.tokenEndpoint(Oauth2DefaultSource.GOOGLE.accessToken())
62+
.userInfoEndpoint(Oauth2DefaultSource.GOOGLE.userInfo())
63+
.userInfoIntrospection(true)
64+
.source(config.getSource())
65+
.sourceName(config.getSourceName())
66+
.enableRegister(config.isEnableRegister())
67+
.enable(config.isEnable())
68+
.scope("openid email profile")
69+
.userCanSelectAccounts(true)
70+
.sourceMappings(sourceMappings)
71+
.clientSecret(config.getClientSecret())
72+
.clientId(config.getClientId())
73+
.authType(GENERIC)
74+
.build());
75+
}
76+
case ORY -> {
77+
HashMap<String, String> sourceMappings = new HashMap<>();
78+
sourceMappings.put("uid", "sub");
79+
sourceMappings.put("email", "email");
80+
sourceMappings.put("username", "email");
81+
sourceMappings.put("avatar", "picture");
82+
Oauth2OryAuthConfig config = (Oauth2OryAuthConfig) context.getAuthConfig();
83+
yield new GenericAuthRequest(Oauth2GenericAuthConfig.builder()
84+
.tokenEndpoint(config.replaceAuthUrlClientIdPlaceholder(Oauth2DefaultSource.ORY.accessToken()))
85+
.userInfoEndpoint(config.replaceAuthUrlClientIdPlaceholder(Oauth2DefaultSource.ORY.userInfo()))
86+
.userInfoIntrospection(true)
87+
.source(config.getSource())
88+
.sourceName(config.getSourceName())
89+
.enableRegister(config.isEnableRegister())
90+
.enable(config.isEnable())
91+
.scope(config.getScope())
92+
.userCanSelectAccounts(false)
93+
.sourceMappings(sourceMappings)
94+
.clientSecret(config.getClientSecret())
95+
.clientId(config.getClientId())
96+
.authType(GENERIC)
97+
.build());
98+
}
99+
case KEYCLOAK -> {
100+
HashMap<String, String> sourceMappings = new HashMap<>();
101+
sourceMappings.put("uid", "sub");
102+
sourceMappings.put("email", "email");
103+
sourceMappings.put("username", "email");
104+
sourceMappings.put("avatar", "false");
105+
Oauth2KeycloakAuthConfig config = (Oauth2KeycloakAuthConfig) context.getAuthConfig();
106+
yield new GenericAuthRequest(Oauth2GenericAuthConfig.builder()
107+
.tokenEndpoint(config.replaceAuthUrlClientIdPlaceholder(Oauth2DefaultSource.KEYCLOAK.accessToken()))
108+
.userInfoEndpoint(config.replaceAuthUrlClientIdPlaceholder(Oauth2DefaultSource.KEYCLOAK.userInfo()))
109+
.userInfoIntrospection(true)
110+
.source(config.getSource())
111+
.sourceName(config.getSourceName())
112+
.enableRegister(config.isEnableRegister())
113+
.enable(config.isEnable())
114+
.scope(config.getScope())
115+
.userCanSelectAccounts(false)
116+
.sourceMappings(sourceMappings)
117+
.clientSecret(config.getClientSecret())
118+
.clientId(config.getClientId())
119+
.authType(GENERIC)
120+
.build());
121+
}
32122
case GENERIC -> new GenericAuthRequest((Oauth2GenericAuthConfig) context.getAuthConfig());
33123
default -> throw new UnsupportedOperationException(context.getAuthConfig().getAuthType());
34124
};

0 commit comments

Comments
 (0)