Open platform to collect and prioritize feedback
move jwt to its package
goenning4e95e7ebb1e2
diff --git a/app/handlers/identity.go b/app/handlers/identity.goindex e66503438..c03bd14c9 100644--- a/app/handlers/identity.go+++ b/app/handlers/identity.go@@ -7,8 +7,8 @@ import ("time""github.com/WeCanHearYou/wechy/app"- "github.com/WeCanHearYou/wechy/app/infra""github.com/WeCanHearYou/wechy/app/models"+ "github.com/WeCanHearYou/wechy/app/pkg/jwt""github.com/WeCanHearYou/wechy/app/pkg/oauth""github.com/WeCanHearYou/wechy/app/storage")@@ -96,7 +96,7 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {}var token string- if token, err = infra.Encode(claims); err != nil {+ if token, err = jwt.Encode(claims); err != nil {c.Logger().Errorf("Encoding claims failed with %s", err)return c.Failure(err)}diff --git a/app/middlewares/jwt.go b/app/middlewares/jwt.goindex 604e92bc9..532912613 100644--- a/app/middlewares/jwt.go+++ b/app/middlewares/jwt.go@@ -5,7 +5,7 @@ import ("strings""github.com/WeCanHearYou/wechy/app"- "github.com/WeCanHearYou/wechy/app/infra"+ "github.com/WeCanHearYou/wechy/app/pkg/jwt""github.com/WeCanHearYou/wechy/app/storage")@@ -32,7 +32,7 @@ func JwtGetter(users storage.User) app.MiddlewareFunc {return func(c app.Context) error {if cookie, err := c.Cookie("auth"); err == nil {- if claims, err := infra.Decode(cookie.Value); err == nil {+ if claims, err := jwt.Decode(cookie.Value); err == nil {if user, err := users.GetByID(claims.UserID); err == nil {if user.Tenant.ID == c.Tenant().ID {c.SetUser(user)diff --git a/app/middlewares/jwt_test.go b/app/middlewares/jwt_test.goindex 234ecf6b3..28b39fd51 100644--- a/app/middlewares/jwt_test.go+++ b/app/middlewares/jwt_test.go@@ -6,11 +6,11 @@ import ("testing""github.com/WeCanHearYou/wechy/app"- "github.com/WeCanHearYou/wechy/app/infra""github.com/WeCanHearYou/wechy/app/middlewares""github.com/WeCanHearYou/wechy/app/mock""github.com/WeCanHearYou/wechy/app/models""github.com/WeCanHearYou/wechy/app/pkg/dbx"+ "github.com/WeCanHearYou/wechy/app/pkg/jwt""github.com/WeCanHearYou/wechy/app/storage/postgres""github.com/labstack/echo". "github.com/onsi/gomega"@@ -129,7 +129,7 @@ func TestJwtGetter_WithCookie(t *testing.T) {RegisterTestingT(t)setup()- token, _ := infra.Encode(&models.WechyClaims{+ token, _ := jwt.Encode(&models.WechyClaims{UserID: 300,UserName: "Jon Snow",})@@ -157,7 +157,7 @@ func TestJwtGetter_WithCookie_DifferentTenant(t *testing.T) {RegisterTestingT(t)setup()- token, _ := infra.Encode(&models.WechyClaims{+ token, _ := jwt.Encode(&models.WechyClaims{UserID: 300,UserName: "Jon Snow",})@@ -203,7 +203,7 @@ func TestJwtSetter_WithoutJwt(t *testing.T) {func TestJwtSetter_WithJwt_WithoutParameter(t *testing.T) {RegisterTestingT(t)- token, _ := infra.Encode(&models.WechyClaims{+ token, _ := jwt.Encode(&models.WechyClaims{UserName: "Jon Snow",})@@ -225,7 +225,7 @@ func TestJwtSetter_WithJwt_WithoutParameter(t *testing.T) {func TestJwtSetter_WithJwt_WithParameter(t *testing.T) {RegisterTestingT(t)- token, _ := infra.Encode(&models.WechyClaims{+ token, _ := jwt.Encode(&models.WechyClaims{UserName: "Jon Snow",})diff --git a/app/infra/jwt.go b/app/pkg/jwt/jwt.gosimilarity index 71%rename from app/infra/jwt.gorename to app/pkg/jwt/jwt.goindex 865ec121d..c3c5fc946 100644--- a/app/infra/jwt.go+++ b/app/pkg/jwt/jwt.go@@ -1,23 +1,23 @@-package infra+package jwtimport ("github.com/WeCanHearYou/wechy/app/models""github.com/WeCanHearYou/wechy/app/pkg/env"- jwt "github.com/dgrijalva/jwt-go"+ jwtgo "github.com/dgrijalva/jwt-go")var jwtSecret = env.MustGet("JWT_SECRET")//Encode creates new JWT tokens with given claimsfunc Encode(claims *models.WechyClaims) (string, error) {- jwtToken := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), claims)+ jwtToken := jwtgo.NewWithClaims(jwtgo.GetSigningMethod("HS256"), claims)return jwtToken.SignedString([]byte(jwtSecret))}//Decode extract claims from JWT tokensfunc Decode(token string) (*models.WechyClaims, error) {claims := &models.WechyClaims{}- jwtToken, err := jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (interface{}, error) {+ jwtToken, err := jwtgo.ParseWithClaims(token, claims, func(t *jwtgo.Token) (interface{}, error) {return []byte(jwtSecret), nil})diff --git a/app/infra/jwt_test.go b/app/pkg/jwt/jwt_test.gosimilarity index 80%rename from app/infra/jwt_test.gorename to app/pkg/jwt/jwt_test.goindex a78598eaf..cbbe2b502 100644--- a/app/infra/jwt_test.go+++ b/app/pkg/jwt/jwt_test.go@@ -1,10 +1,10 @@-package infra_test+package jwt_testimport ("testing"- "github.com/WeCanHearYou/wechy/app/infra""github.com/WeCanHearYou/wechy/app/models"+ "github.com/WeCanHearYou/wechy/app/pkg/jwt". "github.com/onsi/gomega")@@ -17,7 +17,7 @@ func TestJWT_Encode(t *testing.T) {UserEmail: "[email protected]",}- token, err := infra.Encode(claims)+ token, err := jwt.Encode(claims)Expect(token).NotTo(BeNil())Expect(err).To(BeNil())}@@ -31,9 +31,9 @@ func TestJWT_Decode(t *testing.T) {UserEmail: "[email protected]",}- token, _ := infra.Encode(claims)+ token, _ := jwt.Encode(claims)- decoded, err := infra.Decode(token)+ decoded, err := jwt.Decode(token)Expect(err).To(BeNil())Expect(decoded.UserID).To(Equal(claims.UserID))Expect(decoded.UserName).To(Equal(claims.UserName))