Add OAuth2 API scope registration and enforcement
Register v1 API scopes in coredata, advertise them in OIDC discovery and protected-resource metadata, show them on the consent screen, and enforce scope-to-action mapping in the IAM Authorizer before policy evaluation. Signed-off-by: Ludovic Vielle <ludovic@probo.com>
This commit is contained in:
103
e2e/console/oauth2_scope_test.go
Normal file
103
e2e/console/oauth2_scope_test.go
Normal file
@@ -0,0 +1,103 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package console_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.probo.inc/probo/e2e/internal/factory"
|
||||
"go.probo.inc/probo/e2e/internal/testutil"
|
||||
)
|
||||
|
||||
func TestOAuth2_ScopeEnforcementOnConsoleGraphQL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
owner := testutil.NewClient(t, testutil.RoleOwner)
|
||||
factory.CreateThirdParty(owner, factory.Attrs{"name": "Scoped OAuth Vendor"})
|
||||
|
||||
const redirectURI = "http://localhost:9999/callback"
|
||||
|
||||
client := factory.CreateOAuth2ClientWithAPIScopes(
|
||||
owner,
|
||||
"openid v1:org:read",
|
||||
nil,
|
||||
)
|
||||
|
||||
tokenResp := testutil.OAuth2PerformAuthorizationCodeFlowWithScopes(
|
||||
t,
|
||||
owner,
|
||||
client.ClientID,
|
||||
client.ClientSecret,
|
||||
redirectURI,
|
||||
"openid v1:org:read",
|
||||
)
|
||||
require.NotEmpty(t, tokenResp.AccessToken)
|
||||
|
||||
const getOrganizationQuery = `
|
||||
query GetOrganization($id: ID!) {
|
||||
node(id: $id) {
|
||||
... on Organization {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
allowedResp, err := testutil.ConsoleGraphQLWithAccessToken(
|
||||
t,
|
||||
tokenResp.AccessToken,
|
||||
getOrganizationQuery,
|
||||
map[string]any{
|
||||
"id": owner.GetOrganizationID().String(),
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, allowedResp)
|
||||
|
||||
const listThirdPartiesQuery = `
|
||||
query ListThirdParties($orgId: ID!) {
|
||||
node(id: $orgId) {
|
||||
... on Organization {
|
||||
thirdParties(first: 10) {
|
||||
totalCount
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
deniedResp, err := testutil.ConsoleGraphQLWithAccessToken(
|
||||
t,
|
||||
tokenResp.AccessToken,
|
||||
listThirdPartiesQuery,
|
||||
map[string]any{
|
||||
"orgId": owner.GetOrganizationID().String(),
|
||||
},
|
||||
)
|
||||
require.Error(t, err)
|
||||
require.NotNil(t, deniedResp)
|
||||
require.NotEmpty(t, deniedResp.Errors)
|
||||
|
||||
code := deniedResp.Errors[0].Code()
|
||||
msg := deniedResp.Errors[0].Message
|
||||
isForbidden := code == "FORBIDDEN" ||
|
||||
(code == "" && (strings.Contains(msg, "does not have sufficient permissions") || strings.Contains(msg, "insufficient permissions")))
|
||||
require.True(t, isForbidden, "expected FORBIDDEN error, got code=%q message=%q", code, msg)
|
||||
assert.Empty(t, deniedResp.DataString(), "expected no data on denied request")
|
||||
}
|
||||
@@ -65,6 +65,7 @@ func TestOAuth2_Discovery(t *testing.T) {
|
||||
assert.Contains(t, discovery.ScopesSupported, "profile")
|
||||
assert.Contains(t, discovery.ScopesSupported, "email")
|
||||
assert.Contains(t, discovery.ScopesSupported, "offline_access")
|
||||
assert.Contains(t, discovery.ScopesSupported, "v1:document:read")
|
||||
|
||||
assert.Contains(t, discovery.ResponseTypesSupported, "code")
|
||||
assert.Contains(t, discovery.CodeChallengeMethodsSupported, "S256")
|
||||
@@ -97,6 +98,46 @@ func TestOAuth2_Discovery(t *testing.T) {
|
||||
assert.Contains(t, discovery.ClaimsSupported, "name")
|
||||
}
|
||||
|
||||
func TestOAuth2_ProtectedResourceMetadata(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
owner := testutil.NewClient(t, testutil.RoleOwner)
|
||||
|
||||
metadata, raw, err := testutil.OAuth2ProtectedResourceMetadata(owner)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, raw.StatusCode)
|
||||
require.NotNil(t, metadata)
|
||||
|
||||
expectedResource := owner.BaseURL()
|
||||
assert.Equal(t, expectedResource, metadata.Resource)
|
||||
assert.Contains(t, metadata.AuthorizationServers, expectedResource)
|
||||
assert.Contains(t, metadata.BearerMethodsSupported, "header")
|
||||
assert.Contains(t, metadata.ScopesSupported, "openid")
|
||||
assert.Contains(t, metadata.ScopesSupported, "v1:document:read")
|
||||
assert.NotContains(t, metadata.ScopesSupported, "profile")
|
||||
}
|
||||
|
||||
func TestOAuth2_RegisterClientWithAPIScope(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
owner := testutil.NewClient(t, testutil.RoleOwner)
|
||||
|
||||
resp, raw, err := testutil.OAuth2RegisterClient(owner, map[string]any{
|
||||
"organization_id": owner.GetOrganizationID().String(),
|
||||
"client_name": "API scope client",
|
||||
"visibility": "private",
|
||||
"redirect_uris": []string{"http://localhost:9999/callback"},
|
||||
"grant_types": []string{"authorization_code"},
|
||||
"response_types": []string{"code"},
|
||||
"token_endpoint_auth_method": "client_secret_basic",
|
||||
"scopes": "openid v1:document:read",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusCreated, raw.StatusCode)
|
||||
require.NotNil(t, resp)
|
||||
assert.Contains(t, resp.Scopes, "v1:document:read")
|
||||
}
|
||||
|
||||
func TestOAuth2_JWKS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user