From c26834e9cdb1e291b97820c442b54ee8df881cc9 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 2 Aug 2023 07:44:04 +0800 Subject: [PATCH] chore: update api v1 user context name --- api/auth/auth.go | 3 --- api/v1/jwt.go | 8 +++++++- api/v1/redirector.go | 3 +-- api/v1/shortcut.go | 9 ++++----- api/v1/user.go | 9 ++++----- api/v1/workspace.go | 5 ++--- api/v2/jwt.go | 4 ++-- 7 files changed, 20 insertions(+), 21 deletions(-) diff --git a/api/auth/auth.go b/api/auth/auth.go index 3330481..7517ad4 100644 --- a/api/auth/auth.go +++ b/api/auth/auth.go @@ -5,9 +5,6 @@ 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" // issuer is the issuer of the jwt token. Issuer = "slash" // Signing key section. For now, this is only used for signing, not for verifying since we only diff --git a/api/v1/jwt.go b/api/v1/jwt.go index c17ddc4..64c0daf 100644 --- a/api/v1/jwt.go +++ b/api/v1/jwt.go @@ -15,6 +15,12 @@ import ( "github.com/pkg/errors" ) +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" +) + type claimsMessage struct { Name string `json:"name"` jwt.RegisteredClaims @@ -183,7 +189,7 @@ func JWTMiddleware(server *APIV1Service, next echo.HandlerFunc, secret string) e } // Stores userID into context. - c.Set(auth.UserIDContextKey, userID) + c.Set(UserIDContextKey, userID) return next(c) } } diff --git a/api/v1/redirector.go b/api/v1/redirector.go index 6ea4c00..fcfb435 100644 --- a/api/v1/redirector.go +++ b/api/v1/redirector.go @@ -8,7 +8,6 @@ import ( "net/url" "strings" - "github.com/boojack/slash/api/auth" "github.com/boojack/slash/store" "github.com/labstack/echo/v4" "github.com/pkg/errors" @@ -32,7 +31,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 != store.VisibilityPublic { - userID, ok := c.Get(auth.UserIDContextKey).(int) + userID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") } diff --git a/api/v1/shortcut.go b/api/v1/shortcut.go index 793ba24..5a66492 100644 --- a/api/v1/shortcut.go +++ b/api/v1/shortcut.go @@ -8,7 +8,6 @@ import ( "strconv" "strings" - "github.com/boojack/slash/api/auth" "github.com/boojack/slash/store" "github.com/labstack/echo/v4" "github.com/pkg/errors" @@ -81,7 +80,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(auth.UserIDContextKey).(int) + userID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } @@ -125,7 +124,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(auth.UserIDContextKey).(int) + userID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } @@ -196,7 +195,7 @@ func (s *APIV1Service) registerShortcutRoutes(g *echo.Group) { g.GET("/shortcut", func(c echo.Context) error { ctx := c.Request().Context() - userID, ok := c.Get(auth.UserIDContextKey).(int) + userID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } @@ -263,7 +262,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(auth.UserIDContextKey).(int) + userID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } diff --git a/api/v1/user.go b/api/v1/user.go index 16b98be..67d1f2c 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -7,7 +7,6 @@ import ( "net/mail" "strconv" - "github.com/boojack/slash/api/auth" "github.com/boojack/slash/store" "github.com/labstack/echo/v4" "golang.org/x/crypto/bcrypt" @@ -84,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(auth.UserIDContextKey).(int) + userID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "Missing auth session") } @@ -145,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(auth.UserIDContextKey).(int) + userID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing auth session") } @@ -183,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(auth.UserIDContextKey).(int) + currentUserID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } @@ -255,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(auth.UserIDContextKey).(int) + currentUserID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } diff --git a/api/v1/workspace.go b/api/v1/workspace.go index 5c06df3..815211c 100644 --- a/api/v1/workspace.go +++ b/api/v1/workspace.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" - "github.com/boojack/slash/api/auth" "github.com/boojack/slash/server/profile" "github.com/boojack/slash/store" "github.com/labstack/echo/v4" @@ -63,7 +62,7 @@ func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) { g.POST("/workspace/setting", func(c echo.Context) error { ctx := c.Request().Context() - userID, ok := c.Get(auth.UserIDContextKey).(int) + userID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } @@ -98,7 +97,7 @@ func (s *APIV1Service) registerWorkspaceRoutes(g *echo.Group) { g.GET("/workspace/setting", func(c echo.Context) error { ctx := c.Request().Context() - userID, ok := c.Get(auth.UserIDContextKey).(int) + userID, ok := c.Get(UserIDContextKey).(int) if !ok { return echo.NewHTTPError(http.StatusUnauthorized, "missing user in session") } diff --git a/api/v2/jwt.go b/api/v2/jwt.go index 0153178..7399f36 100644 --- a/api/v2/jwt.go +++ b/api/v2/jwt.go @@ -7,10 +7,10 @@ import ( "strings" "time" - "github.com/golang-jwt/jwt/v4" - "github.com/pkg/errors" "github.com/boojack/slash/api/auth" "github.com/boojack/slash/store" + "github.com/golang-jwt/jwt/v4" + "github.com/pkg/errors" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata"