Skip to content

Commit 6845453

Browse files
committed
Add functionality to allow users to link to auth providers while being logged in
1 parent 9595937 commit 6845453

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ protected Mono<AuthUser> authenticate(String authId, @Deprecated String source,
130130

131131
@Override
132132
public Mono<Void> loginOrRegister(AuthUser authUser, ServerWebExchange exchange,
133-
String invitationId) {
134-
return updateOrCreateUser(authUser)
133+
String invitationId, boolean linKExistingUser) {
134+
return updateOrCreateUser(authUser, linKExistingUser)
135135
.delayUntil(user -> ReactiveSecurityContextHolder.getContext()
136136
.doOnNext(securityContext -> securityContext.setAuthentication(AuthenticationUtils.toAuthentication(user))))
137137
// save token and set cookie
@@ -160,7 +160,13 @@ public Mono<Void> loginOrRegister(AuthUser authUser, ServerWebExchange exchange,
160160
.then(businessEventPublisher.publishUserLoginEvent(authUser.getSource()));
161161
}
162162

163-
private Mono<User> updateOrCreateUser(AuthUser authUser) {
163+
private Mono<User> updateOrCreateUser(AuthUser authUser, boolean linkExistingUser) {
164+
165+
if(linkExistingUser) {
166+
return sessionUserService.getVisitor()
167+
.flatMap(user -> userService.addNewConnectionAndReturnUser(user.getId(), authUser.toAuthConnection()));
168+
}
169+
164170
return findByAuthUserSourceAndRawId(authUser).zipWith(findByAuthUserRawId(authUser))
165171
.flatMap(tuple -> {
166172

0 commit comments

Comments
 (0)