@@ -29,14 +29,12 @@ import (
2929 "time"
3030
3131 "github.com/golang/mock/gomock"
32-
33- "github.com/ory/fosite/internal"
34-
3532 "github.com/pkg/errors"
3633 "github.com/stretchr/testify/assert"
3734 "github.com/stretchr/testify/require"
3835
3936 "github.com/ory/fosite"
37+ "github.com/ory/fosite/internal"
4038 "github.com/ory/fosite/storage"
4139)
4240
@@ -191,12 +189,99 @@ func TestRefreshFlow_HandleTokenEndpointRequest(t *testing.T) {
191189 assert .NotEqual (t , sess , areq .Session )
192190 assert .NotEqual (t , time .Now ().UTC ().Add (- time .Hour ).Round (time .Hour ), areq .RequestedAt )
193191 assert .Equal (t , fosite.Arguments {"foo" , "offline" }, areq .GrantedScope )
194- assert .Equal (t , fosite.Arguments {"foo" , "bar" , " offline" }, areq .RequestedScope )
192+ assert .Equal (t , fosite.Arguments {"foo" , "offline" }, areq .RequestedScope )
195193 assert .NotEqual (t , url.Values {"foo" : []string {"bar" }}, areq .Form )
196194 assert .Equal (t , time .Now ().Add (time .Hour ).UTC ().Round (time .Second ), areq .GetSession ().GetExpiresAt (fosite .AccessToken ))
197195 assert .Equal (t , time .Now ().Add (time .Hour ).UTC ().Round (time .Second ), areq .GetSession ().GetExpiresAt (fosite .RefreshToken ))
198196 },
199197 },
198+ {
199+ description : "should pass with scope in form" ,
200+ setup : func (config * fosite.Config ) {
201+ areq .GrantTypes = fosite.Arguments {"refresh_token" }
202+ areq .Client = & fosite.DefaultClient {
203+ ID : "foo" ,
204+ GrantTypes : fosite.Arguments {"refresh_token" },
205+ Scopes : []string {"foo" , "bar" , "baz" , "offline" },
206+ }
207+
208+ token , sig , err := strategy .GenerateRefreshToken (nil , nil )
209+ require .NoError (t , err )
210+
211+ areq .Form .Add ("refresh_token" , token )
212+ areq .Form .Add ("scope" , "foo bar baz offline" )
213+ err = store .CreateRefreshTokenSession (nil , sig , & fosite.Request {
214+ Client : areq .Client ,
215+ GrantedScope : fosite.Arguments {"foo" , "bar" , "baz" , "offline" },
216+ RequestedScope : fosite.Arguments {"foo" , "bar" , "baz" , "offline" },
217+ Session : sess ,
218+ Form : url.Values {"foo" : []string {"bar" }},
219+ RequestedAt : time .Now ().UTC ().Add (- time .Hour ).Round (time .Hour ),
220+ })
221+ require .NoError (t , err )
222+ },
223+ expect : func (t * testing.T ) {
224+ assert .Equal (t , fosite.Arguments {"foo" , "bar" , "baz" , "offline" }, areq .GrantedScope )
225+ assert .Equal (t , fosite.Arguments {"foo" , "bar" , "baz" , "offline" }, areq .RequestedScope )
226+ },
227+ },
228+ {
229+ description : "should pass with scope in form and should narrow scopes" ,
230+ setup : func (config * fosite.Config ) {
231+ areq .GrantTypes = fosite.Arguments {"refresh_token" }
232+ areq .Client = & fosite.DefaultClient {
233+ ID : "foo" ,
234+ GrantTypes : fosite.Arguments {"refresh_token" },
235+ Scopes : []string {"foo" , "bar" , "baz" , "offline" },
236+ }
237+
238+ token , sig , err := strategy .GenerateRefreshToken (nil , nil )
239+ require .NoError (t , err )
240+
241+ areq .Form .Add ("refresh_token" , token )
242+ areq .Form .Add ("scope" , "foo bar offline" )
243+ err = store .CreateRefreshTokenSession (nil , sig , & fosite.Request {
244+ Client : areq .Client ,
245+ GrantedScope : fosite.Arguments {"foo" , "bar" , "baz" , "offline" },
246+ RequestedScope : fosite.Arguments {"foo" , "bar" , "baz" , "offline" },
247+ Session : sess ,
248+ Form : url.Values {"foo" : []string {"bar" }},
249+ RequestedAt : time .Now ().UTC ().Add (- time .Hour ).Round (time .Hour ),
250+ })
251+ require .NoError (t , err )
252+ },
253+ expect : func (t * testing.T ) {
254+ assert .Equal (t , fosite.Arguments {"foo" , "bar" , "offline" }, areq .GrantedScope )
255+ assert .Equal (t , fosite.Arguments {"foo" , "bar" , "offline" }, areq .RequestedScope )
256+ },
257+ },
258+ {
259+ description : "should fail with broadened scopes even if the client can request it" ,
260+ setup : func (config * fosite.Config ) {
261+ areq .GrantTypes = fosite.Arguments {"refresh_token" }
262+ areq .Client = & fosite.DefaultClient {
263+ ID : "foo" ,
264+ GrantTypes : fosite.Arguments {"refresh_token" },
265+ Scopes : []string {"foo" , "bar" , "baz" , "offline" },
266+ }
267+
268+ token , sig , err := strategy .GenerateRefreshToken (nil , nil )
269+ require .NoError (t , err )
270+
271+ areq .Form .Add ("refresh_token" , token )
272+ areq .Form .Add ("scope" , "foo bar offline" )
273+ err = store .CreateRefreshTokenSession (nil , sig , & fosite.Request {
274+ Client : areq .Client ,
275+ GrantedScope : fosite.Arguments {"foo" , "baz" , "offline" },
276+ RequestedScope : fosite.Arguments {"foo" , "baz" , "offline" },
277+ Session : sess ,
278+ Form : url.Values {"foo" : []string {"bar" }},
279+ RequestedAt : time .Now ().UTC ().Add (- time .Hour ).Round (time .Hour ),
280+ })
281+ require .NoError (t , err )
282+ },
283+ expectErr : fosite .ErrInvalidScope ,
284+ },
200285 {
201286 description : "should pass with custom client lifespans" ,
202287 setup : func (config * fosite.Config ) {
@@ -229,7 +314,7 @@ func TestRefreshFlow_HandleTokenEndpointRequest(t *testing.T) {
229314 assert .NotEqual (t , sess , areq .Session )
230315 assert .NotEqual (t , time .Now ().UTC ().Add (- time .Hour ).Round (time .Hour ), areq .RequestedAt )
231316 assert .Equal (t , fosite.Arguments {"foo" , "offline" }, areq .GrantedScope )
232- assert .Equal (t , fosite.Arguments {"foo" , "bar" , " offline" }, areq .RequestedScope )
317+ assert .Equal (t , fosite.Arguments {"foo" , "offline" }, areq .RequestedScope )
233318 assert .NotEqual (t , url.Values {"foo" : []string {"bar" }}, areq .Form )
234319 internal .RequireEqualTime (t , time .Now ().Add (* internal .TestLifespans .RefreshTokenGrantAccessTokenLifespan ).UTC (), areq .GetSession ().GetExpiresAt (fosite .AccessToken ), time .Minute )
235320 internal .RequireEqualTime (t , time .Now ().Add (* internal .TestLifespans .RefreshTokenGrantRefreshTokenLifespan ).UTC (), areq .GetSession ().GetExpiresAt (fosite .RefreshToken ), time .Minute )
@@ -290,7 +375,7 @@ func TestRefreshFlow_HandleTokenEndpointRequest(t *testing.T) {
290375 assert .NotEqual (t , sess , areq .Session )
291376 assert .NotEqual (t , time .Now ().UTC ().Add (- time .Hour ).Round (time .Hour ), areq .RequestedAt )
292377 assert .Equal (t , fosite.Arguments {"foo" }, areq .GrantedScope )
293- assert .Equal (t , fosite.Arguments {"foo" , "bar" }, areq .RequestedScope )
378+ assert .Equal (t , fosite.Arguments {"foo" }, areq .RequestedScope )
294379 assert .NotEqual (t , url.Values {"foo" : []string {"bar" }}, areq .Form )
295380 assert .Equal (t , time .Now ().Add (time .Hour ).UTC ().Round (time .Second ), areq .GetSession ().GetExpiresAt (fosite .AccessToken ))
296381 assert .Equal (t , time .Now ().Add (time .Hour ).UTC ().Round (time .Second ), areq .GetSession ().GetExpiresAt (fosite .RefreshToken ))
0 commit comments