Open platform to collect and prioritize feedback
refactor: removing old code + renaming middleware (#613)
Guilherme Oenningdbf0f39cfa15
diff --git a/app/cmd/routes.go b/app/cmd/routes.goindex 7242845df..5269fc44a 100644--- a/app/cmd/routes.go+++ b/app/cmd/routes.go@@ -72,8 +72,8 @@ func routes(r *web.Engine) *web.Engine {r.Get("/oauth/:provider/token", handlers.OAuthToken())r.Get("/oauth/:provider/echo", handlers.OAuthEcho())- //From this step, a only active Tenants are allowed- r.Use(middlewares.OnlyActiveTenants())+ //If tenant is pending, block it from using any other route+ r.Use(middlewares.BlockPendingTenants())r.Get("/signin", handlers.SignInPage())r.Get("/not-invited", handlers.NotInvitedPage())@@ -85,9 +85,6 @@ func routes(r *web.Engine) *web.Engine {//Extract user from cookie and inject it into web.Context (if available)r.Use(middlewares.User())- //Block if an API-originated AuthToken requests a non-API resource- r.Use(middlewares.CheckAuthTokenOrigin())-//Block if it's private tenant with unauthenticated userr.Use(middlewares.CheckTenantPrivacy())diff --git a/app/middlewares/auth.go b/app/middlewares/auth.goindex 360322b39..fa9f68309 100644--- a/app/middlewares/auth.go+++ b/app/middlewares/auth.go@@ -2,7 +2,6 @@ package middlewaresimport ("github.com/getfider/fider/app/models"- "github.com/getfider/fider/app/pkg/jwt""github.com/getfider/fider/app/pkg/web")@@ -18,21 +17,6 @@ func IsAuthenticated() web.MiddlewareFunc {}}-// CheckAuthTokenOrigin blocks API-originated AuthToken requests a non-API resource-func CheckAuthTokenOrigin() web.MiddlewareFunc {- return func(next web.HandlerFunc) web.HandlerFunc {- return func(c web.Context) error {- claims := c.Claims()- if claims != nil {- if !c.Request.IsAPI() && claims.Origin == jwt.FiderClaimsOriginAPI {- return c.Unauthorized()- }- }- return next(c)- }- }-}-// IsAuthorized blocks non-authorized requestsfunc IsAuthorized(roles ...models.Role) web.MiddlewareFunc {return func(next web.HandlerFunc) web.HandlerFunc {diff --git a/app/middlewares/auth_test.go b/app/middlewares/auth_test.goindex a638d5ad0..79b1185c6 100644--- a/app/middlewares/auth_test.go+++ b/app/middlewares/auth_test.go@@ -7,7 +7,6 @@ import ("github.com/getfider/fider/app/middlewares""github.com/getfider/fider/app/models". "github.com/getfider/fider/app/pkg/assert"- "github.com/getfider/fider/app/pkg/jwt""github.com/getfider/fider/app/pkg/mock""github.com/getfider/fider/app/pkg/web")@@ -60,64 +59,3 @@ func TestIsAuthenticated_WithoutUser(t *testing.T) {Expect(status).Equals(http.StatusForbidden)}--func TestCheckAuthTokenOrigin_WithoutClaims(t *testing.T) {- RegisterT(t)-- server, _ := mock.NewServer()- server.Use(middlewares.CheckAuthTokenOrigin())-- status, _ := server.Execute(func(c web.Context) error {- return c.NoContent(http.StatusOK)- })-- Expect(status).Equals(http.StatusOK)-}--func TestCheckAuthTokenOrigin_WithUIClaims_OnAPIResource(t *testing.T) {- RegisterT(t)-- server, _ := mock.NewServer()- server.Use(middlewares.CheckAuthTokenOrigin())-- status, _ := server.- WithURL("http://example.com/api/echo").- WithClaims(&jwt.FiderClaims{Origin: jwt.FiderClaimsOriginUI}).- Execute(func(c web.Context) error {- return c.NoContent(http.StatusOK)- })-- Expect(status).Equals(http.StatusOK)-}--func TestCheckAuthTokenOrigin_WithAPIClaims_OnAPIResource(t *testing.T) {- RegisterT(t)-- server, _ := mock.NewServer()- server.Use(middlewares.CheckAuthTokenOrigin())-- status, _ := server.- WithURL("http://example.com/api/echo").- WithClaims(&jwt.FiderClaims{Origin: jwt.FiderClaimsOriginAPI}).- Execute(func(c web.Context) error {- return c.NoContent(http.StatusOK)- })-- Expect(status).Equals(http.StatusOK)-}--func TestCheckAuthTokenOrigin_WithAPIClaims_OnUIResource(t *testing.T) {- RegisterT(t)-- server, _ := mock.NewServer()- server.Use(middlewares.CheckAuthTokenOrigin())-- status, _ := server.- WithURL("http://example.com/").- WithClaims(&jwt.FiderClaims{Origin: jwt.FiderClaimsOriginAPI}).- Execute(func(c web.Context) error {- return c.NoContent(http.StatusOK)- })-- Expect(status).Equals(http.StatusForbidden)-}diff --git a/app/middlewares/tenant.go b/app/middlewares/tenant.goindex 74493c29a..b0f62e5bb 100644--- a/app/middlewares/tenant.go+++ b/app/middlewares/tenant.go@@ -90,17 +90,17 @@ func RequireTenant() web.MiddlewareFunc {}}-// OnlyActiveTenants blocks requests for inactive tenants-func OnlyActiveTenants() web.MiddlewareFunc {+// BlockPendingTenants blocks requests for pending tenants+func BlockPendingTenants() web.MiddlewareFunc {return func(next web.HandlerFunc) web.HandlerFunc {return func(c web.Context) error {- if c.Tenant().Status == models.TenantActive {- return next(c)+ if c.Tenant().Status == models.TenantPending {+ return c.Render(http.StatusOK, "pending-activation.html", web.Props{+ Title: "Pending Activation",+ Description: "We sent you a confirmation email with a link to activate your site. Please check your inbox to activate it.",+ })}- return c.Render(http.StatusOK, "pending-activation.html", web.Props{- Title: "Pending Activation",- Description: "We sent you a confirmation email with a link to activate your site. Please check your inbox to activate it.",- })+ return next(c)}}}diff --git a/app/middlewares/tenant_test.go b/app/middlewares/tenant_test.goindex 46d423473..6c24b4996 100644--- a/app/middlewares/tenant_test.go+++ b/app/middlewares/tenant_test.go@@ -173,12 +173,13 @@ func TestSingleTenant_WithTenants_ShouldSetFirstToContext(t *testing.T) {Expect(response.Body.String()).Equals("MyCompany")}-func TestOnlyActiveTenants_Active(t *testing.T) {+func TestBlockPendingTenants_Active(t *testing.T) {RegisterT(t)server, _ := mock.NewServer()+ mock.DemoTenant.Status = models.TenantActive- server.Use(middlewares.OnlyActiveTenants())+ server.Use(middlewares.BlockPendingTenants())status, _ := server.OnTenant(mock.DemoTenant).Execute(func(c web.Context) error {return c.NoContent(http.StatusOK)})@@ -186,13 +187,13 @@ func TestOnlyActiveTenants_Active(t *testing.T) {Expect(status).Equals(http.StatusOK)}-func TestOnlyActiveTenants_Inactive(t *testing.T) {+func TestBlockPendingTenants_Pending(t *testing.T) {RegisterT(t)server, _ := mock.NewServer()mock.DemoTenant.Status = models.TenantPending- server.Use(middlewares.OnlyActiveTenants())+ server.Use(middlewares.BlockPendingTenants())status, _ := server.OnTenant(mock.DemoTenant).Execute(func(c web.Context) error {return c.NoContent(http.StatusTeapot)})diff --git a/app/models/identity.go b/app/models/identity.goindex 386b4868e..ef4dc2d76 100644--- a/app/models/identity.go+++ b/app/models/identity.go@@ -26,6 +26,8 @@ var (TenantActive = 1//TenantPending is used for signup via email that requires user confirmationTenantPending = 2+ //TenantInactive is used when tenants are inative for various reasons+ TenantInactive = 3)//Upload represents a file that has been uploaded to Fider