mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
chore: improve error message for incorrect login type (#8349)
* chore: add better error in wrong login type
This commit is contained in:
@ -32,6 +32,7 @@ import (
|
|||||||
"github.com/coder/coder/coderd/userpassword"
|
"github.com/coder/coder/coderd/userpassword"
|
||||||
"github.com/coder/coder/codersdk"
|
"github.com/coder/coder/codersdk"
|
||||||
"github.com/coder/coder/cryptorand"
|
"github.com/coder/coder/cryptorand"
|
||||||
|
"github.com/coder/coder/site"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -625,10 +626,7 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
|
|||||||
defer params.CommitAuditLogs()
|
defer params.CommitAuditLogs()
|
||||||
var httpErr httpError
|
var httpErr httpError
|
||||||
if xerrors.As(err, &httpErr) {
|
if xerrors.As(err, &httpErr) {
|
||||||
httpapi.Write(ctx, rw, httpErr.code, codersdk.Response{
|
httpErr.Write(rw, r)
|
||||||
Message: httpErr.msg,
|
|
||||||
Detail: httpErr.detail,
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -969,10 +967,7 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
|
|||||||
defer params.CommitAuditLogs()
|
defer params.CommitAuditLogs()
|
||||||
var httpErr httpError
|
var httpErr httpError
|
||||||
if xerrors.As(err, &httpErr) {
|
if xerrors.As(err, &httpErr) {
|
||||||
httpapi.Write(ctx, rw, httpErr.code, codersdk.Response{
|
httpErr.Write(rw, r)
|
||||||
Message: httpErr.msg,
|
|
||||||
Detail: httpErr.detail,
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1079,6 +1074,25 @@ type httpError struct {
|
|||||||
code int
|
code int
|
||||||
msg string
|
msg string
|
||||||
detail string
|
detail string
|
||||||
|
renderStaticPage bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e httpError) Write(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
if e.renderStaticPage {
|
||||||
|
site.RenderStaticErrorPage(rw, r, site.ErrorPageData{
|
||||||
|
Status: e.code,
|
||||||
|
HideStatus: true,
|
||||||
|
Title: e.msg,
|
||||||
|
Description: e.detail,
|
||||||
|
RetryEnabled: false,
|
||||||
|
DashboardURL: "/login",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
httpapi.Write(r.Context(), rw, e.code, codersdk.Response{
|
||||||
|
Message: e.msg,
|
||||||
|
Detail: e.detail,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e httpError) Error() string {
|
func (e httpError) Error() string {
|
||||||
@ -1126,13 +1140,7 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
|
|||||||
}
|
}
|
||||||
|
|
||||||
if user.ID != uuid.Nil && user.LoginType != params.LoginType {
|
if user.ID != uuid.Nil && user.LoginType != params.LoginType {
|
||||||
return httpError{
|
return wrongLoginTypeHTTPError(user.LoginType, params.LoginType)
|
||||||
code: http.StatusForbidden,
|
|
||||||
msg: fmt.Sprintf("Incorrect login type, attempting to use %q but user is of login type %q",
|
|
||||||
params.LoginType,
|
|
||||||
user.LoginType,
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This can happen if a user is a built-in user but is signing in
|
// This can happen if a user is a built-in user but is signing in
|
||||||
@ -1355,13 +1363,7 @@ func (api *API) convertUserToOauth(ctx context.Context, r *http.Request, db data
|
|||||||
|
|
||||||
// If we do not allow converting to oauth, return an error.
|
// If we do not allow converting to oauth, return an error.
|
||||||
if !api.Experiments.Enabled(codersdk.ExperimentConvertToOIDC) {
|
if !api.Experiments.Enabled(codersdk.ExperimentConvertToOIDC) {
|
||||||
return database.User{}, httpError{
|
return database.User{}, wrongLoginTypeHTTPError(user.LoginType, params.LoginType)
|
||||||
code: http.StatusForbidden,
|
|
||||||
msg: fmt.Sprintf("Incorrect login type, attempting to use %q but user is of login type %q",
|
|
||||||
params.LoginType,
|
|
||||||
user.LoginType,
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if claims.RegisteredClaims.Issuer != api.DeploymentID {
|
if claims.RegisteredClaims.Issuer != api.DeploymentID {
|
||||||
@ -1487,3 +1489,17 @@ func clearOAuthConvertCookie() *http.Cookie {
|
|||||||
MaxAge: -1,
|
MaxAge: -1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func wrongLoginTypeHTTPError(user database.LoginType, params database.LoginType) httpError {
|
||||||
|
addedMsg := ""
|
||||||
|
if user == database.LoginTypePassword {
|
||||||
|
addedMsg = " You can convert your account to use this login type by visiting your account settings."
|
||||||
|
}
|
||||||
|
return httpError{
|
||||||
|
code: http.StatusForbidden,
|
||||||
|
renderStaticPage: true,
|
||||||
|
msg: "Incorrect login type",
|
||||||
|
detail: fmt.Sprintf("Attempting to use login type %q, but the user has the login type %q.%s",
|
||||||
|
params, user, addedMsg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user