@@ -32,6 +32,7 @@ import (
32
32
"github.com/coder/coder/coderd/userpassword"
33
33
"github.com/coder/coder/codersdk"
34
34
"github.com/coder/coder/cryptorand"
35
+ "github.com/coder/coder/site"
35
36
)
36
37
37
38
const (
@@ -625,10 +626,7 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
625
626
defer params .CommitAuditLogs ()
626
627
var httpErr httpError
627
628
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 )
632
630
return
633
631
}
634
632
if err != nil {
@@ -969,10 +967,7 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
969
967
defer params .CommitAuditLogs ()
970
968
var httpErr httpError
971
969
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 )
976
971
return
977
972
}
978
973
if err != nil {
@@ -1076,9 +1071,28 @@ func (p *oauthLoginParams) CommitAuditLogs() {
1076
1071
}
1077
1072
1078
1073
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
+ })
1082
1096
}
1083
1097
1084
1098
func (e httpError ) Error () string {
@@ -1126,13 +1140,7 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
1126
1140
}
1127
1141
1128
1142
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 )
1136
1144
}
1137
1145
1138
1146
// 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
1355
1363
1356
1364
// If we do not allow converting to oauth, return an error.
1357
1365
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 )
1365
1367
}
1366
1368
1367
1369
if claims .RegisteredClaims .Issuer != api .DeploymentID {
@@ -1487,3 +1489,17 @@ func clearOAuthConvertCookie() *http.Cookie {
1487
1489
MaxAge : - 1 ,
1488
1490
}
1489
1491
}
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