Skip to content
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

Show error message to OAuth2 user when a matching local account already exists #116

Merged

Conversation

emmdurin
Copy link
Contributor

If a user tries to log with OAuth2 and a local account with the same e-mail address already exists, it should be presented a clear error messages about this conflict, and the user should not be considered logged in.

Follow up to PR#86 after a change of source branch.

Copy link
Collaborator

@f-necas f-necas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly a Mono thing, the rest seems good to me :)

Comment on lines 94 to 111
.map(auth -> {
try {
return Pair.of(resolver.resolve(auth), "");
} catch (DuplicatedEmailFoundException exp) {
Optional<GeorchestraUser> op = Optional.empty();
return Pair.of(op, "/login?error=" + DUPLICATE_ACCOUNT);
}
})//
.map(user -> {
GeorchestraUser usr = user.orElse(null);
if (!user.getRight().isEmpty()) {
SecurityContextHolder.getContext();
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.FOUND);
response.getHeaders().setLocation(URI.create(user.getRight()));
return exchange;
}

GeorchestraUser usr = user.getLeft().orElse(null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to better understand Mono and ended up with something like (not fully working) :

.map(resolver::resolve)//
                .map(user -> {
                    GeorchestraUser usr = user.orElse(null);
                    GeorchestraUsers.store(exchange, usr);
                    if (usr != null && usr instanceof ExtendedGeorchestraUser) {
                        ExtendedGeorchestraUser eu = (ExtendedGeorchestraUser) usr;
                        Organization org = eu.getOrg();
                        GeorchestraOrganizations.store(exchange, org);
                    }
                    return exchange;
                })//
                .doOnError(DuplicatedEmailFoundException.class, exp -> {
                    ServerHttpResponse response = exchange.getResponse();
                    response.setStatusCode(HttpStatus.FOUND);
                    response.getHeaders().setLocation(URI.create("/login?error=" + DUPLICATE_ACCOUNT));
                    exchange.getSession().flatMap(WebSession::invalidate);
                })

I think we can do something way better. I couldn't succeed to handle status code in error.

Copy link
Contributor Author

@emmdurin emmdurin Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think we can do something this way, but still have to find the correct syntax. Your proposal run additional code if an exception is thrown but doesn't cancel the exception which is thrown anyway at the end of the Mono chain. In fact it's like a try-finally but we need instead something like a try-catch.

@emmdurin emmdurin merged commit ce36b3d into main Apr 24, 2024
3 checks passed
@emmdurin emmdurin deleted the show_error_message_to_user_when_duplicate_email_or_username_oauth2 branch April 24, 2024 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants