Skip to content

Commit

Permalink
chore: rename context name
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Sep 17, 2023
1 parent 75d1529 commit 6cb493b
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions api/v1/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1/redirector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions api/v1/shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions api/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions api/v2/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions api/v2/shortcut_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
})
Expand Down
10 changes: 5 additions & 5 deletions api/v2/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion api/v2/user_setting_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 6cb493b

Please sign in to comment.