Skip to content

Commit

Permalink
Merge branch 'dev' into docker_build_update
Browse files Browse the repository at this point in the history
  • Loading branch information
FalkWolsky authored Dec 10, 2023
2 parents b0d08a8 + b106575 commit 6292b35
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 @@ -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;
Expand Down Expand Up @@ -85,6 +86,9 @@ public class AuthenticationApiServiceImpl implements AuthenticationApiService {
@Autowired
private JWTUtils jwtUtils;

@Autowired
private AuthProperties authProperties;

@Override
public Mono<AuthUser> authenticateByForm(String loginId, String password, String source, boolean register, String authId, String orgId) {
return authenticate(authId, source, new FormAuthRequestContext(loginId, password, register, orgId));
Expand Down Expand Up @@ -130,8 +134,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 All @@ -142,7 +146,9 @@ public Mono<Void> 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();
Expand All @@ -160,7 +166,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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,4 +61,5 @@ auth:
secret: 5a41b090758b39b226603177ef48d73ae9839dd458ccb7e66f7e7cc028d5a50b
email:
enable: true
enable-register: true
enable-register: true
workspace-creation: false
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ auth:
email:
enable: true
enable-register: ${ENABLE_USER_SIGN_UP:true}
workspace-creation: ${LOWCODER_CREATE_SIGNUP_WORKSPACE:true}

spring:
data:
Expand Down

0 comments on commit 6292b35

Please sign in to comment.