Skip to content

Commit

Permalink
wrap legacy oauth provider with genericoauth
Browse files Browse the repository at this point in the history
  • Loading branch information
goldants committed Jul 12, 2024
1 parent d3e6771 commit 2bd9522
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
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

0 comments on commit 2bd9522

Please sign in to comment.