Skip to content

Commit

Permalink
Abort middleware chain correctly for Gin.
Browse files Browse the repository at this point in the history
  • Loading branch information
bittrance committed May 11, 2023
1 parent 0a38ecf commit 927a370
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
4 changes: 2 additions & 2 deletions oidcgin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func onError(c *gin.Context, errorHandler options.ErrorHandler, statusCode int,
c.Header(k, v)
}
c.Data(response.StatusCode, response.ContentType(), response.Body)
c.Error(err)
c.Abort()
return nil
}

Expand All @@ -61,7 +63,5 @@ func toGinHandler[T any](parseToken oidc.ParseTokenFunc[T], setters ...options.O
}

c.Set(string(opts.ClaimsContextKeyName), claims)

c.Next()
}
}
44 changes: 39 additions & 5 deletions oidcgin/gin_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package oidcgin

import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
"github.com/xenitab/go-oidc-middleware/internal/oidc"
"github.com/xenitab/go-oidc-middleware/internal/oidctesting"
"github.com/xenitab/go-oidc-middleware/optest"
"github.com/xenitab/go-oidc-middleware/options"

"github.com/gin-gonic/gin"
Expand All @@ -23,7 +26,7 @@ func BenchmarkSuite(b *testing.B) {
oidctesting.RunBenchmarks(b, testName, newTestHandler(b))
}

func testGetGinRouter(tb testing.TB, middleware gin.HandlerFunc) *gin.Engine {
func testGetGinRouter(tb testing.TB, middlewares []gin.HandlerFunc) *gin.Engine {
tb.Helper()

// remove debug output from tests
Expand All @@ -33,7 +36,9 @@ func testGetGinRouter(tb testing.TB, middleware gin.HandlerFunc) *gin.Engine {

r := gin.Default()

r.Use(middleware)
for _, m := range middlewares {
r.Use(m)
}

r.GET("/", func(c *gin.Context) {
claimsValue, found := c.Get("claims")
Expand Down Expand Up @@ -98,19 +103,48 @@ func (h *testHandler) NewHandlerFn(claimsValidationFn options.ClaimsValidationFn
h.tb.Helper()

middleware := New(claimsValidationFn, opts...)
return testGetGinRouter(h.tb, middleware)
return testGetGinRouter(h.tb, []gin.HandlerFunc{middleware})
}

func (h *testHandler) ToHandlerFn(parseToken oidc.ParseTokenFunc[oidctesting.TestClaims], opts ...options.Option) http.Handler {
h.tb.Helper()

middleware := toGinHandler(parseToken, opts...)
return testGetGinRouter(h.tb, middleware)
return testGetGinRouter(h.tb, []gin.HandlerFunc{middleware})
}

func (h *testHandler) NewTestServer(opts ...options.Option) oidctesting.ServerTester {
h.tb.Helper()

middleware := New[oidctesting.TestClaims](nil, opts...)
return newTestServer(h.tb, testGetGinRouter(h.tb, middleware))
return newTestServer(h.tb, testGetGinRouter(h.tb, []gin.HandlerFunc{middleware}))
}

func TestAbortion(t *testing.T) {
op := optest.NewTesting(t)
defer op.Close(t)

errorHandler := func(ctx context.Context, oidcErr *options.OidcError) *options.Response {
return &options.Response{
StatusCode: 418,
Headers: map[string]string{},
Body: []byte("badness"),
}
}
opts := []options.Option{
options.WithIssuer(op.GetURL(t)),
options.WithRequiredAudience("test-client"),
options.WithRequiredTokenType("JWT+AT"),
options.WithErrorHandler(errorHandler),
}
nextCalled := false
next := func(c *gin.Context) {
nextCalled = true
}

middleware := New[oidctesting.TestClaims](nil, opts...)
r := testGetGinRouter(t, []gin.HandlerFunc{middleware, next})
req := httptest.NewRequest(http.MethodGet, "/", nil)
r.ServeHTTP(httptest.NewRecorder(), req)
require.Equal(t, false, nextCalled)
}

0 comments on commit 927a370

Please sign in to comment.