@@ -14,6 +14,7 @@ import (
1414 "net"
1515 "net/http"
1616 "net/http/httptest"
17+ "net/url"
1718 "slices"
1819 "testing"
1920 "time"
@@ -24,6 +25,7 @@ import (
2425 "github.com/lestrrat-go/jwx/v2/jwt"
2526 "github.com/opentdf/platform/protocol/go/kas"
2627 sdkauth "github.com/opentdf/platform/sdk/auth"
28+ "github.com/stretchr/testify/require"
2729 "github.com/stretchr/testify/suite"
2830 "google.golang.org/grpc"
2931 "google.golang.org/grpc/codes"
@@ -32,6 +34,7 @@ import (
3234 "google.golang.org/grpc/status"
3335 "google.golang.org/grpc/test/bufconn"
3436 "google.golang.org/protobuf/types/known/wrapperspb"
37+ "gotest.tools/v3/assert"
3538)
3639
3740type AuthSuite struct {
@@ -164,6 +167,23 @@ func TestAuthSuite(t *testing.T) {
164167 suite .Run (t , new (AuthSuite ))
165168}
166169
170+ func TestNormalizeUrl (t * testing.T ) {
171+ for _ , tt := range []struct {
172+ origin , path , out string
173+ }{
174+ {"http://localhost" , "/" , "http://localhost/" },
175+ {"https://localhost" , "/somewhere" , "https://localhost/somewhere" },
176+ {"http://localhost" , "" , "http://localhost" },
177+ } {
178+ t .Run (tt .origin + tt .path , func (t * testing.T ) {
179+ u , err := url .Parse (tt .path )
180+ require .NoError (t , err )
181+ s := normalizeURL (tt .origin , u )
182+ assert .Equal (t , s , tt .out )
183+ })
184+ }
185+ }
186+
167187func (s * AuthSuite ) Test_CheckToken_When_JWT_Expired_Expect_Error () {
168188 tok := jwt .New ()
169189 s .Require ().NoError (tok .Set (jwt .ExpirationKey , time .Date (2009 , 11 , 17 , 20 , 34 , 58 , 651387237 , time .UTC )))
@@ -173,7 +193,7 @@ func (s *AuthSuite) Test_CheckToken_When_JWT_Expired_Expect_Error() {
173193 s .NotNil (signedTok )
174194 s .Require ().NoError (err )
175195
176- _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, dpopInfo {} )
196+ _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, receiverInfo {}, nil )
177197 s .Require ().Error (err )
178198 s .Equal ("\" exp\" not satisfied" , err .Error ())
179199}
@@ -197,7 +217,7 @@ func (s *AuthSuite) Test_UnaryServerInterceptor_When_Authorization_Header_Missin
197217}
198218
199219func (s * AuthSuite ) Test_CheckToken_When_Authorization_Header_Invalid_Expect_Error () {
200- _ , _ , err := s .auth .checkToken (context .Background (), []string {"BPOP " }, dpopInfo {} )
220+ _ , _ , err := s .auth .checkToken (context .Background (), []string {"BPOP " }, receiverInfo {}, nil )
201221 s .Require ().Error (err )
202222 s .Equal ("not of type bearer or dpop" , err .Error ())
203223}
@@ -211,7 +231,7 @@ func (s *AuthSuite) Test_CheckToken_When_Missing_Issuer_Expect_Error() {
211231 s .NotNil (signedTok )
212232 s .Require ().NoError (err )
213233
214- _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, dpopInfo {} )
234+ _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, receiverInfo {}, nil )
215235 s .Require ().Error (err )
216236 s .Equal ("missing issuer" , err .Error ())
217237}
@@ -226,7 +246,7 @@ func (s *AuthSuite) Test_CheckToken_When_Invalid_Issuer_Value_Expect_Error() {
226246 s .NotNil (signedTok )
227247 s .Require ().NoError (err )
228248
229- _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, dpopInfo {} )
249+ _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, receiverInfo {}, nil )
230250 s .Require ().Error (err )
231251 s .Equal ("invalid issuer" , err .Error ())
232252}
@@ -240,7 +260,7 @@ func (s *AuthSuite) Test_CheckToken_When_Audience_Missing_Expect_Error() {
240260 s .NotNil (signedTok )
241261 s .Require ().NoError (err )
242262
243- _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, dpopInfo {} )
263+ _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, receiverInfo {}, nil )
244264 s .Require ().Error (err )
245265 s .Equal ("claim \" aud\" not found" , err .Error ())
246266}
@@ -255,7 +275,7 @@ func (s *AuthSuite) Test_CheckToken_When_Audience_Invalid_Expect_Error() {
255275 s .NotNil (signedTok )
256276 s .Require ().NoError (err )
257277
258- _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, dpopInfo {} )
278+ _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, receiverInfo {}, nil )
259279 s .Require ().Error (err )
260280 s .Equal ("\" aud\" not satisfied" , err .Error ())
261281}
@@ -271,7 +291,7 @@ func (s *AuthSuite) Test_CheckToken_When_Valid_No_DPoP_Expect_Error() {
271291 s .NotNil (signedTok )
272292 s .Require ().NoError (err )
273293
274- _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, dpopInfo {} )
294+ _ , _ , err = s .auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, receiverInfo {}, nil )
275295 s .Require ().Error (err )
276296 s .Require ().Contains (err .Error (), "dpop" )
277297}
@@ -348,15 +368,15 @@ func (s *AuthSuite) TestInvalid_DPoP_Cases() {
348368 _ , _ , err = s .auth .checkToken (
349369 context .Background (),
350370 []string {fmt .Sprintf ("DPoP %s" , string (testCase .accessToken ))},
351- dpopInfo {
352- headers : []string {dpopToken },
353- path : "/a/path" ,
354- method : http .MethodPost ,
371+ receiverInfo {
372+ u : "/a/path" ,
373+ m : http .MethodPost ,
355374 },
375+ []string {dpopToken },
356376 )
357377
358378 s .Require ().Error (err )
359- s .Equal ( testCase . errorMessage , err .Error ())
379+ s .Contains ( err .Error (), testCase . errorMessage )
360380 }
361381}
362382
@@ -567,7 +587,7 @@ func (s *AuthSuite) Test_Allowing_Auth_With_No_DPoP() {
567587 s .NotNil (signedTok )
568588 s .Require ().NoError (err )
569589
570- _ , ctx , err := auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, dpopInfo {} )
590+ _ , ctx , err := auth .checkToken (context .Background (), []string {fmt .Sprintf ("Bearer %s" , string (signedTok ))}, receiverInfo {}, nil )
571591 s .Require ().NoError (err )
572592 s .Require ().Nil (GetJWKFromContext (ctx ))
573593}
0 commit comments