AuthN: Login error handling (#64239)
* Social: Fix type so it appears in error responses * AuthN: construct errutil.Error from social.Error * login: Check for errutil.Error and use public message * Login: redirectURLWithErrorCookie for authn errors Co-authored-by: Jo <joao.guerreiro@grafana.com>
This commit is contained in:
@@ -25,6 +25,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
@@ -441,5 +442,10 @@ func getLoginExternalError(err error) string {
|
||||
return createTokenErr.ExternalErr
|
||||
}
|
||||
|
||||
gfErr := &errutil.Error{}
|
||||
if errors.As(err, gfErr) {
|
||||
return gfErr.Public().Message
|
||||
}
|
||||
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
+2
-16
@@ -24,7 +24,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
@@ -90,7 +89,7 @@ func (hs *HTTPServer) OAuthLogin(ctx *contextmodel.ReqContext) {
|
||||
if code == "" {
|
||||
redirect, err := hs.authnService.RedirectURL(ctx.Req.Context(), authn.ClientWithPrefix(name), req)
|
||||
if err != nil {
|
||||
hs.handleAuthnOAuthErr(ctx, "failed to generate oauth redirect url", err)
|
||||
ctx.Redirect(hs.redirectURLWithErrorCookie(ctx, err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -109,7 +108,7 @@ func (hs *HTTPServer) OAuthLogin(ctx *contextmodel.ReqContext) {
|
||||
cookies.DeleteCookie(ctx.Resp, OauthStateCookieName, hs.CookieOptionsFromCfg)
|
||||
|
||||
if err != nil {
|
||||
hs.handleAuthnOAuthErr(ctx, "failed to perform login for oauth request", err)
|
||||
ctx.Redirect(hs.redirectURLWithErrorCookie(ctx, err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -380,19 +379,6 @@ func (hs *HTTPServer) hashStatecode(code, seed string) string {
|
||||
return hex.EncodeToString(hashBytes[:])
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) handleAuthnOAuthErr(c *contextmodel.ReqContext, msg string, err error) {
|
||||
gfErr := &errutil.Error{}
|
||||
if errors.As(err, gfErr) {
|
||||
if gfErr.Public().Message != "" {
|
||||
c.Handle(hs.Cfg, gfErr.Public().StatusCode, gfErr.Public().Message, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.Logger.Warn(msg, "err", err)
|
||||
c.Redirect(hs.Cfg.AppSubURL + "/login")
|
||||
}
|
||||
|
||||
type LoginError struct {
|
||||
HttpStatus int
|
||||
PublicMessage string
|
||||
|
||||
Reference in New Issue
Block a user