Skip to content

Commit a297a01

Browse files
authored
chore: improve error message for incorrect login type (#8349)
* chore: add better error in wrong login type
1 parent c1ab5cf commit a297a01

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

coderd/userauth.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/coder/coder/coderd/userpassword"
3333
"github.com/coder/coder/codersdk"
3434
"github.com/coder/coder/cryptorand"
35+
"github.com/coder/coder/site"
3536
)
3637

3738
const (
@@ -625,10 +626,7 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
625626
defer params.CommitAuditLogs()
626627
var httpErr httpError
627628
if xerrors.As(err, &httpErr) {
628-
httpapi.Write(ctx, rw, httpErr.code, codersdk.Response{
629-
Message: httpErr.msg,
630-
Detail: httpErr.detail,
631-
})
629+
httpErr.Write(rw, r)
632630
return
633631
}
634632
if err != nil {
@@ -969,10 +967,7 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
969967
defer params.CommitAuditLogs()
970968
var httpErr httpError
971969
if xerrors.As(err, &httpErr) {
972-
httpapi.Write(ctx, rw, httpErr.code, codersdk.Response{
973-
Message: httpErr.msg,
974-
Detail: httpErr.detail,
975-
})
970+
httpErr.Write(rw, r)
976971
return
977972
}
978973
if err != nil {
@@ -1076,9 +1071,28 @@ func (p *oauthLoginParams) CommitAuditLogs() {
10761071
}
10771072

10781073
type httpError struct {
1079-
code int
1080-
msg string
1081-
detail string
1074+
code int
1075+
msg string
1076+
detail string
1077+
renderStaticPage bool
1078+
}
1079+
1080+
func (e httpError) Write(rw http.ResponseWriter, r *http.Request) {
1081+
if e.renderStaticPage {
1082+
site.RenderStaticErrorPage(rw, r, site.ErrorPageData{
1083+
Status: e.code,
1084+
HideStatus: true,
1085+
Title: e.msg,
1086+
Description: e.detail,
1087+
RetryEnabled: false,
1088+
DashboardURL: "/login",
1089+
})
1090+
return
1091+
}
1092+
httpapi.Write(r.Context(), rw, e.code, codersdk.Response{
1093+
Message: e.msg,
1094+
Detail: e.detail,
1095+
})
10821096
}
10831097

10841098
func (e httpError) Error() string {
@@ -1126,13 +1140,7 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
11261140
}
11271141

11281142
if user.ID != uuid.Nil && user.LoginType != params.LoginType {
1129-
return httpError{
1130-
code: http.StatusForbidden,
1131-
msg: fmt.Sprintf("Incorrect login type, attempting to use %q but user is of login type %q",
1132-
params.LoginType,
1133-
user.LoginType,
1134-
),
1135-
}
1143+
return wrongLoginTypeHTTPError(user.LoginType, params.LoginType)
11361144
}
11371145

11381146
// 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
13551363

13561364
// If we do not allow converting to oauth, return an error.
13571365
if !api.Experiments.Enabled(codersdk.ExperimentConvertToOIDC) {
1358-
return database.User{}, httpError{
1359-
code: http.StatusForbidden,
1360-
msg: fmt.Sprintf("Incorrect login type, attempting to use %q but user is of login type %q",
1361-
params.LoginType,
1362-
user.LoginType,
1363-
),
1364-
}
1366+
return database.User{}, wrongLoginTypeHTTPError(user.LoginType, params.LoginType)
13651367
}
13661368

13671369
if claims.RegisteredClaims.Issuer != api.DeploymentID {
@@ -1487,3 +1489,17 @@ func clearOAuthConvertCookie() *http.Cookie {
14871489
MaxAge: -1,
14881490
}
14891491
}
1492+
1493+
func wrongLoginTypeHTTPError(user database.LoginType, params database.LoginType) httpError {
1494+
addedMsg := ""
1495+
if user == database.LoginTypePassword {
1496+
addedMsg = " You can convert your account to use this login type by visiting your account settings."
1497+
}
1498+
return httpError{
1499+
code: http.StatusForbidden,
1500+
renderStaticPage: true,
1501+
msg: "Incorrect login type",
1502+
detail: fmt.Sprintf("Attempting to use login type %q, but the user has the login type %q.%s",
1503+
params, user, addedMsg),
1504+
}
1505+
}

0 commit comments

Comments
 (0)