Skip to content

Commit

Permalink
fix: implicit scopes not granted
Browse files Browse the repository at this point in the history
This fixes an issue where the intended implicit scopes were not granted.
  • Loading branch information
james-d-elliott committed Mar 11, 2024
1 parent ec78472 commit 5bc6215
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
12 changes: 6 additions & 6 deletions handler/oauth2/flow_client_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func (c *ClientCredentialsGrantHandler) HandleTokenEndpointRequest(ctx context.C

if len(scopes) == 0 {
if pclient, ok := client.(oauth2.ClientCredentialsFlowRequestedScopeImplicitClient); ok && pclient.GetClientCredentialsFlowRequestedScopeImplicit() {
scopes = client.GetScopes()
request.SetRequestedScopes(client.GetScopes())
}
}

for _, scope := range scopes {
if !c.Config.GetScopeStrategy(ctx)(client.GetScopes(), scope) {
return errorsx.WithStack(oauth2.ErrInvalidScope.WithHintf("The OAuth 2.0 Client is not allowed to request scope '%s'.", scope))
} else {
for _, scope := range scopes {
if !c.Config.GetScopeStrategy(ctx)(client.GetScopes(), scope) {
return errorsx.WithStack(oauth2.ErrInvalidScope.WithHintf("The OAuth 2.0 Client is not allowed to request scope '%s'.", scope))
}
}
}

Expand Down
69 changes: 66 additions & 3 deletions handler/oauth2/flow_client_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func TestClientCredentialsGrantHandler_HandleTokenEndpointRequest(t *testing.T)
ID: "test",
GrantTypes: []string{consts.GrantTypeClientCredentials},
Audience: []string{"https://example.com"},
Scopes: []string{"openid"},
},
},
},
Expand All @@ -205,6 +206,7 @@ func TestClientCredentialsGrantHandler_HandleTokenEndpointRequest(t *testing.T)
ID: "test",
GrantTypes: []string{consts.GrantTypeClientCredentials},
Audience: []string{"https://example.com"},
Scopes: []string{"openid"},
},
},
},
Expand All @@ -225,8 +227,9 @@ func TestClientCredentialsGrantHandler_HandleTokenEndpointRequest(t *testing.T)
ID: "test",
GrantTypes: []string{consts.GrantTypeClientCredentials},
Audience: []string{"https://example.com"},
Scopes: []string{"openid"},
},
implicit: true,
audience: true,
},
},
},
Expand All @@ -239,6 +242,61 @@ func TestClientCredentialsGrantHandler_HandleTokenEndpointRequest(t *testing.T)
},
"",
},
{
"ShouldSuccessfullyGrantAllScopes",
&oauth2.AccessRequest{
GrantTypes: oauth2.Arguments{consts.GrantTypeClientCredentials},
Request: oauth2.Request{
Session: &oauth2.DefaultSession{},
Client: &TestRequestedAudienceClient{
DefaultClient: &oauth2.DefaultClient{
ID: "test",
GrantTypes: []string{consts.GrantTypeClientCredentials},
Audience: []string{"https://exmaple.com"},
Scopes: []string{"openid"},
},
scopes: true,
},
},
},
&oauth2.AccessRequest{
GrantTypes: oauth2.Arguments{consts.GrantTypeClientCredentials},
Request: oauth2.Request{
RequestedScope: []string{"openid"},
GrantedScope: []string{"openid"},
},
},
"",
},
{
"ShouldSuccessfullyGrantAllScopesAndAudiences",
&oauth2.AccessRequest{
GrantTypes: oauth2.Arguments{consts.GrantTypeClientCredentials},
Request: oauth2.Request{
Session: &oauth2.DefaultSession{},
Client: &TestRequestedAudienceClient{
DefaultClient: &oauth2.DefaultClient{
ID: "test",
GrantTypes: []string{consts.GrantTypeClientCredentials},
Audience: []string{"https://exmaple.com"},
Scopes: []string{"openid"},
},
scopes: true,
audience: true,
},
},
},
&oauth2.AccessRequest{
GrantTypes: oauth2.Arguments{consts.GrantTypeClientCredentials},
Request: oauth2.Request{
RequestedScope: []string{"openid"},
RequestedAudience: []string{"https://exmaple.com"},
GrantedScope: []string{"openid"},
GrantedAudience: []string{"https://exmaple.com"},
},
},
"",
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -296,9 +354,14 @@ func TestClientCredentialsGrantHandler_HandleTokenEndpointRequest(t *testing.T)
type TestRequestedAudienceClient struct {
*oauth2.DefaultClient

implicit bool
audience bool
scopes bool
}

func (c *TestRequestedAudienceClient) GetRequestedAudienceImplicit() bool {
return c.implicit
return c.audience
}

func (c *TestRequestedAudienceClient) GetClientCredentialsFlowRequestedScopeImplicit() bool {
return c.scopes
}

0 comments on commit 5bc6215

Please sign in to comment.