Skip to content

Commit 019cb00

Browse files
committed
dedup code
1 parent e7ff51f commit 019cb00

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

coderd/userauth.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,17 +1140,7 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
11401140
}
11411141

11421142
if user.ID != uuid.Nil && user.LoginType != params.LoginType {
1143-
addedMsg := ""
1144-
if user.LoginType == database.LoginTypePassword {
1145-
addedMsg = " You can convert your account to use this login type by visiting your account settings."
1146-
}
1147-
return httpError{
1148-
code: http.StatusForbidden,
1149-
renderStaticPage: true,
1150-
msg: "Incorrect login type",
1151-
detail: fmt.Sprintf("Attempting to use login type %q, but the user has the login type %q.%s",
1152-
params.LoginType, user.LoginType, addedMsg),
1153-
}
1143+
return wrongLoginTypeHTTPError(user.LoginType, params.LoginType)
11541144
}
11551145

11561146
// This can happen if a user is a built-in user but is signing in
@@ -1373,17 +1363,7 @@ func (api *API) convertUserToOauth(ctx context.Context, r *http.Request, db data
13731363

13741364
// If we do not allow converting to oauth, return an error.
13751365
if !api.Experiments.Enabled(codersdk.ExperimentConvertToOIDC) {
1376-
addedMsg := ""
1377-
if user.LoginType == database.LoginTypePassword {
1378-
addedMsg = " You can convert your account to use this login type by visiting your account settings."
1379-
}
1380-
return database.User{}, httpError{
1381-
code: http.StatusForbidden,
1382-
renderStaticPage: true,
1383-
msg: "Incorrect login type",
1384-
detail: fmt.Sprintf("Attempting to use login type %q, but the user has the login type %q.%s",
1385-
params.LoginType, user.LoginType, addedMsg),
1386-
}
1366+
return database.User{}, wrongLoginTypeHTTPError(user.LoginType, params.LoginType)
13871367
}
13881368

13891369
if claims.RegisteredClaims.Issuer != api.DeploymentID {
@@ -1509,3 +1489,17 @@ func clearOAuthConvertCookie() *http.Cookie {
15091489
MaxAge: -1,
15101490
}
15111491
}
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)