From 6845453ccf757fd37ebd681c734bb65cf09d8265 Mon Sep 17 00:00:00 2001 From: Abdul Qadir Date: Sat, 9 Dec 2023 12:27:49 +0500 Subject: [PATCH 1/2] Add functionality to allow users to link to auth providers while being logged in --- .../AuthenticationController.java | 17 +++++++++++++++-- .../AuthenticationEndpoints.java | 18 ++++++++++++++++++ .../service/AuthenticationApiService.java | 2 +- .../service/AuthenticationApiServiceImpl.java | 12 +++++++++--- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/AuthenticationController.java b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/AuthenticationController.java index c80d7536d..81dbd09b5 100644 --- a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/AuthenticationController.java +++ b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/AuthenticationController.java @@ -46,7 +46,7 @@ public Mono> formLogin(@RequestBody FormLoginRequest formL ServerWebExchange exchange) { return authenticationApiService.authenticateByForm(formLoginRequest.loginId(), formLoginRequest.password(), formLoginRequest.source(), formLoginRequest.register(), formLoginRequest.authId(), orgId) - .flatMap(user -> authenticationApiService.loginOrRegister(user, exchange, invitationId)) + .flatMap(user -> authenticationApiService.loginOrRegister(user, exchange, invitationId, Boolean.FALSE)) .thenReturn(ResponseView.success(true)); } @@ -63,7 +63,20 @@ public Mono> loginWithThirdParty( @RequestParam String orgId, ServerWebExchange exchange) { return authenticationApiService.authenticateByOauth2(authId, source, code, redirectUrl, orgId) - .flatMap(authUser -> authenticationApiService.loginOrRegister(authUser, exchange, invitationId)) + .flatMap(authUser -> authenticationApiService.loginOrRegister(authUser, exchange, invitationId, Boolean.FALSE)) + .thenReturn(ResponseView.success(true)); + } + + @Override + public Mono> linkAccountWithThirdParty( + @RequestParam(required = false) String authId, + @RequestParam(required = false) String source, + @RequestParam String code, + @RequestParam String redirectUrl, + @RequestParam String orgId, + ServerWebExchange exchange) { + return authenticationApiService.authenticateByOauth2(authId, source, code, redirectUrl, orgId) + .flatMap(authUser -> authenticationApiService.loginOrRegister(authUser, exchange, null, Boolean.TRUE)) .thenReturn(ResponseView.success(true)); } diff --git a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/AuthenticationEndpoints.java b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/AuthenticationEndpoints.java index 2645c1035..d66e252ae 100644 --- a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/AuthenticationEndpoints.java +++ b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/AuthenticationEndpoints.java @@ -69,6 +69,24 @@ public Mono> loginWithThirdParty( @RequestParam String orgId, ServerWebExchange exchange); + /** + * Link current account with third party auth provider + */ + @Operation( + tags = TAG_AUTHENTICATION, + operationId = "linkAccountWithTP", + summary = "Link current account with third party auth provider", + description = "Authenticate a Lowcoder User using third-party login credentials and link to the existing session/account" + ) + @PostMapping("/tp/link") + public Mono> linkAccountWithThirdParty( + @RequestParam(required = false) String authId, + @RequestParam(required = false) String source, + @RequestParam String code, + @RequestParam String redirectUrl, + @RequestParam String orgId, + ServerWebExchange exchange); + @Operation( tags = TAG_AUTHENTICATION, operationId = "logout", diff --git a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiService.java b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiService.java index d47748662..cdf8cea97 100644 --- a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiService.java +++ b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiService.java @@ -16,7 +16,7 @@ public interface AuthenticationApiService { Mono authenticateByOauth2(String authId, String source, String code, String redirectUrl, String orgId); - Mono loginOrRegister(AuthUser authUser, ServerWebExchange exchange, String invitationId); + Mono loginOrRegister(AuthUser authUser, ServerWebExchange exchange, String invitationId, boolean linKExistingUser); Mono enableAuthConfig(AuthConfigRequest authConfigRequest); diff --git a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiServiceImpl.java b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiServiceImpl.java index 966b86bb7..7b2482d82 100644 --- a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiServiceImpl.java +++ b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiServiceImpl.java @@ -130,8 +130,8 @@ protected Mono authenticate(String authId, @Deprecated String source, @Override public Mono loginOrRegister(AuthUser authUser, ServerWebExchange exchange, - String invitationId) { - return updateOrCreateUser(authUser) + String invitationId, boolean linKExistingUser) { + return updateOrCreateUser(authUser, linKExistingUser) .delayUntil(user -> ReactiveSecurityContextHolder.getContext() .doOnNext(securityContext -> securityContext.setAuthentication(AuthenticationUtils.toAuthentication(user)))) // save token and set cookie @@ -160,7 +160,13 @@ public Mono loginOrRegister(AuthUser authUser, ServerWebExchange exchange, .then(businessEventPublisher.publishUserLoginEvent(authUser.getSource())); } - private Mono updateOrCreateUser(AuthUser authUser) { + private Mono updateOrCreateUser(AuthUser authUser, boolean linkExistingUser) { + + if(linkExistingUser) { + return sessionUserService.getVisitor() + .flatMap(user -> userService.addNewConnectionAndReturnUser(user.getId(), authUser.toAuthConnection())); + } + return findByAuthUserSourceAndRawId(authUser).zipWith(findByAuthUserRawId(authUser)) .flatMap(tuple -> { From 95aae3c1ce8080ce223c612c5cc60734a801c096 Mon Sep 17 00:00:00 2001 From: Abdul Qadir Date: Sat, 9 Dec 2023 14:46:06 +0500 Subject: [PATCH 2/2] Add handling for LOWCODER_CREATE_SIGNUP_WORKSPACE --- .../main/java/org/lowcoder/sdk/config/AuthProperties.java | 1 + .../service/AuthenticationApiServiceImpl.java | 8 +++++++- .../src/main/resources/application-lowcoder.yml | 7 ++++--- .../main/resources/selfhost/ce/application-selfhost.yml | 1 + .../src/main/resources/selfhost/ce/application.yml | 1 + 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/server/api-service/lowcoder-sdk/src/main/java/org/lowcoder/sdk/config/AuthProperties.java b/server/api-service/lowcoder-sdk/src/main/java/org/lowcoder/sdk/config/AuthProperties.java index 178571bea..789a10fa0 100644 --- a/server/api-service/lowcoder-sdk/src/main/java/org/lowcoder/sdk/config/AuthProperties.java +++ b/server/api-service/lowcoder-sdk/src/main/java/org/lowcoder/sdk/config/AuthProperties.java @@ -28,6 +28,7 @@ public class AuthProperties { private Oauth2Simple google = new Oauth2Simple(); private Oauth2Simple github = new Oauth2Simple(); private ApiKey apiKey = new ApiKey(); + private Boolean workspaceCreation; @Getter @Setter diff --git a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiServiceImpl.java b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiServiceImpl.java index 7b2482d82..ad6e3101b 100644 --- a/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiServiceImpl.java +++ b/server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiServiceImpl.java @@ -29,6 +29,7 @@ import org.lowcoder.domain.user.model.*; import org.lowcoder.domain.user.service.UserService; import org.lowcoder.sdk.auth.AbstractAuthConfig; +import org.lowcoder.sdk.config.AuthProperties; import org.lowcoder.sdk.exception.BizError; import org.lowcoder.sdk.exception.BizException; import org.lowcoder.sdk.util.CookieHelper; @@ -85,6 +86,9 @@ public class AuthenticationApiServiceImpl implements AuthenticationApiService { @Autowired private JWTUtils jwtUtils; + @Autowired + private AuthProperties authProperties; + @Override public Mono authenticateByForm(String loginId, String password, String source, boolean register, String authId, String orgId) { return authenticate(authId, source, new FormAuthRequestContext(loginId, password, register, orgId)); @@ -142,7 +146,9 @@ public Mono loginOrRegister(AuthUser authUser, ServerWebExchange exchange, }) // after register .delayUntil(user -> { - if (user.getIsNewUser()) { + boolean createWorkspace = + authUser.getOrgId() == null && StringUtils.isBlank(invitationId) && authProperties.getWorkspaceCreation(); + if (user.getIsNewUser() && createWorkspace) { return onUserRegister(user); } return Mono.empty(); diff --git a/server/api-service/lowcoder-server/src/main/resources/application-lowcoder.yml b/server/api-service/lowcoder-server/src/main/resources/application-lowcoder.yml index 8adbb1a6f..b223846d4 100644 --- a/server/api-service/lowcoder-server/src/main/resources/application-lowcoder.yml +++ b/server/api-service/lowcoder-server/src/main/resources/application-lowcoder.yml @@ -3,9 +3,9 @@ spring: mongodb: authentication-database: admin auto-index-creation: false - uri: mongodb://lowcoder:secret123@localhost:27017/lowcoder?authSource=admin + uri: mongodb://192.168.8.103:27017/lowcoder?authSource=admin redis: - url: redis://localhost:6379 + url: redis://192.168.8.103:6379 main: allow-bean-definition-overriding: true allow-circular-references: true @@ -61,4 +61,5 @@ auth: secret: 5a41b090758b39b226603177ef48d73ae9839dd458ccb7e66f7e7cc028d5a50b email: enable: true - enable-register: true \ No newline at end of file + enable-register: true + workspace-creation: false \ No newline at end of file diff --git a/server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application-selfhost.yml b/server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application-selfhost.yml index 8dc5a265b..e35938de4 100644 --- a/server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application-selfhost.yml +++ b/server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application-selfhost.yml @@ -13,6 +13,7 @@ auth: email: enable: ${LOGIN_CHANNEL_EMAIL:true} enable-register: ${ENABLE_USER_SIGN_UP:true} + workspace-creation: ${LOWCODER_CREATE_SIGNUP_WORKSPACE:true} spring: data: diff --git a/server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application.yml b/server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application.yml index 4a8a0b11c..c39b5350b 100644 --- a/server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application.yml +++ b/server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application.yml @@ -4,6 +4,7 @@ auth: email: enable: true enable-register: ${ENABLE_USER_SIGN_UP:true} + workspace-creation: ${LOWCODER_CREATE_SIGNUP_WORKSPACE:true} spring: data: