Skip to content

Commit c0f0c3e

Browse files
committed
refactor: pass actual context to GetUserIdByContext
1 parent 49ee544 commit c0f0c3e

File tree

6 files changed

+97
-72
lines changed

6 files changed

+97
-72
lines changed

backend/pkg/api/handlers/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ func (h *HandlerService) GetUserIdByApiKey(r *http.Request) (uint64, error) {
197197
}
198198

199199
// if this is used, user ID should've been stored in context (by GetUserIdStoreMiddleware)
200-
func GetUserIdByContext(r *http.Request) (uint64, error) {
201-
userId, ok := r.Context().Value(types.CtxUserIdKey).(uint64)
200+
func GetUserIdByContext(ctx context.Context) (uint64, error) {
201+
userId, ok := ctx.Value(types.CtxUserIdKey).(uint64)
202202
if !ok {
203203
return 0, newUnauthorizedErr("user not authenticated")
204204
}

backend/pkg/api/handlers/handler_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func logApiError(r *http.Request, err error, callerSkip int, additionalInfos ...
338338
if body, _ := io.ReadAll(io.LimitReader(r.Body, maxBodySize)); len(body) > 0 {
339339
requestFields["request_body"] = string(body)
340340
}
341-
if userId, _ := GetUserIdByContext(r); userId != 0 {
341+
if userId, _ := GetUserIdByContext(r.Context()); userId != 0 {
342342
requestFields["request_user_id"] = userId
343343
}
344344
log.Error(err, "error handling request", callerSkip+1, append(additionalInfos, requestFields)...)

backend/pkg/api/handlers/internal.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,13 @@ func (h *HandlerService) InternalGetValidatorDashboardRocketPoolMinipools(w http
511511
// even though this endpoint is internal only, it should still not be broken since it is used by the mobile app
512512
func (h *HandlerService) InternalGetValidatorDashboardMobileWidget(w http.ResponseWriter, r *http.Request) {
513513
var v validationError
514+
ctx := r.Context()
514515
dashboardId := v.checkPrimaryDashboardId(mux.Vars(r)["dashboard_id"])
515516
if v.hasErrors() {
516517
handleErr(w, r, v)
517518
return
518519
}
519-
userId, err := GetUserIdByContext(r)
520+
userId, err := GetUserIdByContext(ctx)
520521
if err != nil {
521522
handleErr(w, r, err)
522523
return
@@ -530,7 +531,7 @@ func (h *HandlerService) InternalGetValidatorDashboardMobileWidget(w http.Respon
530531
returnForbidden(w, r, errors.New("user does not have access to mobile app widget"))
531532
return
532533
}
533-
data, err := h.daService.GetValidatorDashboardMobileWidget(r.Context(), dashboardId)
534+
data, err := h.daService.GetValidatorDashboardMobileWidget(ctx, dashboardId)
534535
if err != nil {
535536
handleErr(w, r, err)
536537
return

backend/pkg/api/handlers/machine_metrics.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ func (h *HandlerService) InternalGetUserMachineMetrics(w http.ResponseWriter, r
2323
}
2424

2525
func (h *HandlerService) PublicGetUserMachineMetrics(w http.ResponseWriter, r *http.Request) {
26+
ctx := r.Context()
2627
var v validationError
27-
userId, err := GetUserIdByContext(r)
28+
userId, err := GetUserIdByContext(ctx)
2829
if err != nil {
2930
handleErr(w, r, err)
3031
return
@@ -51,7 +52,7 @@ func (h *HandlerService) PublicGetUserMachineMetrics(w http.ResponseWriter, r *h
5152
offset = 0
5253
}
5354

54-
data, err := h.daService.GetUserMachineMetrics(r.Context(), userId, int(limit), int(offset))
55+
data, err := h.daService.GetUserMachineMetrics(ctx, userId, int(limit), int(offset))
5556
if err != nil {
5657
handleErr(w, r, err)
5758
return

backend/pkg/api/handlers/middlewares.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ func (h *HandlerService) VDBAuthMiddleware(next http.Handler) http.Handler {
6565
return
6666
}
6767
// primary id is used -> user needs to have access to dashboard
68-
69-
userId, err := GetUserIdByContext(r)
68+
ctx := r.Context()
69+
userId, err := GetUserIdByContext(ctx)
7070
if err != nil {
7171
handleErr(w, r, err)
7272
return
7373
}
74-
dashboardUser, err := h.daService.GetValidatorDashboardUser(r.Context(), types.VDBIdPrimary(dashboardId))
74+
dashboardUser, err := h.daService.GetValidatorDashboardUser(ctx, types.VDBIdPrimary(dashboardId))
7575
if err != nil {
7676
handleErr(w, r, err)
7777
return
@@ -92,14 +92,15 @@ func (h *HandlerService) VDBAuthMiddleware(next http.Handler) http.Handler {
9292
func (h *HandlerService) PremiumPerkCheckMiddleware(next http.Handler, hasRequiredPerk func(premiumPerks types.PremiumPerks) bool) http.Handler {
9393
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9494
// get user id from context
95-
userId, err := GetUserIdByContext(r)
95+
ctx := r.Context()
96+
userId, err := GetUserIdByContext(ctx)
9697
if err != nil {
9798
handleErr(w, r, err)
9899
return
99100
}
100101

101102
// get user info
102-
userInfo, err := h.daService.GetUserInfo(r.Context(), userId)
103+
userInfo, err := h.daService.GetUserInfo(ctx, userId)
103104
if err != nil {
104105
handleErr(w, r, err)
105106
return

0 commit comments

Comments
 (0)