feat(oidc): add RFC 7591 dynamic client registration (#12313)

# Which Problems Are Solved

ZITADEL cannot currently be used as an OAuth 2.0 Authorization Server by
clients that need to register themselves at runtime:

- There is no [OAuth 2.0 Dynamic Client Registration (RFC
7591)](https://datatracker.ietf.org/doc/html/rfc7591) endpoint, and no
`registration_endpoint` is advertised in the discovery document.
- This blocks [Model Context Protocol
(MCP)](https://modelcontextprotocol.io) clients (Claude Desktop,
claude.ai, Cursor, the MCP SDKs), whose OAuth 2.1 authorization profile
expects an Authorization Server that supports dynamic client
registration. They self-register before any user context exists and only
then start the authorization code + PKCE flow.

# How the Problems Are Solved

## Control plane: instance security settings

No feature flag and no runtime config. A
`DynamicClientRegistrationSettings` message is nested under the instance
`SecuritySettings` in `settings/v2`, next to `embedded_iframe` and
`enable_impersonation`:

| `enabled` | `allow_unauthenticated` | Behaviour |
| --- | --- | --- |
| `false` (default) | — | `POST /oauth/v2/register` returns `404`,
`registration_endpoint` absent from discovery |
| `true` | `false` (default) | Token-gated registration, client homed in
the token's organization |
| `true` | `true` | Open registration for MCP hosts, client homed in the
instance default organization |

Both values are carried on `authz.Instance` and loaded with the
instance, exactly like `EnableImpersonation`, so the discovery document
and the endpoint read them per request without an extra query.
`allow_unauthenticated` implies `enabled` in both implementations, so a
single setting closes the endpoint.

Adding the two columns bumps the projection table to
`projections.security_policies3`, following what the impersonation
setting did for `security_policies2`.

## Authorization: a dedicated permission, not `project.app.write`

- New org-scoped permission **`project.app.register_dynamic`**, granted
by default to `ORG_OWNER`, `IAM_OWNER` and `IAM_ORG_MANAGER` — in both
`InternalAuthZ` and `SystemAuthZ`, mirroring where `project.app.write`
already lives.
- New built-in role **`ORG_DYNAMIC_CLIENT_REGISTRAR`** carrying only
that permission, so a service user can self-register clients without
gaining write access to the organization's existing applications.
- Open mode (`allow_unauthenticated = true`) requires no token and no
permission: that is the MCP self-register path.
- A valid token whose user lacks the permission is answered with **`403
insufficient_scope`** (RFC 6750 §3.1) rather than `401 invalid_token`,
so a caller can tell "your token is bad" from "your token may not do
this". `verifyAccessToken` also reports invalid tokens as permission
denied, so the authorization failure is marked explicitly to keep the
two apart.

The check runs once, at the endpoint, right after the token is verified
and before any state is created, so it also gates the auto-provisioning
of the organization's DCR project. The permission itself lives in the
command layer next to the others
(`Commands.CheckPermissionRegisterDynamicClient`). As the OIDC endpoints
are not behind the authorization interceptor, the caller identity is
derived from the verified token, the way token exchange does for the
impersonating actor.

## Registration itself

- **Registered clients are ordinary OIDC applications.** The endpoint
reuses the existing application-creation events
(`NewApplicationAddedEvent` and `NewOIDCConfigAddedEvent`), so the whole
token, authorization and introspection flow keeps working unchanged.
They are stored in a dedicated, auto-provisioned project named `ZITADEL
DCR` per organization (name lookup), so they do not pollute the
`IAMProject` or other projects.
- Input is validated with the existing `GetOIDCV1Compliance` rules;
`private_key_jwt` and `jwks`/`jwks_uri` are rejected with
`invalid_client_metadata`.
- **Endpoint wiring without forking the library.** `NewServer` calls
`op.RegisterServer` instead of `op.RegisterLegacyServer`, so the new
route can be added through the same `op.WithSetRouter` hook as the
authorize callback. `op.RegisterLegacyServer` appends a middleware after
the caller options, which chi forbids once a route has been registered;
`op.NewIssuerInterceptor` reproduces the issuer middleware it would
otherwise add. The `/oauth/v2` routing prefix is unchanged.

# Additional Changes

- **`application_type` inference for custom-scheme redirects** (last
commit, isolated). `application_type` is an OpenID Connect member; RFC
7591 does not define it, so clients that only implement RFC 7591 omit
it. Native MCP hosts doing so register custom-scheme redirect URIs,
which ZITADEL reserves for native applications, and the OIDC default of
`web` rejected them with `invalid_redirect_uri`. Inference is limited to
custom schemes and to an absent `application_type`, so every request
accepted today keeps its current application type and auth method — an
`http` loopback redirect still yields a web application. Happy to drop
this commit if you would rather keep it out of this PR.
- **Docs**: a Dynamic Client Registration integration guide, a
`registration_endpoint` section on the OpenID Connect endpoints page,
and the new role in the administrators table (plus its description in
the Console translations).

# Additional Context

- Part of #9810. This is **Phase 1 (DCR)**; #12315 adds RFC 7592 and
#12316 adds CIMD, both stacked on this branch and rebased once this
lands.
- Implements the control-plane and authorization model requested in the
review of this PR, itself a refinement of the direction in
https://github.com/zitadel/zitadel/issues/9810#issuecomment-4891176997.
- **Out of scope (deliberately)**: RFC 7592, CIMD, persisting free-form
metadata (`logo_uri`, `contacts`, and similar), and `private_key_jwt` /
`jwks_uri` (rejected cleanly). No new event types.
- Follow-ups explicitly left out, per the review: DCR client lifecycle
and richer audit, consent and custom scopes, JWT / `aud` (RFC 8707), and
RFC 8414 AS metadata (currently absent from both ZITADEL and
`zitadel/oidc`).
This commit is contained in:
jmarette
2026-07-30 14:43:47 +00:00
committed by GitHub
parent adb2974662
commit f34cc45eac
57 changed files with 2651 additions and 87 deletions
+7
View File
@@ -22,6 +22,13 @@ func WithMockFeatures(features feature.Features) MockContextInstanceOpts {
}
}
func WithMockDynamicClientRegistration(enabled, allowUnauthenticated bool) MockContextInstanceOpts {
return func(i *instance) {
i.enableDCR = enabled
i.allowUnauthenticatedDCR = allowUnauthenticated
}
}
func WithMockProjectID(projectID string) MockContextInstanceOpts {
return func(i *instance) {
i.projectID = projectID
+25 -9
View File
@@ -26,6 +26,12 @@ type Instance interface {
DefaultOrganisationID() string
SecurityPolicyAllowedOrigins() []string
EnableImpersonation() bool
// EnableDynamicClientRegistration states if the OAuth 2.0 Dynamic Client Registration
// endpoint (RFC 7591) is served and advertised in the discovery document.
EnableDynamicClientRegistration() bool
// AllowUnauthenticatedDynamicClientRegistration states if clients may register without
// an access token. It implies EnableDynamicClientRegistration.
AllowUnauthenticatedDynamicClientRegistration() bool
Block() *bool
AuditLogRetention() *time.Duration
Features() feature.Features
@@ -41,15 +47,17 @@ type InstanceVerifier interface {
}
type instance struct {
id string
projectID string
appID string
clientID string
orgID string
defaultLanguage language.Tag
allowedLanguages []language.Tag
features feature.Features
executionTargets target.Router
id string
projectID string
appID string
clientID string
orgID string
defaultLanguage language.Tag
allowedLanguages []language.Tag
features feature.Features
executionTargets target.Router
enableDCR bool
allowUnauthenticatedDCR bool
}
func (i *instance) Block() *bool {
@@ -96,6 +104,14 @@ func (i *instance) EnableImpersonation() bool {
return false
}
func (i *instance) EnableDynamicClientRegistration() bool {
return i.enableDCR
}
func (i *instance) AllowUnauthenticatedDynamicClientRegistration() bool {
return i.enableDCR && i.allowUnauthenticatedDCR
}
func (i *instance) Features() feature.Features {
return i.features
}
+34
View File
@@ -131,6 +131,14 @@ func (m *mockInstance) EnableImpersonation() bool {
return false
}
func (m *mockInstance) EnableDynamicClientRegistration() bool {
return false
}
func (m *mockInstance) AllowUnauthenticatedDynamicClientRegistration() bool {
return false
}
func (m *mockInstance) Features() feature.Features {
return feature.Features{}
}
@@ -138,3 +146,29 @@ func (m *mockInstance) Features() feature.Features {
func (m *mockInstance) ExecutionRouter() target.Router {
return target.NewRouter(nil)
}
// Open registration must never be reachable while dynamic client registration itself is
// off, so that a single setting is enough to close the endpoint.
func Test_instance_dynamicClientRegistration(t *testing.T) {
t.Parallel()
tests := []struct {
name string
enabled bool
allowUnauthenticated bool
wantEnabled bool
wantAllowUnauthenticated bool
}{
{name: "disabled", enabled: false, allowUnauthenticated: false, wantEnabled: false, wantAllowUnauthenticated: false},
{name: "disabled but unauthenticated allowed", enabled: false, allowUnauthenticated: true, wantEnabled: false, wantAllowUnauthenticated: false},
{name: "enabled, token mode", enabled: true, allowUnauthenticated: false, wantEnabled: true, wantAllowUnauthenticated: false},
{name: "enabled, open registration", enabled: true, allowUnauthenticated: true, wantEnabled: true, wantAllowUnauthenticated: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
i := &instance{enableDCR: tt.enabled, allowUnauthenticatedDCR: tt.allowUnauthenticated}
assert.Equal(t, tt.wantEnabled, i.EnableDynamicClientRegistration())
assert.Equal(t, tt.wantAllowUnauthenticated, i.AllowUnauthenticatedDynamicClientRegistration())
})
}
}
@@ -26,6 +26,7 @@ var iamRoles = []string{
"ORG_USER_PERMISSION_EDITOR",
"ORG_PROJECT_PERMISSION_EDITOR",
"ORG_PROJECT_CREATOR",
"ORG_DYNAMIC_CLIENT_REGISTRAR",
"ORG_USER_SELF_MANAGER",
"ORG_ADMIN_IMPERSONATOR",
"ORG_END_USER_IMPERSONATOR",
@@ -309,6 +309,14 @@ func (m *mockInstance) EnableImpersonation() bool {
return false
}
func (m *mockInstance) EnableDynamicClientRegistration() bool {
return false
}
func (m *mockInstance) AllowUnauthenticatedDynamicClientRegistration() bool {
return false
}
func (m *mockInstance) Features() feature.Features {
return feature.Features{}
}
@@ -246,6 +246,10 @@ func securityPolicyToSettingsPb(policy *query.SecurityPolicy) *settings.Security
AllowedOrigins: policy.AllowedOrigins,
},
EnableImpersonation: policy.EnableImpersonation,
DynamicClientRegistration: &settings.DynamicClientRegistrationSettings{
Enabled: policy.EnableDynamicClientRegistration,
AllowUnauthenticated: policy.AllowUnauthenticatedDynamicClientRegistration,
},
}
}
@@ -254,6 +258,9 @@ func securitySettingsToCommand(req *settings.SetSecuritySettingsRequest) *comman
EnableIframeEmbedding: req.GetEmbeddedIframe().GetEnabled(),
AllowedOrigins: req.GetEmbeddedIframe().GetAllowedOrigins(),
EnableImpersonation: req.GetEnableImpersonation(),
EnableDynamicClientRegistration: req.GetDynamicClientRegistration().GetEnabled(),
AllowUnauthenticatedDynamicClientRegistration: req.GetDynamicClientRegistration().GetAllowUnauthenticated(),
}
}
@@ -536,20 +536,35 @@ func Test_securityPolicyToSettingsPb(t *testing.T) {
AllowedOrigins: []string{"foo", "bar"},
},
EnableImpersonation: true,
DynamicClientRegistration: &settings.DynamicClientRegistrationSettings{
Enabled: true,
AllowUnauthenticated: true,
},
}
got := securityPolicyToSettingsPb(&query.SecurityPolicy{
EnableIframeEmbedding: true,
AllowedOrigins: []string{"foo", "bar"},
EnableImpersonation: true,
EnableDynamicClientRegistration: true,
AllowUnauthenticatedDynamicClientRegistration: true,
})
assert.Equal(t, want, got)
}
func Test_securityPolicyToSettingsPb_dynamicClientRegistrationDisabled(t *testing.T) {
got := securityPolicyToSettingsPb(&query.SecurityPolicy{})
assert.Equal(t, &settings.DynamicClientRegistrationSettings{}, got.GetDynamicClientRegistration())
}
func Test_securitySettingsToCommand(t *testing.T) {
want := &command.SecurityPolicy{
EnableIframeEmbedding: true,
AllowedOrigins: []string{"foo", "bar"},
EnableImpersonation: true,
EnableDynamicClientRegistration: true,
AllowUnauthenticatedDynamicClientRegistration: true,
}
got := securitySettingsToCommand(&settings.SetSecuritySettingsRequest{
EmbeddedIframe: &settings.EmbeddedIframeSettings{
@@ -557,6 +572,18 @@ func Test_securitySettingsToCommand(t *testing.T) {
AllowedOrigins: []string{"foo", "bar"},
},
EnableImpersonation: true,
DynamicClientRegistration: &settings.DynamicClientRegistrationSettings{
Enabled: true,
AllowUnauthenticated: true,
},
})
assert.Equal(t, want, got)
}
// A request that omits dynamic_client_registration must leave the feature off rather than
// panicking on the nil message.
func Test_securitySettingsToCommand_dynamicClientRegistrationOmitted(t *testing.T) {
got := securitySettingsToCommand(&settings.SetSecuritySettingsRequest{})
assert.False(t, got.EnableDynamicClientRegistration)
assert.False(t, got.AllowUnauthenticatedDynamicClientRegistration)
}
@@ -397,6 +397,14 @@ func (m *mockInstance) EnableImpersonation() bool {
return false
}
func (m *mockInstance) EnableDynamicClientRegistration() bool {
return false
}
func (m *mockInstance) AllowUnauthenticatedDynamicClientRegistration() bool {
return false
}
func (m *mockInstance) Features() feature.Features {
return feature.Features{}
}
+559
View File
@@ -0,0 +1,559 @@
package oidc
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/muhlemmer/gu"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/command"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/query"
"github.com/zitadel/zitadel/internal/zerrors"
)
// registrationMaxBodyBytes bounds the size of a dynamic client registration request body.
const registrationMaxBodyBytes = 100 * 1024
// dynamicClientRegistration handles POST requests to the OAuth 2.0 Dynamic Client
// Registration endpoint (RFC 7591). The route is always mounted; it only registers clients
// when dynamic client registration is enabled in the instance's security settings,
// otherwise it behaves as if the endpoint did not exist.
func (s *Server) dynamicClientRegistration(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if !authz.GetInstance(ctx).EnableDynamicClientRegistration() {
http.NotFound(w, r)
return
}
ctx, resourceOwner, err := s.dynamicClientRegistrationResourceOwner(ctx, r)
if err != nil {
switch {
// The caller authenticated but is not allowed to register clients: RFC 6750 §3.1
// insufficient_scope, rather than pretending the token itself is invalid.
case errors.Is(err, errRegistrationForbidden):
s.writeRegistrationForbidden(ctx, w)
// Only missing/invalid credentials are a 401; unexpected errors (e.g. a failed
// token lookup) must not be masked as invalid_token.
case zerrors.IsUnauthenticated(err), zerrors.IsPermissionDenied(err):
s.writeRegistrationUnauthorized(ctx, w)
default:
s.writeRegistrationServerError(ctx, w, err)
}
return
}
var req clientRegistrationRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, registrationMaxBodyBytes)).Decode(&req); err != nil {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
s.writeRegistrationJSON(ctx, w, http.StatusRequestEntityTooLarge, newRegistrationError(registrationErrorInvalidClientMetadata, "the request body is too large"))
return
}
s.writeRegistrationError(ctx, w, newRegistrationError(registrationErrorInvalidClientMetadata, "the request body could not be parsed"))
return
}
app, regErr := req.toOIDCApp()
if regErr != nil {
s.writeRegistrationError(ctx, w, regErr)
return
}
projectID, err := s.ensureDCRProject(ctx, resourceOwner)
if err != nil {
s.writeRegistrationServerError(ctx, w, err)
return
}
registered, err := s.command.AddDynamicOIDCClient(ctx, projectID, resourceOwner, app)
if err != nil {
s.writeRegistrationCommandError(ctx, w, err)
return
}
s.writeRegistrationJSON(ctx, w, http.StatusCreated, newClientRegistrationResponse(registered, req.ClientName, time.Now().Unix()))
}
// dynamicClientRegistrationResourceOwner authorizes the registration and returns the
// organization the client is homed in.
//
// In token mode (the default) the caller presents an access token, the client is homed in
// the token's organization and the token's user must hold the org-scoped
// project.app.register_dynamic permission there. That permission is deliberately not
// project.app.write: a service user can be allowed to self-register clients — through the
// dedicated ORG_DYNAMIC_CLIENT_REGISTRAR role — without gaining write access to existing
// applications.
//
// In open mode (allow_unauthenticated in the instance's security settings) no token is
// required and no permission is checked, as required by the Model Context Protocol flow.
// Those clients are homed in the instance's default organization.
//
// The check runs before any state is created, so it also gates the auto-provisioning of the
// organization's dedicated DCR project.
func (s *Server) dynamicClientRegistrationResourceOwner(ctx context.Context, r *http.Request) (context.Context, string, error) {
token := bearerToken(r)
if token == "" {
if !authz.GetInstance(ctx).AllowUnauthenticatedDynamicClientRegistration() {
return ctx, "", zerrors.ThrowUnauthenticated(nil, "OIDC-Eich8", "Errors.Token.Invalid")
}
return ctx, authz.GetInstance(ctx).DefaultOrganisationID(), nil
}
accessToken, err := s.verifyAccessToken(ctx, token)
if err != nil {
return ctx, "", err
}
// The permission check resolves the caller's memberships from the context, which the
// OIDC endpoints do not populate: unlike the gRPC APIs they are not behind the
// authorization interceptor. Derive it from the verified token, as the token exchange
// does for the impersonating actor.
ctx = authz.SetCtxData(ctx, authz.CtxData{
UserID: accessToken.userID,
OrgID: accessToken.resourceOwner,
})
if err := s.command.CheckPermissionRegisterDynamicClient(ctx, accessToken.resourceOwner); err != nil {
// verifyAccessToken also reports invalid tokens as permission denied, so mark the
// authorization failure explicitly to keep 401 and 403 apart.
return ctx, "", fmt.Errorf("%w: %w", errRegistrationForbidden, err)
}
return ctx, accessToken.resourceOwner, nil
}
// errRegistrationForbidden marks a caller that presented a valid access token but does not
// hold the project.app.register_dynamic permission in the token's organization.
var errRegistrationForbidden = errors.New("dynamic client registration not permitted")
// ensureDCRProject returns the dedicated project that holds dynamically registered clients
// for the organization, creating it on first use. The common case (the project already
// exists) is served from the projection; the creation and the concurrent-creation race are
// delegated to the command, which resolves them strongly consistently from the eventstore.
func (s *Server) ensureDCRProject(ctx context.Context, resourceOwner string) (string, error) {
projectID, err := s.dcrProjectIDFromProjection(ctx, resourceOwner)
if err != nil || projectID != "" {
return projectID, err
}
return s.command.EnsureDCRProject(ctx, resourceOwner)
}
func (s *Server) dcrProjectIDFromProjection(ctx context.Context, resourceOwner string) (string, error) {
nameQuery, err := query.NewProjectNameSearchQuery(query.TextEquals, command.DCRProjectName)
if err != nil {
return "", err
}
ownerQuery, err := query.NewProjectResourceOwnerSearchQuery(resourceOwner)
if err != nil {
return "", err
}
// No permission check: access to the registration endpoint is the authorization
// boundary for dynamic client registration.
projects, err := s.query.SearchProjects(ctx, &query.ProjectSearchQueries{Queries: []query.SearchQuery{nameQuery, ownerQuery}}, nil)
if err != nil {
return "", err
}
if len(projects.Projects) == 0 {
return "", nil
}
return projects.Projects[0].ID, nil
}
func bearerToken(r *http.Request) string {
auth := r.Header.Get("Authorization")
scheme, token, found := strings.Cut(auth, " ")
if !found || !strings.EqualFold(scheme, "Bearer") {
return ""
}
return strings.TrimSpace(token)
}
func (s *Server) writeRegistrationJSON(ctx context.Context, w http.ResponseWriter, status int, body any) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(body); err != nil {
s.getLogger(ctx).ErrorContext(ctx, "dynamic client registration: encode response", "err", err)
}
}
func (s *Server) writeRegistrationError(ctx context.Context, w http.ResponseWriter, regErr *registrationError) {
s.writeRegistrationJSON(ctx, w, http.StatusBadRequest, regErr)
}
func (s *Server) writeRegistrationUnauthorized(ctx context.Context, w http.ResponseWriter) {
w.Header().Set("WWW-Authenticate", `Bearer error="invalid_token"`)
s.writeRegistrationJSON(ctx, w, http.StatusUnauthorized, newRegistrationError("invalid_token", "a valid access token is required to register a client"))
}
func (s *Server) writeRegistrationForbidden(ctx context.Context, w http.ResponseWriter) {
w.Header().Set("WWW-Authenticate", `Bearer error="insufficient_scope"`)
s.writeRegistrationJSON(ctx, w, http.StatusForbidden, newRegistrationError("insufficient_scope", "the access token is not allowed to register clients"))
}
func (s *Server) writeRegistrationServerError(ctx context.Context, w http.ResponseWriter, err error) {
s.getLogger(ctx).ErrorContext(ctx, "dynamic client registration", "err", err)
s.writeRegistrationJSON(ctx, w, http.StatusInternalServerError, newRegistrationError("server_error", "the client could not be registered"))
}
func (s *Server) writeRegistrationCommandError(ctx context.Context, w http.ResponseWriter, err error) {
if zerrors.IsErrorInvalidArgument(err) {
s.writeRegistrationError(ctx, w, newRegistrationError(registrationErrorInvalidClientMetadata, "the requested client metadata is not supported"))
return
}
s.writeRegistrationServerError(ctx, w, err)
}
// OAuth 2.0 Dynamic Client Registration error codes as defined in RFC 7591 §3.2.2.
const (
registrationErrorInvalidRedirectURI = "invalid_redirect_uri"
registrationErrorInvalidClientMetadata = "invalid_client_metadata"
)
// registrationError is an OAuth 2.0 Dynamic Client Registration error response
// (RFC 7591 §3.2.2). Invalid client metadata is returned with HTTP status 400; the same
// shape is reused for the 401 (invalid_token) and 500 (server_error) responses.
type registrationError struct {
ErrorType string `json:"error"`
ErrorDescription string `json:"error_description,omitempty"`
}
func (e *registrationError) Error() string {
if e.ErrorDescription == "" {
return e.ErrorType
}
return e.ErrorType + ": " + e.ErrorDescription
}
func newRegistrationError(errorType, description string) *registrationError {
return &registrationError{ErrorType: errorType, ErrorDescription: description}
}
// clientRegistrationRequest holds the client metadata of an OAuth 2.0 Dynamic Client
// Registration request (RFC 7591 §2, extended by OpenID Connect Dynamic Client
// Registration 1.0 §2). Only the members relevant for a ZITADEL OIDC application are
// interpreted. Free-form metadata (client_uri, logo_uri, contacts, etc.) is accepted but
// not persisted in this version. Members that imply unsupported behaviour (jwks/jwks_uri,
// private_key_jwt) are rejected explicitly.
type clientRegistrationRequest struct {
RedirectURIs []string `json:"redirect_uris,omitempty"`
ResponseTypes []string `json:"response_types,omitempty"`
GrantTypes []string `json:"grant_types,omitempty"`
ApplicationType string `json:"application_type,omitempty"`
ClientName string `json:"client_name,omitempty"`
TokenEndpointAuthMethod string `json:"token_endpoint_auth_method,omitempty"`
PostLogoutRedirectURIs []string `json:"post_logout_redirect_uris,omitempty"`
JWKsURI string `json:"jwks_uri,omitempty"`
JWKs json.RawMessage `json:"jwks,omitempty"`
}
// clientRegistrationResponse is the client information response of a successful
// registration (RFC 7591 §3.2.1).
type clientRegistrationResponse struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret,omitempty"`
ClientIDIssuedAt int64 `json:"client_id_issued_at,omitempty"`
ClientSecretExpiresAt *int64 `json:"client_secret_expires_at,omitempty"`
RedirectURIs []string `json:"redirect_uris,omitempty"`
ResponseTypes []string `json:"response_types,omitempty"`
GrantTypes []string `json:"grant_types,omitempty"`
ApplicationType string `json:"application_type,omitempty"`
ClientName string `json:"client_name,omitempty"`
TokenEndpointAuthMethod string `json:"token_endpoint_auth_method,omitempty"`
PostLogoutRedirectURIs []string `json:"post_logout_redirect_uris,omitempty"`
}
// toOIDCApp maps the registration request to a domain OIDC application, applying the RFC
// 7591 defaults and rejecting unsupported metadata. The returned application is not yet
// persisted; the application name and the client credentials are assigned by the command.
func (req *clientRegistrationRequest) toOIDCApp() (*domain.OIDCApp, *registrationError) {
if req.JWKsURI != "" || len(req.JWKs) > 0 {
return nil, newRegistrationError(registrationErrorInvalidClientMetadata, "jwks and jwks_uri are not supported")
}
redirectURIs := trimSpaceSlice(req.RedirectURIs)
if len(redirectURIs) == 0 {
return nil, newRegistrationError(registrationErrorInvalidRedirectURI, "at least one redirect_uri is required")
}
applicationType, regErr := registrationApplicationTypeToDomain(req.ApplicationType, redirectURIs)
if regErr != nil {
return nil, regErr
}
authMethod, regErr := registrationAuthMethodToDomain(req.TokenEndpointAuthMethod, applicationType)
if regErr != nil {
return nil, regErr
}
grantTypes, regErr := registrationGrantTypesToDomain(req.GrantTypes)
if regErr != nil {
return nil, regErr
}
responseTypes, regErr := registrationResponseTypesToDomain(req.ResponseTypes)
if regErr != nil {
return nil, regErr
}
app := &domain.OIDCApp{
AppName: strings.TrimSpace(req.ClientName),
RedirectUris: redirectURIs,
ResponseTypes: responseTypes,
GrantTypes: grantTypes,
ApplicationType: gu.Ptr(applicationType),
AuthMethodType: gu.Ptr(authMethod),
PostLogoutRedirectUris: trimSpaceSlice(req.PostLogoutRedirectURIs),
OIDCVersion: gu.Ptr(domain.OIDCVersionV1),
AccessTokenType: gu.Ptr(domain.OIDCTokenTypeBearer),
}
if !app.IsValid() {
return nil, newRegistrationError(registrationErrorInvalidClientMetadata, "the requested grant and response type combination is not supported")
}
if compliance := domain.GetOIDCV1Compliance(app.ApplicationType, app.GrantTypes, app.AuthMethodType, app.RedirectUris); compliance.NoneCompliant {
return nil, registrationComplianceError(compliance)
}
return app, nil
}
// registrationComplianceError maps a domain compliance failure to the matching RFC 7591
// error. Auth-method and grant/response combination problems are client metadata errors;
// redirect URI scheme problems are reported as an invalid redirect URI. The internal
// compliance keys (see internal/domain/application_oidc.go) are not exposed to the client.
func registrationComplianceError(compliance *domain.Compliance) *registrationError {
for _, problem := range compliance.Problems {
if strings.Contains(problem, "AuthMethodType") || strings.Contains(problem, "GrantType") || strings.Contains(problem, "Combinations") {
return newRegistrationError(registrationErrorInvalidClientMetadata, "the requested client metadata is not supported")
}
}
return newRegistrationError(registrationErrorInvalidRedirectURI, "one or more redirect_uris are invalid for the requested application type")
}
func registrationAuthMethodToDomain(method string, applicationType domain.OIDCApplicationType) (domain.OIDCAuthMethodType, *registrationError) {
switch method {
case "":
// Default the auth method to the application type. Native and user-agent
// applications must be public (none); web applications default to
// client_secret_basic per RFC 7591 §2.
if applicationType == domain.OIDCApplicationTypeNative || applicationType == domain.OIDCApplicationTypeUserAgent {
return domain.OIDCAuthMethodTypeNone, nil
}
return domain.OIDCAuthMethodTypeBasic, nil
case "client_secret_basic":
return domain.OIDCAuthMethodTypeBasic, nil
case "client_secret_post":
return domain.OIDCAuthMethodTypePost, nil
case "none":
return domain.OIDCAuthMethodTypeNone, nil
default:
return 0, newRegistrationError(registrationErrorInvalidClientMetadata, "token_endpoint_auth_method "+method+" is not supported")
}
}
func registrationGrantTypesToDomain(grantTypes []string) ([]domain.OIDCGrantType, *registrationError) {
if len(grantTypes) == 0 {
// RFC 7591 §2: authorization_code is the default.
return []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode}, nil
}
mapped := make([]domain.OIDCGrantType, 0, len(grantTypes))
for _, grantType := range grantTypes {
switch grantType {
case "authorization_code":
mapped = append(mapped, domain.OIDCGrantTypeAuthorizationCode)
case "refresh_token":
mapped = append(mapped, domain.OIDCGrantTypeRefreshToken)
case "implicit":
mapped = append(mapped, domain.OIDCGrantTypeImplicit)
case "urn:ietf:params:oauth:grant-type:device_code":
mapped = append(mapped, domain.OIDCGrantTypeDeviceCode)
case "urn:ietf:params:oauth:grant-type:token-exchange":
mapped = append(mapped, domain.OIDCGrantTypeTokenExchange)
default:
return nil, newRegistrationError(registrationErrorInvalidClientMetadata, "grant_type "+grantType+" is not supported")
}
}
return mapped, nil
}
func registrationResponseTypesToDomain(responseTypes []string) ([]domain.OIDCResponseType, *registrationError) {
if len(responseTypes) == 0 {
// RFC 7591 §2: code is the default.
return []domain.OIDCResponseType{domain.OIDCResponseTypeCode}, nil
}
mapped := make([]domain.OIDCResponseType, 0, len(responseTypes))
for _, responseType := range responseTypes {
switch normalizeResponseType(responseType) {
case "code":
mapped = append(mapped, domain.OIDCResponseTypeCode)
case "id_token":
mapped = append(mapped, domain.OIDCResponseTypeIDToken)
case "id_token token":
mapped = append(mapped, domain.OIDCResponseTypeIDTokenToken)
default:
return nil, newRegistrationError(registrationErrorInvalidClientMetadata, "response_type "+responseType+" is not supported")
}
}
return mapped, nil
}
// normalizeResponseType sorts the space separated values of a response_type so that the
// order of "token" and "id_token" does not matter.
func normalizeResponseType(responseType string) string {
fields := strings.Fields(responseType)
hasToken, hasIDToken, hasCode := false, false, false
for _, field := range fields {
switch field {
case "token":
hasToken = true
case "id_token":
hasIDToken = true
case "code":
hasCode = true
}
}
switch {
case hasCode && !hasToken && !hasIDToken:
return "code"
case hasIDToken && hasToken && !hasCode:
return "id_token token"
case hasIDToken && !hasToken && !hasCode:
return "id_token"
default:
return strings.Join(fields, " ")
}
}
// registrationApplicationTypeToDomain resolves the application type of a registration
// request. An explicit application_type is always honoured; it is only inferred when the
// member is absent.
//
// OpenID Connect Dynamic Client Registration 1.0 §2 defaults to web, but application_type is
// an OpenID Connect member: clients that only implement RFC 7591, which does not define it,
// omit it. Native hosts doing so (Cursor and other MCP clients) register custom-scheme
// redirect URIs, which ZITADEL reserves for native applications, so the web default would
// reject them for a redirect URI that is in fact valid for what they are. Infer native in
// exactly that case; everything that is accepted today keeps its current type.
func registrationApplicationTypeToDomain(applicationType string, redirectURIs []string) (domain.OIDCApplicationType, *registrationError) {
switch applicationType {
case "":
if domain.OIDCRedirectURIsRequireNative(redirectURIs) {
return domain.OIDCApplicationTypeNative, nil
}
return domain.OIDCApplicationTypeWeb, nil
case "web":
return domain.OIDCApplicationTypeWeb, nil
case "native":
return domain.OIDCApplicationTypeNative, nil
default:
return 0, newRegistrationError(registrationErrorInvalidClientMetadata, "application_type "+applicationType+" is not supported")
}
}
// newClientRegistrationResponse builds the RFC 7591 §3.2.1 response from the registered
// application. The client name is echoed from the request, as the human-readable name is
// not persisted as-is in this version.
func newClientRegistrationResponse(app *domain.OIDCApp, clientName string, issuedAt int64) *clientRegistrationResponse {
resp := &clientRegistrationResponse{
ClientID: app.ClientID,
ClientSecret: app.ClientSecretString,
ClientIDIssuedAt: issuedAt,
RedirectURIs: app.RedirectUris,
ResponseTypes: responseTypesToRegistration(app.ResponseTypes),
GrantTypes: grantTypesToRegistration(app.GrantTypes),
ApplicationType: applicationTypeToRegistration(gu.Value(app.ApplicationType)),
ClientName: strings.TrimSpace(clientName),
TokenEndpointAuthMethod: authMethodToRegistration(gu.Value(app.AuthMethodType)),
PostLogoutRedirectURIs: app.PostLogoutRedirectUris,
}
if app.ClientSecretString != "" {
// RFC 7591 §3.2.1: client_secret_expires_at is REQUIRED if client_secret is
// issued. 0 means the secret never expires.
resp.ClientSecretExpiresAt = gu.Ptr(int64(0))
}
return resp
}
func authMethodToRegistration(authMethod domain.OIDCAuthMethodType) string {
switch authMethod {
case domain.OIDCAuthMethodTypeBasic:
return "client_secret_basic"
case domain.OIDCAuthMethodTypePost:
return "client_secret_post"
case domain.OIDCAuthMethodTypeNone:
return "none"
case domain.OIDCAuthMethodTypePrivateKeyJWT:
return "private_key_jwt"
default:
return ""
}
}
func grantTypesToRegistration(grantTypes []domain.OIDCGrantType) []string {
mapped := make([]string, 0, len(grantTypes))
for _, grantType := range grantTypes {
switch grantType {
case domain.OIDCGrantTypeAuthorizationCode:
mapped = append(mapped, "authorization_code")
case domain.OIDCGrantTypeRefreshToken:
mapped = append(mapped, "refresh_token")
case domain.OIDCGrantTypeImplicit:
mapped = append(mapped, "implicit")
case domain.OIDCGrantTypeDeviceCode:
mapped = append(mapped, "urn:ietf:params:oauth:grant-type:device_code")
case domain.OIDCGrantTypeTokenExchange:
mapped = append(mapped, "urn:ietf:params:oauth:grant-type:token-exchange")
}
}
return mapped
}
func responseTypesToRegistration(responseTypes []domain.OIDCResponseType) []string {
mapped := make([]string, 0, len(responseTypes))
for _, responseType := range responseTypes {
switch responseType {
case domain.OIDCResponseTypeCode:
mapped = append(mapped, "code")
case domain.OIDCResponseTypeIDToken:
mapped = append(mapped, "id_token")
case domain.OIDCResponseTypeIDTokenToken:
mapped = append(mapped, "id_token token")
case domain.OIDCResponseTypeUnspecified:
// not exposed through dynamic client registration
}
}
return mapped
}
func applicationTypeToRegistration(applicationType domain.OIDCApplicationType) string {
switch applicationType {
case domain.OIDCApplicationTypeWeb:
return "web"
case domain.OIDCApplicationTypeNative:
return "native"
case domain.OIDCApplicationTypeUserAgent:
return "user_agent"
default:
return ""
}
}
func trimSpaceSlice(values []string) []string {
if len(values) == 0 {
return nil
}
trimmed := make([]string, 0, len(values))
for _, value := range values {
value = strings.TrimSpace(value)
if value != "" {
trimmed = append(trimmed, value)
}
}
if len(trimmed) == 0 {
return nil
}
return trimmed
}
@@ -0,0 +1,370 @@
package oidc
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/muhlemmer/gu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/zerrors"
)
func Test_clientRegistrationRequest_toOIDCApp(t *testing.T) {
t.Parallel()
tests := []struct {
name string
req *clientRegistrationRequest
want *domain.OIDCApp
wantErr string // expected registrationError.ErrorType, empty means success
}{
{
name: "defaults applied",
req: &clientRegistrationRequest{
RedirectURIs: []string{"https://app.example.com/callback"},
},
want: &domain.OIDCApp{
RedirectUris: []string{"https://app.example.com/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeWeb),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypeBasic),
OIDCVersion: gu.Ptr(domain.OIDCVersionV1),
AccessTokenType: gu.Ptr(domain.OIDCTokenTypeBearer),
},
},
{
name: "explicit values mapped and trimmed",
req: &clientRegistrationRequest{
RedirectURIs: []string{" https://app.example.com/callback "},
ResponseTypes: []string{"code"},
GrantTypes: []string{"authorization_code", "refresh_token"},
ApplicationType: "native",
ClientName: "My MCP Client",
TokenEndpointAuthMethod: "none",
PostLogoutRedirectURIs: []string{"https://app.example.com/logout"},
},
want: &domain.OIDCApp{
AppName: "My MCP Client",
RedirectUris: []string{"https://app.example.com/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode, domain.OIDCGrantTypeRefreshToken},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeNative),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypeNone),
PostLogoutRedirectUris: []string{"https://app.example.com/logout"},
OIDCVersion: gu.Ptr(domain.OIDCVersionV1),
AccessTokenType: gu.Ptr(domain.OIDCTokenTypeBearer),
},
},
{
name: "native application without auth method defaults to public client",
req: &clientRegistrationRequest{
RedirectURIs: []string{"https://app.example.com/callback"},
ApplicationType: "native",
},
want: &domain.OIDCApp{
RedirectUris: []string{"https://app.example.com/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeNative),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypeNone),
OIDCVersion: gu.Ptr(domain.OIDCVersionV1),
AccessTokenType: gu.Ptr(domain.OIDCTokenTypeBearer),
},
},
{
name: "native application with custom scheme redirect",
req: &clientRegistrationRequest{
RedirectURIs: []string{"com.example.app:/callback"},
ApplicationType: "native",
},
want: &domain.OIDCApp{
RedirectUris: []string{"com.example.app:/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeNative),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypeNone),
OIDCVersion: gu.Ptr(domain.OIDCVersionV1),
AccessTokenType: gu.Ptr(domain.OIDCTokenTypeBearer),
},
},
{
name: "native application with confidential auth method rejected as client metadata",
req: &clientRegistrationRequest{
RedirectURIs: []string{"https://app.example.com/callback"},
ApplicationType: "native",
TokenEndpointAuthMethod: "client_secret_basic",
},
wantErr: registrationErrorInvalidClientMetadata,
},
{
name: "client_secret_post auth method mapped",
req: &clientRegistrationRequest{
RedirectURIs: []string{"https://app.example.com/callback"},
TokenEndpointAuthMethod: "client_secret_post",
},
want: &domain.OIDCApp{
RedirectUris: []string{"https://app.example.com/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeWeb),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypePost),
OIDCVersion: gu.Ptr(domain.OIDCVersionV1),
AccessTokenType: gu.Ptr(domain.OIDCTokenTypeBearer),
},
},
{
name: "native loopback http redirect allowed",
req: &clientRegistrationRequest{
RedirectURIs: []string{"http://127.0.0.1:8080/callback"},
ApplicationType: "native",
TokenEndpointAuthMethod: "none",
},
want: &domain.OIDCApp{
RedirectUris: []string{"http://127.0.0.1:8080/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeNative),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypeNone),
OIDCVersion: gu.Ptr(domain.OIDCVersionV1),
AccessTokenType: gu.Ptr(domain.OIDCTokenTypeBearer),
},
},
{
name: "jwks_uri rejected",
req: &clientRegistrationRequest{
RedirectURIs: []string{"https://app.example.com/callback"},
JWKsURI: "https://app.example.com/jwks.json",
},
wantErr: registrationErrorInvalidClientMetadata,
},
{
name: "private_key_jwt rejected",
req: &clientRegistrationRequest{
RedirectURIs: []string{"https://app.example.com/callback"},
TokenEndpointAuthMethod: "private_key_jwt",
},
wantErr: registrationErrorInvalidClientMetadata,
},
{
name: "unknown grant_type rejected",
req: &clientRegistrationRequest{
RedirectURIs: []string{"https://app.example.com/callback"},
GrantTypes: []string{"client_credentials"},
},
wantErr: registrationErrorInvalidClientMetadata,
},
{
name: "unknown response_type rejected",
req: &clientRegistrationRequest{
RedirectURIs: []string{"https://app.example.com/callback"},
ResponseTypes: []string{"token"},
},
wantErr: registrationErrorInvalidClientMetadata,
},
{
name: "unknown application_type rejected",
req: &clientRegistrationRequest{
RedirectURIs: []string{"https://app.example.com/callback"},
ApplicationType: "service",
},
wantErr: registrationErrorInvalidClientMetadata,
},
{
name: "missing redirect_uris rejected",
req: &clientRegistrationRequest{
ApplicationType: "web",
},
wantErr: registrationErrorInvalidRedirectURI,
},
{
name: "custom scheme redirect for web app rejected",
req: &clientRegistrationRequest{
RedirectURIs: []string{"myapp://callback"},
ApplicationType: "web",
},
wantErr: registrationErrorInvalidRedirectURI,
},
{
// Cursor and other native MCP hosts speak RFC 7591, which has no
// application_type, so they omit it and send a custom scheme.
name: "custom scheme redirect without application_type infers native",
req: &clientRegistrationRequest{
RedirectURIs: []string{"cursor://anysphere.cursor-retrieval/oauth/callback"},
},
want: &domain.OIDCApp{
RedirectUris: []string{"cursor://anysphere.cursor-retrieval/oauth/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeNative),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypeNone),
OIDCVersion: gu.Ptr(domain.OIDCVersionV1),
AccessTokenType: gu.Ptr(domain.OIDCTokenTypeBearer),
},
},
{
// The inference must not widen anything else: an http loopback redirect stays
// a web application, exactly as before, and keeps its confidential default.
name: "loopback redirect without application_type stays web",
req: &clientRegistrationRequest{
RedirectURIs: []string{"http://localhost:8080/callback"},
},
want: &domain.OIDCApp{
RedirectUris: []string{"http://localhost:8080/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeWeb),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypeBasic),
OIDCVersion: gu.Ptr(domain.OIDCVersionV1),
AccessTokenType: gu.Ptr(domain.OIDCTokenTypeBearer),
},
},
{
// Inference only fills an absent member: a client that asks for a confidential
// auth method with a custom scheme is a genuine conflict, reported as such.
name: "custom scheme with confidential auth method rejected as client metadata",
req: &clientRegistrationRequest{
RedirectURIs: []string{"myapp://callback"},
TokenEndpointAuthMethod: "client_secret_basic",
},
wantErr: registrationErrorInvalidClientMetadata,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := tt.req.toOIDCApp()
if tt.wantErr != "" {
require.NotNil(t, err)
assert.Equal(t, tt.wantErr, err.ErrorType)
return
}
require.Nil(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func Test_normalizeResponseType(t *testing.T) {
t.Parallel()
tests := []struct {
in string
want string
}{
{"code", "code"},
{"id_token", "id_token"},
{"id_token token", "id_token token"},
{"token id_token", "id_token token"},
{"token", "token"},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, normalizeResponseType(tt.in))
})
}
}
func Test_Server_dynamicClientRegistrationResourceOwner_withoutToken(t *testing.T) {
t.Parallel()
request := httptest.NewRequest(http.MethodPost, "/oauth/v2/register", nil)
t.Run("no token and open registration disabled is unauthenticated", func(t *testing.T) {
t.Parallel()
s := &Server{}
ctx := authz.NewMockContext("instance", "org", "", authz.WithMockDynamicClientRegistration(true, false))
_, _, err := s.dynamicClientRegistrationResourceOwner(ctx, request)
require.Error(t, err)
assert.True(t, zerrors.IsUnauthenticated(err))
})
t.Run("no token and open registration enabled uses the default organization", func(t *testing.T) {
t.Parallel()
s := &Server{}
ctx := authz.NewMockContext("instance", "default-org", "", authz.WithMockDynamicClientRegistration(true, true))
_, resourceOwner, err := s.dynamicClientRegistrationResourceOwner(ctx, request)
require.NoError(t, err)
assert.Equal(t, "default-org", resourceOwner)
})
}
func Test_bearerToken(t *testing.T) {
t.Parallel()
newRequest := func(authorization string) *http.Request {
r := httptest.NewRequest(http.MethodPost, "/oauth/v2/register", nil)
if authorization != "" {
r.Header.Set("Authorization", authorization)
}
return r
}
tests := []struct {
name string
authorization string
want string
}{
{"no header", "", ""},
{"bearer token", "Bearer abc123", "abc123"},
{"case insensitive scheme", "bearer abc123", "abc123"},
{"basic auth ignored", "Basic dXNlcjpwYXNz", ""},
{"trims spaces", "Bearer abc123 ", "abc123"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, bearerToken(newRequest(tt.authorization)))
})
}
}
func Test_newClientRegistrationResponse(t *testing.T) {
t.Parallel()
t.Run("confidential client returns secret and expiry", func(t *testing.T) {
t.Parallel()
app := &domain.OIDCApp{
ClientID: "client1",
ClientSecretString: "secret",
RedirectUris: []string{"https://app.example.com/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeWeb),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypeBasic),
}
got := newClientRegistrationResponse(app, "My Client", 1700000000)
assert.Equal(t, &clientRegistrationResponse{
ClientID: "client1",
ClientSecret: "secret",
ClientIDIssuedAt: 1700000000,
ClientSecretExpiresAt: gu.Ptr(int64(0)),
RedirectURIs: []string{"https://app.example.com/callback"},
ResponseTypes: []string{"code"},
GrantTypes: []string{"authorization_code"},
ApplicationType: "web",
ClientName: "My Client",
TokenEndpointAuthMethod: "client_secret_basic",
}, got)
})
t.Run("public client returns no secret nor expiry", func(t *testing.T) {
t.Parallel()
app := &domain.OIDCApp{
ClientID: "client2",
RedirectUris: []string{"https://app.example.com/callback"},
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
ApplicationType: gu.Ptr(domain.OIDCApplicationTypeNative),
AuthMethodType: gu.Ptr(domain.OIDCAuthMethodTypeNone),
}
got := newClientRegistrationResponse(app, "", 1700000000)
assert.Empty(t, got.ClientSecret)
assert.Nil(t, got.ClientSecretExpiresAt)
assert.Equal(t, "none", got.TokenEndpointAuthMethod)
assert.Equal(t, "native", got.ApplicationType)
})
}
@@ -0,0 +1,251 @@
//go:build integration
package oidc_test
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zitadel/oidc/v3/pkg/oidc"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/integration"
oidc_pb "github.com/zitadel/zitadel/pkg/grpc/oidc/v2"
"github.com/zitadel/zitadel/pkg/grpc/settings/v2"
)
// TestServer_DynamicClientRegistration covers the OAuth 2.0 Dynamic Client Registration
// endpoint (RFC 7591) in open mode, where no access token is required.
func TestServer_DynamicClientRegistration(t *testing.T) {
issuer := Instance.OIDCIssuer()
enableDynamicClientRegistration(t, CTXIAM, Instance, true)
t.Run("discovery advertises the registration endpoint", func(t *testing.T) {
discovery := fetchDiscovery(t, issuer)
assert.Equal(t, issuer+"/oauth/v2/register", discovery.RegistrationEndpoint)
})
t.Run("public client without secret, usable for authorization code with PKCE", func(t *testing.T) {
status, body := registerDynamicClient(t, issuer, "", map[string]any{
"client_name": "integration public client",
"redirect_uris": []string{redirectURI},
"token_endpoint_auth_method": "none",
})
require.Equal(t, http.StatusCreated, status)
clientID, _ := body["client_id"].(string)
require.NotEmpty(t, clientID)
assert.Empty(t, body["client_secret"])
// The returned client_id must be usable for an authorization code + PKCE flow.
// The client and its dedicated project were just created, so wait for the
// projection to catch up before starting the authorization flow.
var authRequestID string
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
require.EventuallyWithT(t, func(tt *assert.CollectT) {
_, id, err := Instance.CreateOIDCAuthRequest(CTX, clientID, Instance.Users.Get(integration.UserTypeLogin).ID, redirectURI, oidc.ScopeOpenID)
assert.NoError(tt, err)
authRequestID = id
}, retryDuration, tick, "registered client not usable for authorization")
sessionID, sessionToken, _, _ := Instance.CreateVerifiedWebAuthNSession(t, CTXLOGIN, User.GetUserId())
linkResp, err := Instance.Client.OIDCv2.CreateCallback(CTXLOGIN, &oidc_pb.CreateCallbackRequest{
AuthRequestId: authRequestID,
CallbackKind: &oidc_pb.CreateCallbackRequest_Session{
Session: &oidc_pb.Session{
SessionId: sessionID,
SessionToken: sessionToken,
},
},
})
require.NoError(t, err)
code := assertCodeResponse(t, linkResp.GetCallbackUrl())
tokens, err := exchangeTokens(t, Instance, clientID, code, redirectURI)
require.NoError(t, err)
assert.NotEmpty(t, tokens.AccessToken)
// An Authorization header is always treated as a registration token, even with open
// registration enabled: the request must not fall back to anonymous registration.
// This token belongs to an ordinary user without project.app.register_dynamic, so
// it is rejected rather than silently registering a client.
tokenStatus, tokenBody := registerDynamicClient(t, issuer, tokens.AccessToken, map[string]any{
"client_name": "integration token-mode client",
"redirect_uris": []string{redirectURI},
})
require.Equal(t, http.StatusForbidden, tokenStatus)
assert.Equal(t, "insufficient_scope", tokenBody["error"])
assert.Empty(t, tokenBody["client_id"])
})
t.Run("confidential client returns a secret", func(t *testing.T) {
status, body := registerDynamicClient(t, issuer, "", map[string]any{
"client_name": "integration confidential client",
"redirect_uris": []string{redirectURI},
"token_endpoint_auth_method": "client_secret_basic",
})
require.Equal(t, http.StatusCreated, status)
assert.NotEmpty(t, body["client_id"])
assert.NotEmpty(t, body["client_secret"])
})
t.Run("invalid redirect uri is rejected", func(t *testing.T) {
status, body := registerDynamicClient(t, issuer, "", map[string]any{
"redirect_uris": []string{"myapp://callback"},
"application_type": "web",
})
assert.Equal(t, http.StatusBadRequest, status)
assert.Equal(t, "invalid_redirect_uri", body["error"])
})
t.Run("malformed request body is rejected", func(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, issuer+"/oauth/v2/register", bytes.NewReader([]byte("{not valid json")))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
})
t.Run("missing redirect uri is rejected", func(t *testing.T) {
status, body := registerDynamicClient(t, issuer, "", map[string]any{
"client_name": "no redirects",
})
assert.Equal(t, http.StatusBadRequest, status)
assert.Equal(t, "invalid_redirect_uri", body["error"])
})
}
// TestServer_DynamicClientRegistration_tokenMode covers the default mode, where a caller
// must present an access token whose user holds project.app.register_dynamic in the token's
// organization.
func TestServer_DynamicClientRegistration_tokenMode(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
instance := integration.NewInstance(ctx)
iamCTX := instance.WithAuthorization(ctx, integration.UserTypeIAMOwner)
issuer := instance.OIDCIssuer()
enableDynamicClientRegistration(t, iamCTX, instance, false)
metadata := map[string]any{
"client_name": "token mode client",
"redirect_uris": []string{redirectURI},
"token_endpoint_auth_method": "none",
}
t.Run("without a token registration is unauthorized", func(t *testing.T) {
status, body := registerDynamicClient(t, issuer, "", metadata)
assert.Equal(t, http.StatusUnauthorized, status)
assert.Equal(t, "invalid_token", body["error"])
})
t.Run("a token without the permission is forbidden", func(t *testing.T) {
_, pat, err := instance.CreateMachineUserPATWithMembership(iamCTX)
require.NoError(t, err)
status, body := registerDynamicClient(t, issuer, pat, metadata)
assert.Equal(t, http.StatusForbidden, status)
assert.Equal(t, "insufficient_scope", body["error"])
})
// The dedicated least-privilege role carries project.app.register_dynamic and nothing
// else, so it is enough on its own to register a client.
t.Run("a token with ORG_DYNAMIC_CLIENT_REGISTRAR may register", func(t *testing.T) {
_, pat, err := instance.CreateMachineUserPATWithMembership(iamCTX, domain.RoleOrgDynamicClientRegistrar)
require.NoError(t, err)
var body map[string]any
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(ctx, time.Minute)
require.EventuallyWithT(t, func(tt *assert.CollectT) {
var status int
status, body = registerDynamicClient(t, issuer, pat, metadata)
assert.Equal(tt, http.StatusCreated, status)
}, retryDuration, tick, "membership not effective for registration")
assert.NotEmpty(t, body["client_id"])
})
}
// TestServer_DynamicClientRegistration_disabled verifies that with the setting off the
// endpoint is neither advertised nor served.
func TestServer_DynamicClientRegistration_disabled(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
instance := integration.NewInstance(ctx)
issuer := instance.OIDCIssuer()
discovery := fetchDiscovery(t, issuer)
assert.Empty(t, discovery.RegistrationEndpoint)
status, _ := registerDynamicClient(t, issuer, "", map[string]any{
"redirect_uris": []string{redirectURI},
})
assert.Equal(t, http.StatusNotFound, status)
}
// enableDynamicClientRegistration turns the endpoint on through the instance's security
// settings and waits until the change is observable, i.e. until the projection behind the
// cached instance has caught up and discovery advertises the endpoint.
func enableDynamicClientRegistration(t *testing.T, ctx context.Context, instance *integration.Instance, allowUnauthenticated bool) {
t.Helper()
_, err := instance.Client.SettingsV2.SetSecuritySettings(ctx, &settings.SetSecuritySettingsRequest{
DynamicClientRegistration: &settings.DynamicClientRegistrationSettings{
Enabled: true,
AllowUnauthenticated: allowUnauthenticated,
},
})
require.NoError(t, err)
issuer := instance.OIDCIssuer()
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(ctx, time.Minute)
require.EventuallyWithT(t, func(tt *assert.CollectT) {
resp, err := http.Get(issuer + "/.well-known/openid-configuration")
if !assert.NoError(tt, err) {
return
}
defer resp.Body.Close()
var discovery oidc.DiscoveryConfiguration
if !assert.NoError(tt, json.NewDecoder(resp.Body).Decode(&discovery)) {
return
}
assert.Equal(tt, issuer+"/oauth/v2/register", discovery.RegistrationEndpoint)
}, retryDuration, tick, "registration endpoint not advertised")
}
func registerDynamicClient(t testing.TB, issuer, accessToken string, metadata map[string]any) (int, map[string]any) {
t.Helper()
raw, err := json.Marshal(metadata)
require.NoError(t, err)
req, err := http.NewRequest(http.MethodPost, issuer+"/oauth/v2/register", bytes.NewReader(raw))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
if accessToken != "" {
req.Header.Set("Authorization", "Bearer "+accessToken)
}
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() {
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()
var body map[string]any
_ = json.NewDecoder(resp.Body).Decode(&body)
return resp.StatusCode, body
}
func fetchDiscovery(t testing.TB, issuer string) oidc.DiscoveryConfiguration {
t.Helper()
resp, err := http.Get(issuer + "/.well-known/openid-configuration")
require.NoError(t, err)
defer resp.Body.Close()
var discovery oidc.DiscoveryConfiguration
require.NoError(t, json.NewDecoder(resp.Body).Decode(&discovery))
return discovery
}
+39 -3
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/zitadel/oidc/v3/pkg/oidc"
"github.com/zitadel/oidc/v3/pkg/op"
@@ -64,6 +65,7 @@ type EndpointConfig struct {
EndSession *Endpoint
Keys *Endpoint
DeviceAuth *Endpoint
Registration *Endpoint
}
type Endpoint struct {
@@ -191,10 +193,18 @@ func NewServer(
opCrypto: alg,
assetAPIPrefix: assets.AssetAPI(),
httpClient: httpClient,
registrationEndpoint: registrationEndpoint(config.CustomEndpoints),
}
metricTypes := []metrics.MetricType{metrics.MetricTypeRequestCount, metrics.MetricTypeStatusCode, metrics.MetricTypeTotalCount}
server.Handler = op.RegisterLegacyServer(server,
server.authorizeCallbackHandler,
// We register the routes via op.RegisterServer (instead of op.RegisterLegacyServer)
// so that the dynamic client registration route can be added through the same
// WithSetRouter hook as the authorize callback. op.RegisterLegacyServer appends its
// own WithHTTPMiddleware after the caller options, which would violate chi's
// "all middlewares before routes" rule once we add a route via WithSetRouter.
// This mirrors op.RegisterLegacyServer; op.NewIssuerInterceptor reproduces the issuer
// middleware it would otherwise add.
server.Handler = op.RegisterServer(server, server.Endpoints(),
op.WithFallbackLogger(fallbackLogger),
op.WithHTTPMiddleware(
middleware.CallDurationHandler,
@@ -209,11 +219,37 @@ func NewServer(
http_utils.CopyHeadersToContext,
accessHandler.HandleWithPublicAuthPathPrefixes(publicAuthPathPrefixes(config.CustomEndpoints)),
middleware.ActivityHandler,
))
op.NewIssuerInterceptor(server.IssuerFromRequest).Handler,
),
op.WithSetRouter(func(r chi.Router) {
r.HandleFunc(server.Endpoints().Authorization.Relative()+authCallbackPathSuffix, server.authorizeCallbackHandler)
r.Method(http.MethodPost, server.registrationEndpoint.Relative(), http.HandlerFunc(server.dynamicClientRegistration))
}),
)
return server, nil
}
// authCallbackPathSuffix mirrors the unexported suffix used by op.RegisterLegacyServer to
// register the authorize callback handler under the authorization endpoint.
// Keep in sync with github.com/zitadel/oidc/v3/pkg/op (authCallbackPathSuffix). The
// existing authorization-flow integration tests exercise this route and would fail if the
// library changed the suffix.
const authCallbackPathSuffix = "/callback"
// registrationEndpoint builds the dynamic client registration endpoint, optionally
// overridden through the custom endpoint configuration.
// registrationEndpoint resolves the OAuth 2.0 Dynamic Client Registration endpoint
// (RFC 7591). The path is configured in defaults.yaml like the other OIDC endpoints, so it
// can be overridden by environment variable; the literal below is only the fallback for a
// Server constructed without endpoint configuration.
func registrationEndpoint(endpointConfig *EndpointConfig) *op.Endpoint {
if endpointConfig != nil && endpointConfig.Registration != nil {
return op.NewEndpointWithURL(endpointConfig.Registration.Path, endpointConfig.Registration.URL)
}
return op.NewEndpoint("/oauth/v2/register")
}
func ContextToIssuer(ctx context.Context) string {
return http_utils.DomainContext(ctx).Origin()
}
+10
View File
@@ -45,6 +45,8 @@ type Server struct {
assetAPIPrefix func(ctx context.Context) string
httpClient *http.Client
registrationEndpoint *op.Endpoint
}
func endpoints(endpointConfig *EndpointConfig) op.Endpoints {
@@ -176,8 +178,16 @@ func (s *Server) EndSession(ctx context.Context, r *op.Request[oidc.EndSessionRe
func (s *Server) createDiscoveryConfig(ctx context.Context, supportedUILocales oidc.Locales) *oidc.DiscoveryConfiguration {
issuer := op.IssuerFromContext(ctx)
// The registration endpoint is only advertised when dynamic client registration is
// enabled in the instance's security settings.
var registrationEndpoint string
if authz.GetInstance(ctx).EnableDynamicClientRegistration() {
registrationEndpoint = s.registrationEndpoint.Absolute(issuer)
}
return &oidc.DiscoveryConfiguration{
Issuer: issuer,
RegistrationEndpoint: registrationEndpoint,
AuthorizationEndpoint: s.Endpoints().Authorization.Absolute(issuer),
TokenEndpoint: s.Endpoints().Token.Absolute(issuer),
IntrospectionEndpoint: s.Endpoints().Introspection.Absolute(issuer),
+41
View File
@@ -8,6 +8,8 @@ import (
"github.com/zitadel/oidc/v3/pkg/oidc"
"github.com/zitadel/oidc/v3/pkg/op"
"golang.org/x/text/language"
"github.com/zitadel/zitadel/internal/api/authz"
)
func TestServer_createDiscoveryConfig(t *testing.T) {
@@ -121,3 +123,42 @@ func TestServer_createDiscoveryConfig(t *testing.T) {
})
}
}
func TestServer_createDiscoveryConfig_registrationEndpoint(t *testing.T) {
s := &Server{
LegacyServer: op.NewLegacyServer(
func() *op.Provider {
//nolint:staticcheck
provider, _ := op.NewForwardedOpenIDProvider("path", &op.Config{}, nil)
return provider
}(),
op.Endpoints{Authorization: op.NewEndpoint("auth")},
),
registrationEndpoint: op.NewEndpoint("register"),
}
tests := []struct {
name string
ctx context.Context
want string
}{
{
name: "setting disabled, endpoint not advertised",
ctx: op.ContextWithIssuer(context.Background(), "https://issuer.com"),
want: "",
},
{
name: "setting enabled, endpoint advertised",
ctx: op.ContextWithIssuer(
authz.NewMockContext("instance", "org", "", authz.WithMockDynamicClientRegistration(true, false)),
"https://issuer.com",
),
want: "https://issuer.com/register",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := s.createDiscoveryConfig(tt.ctx, nil)
assert.Equal(t, tt.want, got.RegistrationEndpoint)
})
}
}