Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap legacy oauth provider with generic #1040

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Oauth2Constants {
+ "&client_id=" + CLIENT_ID_PLACEHOLDER
+ "&redirect_uri=" + REDIRECT_URL_PLACEHOLDER
+ "&state=" + STATE_PLACEHOLDER
+ "&scope=";
+ "&scope=user";

public static final String GOOGLE_AUTHORIZE_URL = "https://accounts.google.com/o/oauth2/v2/auth"
+ "?response_type=code"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lowcoder.api.authentication.request.oauth2;

import java.util.HashMap;
import java.util.Set;

import org.lowcoder.api.authentication.request.AuthRequest;
Expand All @@ -9,6 +10,7 @@
import org.lowcoder.sdk.auth.Oauth2KeycloakAuthConfig;
import org.lowcoder.sdk.auth.Oauth2OryAuthConfig;
import org.lowcoder.sdk.auth.Oauth2SimpleAuthConfig;
import org.lowcoder.sdk.auth.constants.AuthTypeConstants;
import org.springframework.stereotype.Component;

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

private AbstractOauth2Request<? extends Oauth2SimpleAuthConfig> buildRequest(OAuth2RequestContext context) {
return switch (context.getAuthConfig().getAuthType()) {
case GITHUB -> new GithubRequest((Oauth2SimpleAuthConfig) context.getAuthConfig());
case GOOGLE -> new GoogleRequest((Oauth2SimpleAuthConfig) context.getAuthConfig());
case ORY -> new OryRequest((Oauth2OryAuthConfig) context.getAuthConfig());
case KEYCLOAK -> new KeycloakRequest((Oauth2KeycloakAuthConfig)context.getAuthConfig());
case GITHUB -> {
HashMap<String, String> sourceMappings = new HashMap<>();
sourceMappings.put("uid", "id");
sourceMappings.put("email", "email");
sourceMappings.put("username", "login");
sourceMappings.put("avatar", "avatar_url");
Oauth2SimpleAuthConfig config = (Oauth2SimpleAuthConfig) context.getAuthConfig();
yield new GenericAuthRequest(Oauth2GenericAuthConfig.builder()
.tokenEndpoint(Oauth2DefaultSource.GITHUB.accessToken())
.userInfoEndpoint(Oauth2DefaultSource.GITHUB.userInfo())
.userInfoIntrospection(true)
.source(config.getSource())
.sourceName(config.getSourceName())
.enableRegister(config.isEnableRegister())
.enable(config.isEnable())
.scope("read:email read:user")
.userCanSelectAccounts(true)
.sourceMappings(sourceMappings)
.clientSecret(config.getClientSecret())
.clientId(config.getClientId())
.authType(GENERIC)
.build());
}
case GOOGLE -> {
HashMap<String, String> sourceMappings = new HashMap<>();
sourceMappings.put("uid", "sub");
sourceMappings.put("email", "email");
sourceMappings.put("username", "email");
sourceMappings.put("avatar", "picture");
Oauth2SimpleAuthConfig config = (Oauth2SimpleAuthConfig) context.getAuthConfig();
yield new GenericAuthRequest(Oauth2GenericAuthConfig.builder()
.tokenEndpoint(Oauth2DefaultSource.GOOGLE.accessToken())
.userInfoEndpoint(Oauth2DefaultSource.GOOGLE.userInfo())
.userInfoIntrospection(true)
.source(config.getSource())
.sourceName(config.getSourceName())
.enableRegister(config.isEnableRegister())
.enable(config.isEnable())
.scope("openid email profile")
.userCanSelectAccounts(true)
.sourceMappings(sourceMappings)
.clientSecret(config.getClientSecret())
.clientId(config.getClientId())
.authType(GENERIC)
.build());
}
case ORY -> {
HashMap<String, String> sourceMappings = new HashMap<>();
sourceMappings.put("uid", "sub");
sourceMappings.put("email", "email");
sourceMappings.put("username", "email");
sourceMappings.put("avatar", "picture");
Oauth2OryAuthConfig config = (Oauth2OryAuthConfig) context.getAuthConfig();
yield new GenericAuthRequest(Oauth2GenericAuthConfig.builder()
.tokenEndpoint(config.replaceAuthUrlClientIdPlaceholder(Oauth2DefaultSource.ORY.accessToken()))
.userInfoEndpoint(config.replaceAuthUrlClientIdPlaceholder(Oauth2DefaultSource.ORY.userInfo()))
.userInfoIntrospection(true)
.source(config.getSource())
.sourceName(config.getSourceName())
.enableRegister(config.isEnableRegister())
.enable(config.isEnable())
.scope(config.getScope())
.userCanSelectAccounts(false)
.sourceMappings(sourceMappings)
.clientSecret(config.getClientSecret())
.clientId(config.getClientId())
.authType(GENERIC)
.build());
}
case KEYCLOAK -> {
HashMap<String, String> sourceMappings = new HashMap<>();
sourceMappings.put("uid", "sub");
sourceMappings.put("email", "email");
sourceMappings.put("username", "email");
sourceMappings.put("avatar", "false");
Oauth2KeycloakAuthConfig config = (Oauth2KeycloakAuthConfig) context.getAuthConfig();
yield new GenericAuthRequest(Oauth2GenericAuthConfig.builder()
.tokenEndpoint(config.replaceAuthUrlClientIdPlaceholder(Oauth2DefaultSource.KEYCLOAK.accessToken()))
.userInfoEndpoint(config.replaceAuthUrlClientIdPlaceholder(Oauth2DefaultSource.KEYCLOAK.userInfo()))
.userInfoIntrospection(true)
.source(config.getSource())
.sourceName(config.getSourceName())
.enableRegister(config.isEnableRegister())
.enable(config.isEnable())
.scope(config.getScope())
.userCanSelectAccounts(false)
.sourceMappings(sourceMappings)
.clientSecret(config.getClientSecret())
.clientId(config.getClientId())
.authType(GENERIC)
.build());
}
case GENERIC -> new GenericAuthRequest((Oauth2GenericAuthConfig) context.getAuthConfig());
default -> throw new UnsupportedOperationException(context.getAuthConfig().getAuthType());
};
Expand Down
Loading