Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

moved middlewares to its own package

goenningcb96867e9121
diff --git a/app/infra/middleware.go b/app/middlewares/auth.go
similarity index 98%
rename from app/infra/middleware.go
rename to app/middlewares/auth.go
index 66a5ede3f..a10dbca70 100644
--- a/app/infra/middleware.go
+++ b/app/middlewares/auth.go
@@ -1,4 +1,4 @@
-package infra
+package middlewares
import (
"net/http"
diff --git a/app/infra/middleware_test.go b/app/middlewares/auth_test.go
similarity index 85%
rename from app/infra/middleware_test.go
rename to app/middlewares/auth_test.go
index e54388743..961ca67ea 100644
--- a/app/infra/middleware_test.go
+++ b/app/middlewares/auth_test.go
@@ -1,4 +1,4 @@
-package infra_test
+package middlewares_test
import (
"net/http"
@@ -6,7 +6,7 @@ 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/labstack/echo"
@@ -25,7 +25,7 @@ func TestIsAuthorized_WithAllowedRole(t *testing.T) {
Role: models.RoleMember,
})
- mw := infra.IsAuthorized(models.RoleAdministrator, models.RoleMember)
+ mw := middlewares.IsAuthorized(models.RoleAdministrator, models.RoleMember)
mw(func(c app.Context) error {
return c.NoContent(http.StatusOK)
})(c)
@@ -45,7 +45,7 @@ func TestIsAuthorized_WithForbiddenRole(t *testing.T) {
Role: models.RoleVisitor,
})
- mw := infra.IsAuthorized(models.RoleAdministrator, models.RoleMember)
+ mw := middlewares.IsAuthorized(models.RoleAdministrator, models.RoleMember)
mw(func(c app.Context) error {
return c.NoContent(http.StatusOK)
})(c)
@@ -64,7 +64,7 @@ func TestIsAuthenticated_WithUser(t *testing.T) {
ID: 1,
})
- mw := infra.IsAuthenticated()
+ mw := middlewares.IsAuthenticated()
mw(func(c app.Context) error {
return c.NoContent(http.StatusOK)
})(c)
@@ -80,7 +80,7 @@ func TestIsAuthenticated_WithoutUser(t *testing.T) {
rec := httptest.NewRecorder()
c := server.NewContext(req, rec)
- mw := infra.IsAuthenticated()
+ mw := middlewares.IsAuthenticated()
mw(func(c app.Context) error {
return c.NoContent(http.StatusOK)
})(c)
@@ -97,7 +97,7 @@ func TestHostChecker(t *testing.T) {
c := server.NewContext(req, rec)
c.Request().Host = "login.test.canhearyou.com"
- mw := infra.HostChecker("http://login.test.canhearyou.com")
+ mw := middlewares.HostChecker("http://login.test.canhearyou.com")
mw(func(c app.Context) error {
return c.NoContent(http.StatusOK)
})(c)
@@ -114,7 +114,7 @@ func TestHostChecker_DifferentHost(t *testing.T) {
c := server.NewContext(req, rec)
c.Request().Host = "orange.test.canhearyou.com"
- mw := infra.HostChecker("login.test.canhearyou.com")
+ mw := middlewares.HostChecker("login.test.canhearyou.com")
mw(app.HandlerFunc(func(c app.Context) error {
return c.NoContent(http.StatusOK)
}))(c)
diff --git a/app/identity/middleware.go b/app/middlewares/jwt.go
similarity index 82%
rename from app/identity/middleware.go
rename to app/middlewares/jwt.go
index 74bc31082..38240144c 100644
--- a/app/identity/middleware.go
+++ b/app/middlewares/jwt.go
@@ -1,14 +1,16 @@
-package identity
+package middlewares
import (
"net/http"
+ "strings"
"github.com/WeCanHearYou/wechy/app"
+ "github.com/WeCanHearYou/wechy/app/identity"
"github.com/WeCanHearYou/wechy/app/infra"
)
// MultiTenant extract tenant information from hostname and inject it into current context
-func MultiTenant(tenantService TenantService) app.MiddlewareFunc {
+func MultiTenant(tenantService identity.TenantService) app.MiddlewareFunc {
return func(next app.HandlerFunc) app.HandlerFunc {
return func(c app.Context) error {
hostname := stripPort(c.Request().Host)
@@ -25,7 +27,7 @@ func MultiTenant(tenantService TenantService) app.MiddlewareFunc {
}
// JwtGetter gets JWT token from cookie and add into context
-func JwtGetter(userService UserService) app.MiddlewareFunc {
+func JwtGetter(userService identity.UserService) app.MiddlewareFunc {
return func(next app.HandlerFunc) app.HandlerFunc {
return func(c app.Context) error {
@@ -82,3 +84,11 @@ func JwtSetter() app.MiddlewareFunc {
}
}
}
+
+func stripPort(hostport string) string {
+ colon := strings.IndexByte(hostport, ':')
+ if colon == -1 {
+ return hostport
+ }
+ return hostport[:colon]
+}
diff --git a/app/identity/middleware_test.go b/app/middlewares/jwt_test.go
similarity index 87%
rename from app/identity/middleware_test.go
rename to app/middlewares/jwt_test.go
index 19b20e8b5..06b8a1723 100644
--- a/app/identity/middleware_test.go
+++ b/app/middlewares/jwt_test.go
@@ -1,4 +1,4 @@
-package identity_test
+package middlewares_test
import (
"net/http"
@@ -6,10 +6,12 @@ import (
"testing"
"github.com/WeCanHearYou/wechy/app"
- "github.com/WeCanHearYou/wechy/app/identity"
"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/postgres"
"github.com/labstack/echo"
. "github.com/onsi/gomega"
)
@@ -38,6 +40,19 @@ var testCases = []struct {
},
}
+var (
+ db *dbx.Database
+ tenantService *postgres.TenantService
+ userService *postgres.UserService
+)
+
+func setup() {
+ db, _ = dbx.New()
+
+ tenantService = &postgres.TenantService{DB: db}
+ userService = &postgres.UserService{DB: db}
+}
+
type mockTenantService struct{}
func (svc mockTenantService) GetByDomain(domain string) (*models.Tenant, error) {
@@ -61,7 +76,7 @@ func TestMultiTenant(t *testing.T) {
c := server.NewContext(req, rec)
c.Request().Host = host
- mw := identity.MultiTenant(&mockTenantService{})
+ mw := middlewares.MultiTenant(&mockTenantService{})
mw(func(c app.Context) error {
return c.String(http.StatusOK, c.Tenant().Name)
})(c)
@@ -81,7 +96,7 @@ func TestMultiTenant_UnknownDomain(t *testing.T) {
c := server.NewContext(req, rec)
c.Request().Host = "somedomain.com"
- mw := identity.MultiTenant(&mockTenantService{})
+ mw := middlewares.MultiTenant(&mockTenantService{})
mw(func(c app.Context) error {
return c.String(http.StatusOK, c.Tenant().Name)
})(c)
@@ -98,7 +113,7 @@ func TestJwtGetter_NoCookie(t *testing.T) {
rec := httptest.NewRecorder()
c := server.NewContext(req, rec)
- mw := identity.JwtGetter(userService)
+ mw := middlewares.JwtGetter(userService)
mw(func(c app.Context) error {
if c.IsAuthenticated() {
return c.NoContent(http.StatusOK)
@@ -129,7 +144,7 @@ func TestJwtGetter_WithCookie(t *testing.T) {
Value: token,
})
- mw := identity.JwtGetter(userService)
+ mw := middlewares.JwtGetter(userService)
mw(func(c app.Context) error {
return c.String(http.StatusOK, c.User().Name)
})(c)
@@ -157,7 +172,7 @@ func TestJwtGetter_WithCookie_DifferentTenant(t *testing.T) {
Value: token,
})
- mw := identity.JwtGetter(userService)
+ mw := middlewares.JwtGetter(userService)
mw(func(c app.Context) error {
if c.User() == nil {
return c.NoContent(http.StatusNoContent)
@@ -177,7 +192,7 @@ func TestJwtSetter_WithoutJwt(t *testing.T) {
c := server.NewContext(req, rec)
c.Request().Host = "orange.test.canhearyou.com"
- mw := identity.JwtSetter()
+ mw := middlewares.JwtSetter()
mw(func(c app.Context) error {
return c.NoContent(http.StatusOK)
})(c)
@@ -198,7 +213,7 @@ func TestJwtSetter_WithJwt_WithoutParameter(t *testing.T) {
c := server.NewContext(req, rec)
c.Request().Host = "orange.test.canhearyou.com"
- mw := identity.JwtSetter()
+ mw := middlewares.JwtSetter()
mw(func(c app.Context) error {
return c.NoContent(http.StatusOK)
})(c)
@@ -220,7 +235,7 @@ func TestJwtSetter_WithJwt_WithParameter(t *testing.T) {
c := server.NewContext(req, rec)
c.Request().Host = "orange.test.canhearyou.com"
- mw := identity.JwtSetter()
+ mw := middlewares.JwtSetter()
mw(func(c app.Context) error {
return c.NoContent(http.StatusOK)
})(c)
diff --git a/router.go b/router.go
index 63d25864c..e2ab93084 100644
--- a/router.go
+++ b/router.go
@@ -9,6 +9,7 @@ import (
"github.com/WeCanHearYou/wechy/app/feedback"
"github.com/WeCanHearYou/wechy/app/identity"
"github.com/WeCanHearYou/wechy/app/infra"
+ "github.com/WeCanHearYou/wechy/app/middlewares"
"github.com/WeCanHearYou/wechy/app/models"
"github.com/WeCanHearYou/wechy/app/pkg/env"
"github.com/labstack/echo"
@@ -99,7 +100,7 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {
oauthHandlers := identity.OAuth(ctx.Tenant, ctx.OAuth, ctx.User)
authGroup := group(router, "/oauth")
{
- use(authGroup, infra.HostChecker(env.MustGet("AUTH_ENDPOINT")))
+ use(authGroup, middlewares.HostChecker(env.MustGet("AUTH_ENDPOINT")))
get(authGroup, "/facebook", oauthHandlers.Login(identity.OAuthFacebookProvider))
get(authGroup, "/facebook/callback", oauthHandlers.Callback(identity.OAuthFacebookProvider))
@@ -109,9 +110,9 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {
appGroup := group(router, "")
{
- use(appGroup, identity.MultiTenant(ctx.Tenant))
- use(appGroup, identity.JwtGetter(ctx.User))
- use(appGroup, identity.JwtSetter())
+ use(appGroup, middlewares.MultiTenant(ctx.Tenant))
+ use(appGroup, middlewares.JwtGetter(ctx.User))
+ use(appGroup, middlewares.JwtSetter())
get(appGroup, "/", feedback.Handlers(ctx.Idea).List())
get(appGroup, "/ideas/:number", feedback.Handlers(ctx.Idea).Details())
@@ -121,10 +122,10 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {
securedGroup := group(router, "/api")
{
- use(securedGroup, identity.MultiTenant(ctx.Tenant))
- use(securedGroup, identity.JwtGetter(ctx.User))
- use(securedGroup, identity.JwtSetter())
- use(securedGroup, infra.IsAuthenticated())
+ use(securedGroup, middlewares.MultiTenant(ctx.Tenant))
+ use(securedGroup, middlewares.JwtGetter(ctx.User))
+ use(securedGroup, middlewares.JwtSetter())
+ use(securedGroup, middlewares.IsAuthenticated())
post(securedGroup, "/ideas", feedback.Handlers(ctx.Idea).PostIdea())
post(securedGroup, "/ideas/:id/comments", feedback.Handlers(ctx.Idea).PostComment())
@@ -132,11 +133,11 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {
adminGroup := group(router, "/admin")
{
- use(adminGroup, identity.MultiTenant(ctx.Tenant))
- use(adminGroup, identity.JwtGetter(ctx.User))
- use(adminGroup, identity.JwtSetter())
- use(adminGroup, infra.IsAuthenticated())
- use(adminGroup, infra.IsAuthorized(models.RoleMember, models.RoleAdministrator))
+ use(adminGroup, middlewares.MultiTenant(ctx.Tenant))
+ use(adminGroup, middlewares.JwtGetter(ctx.User))
+ use(adminGroup, middlewares.JwtSetter())
+ use(adminGroup, middlewares.IsAuthenticated())
+ use(adminGroup, middlewares.IsAuthorized(models.RoleMember, models.RoleAdministrator))
get(adminGroup, "", func(ctx app.Context) error {
return ctx.HTML(http.StatusOK, "Welcome to Admin Page :)")