From 0b1738dc5db1496791da5d4410dd70177d53f521 Mon Sep 17 00:00:00 2001 From: Stefan Benz <46600784+stebenz@users.noreply.github.com> Date: Thu, 8 Jun 2023 00:16:02 +0200 Subject: [PATCH] fix: check linked users before postAuthentication action (#5980) * fix: check linked users before postAuthentication action * fix: apply suggestions from code review Co-authored-by: Silvan --------- Co-authored-by: Silvan --- .../api/ui/login/external_provider_handler.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/api/ui/login/external_provider_handler.go b/internal/api/ui/login/external_provider_handler.go index a5a365ea55..2b7035c35e 100644 --- a/internal/api/ui/login/external_provider_handler.go +++ b/internal/api/ui/login/external_provider_handler.go @@ -286,17 +286,19 @@ func (l *Login) handleExternalUserAuthenticated( callback func(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest), ) { externalUser := mapIDPUserToExternalUser(user, provider.ID) - externalUser, externalUserChange, err := l.runPostExternalAuthenticationActions(externalUser, tokens(session), authReq, r, user, nil) + // check and fill in local linked user + externalErr := l.authRepo.CheckExternalUserLogin(setContext(r.Context(), ""), authReq.ID, authReq.AgentID, externalUser, domain.BrowserInfoFromRequest(r)) + if !errors.IsNotFound(externalErr) { + l.renderError(w, r, authReq, externalErr) + return + } + externalUser, externalUserChange, err := l.runPostExternalAuthenticationActions(externalUser, tokens(session), authReq, r, user, externalErr) if err != nil { l.renderError(w, r, authReq, err) return } - err = l.authRepo.CheckExternalUserLogin(setContext(r.Context(), ""), authReq.ID, authReq.AgentID, externalUser, domain.BrowserInfoFromRequest(r)) - if err != nil { - if !errors.IsNotFound(err) { - l.renderError(w, r, authReq, err) - return - } + // if action is done and no user linked then link or register + if errors.IsNotFound(externalErr) { l.externalUserNotExisting(w, r, authReq, provider, externalUser) return }