Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fixes #33

goenning4194ac99f89b
diff --git a/app/context.go b/app/context.go
index 014ef158b..7a631e96f 100644
--- a/app/context.go
+++ b/app/context.go
@@ -54,7 +54,11 @@ func (ctx *Context) Page(dict echo.Map) error {
//Claims returns authenticated user claims
func (ctx *Context) Claims() *WechyClaims {
- return ctx.Get(claimsContextKey).(*WechyClaims)
+ claims, ok := ctx.Get(claimsContextKey).(*WechyClaims)
+ if ok {
+ return claims
+ }
+ return nil
}
//SetClaims update HTTP context with current claims
diff --git a/app/infra/middleware.go b/app/infra/middleware.go
index ba9b9f143..77d64f04d 100644
--- a/app/infra/middleware.go
+++ b/app/infra/middleware.go
@@ -11,8 +11,8 @@ import (
func IsAuthenticated() app.MiddlewareFunc {
return func(next app.HandlerFunc) app.HandlerFunc {
return func(c app.Context) error {
- _, err := c.Cookie("auth")
- if err != nil {
+ claims := c.Claims()
+ if claims == nil {
return c.NoContent(http.StatusForbidden)
}
return next(c)
@@ -27,7 +27,9 @@ func JwtGetter() app.MiddlewareFunc {
if cookie, err := c.Cookie("auth"); err == nil {
if claims, err := Decode(cookie.Value); err == nil {
- c.SetClaims(claims)
+ if claims.TenantID == c.Tenant().ID {
+ c.SetClaims(claims)
+ }
} else {
c.Logger().Error(err)
}
diff --git a/app/infra/middleware_test.go b/app/infra/middleware_test.go
index 5a8550759..255e17fb1 100644
--- a/app/infra/middleware_test.go
+++ b/app/infra/middleware_test.go
@@ -12,20 +12,15 @@ import (
. "github.com/onsi/gomega"
)
-func TestIsAuthenticated_WithToken(t *testing.T) {
+func TestIsAuthenticated_WithClaims(t *testing.T) {
RegisterTestingT(t)
- token, _ := infra.Encode(&app.WechyClaims{
- UserName: "Jon Snow",
- })
-
server := mock.NewServer()
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := server.NewContext(req, rec)
- c.Request().AddCookie(&http.Cookie{
- Name: "auth",
- Value: token,
+ c.SetClaims(&app.WechyClaims{
+ UserID: 1,
})
mw := infra.IsAuthenticated()
@@ -36,7 +31,7 @@ func TestIsAuthenticated_WithToken(t *testing.T) {
Expect(rec.Code).To(Equal(http.StatusOK))
}
-func TestIsAuthenticated_WithoutToken(t *testing.T) {
+func TestIsAuthenticated_WithoutClaims(t *testing.T) {
RegisterTestingT(t)
server := mock.NewServer()
@@ -77,12 +72,14 @@ func TestJwtGetter_WithCookie(t *testing.T) {
token, _ := infra.Encode(&app.WechyClaims{
UserName: "Jon Snow",
+ TenantID: 2,
})
server := mock.NewServer()
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := server.NewContext(req, rec)
+ c.SetTenant(&app.Tenant{ID: 2})
c.Request().AddCookie(&http.Cookie{
Name: "auth",
Value: token,
@@ -97,6 +94,35 @@ func TestJwtGetter_WithCookie(t *testing.T) {
Expect(rec.Body.String()).To(Equal("Jon Snow"))
}
+func TestJwtGetter_WithCookie_DifferentTenant(t *testing.T) {
+ RegisterTestingT(t)
+
+ token, _ := infra.Encode(&app.WechyClaims{
+ UserName: "Jon Snow",
+ TenantID: 3,
+ })
+
+ server := mock.NewServer()
+ req, _ := http.NewRequest(echo.GET, "/", nil)
+ rec := httptest.NewRecorder()
+ c := server.NewContext(req, rec)
+ c.SetTenant(&app.Tenant{ID: 2})
+ c.Request().AddCookie(&http.Cookie{
+ Name: "auth",
+ Value: token,
+ })
+
+ mw := infra.JwtGetter()
+ mw(func(c app.Context) error {
+ if c.Claims() == nil {
+ return c.NoContent(http.StatusNoContent)
+ }
+ return c.NoContent(http.StatusOK)
+ })(c)
+
+ Expect(rec.Code).To(Equal(http.StatusNoContent))
+}
+
func TestJwtSetter_WithoutJwt(t *testing.T) {
RegisterTestingT(t)
diff --git a/router.go b/router.go
index 6b7018450..57d938fcc 100644
--- a/router.go
+++ b/router.go
@@ -109,9 +109,9 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {
appGroup := group(router, "")
{
+ use(appGroup, identity.MultiTenant(ctx.Tenant))
use(appGroup, infra.JwtGetter())
use(appGroup, infra.JwtSetter())
- use(appGroup, identity.MultiTenant(ctx.Tenant))
get(appGroup, "/", feedback.Handlers(ctx.Idea).List())
get(appGroup, "/ideas/:id", feedback.Handlers(ctx.Idea).Details())
@@ -121,10 +121,10 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {
securedGroup := group(router, "")
{
- use(securedGroup, infra.IsAuthenticated())
+ use(securedGroup, identity.MultiTenant(ctx.Tenant))
use(securedGroup, infra.JwtGetter())
use(securedGroup, infra.JwtSetter())
- use(securedGroup, identity.MultiTenant(ctx.Tenant))
+ use(securedGroup, infra.IsAuthenticated())
post(securedGroup, "/api/ideas", feedback.Handlers(ctx.Idea).PostIdea())
post(securedGroup, "/api/ideas/:id/comments", feedback.Handlers(ctx.Idea).PostComment())