@@ -5,13 +5,16 @@ package client_test
55
66import (
77 "context"
8+ "encoding/json"
89 "fmt"
910 "net/http"
1011 "net/http/httptest"
12+ "strings"
1113 "testing"
1214
1315 "github.com/hashicorp/go-retryablehttp"
1416
17+ "github.com/ory/fosite"
1518 "github.com/ory/hydra/v2/driver"
1619 "github.com/ory/x/httpx"
1720
@@ -38,11 +41,16 @@ func TestValidate(t *testing.T) {
3841
3942 testCtx := context .TODO ()
4043
44+ dec := json .NewDecoder (strings .NewReader (validJWKS ))
45+ dec .DisallowUnknownFields ()
46+ var goodJWKS jose.JSONWebKeySet
47+ require .NoError (t , dec .Decode (& goodJWKS ))
48+
4149 for k , tc := range []struct {
4250 in * Client
43- check func (t * testing.T , c * Client )
44- expectErr bool
45- v func (t * testing.T ) * Validator
51+ check func (* testing.T , * Client )
52+ assertErr func ( t assert. TestingT , err error , msg ... interface {}) bool
53+ v func (* testing.T ) * Validator
4654 }{
4755 {
4856 in : new (Client ),
@@ -66,31 +74,57 @@ func TestValidate(t *testing.T) {
6674 },
6775 {
6876 in : & Client {LegacyClientID : "foo" , UserinfoSignedResponseAlg : "foo" },
69- expectErr : true ,
77+ assertErr : assert . Error ,
7078 },
7179 {
7280 in : & Client {LegacyClientID : "foo" , TokenEndpointAuthMethod : "private_key_jwt" },
73- expectErr : true ,
81+ assertErr : assert . Error ,
7482 },
7583 {
7684 in : & Client {LegacyClientID : "foo" , JSONWebKeys : & x.JoseJSONWebKeySet {JSONWebKeySet : new (jose.JSONWebKeySet )}, JSONWebKeysURI : "asdf" , TokenEndpointAuthMethod : "private_key_jwt" },
77- expectErr : true ,
85+ assertErr : assert . Error ,
7886 },
7987 {
8088 in : & Client {LegacyClientID : "foo" , JSONWebKeys : & x.JoseJSONWebKeySet {JSONWebKeySet : new (jose.JSONWebKeySet )}, TokenEndpointAuthMethod : "private_key_jwt" , TokenEndpointAuthSigningAlgorithm : "HS256" },
81- expectErr : true ,
89+ assertErr : assert .Error ,
90+ },
91+ {
92+ in : & Client {LegacyClientID : "foo" , JSONWebKeys : & x.JoseJSONWebKeySet {JSONWebKeySet : new (jose.JSONWebKeySet )}, JSONWebKeysURI : "https://example.org/jwks.json" },
93+ assertErr : func (_ assert.TestingT , err error , msg ... interface {}) bool {
94+ e := new (fosite.RFC6749Error )
95+ assert .ErrorAs (t , err , & e )
96+ assert .Contains (t , e .HintField , "jwks and jwks_uri can not both be set" )
97+ return true
98+ },
99+ },
100+ {
101+ in : & Client {LegacyClientID : "foo" , JSONWebKeys : & x.JoseJSONWebKeySet {JSONWebKeySet : & goodJWKS }},
102+ check : func (t * testing.T , c * Client ) {
103+ assert .Len (t , c .JSONWebKeys .Keys , 2 )
104+ assert .Equal (t , c .JSONWebKeys .Keys [0 ].KeyID , "1" )
105+ assert .Equal (t , c .JSONWebKeys .Keys [1 ].KeyID , "2011-04-29" )
106+ },
107+ },
108+ {
109+ in : & Client {LegacyClientID : "foo" , JSONWebKeys : & x.JoseJSONWebKeySet {JSONWebKeySet : & jose.JSONWebKeySet {Keys : []jose.JSONWebKey {{}}}}},
110+ assertErr : func (_ assert.TestingT , err error , msg ... interface {}) bool {
111+ e := new (fosite.RFC6749Error )
112+ assert .ErrorAs (t , err , & e )
113+ assert .Contains (t , e .HintField , "Invalid JSON web key in set" )
114+ return true
115+ },
82116 },
83117 {
84118 in : & Client {LegacyClientID : "foo" , PostLogoutRedirectURIs : []string {"https://bar/" }, RedirectURIs : []string {"https://foo/" }},
85- expectErr : true ,
119+ assertErr : assert . Error ,
86120 },
87121 {
88122 in : & Client {LegacyClientID : "foo" , PostLogoutRedirectURIs : []string {"http://foo/" }, RedirectURIs : []string {"https://foo/" }},
89- expectErr : true ,
123+ assertErr : assert . Error ,
90124 },
91125 {
92126 in : & Client {LegacyClientID : "foo" , PostLogoutRedirectURIs : []string {"https://foo:1234/" }, RedirectURIs : []string {"https://foo/" }},
93- expectErr : true ,
127+ assertErr : assert . Error ,
94128 },
95129 {
96130 in : & Client {LegacyClientID : "foo" , PostLogoutRedirectURIs : []string {"https://foo/" }, RedirectURIs : []string {"https://foo/" }},
@@ -122,18 +156,19 @@ func TestValidate(t *testing.T) {
122156 },
123157 {
124158 in : & Client {LegacyClientID : "foo" , SubjectType : "foo" },
125- expectErr : true ,
159+ assertErr : assert . Error ,
126160 },
127161 } {
162+ tc := tc
128163 t .Run (fmt .Sprintf ("case=%d" , k ), func (t * testing.T ) {
129164 if tc .v == nil {
130165 tc .v = func (t * testing.T ) * Validator {
131166 return v
132167 }
133168 }
134169 err := tc .v (t ).Validate (testCtx , tc .in )
135- if tc .expectErr {
136- require . Error (t , err )
170+ if tc .assertErr != nil {
171+ tc . assertErr (t , err )
137172 } else {
138173 require .NoError (t , err )
139174 tc .check (t , tc .in )
@@ -205,6 +240,33 @@ func TestValidateSectorIdentifierURL(t *testing.T) {
205240 }
206241}
207242
243+ // from https://datatracker.ietf.org/doc/html/rfc7517#appendix-A.2
244+ const validJWKS = `
245+ {"keys":
246+ [
247+ {"kty":"EC",
248+ "crv":"P-256",
249+ "x":"MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
250+ "y":"4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
251+ "d":"870MB6gfuTJ4HtUnUvYMyJpr5eUZNP4Bk43bVdj3eAE",
252+ "use":"enc",
253+ "kid":"1"},
254+
255+ {"k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G4sPXkc6Ya9y8oJW9_ILj4xuppu0lzi_H7VTkS8xj5SdX3coE0oimYwxIi2emTAue0UOa5dpgFGyBJ4c8tQ2VF402XRugKDTP8akYhFo5tAA77Qe_NmtuYZc3C3m3I24G2GvR5sSDxUyAN2zq8Lfn9EUms6rY3Ob8YeiKkTiBj0",
262+ "dq":"s9lAH9fggBsoFR8Oac2R_E2gw282rT2kGOAhvIllETE1efrA6huUUvMfBcMpn8lqeW6vzznYY5SSQF7pMdC_agI3nG8Ibp1BUb0JUiraRNqUfLhcQb_d9GF4Dh7e74WbRsobRonujTYN1xCaP6TO61jvWrX-L18txXw494Q_cgk",
263+ "qi":"GyM_p6JrXySiz1toFgKbWV-JdI3jQ4ypu9rbMWx3rQJBfmt0FoYzgUIZEVFEcOqwemRN81zoDAaa-Bk0KWNGDjJHZDdDmFhW3AN7lI-puxk_mHZGJ11rxyR8O55XLSe3SPmRfKwZI6yU24ZxvQKFYItdldUKGzO6Ia6zTKhAVRU",
264+ "alg":"RS256",
265+ "kid":"2011-04-29"}
266+ ]
267+ }
268+ `
269+
208270func TestValidateIPRanges (t * testing.T ) {
209271 ctx := context .Background ()
210272 c := internal .NewConfigurationWithDefaults ()
0 commit comments