Skip to content

Commit 157425a

Browse files
author
th37rose
committed
Implemented the generic Auth feature without additional .well_known endpoint.
1 parent 1225318 commit 157425a

File tree

8 files changed

+34
-112
lines changed

8 files changed

+34
-112
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public String getAuthorizeUrl() {
3838
case AuthTypeConstants.GITHUB -> replaceAuthUrlClientIdPlaceholder(Oauth2Constants.GITHUB_AUTHORIZE_URL);
3939
case AuthTypeConstants.ORY -> replaceAuthUrlClientIdPlaceholder(Oauth2Constants.ORY_AUTHORIZE_URL);
4040
case AuthTypeConstants.KEYCLOAK -> replaceAuthUrlClientIdPlaceholder(Oauth2Constants.KEYCLOAK_AUTHORIZE_URL);
41-
case AuthTypeConstants.GENERIC -> replaceAuthUrlClientIdPlaceholder(((Oauth2GenericAuthConfig)this).getAuthorizationEndpoint());
41+
case AuthTypeConstants.GENERIC -> ((Oauth2GenericAuthConfig)this).getAuthorizationEndpoint();
4242
default -> null;
4343
};
4444
}

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

-20
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,4 @@ public Mono<ResponseView<List<APIKey>>> getAllAPIKeys() {
130130
.collectList()
131131
.map(ResponseView::success);
132132
}
133-
134-
/**
135-
* This endpoint is to get IDP configuration
136-
* @param issuerUri String
137-
* @param source String
138-
* @param sourceName String
139-
* @param clientId String
140-
* @param clientSecret String
141-
* @return Oauth2GenericAuthConfig
142-
*/
143-
@Override
144-
public Mono<ResponseView<Oauth2GenericAuthConfig>> addOAuthProvider(String issuerUri,
145-
String source,
146-
String sourceName,
147-
String clientId,
148-
String clientSecret) {
149-
return authenticationApiService.fetchAndParseConfiguration(issuerUri, source, sourceName, clientId, clientSecret)
150-
.map(ResponseView::success);
151-
}
152-
153133
}

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

-16
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,4 @@ public Mono<ResponseView<Boolean>> linkAccountWithThirdParty(
162162
*/
163163
public record FormLoginRequest(String loginId, String password, boolean register, String source, String authId) {
164164
}
165-
166-
/**
167-
* This endpoint is to get IDP configuration
168-
* @param issuerUri String
169-
* @param source String
170-
* @param sourceName String
171-
* @param clientId String
172-
* @param clientSecret String
173-
* @return Oauth2GenericAuthConfig
174-
*/
175-
@GetMapping("/providers")
176-
public Mono<ResponseView<Oauth2GenericAuthConfig>> addOAuthProvider(@RequestParam String issuerUri,
177-
@RequestParam String source,
178-
@RequestParam String sourceName,
179-
@RequestParam String clientId,
180-
@RequestParam String clientSecret);
181165
}

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/dto/AuthConfigRequest.java

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.lowcoder.api.authentication.dto;
22

33
import jakarta.annotation.Nullable;
4+
import jakarta.validation.constraints.Null;
45
import org.apache.commons.collections4.MapUtils;
56
import org.apache.commons.lang3.ObjectUtils;
67
import org.apache.commons.lang3.StringUtils;
@@ -27,6 +28,30 @@ public boolean isEnableRegister() {
2728
return MapUtils.getBoolean(this, "enableRegister", true);
2829
}
2930

31+
/**
32+
* Additional configs for generic
33+
* config will be updated instead of creating a new one.
34+
*/
35+
@Nullable
36+
public String getIssuerUri() {
37+
return getString("issuer");
38+
}
39+
40+
@Nullable
41+
public String getAuthorizationEndpoint() {
42+
return getString("authorizationEndpoint");
43+
}
44+
45+
@Nullable
46+
public String getTokenEndpoint() {
47+
return getString("tokenEndpoint");
48+
}
49+
50+
@Nullable
51+
public String getUserInfoEndpoint() {
52+
return getString("userInfoEndpoint");
53+
}
54+
3055
@Nullable
3156
public String getInstanceId() {
3257
return getString("instanceId");

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

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.lowcoder.domain.user.model.AuthToken;
77
import org.lowcoder.domain.user.model.AuthUser;
88
import org.lowcoder.sdk.auth.Oauth2GenericAuthConfig;
9-
import org.lowcoder.sdk.auth.Oauth2KeycloakAuthConfig;
109
import org.lowcoder.sdk.util.JsonUtils;
1110
import org.lowcoder.sdk.webclient.WebClientBuildHelper;
1211
import org.springframework.http.MediaType;

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

-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.lowcoder.domain.authentication.FindAuthConfig;
77
import org.lowcoder.domain.user.model.APIKey;
88
import org.lowcoder.domain.user.model.AuthUser;
9-
import org.lowcoder.sdk.auth.Oauth2GenericAuthConfig;
109
import org.springframework.web.server.ServerWebExchange;
1110
import reactor.core.publisher.Flux;
1211
import reactor.core.publisher.Mono;
@@ -30,19 +29,4 @@ public interface AuthenticationApiService {
3029
Mono<Void> deleteAPIKey(String authId);
3130

3231
Flux<APIKey> findAPIKeys();
33-
34-
/**
35-
* This method is to fetch and parse the OpenID configuration from the issuer URI.
36-
* @param issuerUri String
37-
* @param source String
38-
* @param sourceName String
39-
* @param clientId String
40-
* @param clientSecret String
41-
* @return Oauth2GenericAuthConfig
42-
*/
43-
Mono<Oauth2GenericAuthConfig> fetchAndParseConfiguration(String issuerUri,
44-
String source,
45-
String sourceName,
46-
String clientId,
47-
String clientSecret);
4832
}

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

-50
Original file line numberDiff line numberDiff line change
@@ -333,56 +333,6 @@ public Flux<APIKey> findAPIKeys() {
333333
);
334334
}
335335

336-
/**
337-
* This method is to fetch and parse the OpenID configuration from the issuer URI.
338-
* @param issuerUri String
339-
* @param source String
340-
* @param sourceName String
341-
* @param clientId String
342-
* @param clientSecret String
343-
* @return Oauth2GenericAuthConfig
344-
*/
345-
@Override
346-
public Mono<Oauth2GenericAuthConfig> fetchAndParseConfiguration(String issuerUri,
347-
String source,
348-
String sourceName,
349-
String clientId,
350-
String clientSecret) {
351-
String wellKnownUri = issuerUri + "/.well-known/openid-configuration";
352-
return WebClientBuildHelper.builder()
353-
.systemProxy()
354-
.build()
355-
.get()
356-
.uri(wellKnownUri)
357-
.retrieve()
358-
.bodyToMono(Map.class)
359-
.map(map -> mapToConfig(map, source, sourceName, clientId, clientSecret));
360-
}
361-
362-
/**
363-
* This method is to map to config for Generic Auth Provider
364-
* @param map Object that comes from /.well-known endpoint for IDP Configuration
365-
* @return Oauth2GenericAuthConfig
366-
*/
367-
private Oauth2GenericAuthConfig mapToConfig(Map<String, Object> map,
368-
String source,
369-
String sourceName,
370-
String clientId,
371-
String clientSecret) {
372-
return Oauth2GenericAuthConfig.builder()
373-
.authType(AuthTypeConstants.GENERIC)
374-
.source(source)
375-
.sourceName(sourceName)
376-
.clientId(clientId)
377-
.clientSecret(clientSecret)
378-
.issuerUri((String) map.get("issuer"))
379-
.authorizationEndpoint((String) map.get("authorization_endpoint"))
380-
.tokenEndpoint((String) map.get("token_endpoint"))
381-
.userInfoEndpoint((String) map.get("userinfo_endpoint"))
382-
.build();
383-
}
384-
385-
386336
private Mono<Void> removeTokensByAuthId(String authId) {
387337
return sessionUserService.getVisitorOrgMemberCache()
388338
.flatMapMany(orgMember -> orgMemberService.getOrganizationMembers(orgMember.getOrgId()))

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/factory/AuthConfigFactoryImpl.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class AuthConfigFactoryImpl implements AuthConfigFactory {
1919

2020
@Override
2121
public AbstractAuthConfig build(AuthConfigRequest authConfigRequest, boolean enable) {
22+
buildOauth2GenericAuthConfig(authConfigRequest, enable);
2223
return switch (authConfigRequest.getAuthType()) {
2324
case AuthTypeConstants.FORM -> buildEmailAuthConfig(authConfigRequest, enable);
2425
case AuthTypeConstants.GITHUB -> buildOauth2SimpleAuthConfig(GITHUB, GITHUB_NAME, authConfigRequest, enable);
@@ -103,16 +104,15 @@ private Oauth2SimpleAuthConfig buildOauth2GenericAuthConfig(AuthConfigRequest au
103104
.id(authConfigRequest.getId())
104105
.enable(enable)
105106
.enableRegister(authConfigRequest.isEnableRegister())
106-
.source(AuthTypeConstants.GENERIC)
107-
.sourceName(org.lowcoder.sdk.constants.AuthSourceConstants.KEYCLOAK_NAME)
107+
.source(authConfigRequest.getSource(AuthTypeConstants.GENERIC))
108+
.sourceName(authConfigRequest.getSourceName(AuthTypeConstants.GENERIC))
108109
.clientId(requireNonNull(authConfigRequest.getClientId(), "clientId can not be null."))
109110
.clientSecret(authConfigRequest.getClientSecret())
110-
.issuerUri(authConfigRequest.getString("issuer"))
111-
.authorizationEndpoint(authConfigRequest.getString("authorization_endpoint"))
112-
.tokenEndpoint(authConfigRequest.getString("token_endpoint"))
113-
.userInfoEndpoint(authConfigRequest.getString("userinfo_endpoint"))
114-
.authType(authConfigRequest.getAuthType())
111+
.issuerUri(authConfigRequest.getIssuerUri())
112+
.authorizationEndpoint(authConfigRequest.getAuthorizationEndpoint())
113+
.tokenEndpoint(authConfigRequest.getTokenEndpoint())
114+
.userInfoEndpoint(authConfigRequest.getUserInfoEndpoint())
115+
.authType(AuthTypeConstants.GENERIC)
115116
.build();
116-
117117
}
118118
}

0 commit comments

Comments
 (0)