Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: authentication flow should abort early #888

Merged
merged 8 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/http/middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/go-shiori/shiori/internal/http/response"
"github.com/go-shiori/shiori/internal/model"
"github.com/go-shiori/shiori/internal/testutil"
"github.com/sirupsen/logrus"
Expand All @@ -18,8 +19,13 @@ func TestAuthenticationRequiredMiddleware(t *testing.T) {
t.Run("test unauthorized", func(t *testing.T) {
g := testutil.NewGin()
g.Use(AuthenticationRequired())
g.Handle("GET", "/", func(c *gin.Context) {
response.Send(c, http.StatusOK, nil)
})
w := testutil.PerformRequest(g, "GET", "/")
require.Equal(t, http.StatusUnauthorized, w.Code)
// This ensures we are aborting the request and not sending more data
require.Equal(t, `{"ok":false,"message":null}`, w.Body.String())
})

t.Run("test authorized", func(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions internal/http/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Response struct {
Ok bool `json:"ok"`

// Message the payload of the response, depending on the endpoint/response status
Message interface{} `json:"message"`
Message any `json:"message"`

// ErrorParams parameters defined if the response is not successful to help client's debugging
ErrorParams map[string]string `json:"error_params,omitempty"`
Expand All @@ -23,12 +23,16 @@ func (m *Response) IsError() bool {
return m.Ok
}

func (m *Response) GetMessage() any {
return m.Message
}

func (m *Response) Send(c *gin.Context) {
c.Status(m.statusCode)
c.JSON(m.statusCode, m)
}

func NewResponse(ok bool, message interface{}, errorParams map[string]string, statusCode int) *Response {
func NewResponse(ok bool, message any, errorParams map[string]string, statusCode int) *Response {
return &Response{
Ok: ok,
Message: message,
Expand Down
1 change: 1 addition & 0 deletions internal/http/response/shortcuts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Send(ctx *gin.Context, statusCode int, data interface{}) {
// SendError provides a shortcut to send an unsuccessful response
func SendError(ctx *gin.Context, statusCode int, data interface{}) {
New(false, statusCode, data).Send(ctx)
ctx.Abort()
}

// SendErrorWithParams the same as above but for errors that require error parameters
Expand Down
12 changes: 6 additions & 6 deletions internal/testutil/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ import (
)

type testResponse struct {
Response *response.Response
Response response.Response
}

func (r *testResponse) AssertMessageIsEmptyList(t *testing.T) {
require.Equal(t, []interface{}{}, r.Response.Message)
require.Equal(t, []interface{}{}, r.Response.GetMessage())
}

func (r *testResponse) AssertNilMessage(t *testing.T) {
require.Equal(t, nil, r.Response.Message)
require.Equal(t, nil, r.Response.GetMessage())
}

func (r testResponse) AssertMessageEquals(t *testing.T, expected interface{}) {
require.Equal(t, expected, r.Response.Message)
require.Equal(t, expected, r.Response.GetMessage())
}

func (r *testResponse) AssertOk(t *testing.T) {
require.True(t, r.Response.Ok)
require.True(t, !r.Response.IsError())
}

func (r *testResponse) AssertNotOk(t *testing.T) {
require.False(t, r.Response.Ok)
require.False(t, r.Response.IsError())
}

func NewTestResponseFromBytes(b []byte) (*testResponse, error) {
Expand Down
Loading