-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
317846d
commit 63e1f90
Showing
5 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package tenant_test | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"github.com/justinas/alice" | ||
"github.com/kernle32dll/turtleware" | ||
"github.com/kernle32dll/turtleware/tenant" | ||
"github.com/lestrrat-go/jwx/v2/jwa" | ||
"github.com/lestrrat-go/jwx/v2/jwk" | ||
"github.com/lestrrat-go/jwx/v2/jws" | ||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path" | ||
"strings" | ||
) | ||
|
||
type CommonSuite struct { | ||
suite.Suite | ||
|
||
entityUUID string | ||
userUUID string | ||
tenantUUID string | ||
} | ||
|
||
func (s *CommonSuite) SetupTest() { | ||
s.entityUUID = uuid.NewString() | ||
s.userUUID = uuid.NewString() | ||
s.tenantUUID = uuid.NewString() | ||
} | ||
|
||
func (s *CommonSuite) buildEntityUUIDChain(h http.Handler) http.Handler { | ||
return turtleware.EntityUUIDMiddleware(func(r *http.Request) (string, error) { | ||
return s.entityUUID, nil | ||
})(h) | ||
} | ||
|
||
func (s *CommonSuite) buildAuthChain(h http.Handler) http.Handler { | ||
s.T().Helper() | ||
|
||
privateKey, err := jwk.FromRaw([]byte("secret-passphrase")) | ||
s.Require().NoError(err) | ||
s.Require().NoError(privateKey.Set(jwk.KeyIDKey, "super-key")) | ||
s.Require().NoError(privateKey.Set(jwk.AlgorithmKey, jwa.HS512)) | ||
|
||
keySet := jwk.NewSet() | ||
s.Require().NoError(keySet.AddKey(privateKey)) | ||
|
||
return alice.New( | ||
func(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
token := s.generateToken( | ||
jwa.HS512, | ||
privateKey, | ||
map[string]interface{}{ | ||
"uuid": s.userUUID, | ||
"tenant_uuid": s.tenantUUID, | ||
}, | ||
map[string]interface{}{jwk.KeyIDKey: privateKey.KeyID()}, | ||
) | ||
|
||
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
handler.ServeHTTP(w, r) | ||
}) | ||
}, | ||
turtleware.AuthBearerHeaderMiddleware, | ||
turtleware.AuthClaimsMiddleware(keySet), | ||
tenant.UUIDMiddleware, | ||
).Then(h) | ||
} | ||
|
||
func (s *CommonSuite) generateToken( | ||
algo jwa.SignatureAlgorithm, | ||
key interface{}, | ||
claims map[string]interface{}, | ||
headers map[string]interface{}, | ||
) string { | ||
t := jwt.New() | ||
|
||
for k, v := range claims { | ||
if err := t.Set(k, v); err != nil { | ||
s.Require().NoError(err) | ||
} | ||
} | ||
|
||
hdr := jws.NewHeaders() | ||
for k, v := range headers { | ||
if err := hdr.Set(k, v); err != nil { | ||
s.Require().NoError(err) | ||
} | ||
} | ||
|
||
signedT, err := jwt.Sign(t, jwt.WithKey(algo, key, jws.WithProtectedHeaders(hdr))) | ||
if err != nil { | ||
s.Require().NoError(err) | ||
} | ||
|
||
return string(signedT) | ||
} | ||
|
||
func (s *CommonSuite) loadTestDataString(name string) string { | ||
bufBytes, err := io.ReadAll(s.loadTestData(name)) | ||
if err != nil { | ||
s.Require().NoError(err) | ||
} | ||
|
||
return string(bufBytes) | ||
} | ||
|
||
func (s *CommonSuite) loadTestData(name string) io.Reader { | ||
filePath := path.Join("testdata", name) | ||
|
||
f, err := os.Open(filePath) | ||
if err != nil { | ||
s.Require().NoError(err) | ||
} | ||
|
||
s.T().Cleanup(func() { | ||
if err := f.Close(); err != nil && !strings.Contains(err.Error(), "file already closed") { | ||
s.T().Logf("Failed to close file handle for test data %q: %s", name, err) | ||
} | ||
}) | ||
|
||
return f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package tenant_test | ||
|
||
import ( | ||
"context" | ||
"github.com/kernle32dll/turtleware/tenant" | ||
"github.com/stretchr/testify/suite" | ||
"strings" | ||
|
||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
type MiddlewareCommonSuite struct { | ||
CommonSuite | ||
|
||
response *httptest.ResponseRecorder | ||
request *http.Request | ||
} | ||
|
||
func TestMiddlewareCommonSuite(t *testing.T) { | ||
suite.Run(t, &MiddlewareCommonSuite{}) | ||
} | ||
|
||
func (s *MiddlewareCommonSuite) SetupTest() { | ||
s.CommonSuite.SetupTest() | ||
|
||
s.response = httptest.NewRecorder() | ||
s.request = httptest.NewRequest(http.MethodGet, "https://example.com/foo", http.NoBody) | ||
} | ||
|
||
func (s *MiddlewareCommonSuite) SetupSubTest() { | ||
s.SetupTest() | ||
} | ||
|
||
func (s *MiddlewareCommonSuite) Test_UUIDMiddleware_Success() { | ||
// given | ||
recordedUUID := "" | ||
middlewareVerify := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { | ||
tenantUUID, err := tenant.UUIDFromRequestContext(r.Context()) | ||
s.Require().NoError(err) | ||
|
||
recordedUUID = tenantUUID | ||
}) | ||
|
||
// when | ||
s.buildAuthChain(middlewareVerify).ServeHTTP(s.response, s.request) | ||
|
||
// then | ||
s.Empty(s.response.Body.String()) | ||
s.Equal(s.tenantUUID, recordedUUID) | ||
} | ||
|
||
func (s *MiddlewareCommonSuite) Test_UUIDMiddleware_Error() { | ||
// given | ||
middlewareVerify := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { | ||
s.Fail("unexpected middleware invocation") | ||
}) | ||
|
||
middleware := tenant.UUIDMiddleware | ||
|
||
// when | ||
middleware(middlewareVerify).ServeHTTP(s.response, s.request) | ||
|
||
// then | ||
s.Equal(http.StatusInternalServerError, s.response.Code) | ||
s.JSONEq(s.loadTestDataString("errors/missing_auth_claims_error.json"), s.response.Body.String()) | ||
} | ||
|
||
func (s *MiddlewareCommonSuite) Test_UUIDFromRequestContext_Error() { | ||
// given | ||
ctx := context.Background() | ||
|
||
// when | ||
tenantUUID, err := tenant.UUIDFromRequestContext(ctx) | ||
|
||
// then | ||
s.Empty(tenantUUID) | ||
s.ErrorIs(err, tenant.ErrContextMissingTenantUUID) | ||
} | ||
|
||
func (s *MiddlewareCommonSuite) Test_UUIDFromRequestContext_ErrTokenMissingTenantUUID() { | ||
// given | ||
s.tenantUUID = "" | ||
|
||
chain := s.buildAuthChain(nil) | ||
|
||
// when | ||
chain.ServeHTTP(s.response, s.request) | ||
|
||
// then | ||
s.True(strings.Contains(s.response.Body.String(), tenant.ErrTokenMissingTenantUUID.Error())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"status": 500, | ||
"text": "Internal Server Error", | ||
"errors": [ | ||
"missing auth claims in context" | ||
] | ||
} |