Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: reject requests with invalid API Key (#493)

Guilherme Oenning915d1a701874
diff --git a/app/middlewares/domain.go b/app/middlewares/domain.go
index baf1b7fca..85c4c2702 100644
--- a/app/middlewares/domain.go
+++ b/app/middlewares/domain.go
@@ -4,6 +4,8 @@ import (
"fmt"
"strings"
+ "github.com/getfider/fider/app/pkg/validate"
+
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app"
@@ -133,7 +135,7 @@ func User() web.MiddlewareFunc {
user, err = c.Services().Users.GetByAPIKey(apiKey)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
- return next(c)
+ return c.HandleValidation(validate.Failed("API Key is invalid"))
}
return err
}
diff --git a/app/middlewares/domain_test.go b/app/middlewares/domain_test.go
index f4d584e37..a9c18a774 100644
--- a/app/middlewares/domain_test.go
+++ b/app/middlewares/domain_test.go
@@ -2,6 +2,7 @@ package middlewares_test
import (
"errors"
+ "fmt"
"net/http"
"testing"
"time"
@@ -429,3 +430,39 @@ func TestUser_WithSignUpCookie(t *testing.T) {
Expect(cookie.Path).Equals("/")
Expect(cookie.Expires).TemporarilySimilar(time.Now().Add(365*24*time.Hour), 5*time.Second)
}
+
+func TestUser_ValidAPIKey(t *testing.T) {
+ RegisterT(t)
+
+ server, services := mock.NewServer()
+ services.Users.SetCurrentUser(mock.JonSnow)
+ apiKey, _ := services.Users.RegenerateAPIKey()
+
+ server.Use(middlewares.User())
+ status, response := server.
+ OnTenant(mock.DemoTenant).
+ AddHeader("Authorization", fmt.Sprintf("Bearer %s", apiKey)).
+ Execute(func(c web.Context) error {
+ return c.String(http.StatusOK, c.User().Name)
+ })
+
+ Expect(status).Equals(http.StatusOK)
+ Expect(response.Body.String()).Equals("Jon Snow")
+}
+
+func TestUser_InvalidAPIKey(t *testing.T) {
+ RegisterT(t)
+
+ server, _ := mock.NewServer()
+
+ server.Use(middlewares.User())
+ status, query := server.
+ OnTenant(mock.DemoTenant).
+ AddHeader("Authorization", "Bearer MY-KEY").
+ ExecuteAsJSON(func(c web.Context) error {
+ return c.NoContent(http.StatusOK)
+ })
+
+ Expect(status).Equals(http.StatusBadRequest)
+ Expect(query.String("errors[0].message")).Equals("API Key is invalid")
+}
diff --git a/app/pkg/web/context.go b/app/pkg/web/context.go
index c37fe893b..31394f5b0 100644
--- a/app/pkg/web/context.go
+++ b/app/pkg/web/context.go
@@ -180,7 +180,9 @@ func (ctx *Context) IsAuthenticated() bool {
//IsAjax returns true if request is AJAX
func (ctx *Context) IsAjax() bool {
- return strings.Contains(ctx.Request.GetHeader("Accept"), "application/json")
+ accept := ctx.Request.GetHeader("Accept")
+ contentType := ctx.Request.GetHeader("Content-Type")
+ return strings.Contains(accept, JSONContentType) || strings.Contains(contentType, JSONContentType)
}
//Unauthorized returns a 403 response
diff --git a/app/storage/postgres/user.go b/app/storage/postgres/user.go
index ba9dd8520..59dd973fd 100644
--- a/app/storage/postgres/user.go
+++ b/app/storage/postgres/user.go
@@ -287,7 +287,7 @@ func (s *UserStorage) HasSubscribedTo(postID int) (bool, error) {
// Delete removes current user personal data and mark it as deleted
func (s *UserStorage) Delete() error {
if _, err := s.trx.Execute(
- "UPDATE users SET role = $3, status = $4, name = '', email = '' WHERE id = $1 AND tenant_id = $2",
+ "UPDATE users SET role = $3, status = $4, name = '', email = '', api_key = null, api_key_date = null WHERE id = $1 AND tenant_id = $2",
s.user.ID, s.tenant.ID, models.RoleVisitor, models.UserDeleted,
); err != nil {
return errors.Wrap(err, "failed to delete current user")
@@ -339,4 +339,4 @@ func (s *UserStorage) GetByAPIKey(apiKey string) (*models.User, error) {
return nil, errors.Wrap(err, "failed to get user with API Key '%s'", apiKey)
}
return user, nil
-}
\ No newline at end of file
+}