@@ -91,28 +91,16 @@ func chatRequest(token string) key.Request {
9191 return key.Request {APIKey : token , Path : "/v1/chat/completions" }
9292}
9393
94- // validateErr runs the validator and returns only the error, for the many
95- // rejection cases that do not care about the Result. The accepted-token tests
96- // call Validate directly to assert the surfaced subject.
97- func validateErr (val * Validator , req key.Request ) error {
98- _ , err := val .Validate (req )
99- return err
100- }
101-
10294func TestValidateAcceptsValidToken (t * testing.T ) {
10395 pub , priv , _ := ed25519 .GenerateKey (nil )
10496 srv := jwksServer (t , pub , testKID )
10597 defer srv .Close ()
10698 v := newTestValidator (t , srv .URL )
10799
108100 token := mintToken (t , priv , testKID , "at+jwt" , validClaims (time .Now ()), RequiredScope )
109- res , err := v .Validate (chatRequest (token ))
110- if err != nil {
101+ if err := v .Validate (chatRequest (token )); err != nil {
111102 t .Fatalf ("expected valid token, got %v" , err )
112103 }
113- if res .Subject != "user_1" {
114- t .Fatalf ("Subject = %q, want user_1" , res .Subject )
115- }
116104}
117105
118106func TestValidateFallsThroughForOpaqueKey (t * testing.T ) {
@@ -121,7 +109,7 @@ func TestValidateFallsThroughForOpaqueKey(t *testing.T) {
121109 defer srv .Close ()
122110 v := newTestValidator (t , srv .URL )
123111
124- err := validateErr ( v , key.Request {APIKey : "chat_abcdef" })
112+ err := v . Validate ( key.Request {APIKey : "chat_abcdef" })
125113 if ! errors .Is (err , key .ErrUnsupportedToken ) {
126114 t .Fatalf ("expected ErrUnsupportedToken, got %v" , err )
127115 }
@@ -133,7 +121,7 @@ func TestValidateFallsThroughForDottedOpaqueKey(t *testing.T) {
133121 defer srv .Close ()
134122 v := newTestValidator (t , srv .URL )
135123
136- err := validateErr ( v , key.Request {APIKey : "opaque.with.dots" })
124+ err := v . Validate ( key.Request {APIKey : "opaque.with.dots" })
137125 if ! errors .Is (err , key .ErrUnsupportedToken ) {
138126 t .Fatalf ("expected ErrUnsupportedToken, got %v" , err )
139127 }
@@ -148,7 +136,7 @@ func TestValidateRejectsWrongAudience(t *testing.T) {
148136 claims := validClaims (time .Now ())
149137 claims .Audience = josejwt.Audience {"https://example.com" }
150138 token := mintToken (t , priv , testKID , "at+jwt" , claims , RequiredScope )
151- expectStatus (t , validateErr ( v , chatRequest (token )), http .StatusUnauthorized )
139+ expectStatus (t , v . Validate ( chatRequest (token )), http .StatusUnauthorized )
152140}
153141
154142func TestValidateRejectsExpired (t * testing.T ) {
@@ -158,7 +146,7 @@ func TestValidateRejectsExpired(t *testing.T) {
158146 v := newTestValidator (t , srv .URL )
159147
160148 token := mintToken (t , priv , testKID , "at+jwt" , validClaims (time .Now ().Add (- time .Hour )), RequiredScope )
161- expectStatus (t , validateErr ( v , chatRequest (token )), http .StatusUnauthorized )
149+ expectStatus (t , v . Validate ( chatRequest (token )), http .StatusUnauthorized )
162150}
163151
164152func TestValidateRejectsMissingExpiration (t * testing.T ) {
@@ -170,7 +158,7 @@ func TestValidateRejectsMissingExpiration(t *testing.T) {
170158 claims := validClaims (time .Now ())
171159 claims .Expiry = nil
172160 token := mintToken (t , priv , testKID , "at+jwt" , claims , RequiredScope )
173- expectStatus (t , validateErr ( v , chatRequest (token )), http .StatusUnauthorized )
161+ expectStatus (t , v . Validate ( chatRequest (token )), http .StatusUnauthorized )
174162}
175163
176164func TestValidateRejectsMissingScope (t * testing.T ) {
@@ -180,7 +168,7 @@ func TestValidateRejectsMissingScope(t *testing.T) {
180168 v := newTestValidator (t , srv .URL )
181169
182170 token := mintToken (t , priv , testKID , "at+jwt" , validClaims (time .Now ()), "models:read" )
183- expectStatus (t , validateErr ( v , chatRequest (token )), http .StatusForbidden )
171+ expectStatus (t , v . Validate ( chatRequest (token )), http .StatusForbidden )
184172}
185173
186174func TestValidateRejectsWrongIssuer (t * testing.T ) {
@@ -192,7 +180,7 @@ func TestValidateRejectsWrongIssuer(t *testing.T) {
192180 claims := validClaims (time .Now ())
193181 claims .Issuer = "https://evil.example.com"
194182 token := mintToken (t , priv , testKID , "at+jwt" , claims , RequiredScope )
195- expectStatus (t , validateErr ( v , chatRequest (token )), http .StatusUnauthorized )
183+ expectStatus (t , v . Validate ( chatRequest (token )), http .StatusUnauthorized )
196184}
197185
198186func TestValidateFallsThroughForWrongType (t * testing.T ) {
@@ -202,7 +190,7 @@ func TestValidateFallsThroughForWrongType(t *testing.T) {
202190 v := newTestValidator (t , srv .URL )
203191
204192 token := mintToken (t , priv , testKID , "JWT" , validClaims (time .Now ()), RequiredScope )
205- err := validateErr ( v , chatRequest (token ))
193+ err := v . Validate ( chatRequest (token ))
206194 if ! errors .Is (err , key .ErrUnsupportedToken ) {
207195 t .Fatalf ("expected ErrUnsupportedToken, got %v" , err )
208196 }
@@ -218,7 +206,7 @@ func TestValidateRejectsForeignSignature(t *testing.T) {
218206 // verification against the published key must fail.
219207 _ , foreignPriv , _ := ed25519 .GenerateKey (nil )
220208 token := mintToken (t , foreignPriv , testKID , "at+jwt" , validClaims (time .Now ()), RequiredScope )
221- expectStatus (t , validateErr ( v , chatRequest (token )), http .StatusUnauthorized )
209+ expectStatus (t , v . Validate ( chatRequest (token )), http .StatusUnauthorized )
222210}
223211
224212func TestValidateAcceptsApplicationPrefixType (t * testing.T ) {
@@ -229,7 +217,7 @@ func TestValidateAcceptsApplicationPrefixType(t *testing.T) {
229217
230218 // RFC 9068 / RFC 7515 permit the media type with an "application/" prefix.
231219 token := mintToken (t , priv , testKID , "application/at+jwt" , validClaims (time .Now ()), RequiredScope )
232- if err := validateErr ( v , chatRequest (token )); err != nil {
220+ if err := v . Validate ( chatRequest (token )); err != nil {
233221 t .Fatalf ("expected application/at+jwt to be accepted, got %v" , err )
234222 }
235223}
@@ -243,7 +231,7 @@ func TestValidateAcceptsNonChatPath(t *testing.T) {
243231 // The inference:api scope authorizes every inference endpoint, not just
244232 // chat completions, so a non-chat path must validate.
245233 token := mintToken (t , priv , testKID , "at+jwt" , validClaims (time .Now ()), RequiredScope )
246- if err := validateErr ( v , key.Request {APIKey : token , Path : "/v1/embeddings" }); err != nil {
234+ if err := v . Validate ( key.Request {APIKey : token , Path : "/v1/embeddings" }); err != nil {
247235 t .Fatalf ("expected non-chat path to be accepted, got %v" , err )
248236 }
249237}
@@ -322,7 +310,7 @@ func TestValidateRefreshesUnknownKidAfterRecentSuccess(t *testing.T) {
322310 useSecond .Store (true )
323311
324312 token := mintToken (t , secondPriv , "test-key-2" , "at+jwt" , validClaims (time .Now ()), RequiredScope )
325- if err := validateErr ( v , chatRequest (token )); err != nil {
313+ if err := v . Validate ( chatRequest (token )); err != nil {
326314 t .Fatalf ("expected unknown kid to refresh immediately, got %v" , err )
327315 }
328316}
@@ -356,7 +344,7 @@ func TestNewValidatorRecoversWhenJWKSStartsUnavailable(t *testing.T) {
356344 v := newTestValidator (t , srv .URL )
357345
358346 token := mintToken (t , priv , testKID , "at+jwt" , validClaims (time .Now ()), RequiredScope )
359- if err := validateErr ( v , chatRequest (token )); err == nil {
347+ if err := v . Validate ( chatRequest (token )); err == nil {
360348 t .Fatal ("expected rejection while no signing keys are cached" )
361349 }
362350
@@ -367,7 +355,7 @@ func TestNewValidatorRecoversWhenJWKSStartsUnavailable(t *testing.T) {
367355 v .keys .lastAttempt = time .Now ().Add (- 2 * minRefreshInterval )
368356 v .keys .mu .Unlock ()
369357
370- if err := validateErr ( v , chatRequest (token )); err != nil {
358+ if err := v . Validate ( chatRequest (token )); err != nil {
371359 t .Fatalf ("expected token to validate after JWKS became available, got %v" , err )
372360 }
373361}
0 commit comments