Skip to content

Commit b106575

Browse files
authored
Merge pull request #572 from lowcoder-org/link-oauth-providers-for-existing-users
Link Oauth Providers Feature For Existing Users
2 parents 2b20d3f + 0a96750 commit b106575

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

server/api-service/lowcoder-sdk/src/main/java/org/lowcoder/sdk/config/AuthProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class AuthProperties {
2828
private Oauth2Simple google = new Oauth2Simple();
2929
private Oauth2Simple github = new Oauth2Simple();
3030
private ApiKey apiKey = new ApiKey();
31+
private Boolean workspaceCreation;
3132

3233
@Getter
3334
@Setter

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Mono<ResponseView<Boolean>> formLogin(@RequestBody FormLoginRequest formL
4646
ServerWebExchange exchange) {
4747
return authenticationApiService.authenticateByForm(formLoginRequest.loginId(), formLoginRequest.password(),
4848
formLoginRequest.source(), formLoginRequest.register(), formLoginRequest.authId(), orgId)
49-
.flatMap(user -> authenticationApiService.loginOrRegister(user, exchange, invitationId))
49+
.flatMap(user -> authenticationApiService.loginOrRegister(user, exchange, invitationId, Boolean.FALSE))
5050
.thenReturn(ResponseView.success(true));
5151
}
5252

@@ -63,7 +63,20 @@ public Mono<ResponseView<Boolean>> loginWithThirdParty(
6363
@RequestParam String orgId,
6464
ServerWebExchange exchange) {
6565
return authenticationApiService.authenticateByOauth2(authId, source, code, redirectUrl, orgId)
66-
.flatMap(authUser -> authenticationApiService.loginOrRegister(authUser, exchange, invitationId))
66+
.flatMap(authUser -> authenticationApiService.loginOrRegister(authUser, exchange, invitationId, Boolean.FALSE))
67+
.thenReturn(ResponseView.success(true));
68+
}
69+
70+
@Override
71+
public Mono<ResponseView<Boolean>> linkAccountWithThirdParty(
72+
@RequestParam(required = false) String authId,
73+
@RequestParam(required = false) String source,
74+
@RequestParam String code,
75+
@RequestParam String redirectUrl,
76+
@RequestParam String orgId,
77+
ServerWebExchange exchange) {
78+
return authenticationApiService.authenticateByOauth2(authId, source, code, redirectUrl, orgId)
79+
.flatMap(authUser -> authenticationApiService.loginOrRegister(authUser, exchange, null, Boolean.TRUE))
6780
.thenReturn(ResponseView.success(true));
6881
}
6982

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ public Mono<ResponseView<Boolean>> loginWithThirdParty(
6969
@RequestParam String orgId,
7070
ServerWebExchange exchange);
7171

72+
/**
73+
* Link current account with third party auth provider
74+
*/
75+
@Operation(
76+
tags = TAG_AUTHENTICATION,
77+
operationId = "linkAccountWithTP",
78+
summary = "Link current account with third party auth provider",
79+
description = "Authenticate a Lowcoder User using third-party login credentials and link to the existing session/account"
80+
)
81+
@PostMapping("/tp/link")
82+
public Mono<ResponseView<Boolean>> linkAccountWithThirdParty(
83+
@RequestParam(required = false) String authId,
84+
@RequestParam(required = false) String source,
85+
@RequestParam String code,
86+
@RequestParam String redirectUrl,
87+
@RequestParam String orgId,
88+
ServerWebExchange exchange);
89+
7290
@Operation(
7391
tags = TAG_AUTHENTICATION,
7492
operationId = "logout",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface AuthenticationApiService {
1616

1717
Mono<AuthUser> authenticateByOauth2(String authId, String source, String code, String redirectUrl, String orgId);
1818

19-
Mono<Void> loginOrRegister(AuthUser authUser, ServerWebExchange exchange, String invitationId);
19+
Mono<Void> loginOrRegister(AuthUser authUser, ServerWebExchange exchange, String invitationId, boolean linKExistingUser);
2020

2121
Mono<Boolean> enableAuthConfig(AuthConfigRequest authConfigRequest);
2222

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.lowcoder.domain.user.model.*;
3030
import org.lowcoder.domain.user.service.UserService;
3131
import org.lowcoder.sdk.auth.AbstractAuthConfig;
32+
import org.lowcoder.sdk.config.AuthProperties;
3233
import org.lowcoder.sdk.exception.BizError;
3334
import org.lowcoder.sdk.exception.BizException;
3435
import org.lowcoder.sdk.util.CookieHelper;
@@ -85,6 +86,9 @@ public class AuthenticationApiServiceImpl implements AuthenticationApiService {
8586
@Autowired
8687
private JWTUtils jwtUtils;
8788

89+
@Autowired
90+
private AuthProperties authProperties;
91+
8892
@Override
8993
public Mono<AuthUser> authenticateByForm(String loginId, String password, String source, boolean register, String authId, String orgId) {
9094
return authenticate(authId, source, new FormAuthRequestContext(loginId, password, register, orgId));
@@ -130,8 +134,8 @@ protected Mono<AuthUser> authenticate(String authId, @Deprecated String source,
130134

131135
@Override
132136
public Mono<Void> loginOrRegister(AuthUser authUser, ServerWebExchange exchange,
133-
String invitationId) {
134-
return updateOrCreateUser(authUser)
137+
String invitationId, boolean linKExistingUser) {
138+
return updateOrCreateUser(authUser, linKExistingUser)
135139
.delayUntil(user -> ReactiveSecurityContextHolder.getContext()
136140
.doOnNext(securityContext -> securityContext.setAuthentication(AuthenticationUtils.toAuthentication(user))))
137141
// save token and set cookie
@@ -142,7 +146,9 @@ public Mono<Void> loginOrRegister(AuthUser authUser, ServerWebExchange exchange,
142146
})
143147
// after register
144148
.delayUntil(user -> {
145-
if (user.getIsNewUser()) {
149+
boolean createWorkspace =
150+
authUser.getOrgId() == null && StringUtils.isBlank(invitationId) && authProperties.getWorkspaceCreation();
151+
if (user.getIsNewUser() && createWorkspace) {
146152
return onUserRegister(user);
147153
}
148154
return Mono.empty();
@@ -160,7 +166,13 @@ public Mono<Void> loginOrRegister(AuthUser authUser, ServerWebExchange exchange,
160166
.then(businessEventPublisher.publishUserLoginEvent(authUser.getSource()));
161167
}
162168

163-
private Mono<User> updateOrCreateUser(AuthUser authUser) {
169+
private Mono<User> updateOrCreateUser(AuthUser authUser, boolean linkExistingUser) {
170+
171+
if(linkExistingUser) {
172+
return sessionUserService.getVisitor()
173+
.flatMap(user -> userService.addNewConnectionAndReturnUser(user.getId(), authUser.toAuthConnection()));
174+
}
175+
164176
return findByAuthUserSourceAndRawId(authUser).zipWith(findByAuthUserRawId(authUser))
165177
.flatMap(tuple -> {
166178

server/api-service/lowcoder-server/src/main/resources/application-lowcoder.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ spring:
33
mongodb:
44
authentication-database: admin
55
auto-index-creation: false
6-
uri: mongodb://lowcoder:secret123@localhost:27017/lowcoder?authSource=admin
6+
uri: mongodb://192.168.8.103:27017/lowcoder?authSource=admin
77
redis:
8-
url: redis://localhost:6379
8+
url: redis://192.168.8.103:6379
99
main:
1010
allow-bean-definition-overriding: true
1111
allow-circular-references: true
@@ -61,4 +61,5 @@ auth:
6161
secret: 5a41b090758b39b226603177ef48d73ae9839dd458ccb7e66f7e7cc028d5a50b
6262
email:
6363
enable: true
64-
enable-register: true
64+
enable-register: true
65+
workspace-creation: false

server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application-selfhost.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ auth:
1313
email:
1414
enable: ${LOGIN_CHANNEL_EMAIL:true}
1515
enable-register: ${ENABLE_USER_SIGN_UP:true}
16+
workspace-creation: ${LOWCODER_CREATE_SIGNUP_WORKSPACE:true}
1617

1718
spring:
1819
data:

server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ auth:
44
email:
55
enable: true
66
enable-register: ${ENABLE_USER_SIGN_UP:true}
7+
workspace-creation: ${LOWCODER_CREATE_SIGNUP_WORKSPACE:true}
78

89
spring:
910
data:

0 commit comments

Comments
 (0)