Skip to content

Commit c472cd8

Browse files
feat: filtering mode
1 parent 6db02e8 commit c472cd8

File tree

3 files changed

+272
-150
lines changed

3 files changed

+272
-150
lines changed

handler/oauth2/flow_refresh.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ type RefreshTokenGrantHandler struct {
2929
fosite.AudienceStrategyProvider
3030
fosite.RefreshTokenScopesProvider
3131
}
32+
33+
// IgnoreRequestedScopeNotInOriginalGrant determines the action to take when the requested scopes in the refresh
34+
// flow were not originally granted. If false which is the default the handler will automatically return an error.
35+
// If true the handler will filter out / ignore the scopes which were not originally granted.
36+
IgnoreRequestedScopeNotInOriginalGrant bool
3237
}
3338

3439
// HandleTokenEndpointRequest implements https://tools.ietf.org/html/rfc6749#section-6
@@ -89,12 +94,10 @@ func (c *RefreshTokenGrantHandler) HandleTokenEndpointRequest(ctx context.Contex
8994
9095
See https://www.rfc-editor.org/rfc/rfc6749#section-6
9196
*/
92-
switch scope := request.GetRequestForm().Get("scope"); scope {
93-
case "":
94-
// Addresses point 1 of the text in RFC6749 Section 6.
97+
98+
// Addresses point 1 of the text in RFC6749 Section 6.
99+
if len(request.GetRequestedScopes()) == 0 {
95100
request.SetRequestedScopes(originalRequest.GetGrantedScopes())
96-
default:
97-
request.SetRequestedScopes(fosite.RemoveEmpty(strings.Split(scope, " ")))
98101
}
99102

100103
request.SetRequestedAudience(originalRequest.GetRequestedAudience())
@@ -103,9 +106,15 @@ func (c *RefreshTokenGrantHandler) HandleTokenEndpointRequest(ctx context.Contex
103106
originalScopes := originalRequest.GetGrantedScopes()
104107

105108
for _, scope := range request.GetRequestedScopes() {
106-
// Addresses point 2 of the text in RFC6749 Section 6.
107109
if !strategy(originalScopes, scope) {
108-
return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The requested scope '%s' was not originally granted by the resource owner.", scope))
110+
if c.IgnoreRequestedScopeNotInOriginalGrant {
111+
// Skips addressing point 2 of the text in RFC6749 Section 6 and instead just prevents the scope
112+
// requested from being granted.
113+
continue
114+
} else {
115+
// Addresses point 2 of the text in RFC6749 Section 6.
116+
return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The requested scope '%s' was not originally granted by the resource owner.", scope))
117+
}
109118
}
110119

111120
if !strategy(request.GetClient().GetScopes(), scope) {

handler/oauth2/flow_refresh_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ func TestRefreshFlow_HandleTokenEndpointRequest(t *testing.T) {
222222

223223
areq.Form.Add("refresh_token", token)
224224
areq.Form.Add("scope", "foo bar offline")
225+
areq.SetRequestedScopes(fosite.Arguments{"foo", "bar", "offline"})
226+
225227
err = store.CreateRefreshTokenSession(nil, sig, &fosite.Request{
226228
Client: areq.Client,
227229
GrantedScope: fosite.Arguments{"foo", "bar", "baz", "offline"},
@@ -252,6 +254,8 @@ func TestRefreshFlow_HandleTokenEndpointRequest(t *testing.T) {
252254

253255
areq.Form.Add("refresh_token", token)
254256
areq.Form.Add("scope", "foo bar offline")
257+
areq.SetRequestedScopes(fosite.Arguments{"foo", "bar", "offline"})
258+
255259
err = store.CreateRefreshTokenSession(nil, sig, &fosite.Request{
256260
Client: areq.Client,
257261
GrantedScope: fosite.Arguments{"foo", "baz", "offline"},

0 commit comments

Comments
 (0)