refactor: extract request context utilities into dedicated package
Move ClaimsFromContext and MustUserID helpers from httpserver to new requestctx package for better separation of concerns. Update all imports across auth, device, policy, and user handlers. Fix Dockerfile to copy go.sum and run go mod tidy before download.
This commit is contained in:
30
backend/internal/requestctx/context.go
Normal file
30
backend/internal/requestctx/context.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package requestctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/nexavpn/nexavpn/backend/internal/auth"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const claimsKey contextKey = "claims"
|
||||
|
||||
func WithClaims(ctx context.Context, claims auth.Claims) context.Context {
|
||||
return context.WithValue(ctx, claimsKey, claims)
|
||||
}
|
||||
|
||||
func ClaimsFromContext(ctx context.Context) (auth.Claims, bool) {
|
||||
claims, ok := ctx.Value(claimsKey).(auth.Claims)
|
||||
return claims, ok
|
||||
}
|
||||
|
||||
func MustUserID(ctx context.Context) (uuid.UUID, bool) {
|
||||
claims, ok := ClaimsFromContext(ctx)
|
||||
if !ok {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
return claims.UserID, true
|
||||
}
|
||||
Reference in New Issue
Block a user