From 6cb493b4a126af251db96416275c3ad5ed4f8e16 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 17 Sep 2023 16:21:11 +0800 Subject: [PATCH] chore: rename context name --- api/v1/jwt.go | 4 ++-- api/v1/redirector.go | 2 +- api/v1/shortcut.go | 8 ++++---- api/v1/user.go | 8 ++++---- api/v2/acl.go | 4 ++-- api/v2/shortcut_service.go | 8 ++++---- api/v2/user_service.go | 10 +++++----- api/v2/user_setting_service.go | 2 +- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/api/v1/jwt.go b/api/v1/jwt.go index a1b77b6c..da2a643c 100644 --- a/api/v1/jwt.go +++ b/api/v1/jwt.go @@ -17,7 +17,7 @@ import ( const ( // The key name used to store user id in the context // user id is extracted from the jwt token subject field. - UserIDContextKey = "user-id" + userIDContextKey = "user-id" ) func extractTokenFromHeader(c echo.Context) (string, error) { @@ -122,7 +122,7 @@ func JWTMiddleware(s *APIV1Service, next echo.HandlerFunc, secret string) echo.H } // Stores userID into context. - c.Set(UserIDContextKey, userID) + c.Set(userIDContextKey, userID) return next(c) } } diff --git a/api/v1/redirector.go b/api/v1/redirector.go index ce659dd1..829ab3cb 100644 --- a/api/v1/redirector.go +++ b/api/v1/redirector.go @@ -32,7 +32,7 @@ func (s *APIV1Service) registerRedirectorRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found shortcut with name: %s", shortcutName)) } if shortcut.Visibility != storepb.Visibility_PUBLIC { - userID, ok := c.Get(UserIDContextKey).(int32) + userID, ok := c.Get(userIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") } diff --git a/api/v1/shortcut.go b/api/v1/shortcut.go index 8fe16882..564f27d3 100644 --- a/api/v1/shortcut.go +++ b/api/v1/shortcut.go @@ -81,7 +81,7 @@ type PatchShortcutRequest struct { func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { g.POST("/shortcut", func(c echo.Context) error { ctx := c.Request().Context() - userID, ok := c.Get(UserIDContextKey).(int32) + userID, ok := c.Get(userIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } @@ -129,7 +129,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("shortcut ID is not a number: %s", c.Param("shortcutId"))).SetInternal(err) } - userID, ok := c.Get(UserIDContextKey).(int32) + userID, ok := c.Get(userIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } @@ -196,7 +196,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { g.GET("/shortcut", func(c echo.Context) error { ctx := c.Request().Context() - userID, ok := c.Get(UserIDContextKey).(int32) + userID, ok := c.Get(userIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } @@ -263,7 +263,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("shortcut id is not a number: %s", c.Param("id"))).SetInternal(err) } - userID, ok := c.Get(UserIDContextKey).(int32) + userID, ok := c.Get(userIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } diff --git a/api/v1/user.go b/api/v1/user.go index a0ae3b81..4d6101ff 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -83,7 +83,7 @@ type PatchUserRequest struct { func (s *APIV1Service) registerUserRoutes(g *echo.Group) { g.POST("/user", func(c echo.Context) error { ctx := c.Request().Context() - userID, ok := c.Get(UserIDContextKey).(int32) + userID, ok := c.Get(userIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "Missing auth session") } @@ -144,7 +144,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { // GET /api/user/me is used to check if the user is logged in. g.GET("/user/me", func(c echo.Context) error { ctx := c.Request().Context() - userID, ok := c.Get(UserIDContextKey).(int32) + userID, ok := c.Get(userIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing auth session") } @@ -182,7 +182,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("user id is not a number: %s", c.Param("id"))).SetInternal(err) } - currentUserID, ok := c.Get(UserIDContextKey).(int32) + currentUserID, ok := c.Get(userIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } @@ -254,7 +254,7 @@ func (s *APIV1Service) registerUserRoutes(g *echo.Group) { g.DELETE("/user/:id", func(c echo.Context) error { ctx := c.Request().Context() - currentUserID, ok := c.Get(UserIDContextKey).(int32) + currentUserID, ok := c.Get(userIDContextKey).(int32) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } diff --git a/api/v2/acl.go b/api/v2/acl.go index 030be4ec..4eaee303 100644 --- a/api/v2/acl.go +++ b/api/v2/acl.go @@ -23,7 +23,7 @@ type ContextKey int const ( // The key name used to store user id in the context // user id is extracted from the jwt token subject field. - UserIDContextKey ContextKey = iota + userIDContextKey ContextKey = iota ) // GRPCAuthInterceptor is the auth interceptor for gRPC server. @@ -80,7 +80,7 @@ func (in *GRPCAuthInterceptor) AuthenticationInterceptor(ctx context.Context, re } // Stores userID into context. - childCtx := context.WithValue(ctx, UserIDContextKey, userID) + childCtx := context.WithValue(ctx, userIDContextKey, userID) return handler(childCtx, request) } diff --git a/api/v2/shortcut_service.go b/api/v2/shortcut_service.go index 7365ca0a..39481f32 100644 --- a/api/v2/shortcut_service.go +++ b/api/v2/shortcut_service.go @@ -28,7 +28,7 @@ func NewShortcutService(secret string, store *store.Store) *ShortcutService { } func (s *ShortcutService) ListShortcuts(ctx context.Context, _ *apiv2pb.ListShortcutsRequest) (*apiv2pb.ListShortcutsResponse, error) { - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) find := &store.FindShortcut{} find.VisibilityList = []store.Visibility{store.VisibilityWorkspace, store.VisibilityPublic} visibleShortcutList, err := s.Store.ListShortcuts(ctx, find) @@ -66,7 +66,7 @@ func (s *ShortcutService) GetShortcut(ctx context.Context, request *apiv2pb.GetS return nil, status.Errorf(codes.NotFound, "shortcut not found") } - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) if shortcut.Visibility == storepb.Visibility_PRIVATE && shortcut.CreatorId != userID { return nil, status.Errorf(codes.PermissionDenied, "Permission denied") } @@ -78,7 +78,7 @@ func (s *ShortcutService) GetShortcut(ctx context.Context, request *apiv2pb.GetS } func (s *ShortcutService) CreateShortcut(ctx context.Context, request *apiv2pb.CreateShortcutRequest) (*apiv2pb.CreateShortcutResponse, error) { - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) shortcut := &storepb.Shortcut{ CreatorId: userID, Name: request.Shortcut.Name, @@ -111,7 +111,7 @@ func (s *ShortcutService) CreateShortcut(ctx context.Context, request *apiv2pb.C } func (s *ShortcutService) DeleteShortcut(ctx context.Context, request *apiv2pb.DeleteShortcutRequest) (*apiv2pb.DeleteShortcutResponse, error) { - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) currentUser, err := s.Store.GetUser(ctx, &store.FindUser{ ID: &userID, }) diff --git a/api/v2/user_service.go b/api/v2/user_service.go index 5f6c0321..18963951 100644 --- a/api/v2/user_service.go +++ b/api/v2/user_service.go @@ -87,7 +87,7 @@ func (s *UserService) CreateUser(ctx context.Context, request *apiv2pb.CreateUse } func (s *UserService) UpdateUser(ctx context.Context, request *apiv2pb.UpdateUserRequest) (*apiv2pb.UpdateUserResponse, error) { - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) if userID != request.User.Id { return nil, status.Errorf(codes.PermissionDenied, "Permission denied") } @@ -115,7 +115,7 @@ func (s *UserService) UpdateUser(ctx context.Context, request *apiv2pb.UpdateUse } func (s *UserService) DeleteUser(ctx context.Context, request *apiv2pb.DeleteUserRequest) (*apiv2pb.DeleteUserResponse, error) { - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) if userID == request.Id { return nil, status.Errorf(codes.InvalidArgument, "cannot delete yourself") } @@ -131,7 +131,7 @@ func (s *UserService) DeleteUser(ctx context.Context, request *apiv2pb.DeleteUse } func (s *UserService) ListUserAccessTokens(ctx context.Context, request *apiv2pb.ListUserAccessTokensRequest) (*apiv2pb.ListUserAccessTokensResponse, error) { - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) if userID != request.Id { return nil, status.Errorf(codes.PermissionDenied, "Permission denied") } @@ -182,7 +182,7 @@ func (s *UserService) ListUserAccessTokens(ctx context.Context, request *apiv2pb } func (s *UserService) CreateUserAccessToken(ctx context.Context, request *apiv2pb.CreateUserAccessTokenRequest) (*apiv2pb.CreateUserAccessTokenResponse, error) { - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) if userID != request.Id { return nil, status.Errorf(codes.PermissionDenied, "Permission denied") } @@ -238,7 +238,7 @@ func (s *UserService) CreateUserAccessToken(ctx context.Context, request *apiv2p } func (s *UserService) DeleteUserAccessToken(ctx context.Context, request *apiv2pb.DeleteUserAccessTokenRequest) (*apiv2pb.DeleteUserAccessTokenResponse, error) { - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) if userID != request.Id { return nil, status.Errorf(codes.PermissionDenied, "Permission denied") } diff --git a/api/v2/user_setting_service.go b/api/v2/user_setting_service.go index 3d5bb4bc..0a7dad9b 100644 --- a/api/v2/user_setting_service.go +++ b/api/v2/user_setting_service.go @@ -39,7 +39,7 @@ func (s *UserSettingService) UpdateUserSetting(ctx context.Context, request *api return nil, status.Errorf(codes.InvalidArgument, "update mask is empty") } - userID := ctx.Value(UserIDContextKey).(int32) + userID := ctx.Value(userIDContextKey).(int32) for _, path := range request.UpdateMask { if path == "locale" { if _, err := s.Store.UpsertUserSetting(ctx, &storepb.UserSetting{