Skip to content

Link Oauth Providers Feature For Existing Users #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add functionality to allow users to link to auth providers while bein…
…g logged in
  • Loading branch information
aq-ikhwa-tech committed Dec 9, 2023
commit 6845453ccf757fd37ebd681c734bb65cf09d8265
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Mono<ResponseView<Boolean>> 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));
}

Expand All @@ -63,7 +63,20 @@ public Mono<ResponseView<Boolean>> 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<ResponseView<Boolean>> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ public Mono<ResponseView<Boolean>> 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<ResponseView<Boolean>> 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface AuthenticationApiService {

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

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

Mono<Boolean> enableAuthConfig(AuthConfigRequest authConfigRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ protected Mono<AuthUser> authenticate(String authId, @Deprecated String source,

@Override
public Mono<Void> 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
Expand Down Expand Up @@ -160,7 +160,13 @@ public Mono<Void> loginOrRegister(AuthUser authUser, ServerWebExchange exchange,
.then(businessEventPublisher.publishUserLoginEvent(authUser.getSource()));
}

private Mono<User> updateOrCreateUser(AuthUser authUser) {
private Mono<User> 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 -> {

Expand Down