Skip to content

Commit 2947b82

Browse files
authored
chore: use httpError to allow better error elevation (#11065)
1 parent dd01bde commit 2947b82

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

coderd/userauth.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -925,18 +925,15 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
925925
picture, _ = pictureRaw.(string)
926926
}
927927

928-
usingGroups, groups, err := api.oidcGroups(ctx, mergedClaims)
929-
if err != nil {
930-
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
931-
Message: "Failed to sync groups from OIDC claims",
932-
Detail: err.Error(),
933-
})
928+
usingGroups, groups, groupErr := api.oidcGroups(ctx, mergedClaims)
929+
if groupErr != nil {
930+
groupErr.Write(rw, r)
934931
return
935932
}
936933

937-
roles, ok := api.oidcRoles(ctx, rw, r, mergedClaims)
938-
if !ok {
939-
// oidcRoles writes the error to the response writer for us.
934+
roles, roleErr := api.oidcRoles(ctx, mergedClaims)
935+
if roleErr != nil {
936+
roleErr.Write(rw, r)
940937
return
941938
}
942939

@@ -1009,7 +1006,7 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) {
10091006
}
10101007

10111008
// oidcGroups returns the groups for the user from the OIDC claims.
1012-
func (api *API) oidcGroups(ctx context.Context, mergedClaims map[string]interface{}) (bool, []string, error) {
1009+
func (api *API) oidcGroups(ctx context.Context, mergedClaims map[string]interface{}) (bool, []string, *httpError) {
10131010
logger := api.Logger.Named(userAuthLoggerName)
10141011
usingGroups := false
10151012
var groups []string
@@ -1026,7 +1023,12 @@ func (api *API) oidcGroups(ctx context.Context, mergedClaims map[string]interfac
10261023
slog.F("type", fmt.Sprintf("%T", groupsRaw)),
10271024
slog.Error(err),
10281025
)
1029-
return false, nil, err
1026+
return false, nil, &httpError{
1027+
code: http.StatusBadRequest,
1028+
msg: "Failed to sync groups from OIDC claims",
1029+
detail: err.Error(),
1030+
renderStaticPage: false,
1031+
}
10301032
}
10311033

10321034
api.Logger.Debug(ctx, "groups returned in oidc claims",
@@ -1058,10 +1060,10 @@ func (api *API) oidcGroups(ctx context.Context, mergedClaims map[string]interfac
10581060
// It would be preferred to just return an error, however this function
10591061
// decorates returned errors with the appropriate HTTP status codes and details
10601062
// that are hard to carry in a standard `error` without more work.
1061-
func (api *API) oidcRoles(ctx context.Context, rw http.ResponseWriter, r *http.Request, mergedClaims map[string]interface{}) ([]string, bool) {
1063+
func (api *API) oidcRoles(ctx context.Context, mergedClaims map[string]interface{}) ([]string, *httpError) {
10621064
roles := api.OIDCConfig.UserRolesDefault
10631065
if !api.OIDCConfig.RoleSyncEnabled() {
1064-
return roles, true
1066+
return roles, nil
10651067
}
10661068

10671069
rolesRow, ok := mergedClaims[api.OIDCConfig.UserRoleField]
@@ -1080,15 +1082,12 @@ func (api *API) oidcRoles(ctx context.Context, rw http.ResponseWriter, r *http.R
10801082
slog.F("type", fmt.Sprintf("%T", rolesRow)),
10811083
slog.Error(err),
10821084
)
1083-
site.RenderStaticErrorPage(rw, r, site.ErrorPageData{
1084-
Status: http.StatusInternalServerError,
1085-
HideStatus: true,
1086-
Title: "Login disabled until OIDC config is fixed",
1087-
Description: fmt.Sprintf("Roles claim must be an array of strings, type found: %T. Disabling role sync will allow login to proceed.", rolesRow),
1088-
RetryEnabled: false,
1089-
DashboardURL: "/login",
1090-
})
1091-
return nil, false
1085+
return nil, &httpError{
1086+
code: http.StatusInternalServerError,
1087+
msg: "Login disabled until OIDC config is fixed",
1088+
detail: fmt.Sprintf("Roles claim must be an array of strings, type found: %T. Disabling role sync will allow login to proceed.", rolesRow),
1089+
renderStaticPage: false,
1090+
}
10921091
}
10931092

10941093
api.Logger.Debug(ctx, "roles returned in oidc claims",
@@ -1107,7 +1106,7 @@ func (api *API) oidcRoles(ctx context.Context, rw http.ResponseWriter, r *http.R
11071106

11081107
roles = append(roles, role)
11091108
}
1110-
return roles, true
1109+
return roles, nil
11111110
}
11121111

11131112
// claimFields returns the sorted list of fields in the claims map.

0 commit comments

Comments
 (0)