Skip to content

Allow Email/Form Login/Register To Bind User To Org On Login/SignUp #522

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
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public class FormAuthRequestContext extends AuthRequestContext {
private final String password;
private final boolean register;

public FormAuthRequestContext(String loginId, String password, boolean register) {
public FormAuthRequestContext(String loginId, String password, boolean register, String orgId) {
this.loginId = loginId;
this.password = password;
this.register = register;
this.setOrgId(orgId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public class AuthenticationController implements AuthenticationEndpoints
*/
@Override
public Mono<ResponseView<Boolean>> formLogin(@RequestBody FormLoginRequest formLoginRequest,
@RequestParam(required = false) String invitationId,
ServerWebExchange exchange) {
@RequestParam(required = false) String invitationId,
@RequestParam(required = false) String orgId,
ServerWebExchange exchange) {
return authenticationApiService.authenticateByForm(formLoginRequest.loginId(), formLoginRequest.password(),
formLoginRequest.source(), formLoginRequest.register(), formLoginRequest.authId())
formLoginRequest.source(), formLoginRequest.register(), formLoginRequest.authId(), orgId)
.flatMap(user -> authenticationApiService.loginOrRegister(user, exchange, invitationId))
.thenReturn(ResponseView.success(true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public interface AuthenticationEndpoints
@PostMapping("/form/login")
public Mono<ResponseView<Boolean>> formLogin(@RequestBody FormLoginRequest formLoginRequest,
@RequestParam(required = false) String invitationId,
@RequestParam(required = false) String orgId,
ServerWebExchange exchange);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public interface AuthenticationApiService {

Mono<AuthUser> authenticateByForm(String loginId, String password, String source, boolean register, String authId);
Mono<AuthUser> authenticateByForm(String loginId, String password, String source, boolean register, String authId, String orgId);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public class AuthenticationApiServiceImpl implements AuthenticationApiService {
private JWTUtils jwtUtils;

@Override
public Mono<AuthUser> authenticateByForm(String loginId, String password, String source, boolean register, String authId) {
return authenticate(authId, source, new FormAuthRequestContext(loginId, password, register));
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));
}

@Override
Expand All @@ -105,7 +105,13 @@ protected Mono<AuthUser> authenticate(String authId, @Deprecated String source,
})
.doOnNext(findAuthConfig -> {
context.setAuthConfig(findAuthConfig.authConfig());
context.setOrgId(Optional.ofNullable(findAuthConfig.organization()).map(Organization::getId).orElse(null));
if (findAuthConfig.authConfig().getSource().equals("EMAIL")) {
if(StringUtils.isBlank(context.getOrgId())) {
context.setOrgId(Optional.ofNullable(findAuthConfig.organization()).map(Organization::getId).orElse(null));
}
} else {
context.setOrgId(Optional.ofNullable(findAuthConfig.organization()).map(Organization::getId).orElse(null));
}
})
.then(authRequestFactory.build(context))
.flatMap(authRequest -> authRequest.auth(context))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testFormRegisterSuccess() {
MockServerHttpRequest request = MockServerHttpRequest.post("").build();
MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

Mono<User> userMono = authenticationController.formLogin(formLoginRequest, null, exchange)
Mono<User> userMono = authenticationController.formLogin(formLoginRequest, null, null, exchange)
.then(userRepository.findByConnections_SourceAndConnections_RawId(source, email));

StepVerifier.create(userMono)
Expand Down Expand Up @@ -115,8 +115,8 @@ public void testFormLoginSuccess() {
MockServerHttpRequest loginRequest = MockServerHttpRequest.post("").build();
MockServerWebExchange loginExchange = MockServerWebExchange.builder(loginRequest).build();

Mono<User> userMono = authenticationController.formLogin(formRegisterRequest, null, registerExchange)
.then(authenticationController.formLogin(formLoginRequest, null, loginExchange))
Mono<User> userMono = authenticationController.formLogin(formRegisterRequest, null,null, registerExchange)
.then(authenticationController.formLogin(formLoginRequest, null, null,loginExchange))
.then(userRepository.findByConnections_SourceAndConnections_RawId(source, email));

StepVerifier.create(userMono)
Expand Down Expand Up @@ -163,8 +163,8 @@ public void testRegisterFailByLoginIdExist() {
MockServerHttpRequest request = MockServerHttpRequest.post("").build();
MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

Mono<ResponseView<Boolean>> loginMono = authenticationController.formLogin(formLoginRequest, null, exchange)
.then(authenticationController.formLogin(formLoginRequest, null, exchange));
Mono<ResponseView<Boolean>> loginMono = authenticationController.formLogin(formLoginRequest, null, null,exchange)
.then(authenticationController.formLogin(formLoginRequest, null,null, exchange));
StepVerifier.create(loginMono)
.verifyErrorMatches(throwable -> {
BizException bizException = (BizException) throwable;
Expand All @@ -184,7 +184,7 @@ public void testLoginFailByLoginIdNotExist() {
MockServerHttpRequest request = MockServerHttpRequest.post("").build();
MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

Mono<ResponseView<Boolean>> loginMono = authenticationController.formLogin(formLoginRequest, null, exchange);
Mono<ResponseView<Boolean>> loginMono = authenticationController.formLogin(formLoginRequest, null, null, exchange);
StepVerifier.create(loginMono)
.verifyErrorMatches(throwable -> {
BizException bizException = (BizException) throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testGoogleRegisterSuccess() {
MockServerHttpRequest request = MockServerHttpRequest.post("").build();
MockServerWebExchange exchange = MockServerWebExchange.builder(request).build();

Mono<User> userMono = authenticationController.formLogin(formLoginRequest, null, exchange)
Mono<User> userMono = authenticationController.formLogin(formLoginRequest, null,null, exchange)
.then(userRepository.findByConnections_SourceAndConnections_RawId(source, email));

StepVerifier.create(userMono)
Expand Down