Livio Spring 8f88c4cf5b
feat: add PKCE option to generic OAuth2 / OIDC identity providers (#9373)
# Which Problems Are Solved

Some OAuth2 and OIDC providers require the use of PKCE for all their
clients. While ZITADEL already recommended the same for its clients, it
did not yet support the option on the IdP configuration.

# How the Problems Are Solved

- A new boolean `use_pkce` is added to the add/update generic OAuth/OIDC
endpoints.
- A new checkbox is added to the generic OAuth and OIDC provider
templates.
- The `rp.WithPKCE` option is added to the provider if the use of PKCE
has been set.
- The `rp.WithCodeChallenge` and `rp.WithCodeVerifier` options are added
to the OIDC/Auth BeginAuth and CodeExchange function.
- Store verifier or any other persistent argument in the intent or auth
request.
- Create corresponding session object before creating the intent, to be
able to store the information.
- (refactored session structs to use a constructor for unified creation
and better overview of actual usage)

Here's a screenshot showing the URI including the PKCE params:


![use_pkce_in_url](https://github.com/zitadel/zitadel/assets/30386061/eaeab123-a5da-4826-b001-2ae9efa35169)

# Additional Changes

None.

# Additional Context

- Closes #6449
- This PR replaces the existing PR (#8228) of @doncicuto. The base he
did was cherry picked. Thank you very much for that!

---------

Co-authored-by: Miguel Cabrerizo <doncicuto@gmail.com>
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2025-02-26 12:20:47 +00:00

191 lines
5.3 KiB
Go

package idp
import (
"github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/zerrors"
)
type OAuthIDPAddedEvent struct {
eventstore.BaseEvent `json:"-"`
ID string `json:"id"`
Name string `json:"name,omitempty"`
ClientID string `json:"clientId,omitempty"`
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
AuthorizationEndpoint string `json:"authorizationEndpoint,omitempty"`
TokenEndpoint string `json:"tokenEndpoint,omitempty"`
UserEndpoint string `json:"userEndpoint,omitempty"`
Scopes []string `json:"scopes,omitempty"`
IDAttribute string `json:"idAttribute,omitempty"`
UsePKCE bool `json:"usePKCE,omitempty"`
Options
}
func NewOAuthIDPAddedEvent(
base *eventstore.BaseEvent,
id,
name,
clientID string,
clientSecret *crypto.CryptoValue,
authorizationEndpoint,
tokenEndpoint,
userEndpoint,
idAttribute string,
scopes []string,
usePKCE bool,
options Options,
) *OAuthIDPAddedEvent {
return &OAuthIDPAddedEvent{
BaseEvent: *base,
ID: id,
Name: name,
ClientID: clientID,
ClientSecret: clientSecret,
AuthorizationEndpoint: authorizationEndpoint,
TokenEndpoint: tokenEndpoint,
UserEndpoint: userEndpoint,
Scopes: scopes,
IDAttribute: idAttribute,
UsePKCE: usePKCE,
Options: options,
}
}
func (e *OAuthIDPAddedEvent) Payload() interface{} {
return e
}
func (e *OAuthIDPAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func OAuthIDPAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
e := &OAuthIDPAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(e)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IDP-Et1dq", "unable to unmarshal event")
}
return e, nil
}
type OAuthIDPChangedEvent struct {
eventstore.BaseEvent `json:"-"`
ID string `json:"id"`
Name *string `json:"name,omitempty"`
ClientID *string `json:"clientId,omitempty"`
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
AuthorizationEndpoint *string `json:"authorizationEndpoint,omitempty"`
TokenEndpoint *string `json:"tokenEndpoint,omitempty"`
UserEndpoint *string `json:"userEndpoint,omitempty"`
Scopes []string `json:"scopes,omitempty"`
IDAttribute *string `json:"idAttribute,omitempty"`
UsePKCE *bool `json:"usePKCE,omitempty"`
OptionChanges
}
func NewOAuthIDPChangedEvent(
base *eventstore.BaseEvent,
id string,
changes []OAuthIDPChanges,
) (*OAuthIDPChangedEvent, error) {
if len(changes) == 0 {
return nil, zerrors.ThrowPreconditionFailed(nil, "IDP-BH3dl", "Errors.NoChangesFound")
}
changedEvent := &OAuthIDPChangedEvent{
BaseEvent: *base,
ID: id,
}
for _, change := range changes {
change(changedEvent)
}
return changedEvent, nil
}
type OAuthIDPChanges func(*OAuthIDPChangedEvent)
func ChangeOAuthName(name string) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.Name = &name
}
}
func ChangeOAuthClientID(clientID string) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.ClientID = &clientID
}
}
func ChangeOAuthClientSecret(clientSecret *crypto.CryptoValue) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.ClientSecret = clientSecret
}
}
func ChangeOAuthOptions(options OptionChanges) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.OptionChanges = options
}
}
func ChangeOAuthAuthorizationEndpoint(authorizationEndpoint string) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.AuthorizationEndpoint = &authorizationEndpoint
}
}
func ChangeOAuthTokenEndpoint(tokenEndpoint string) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.TokenEndpoint = &tokenEndpoint
}
}
func ChangeOAuthUserEndpoint(userEndpoint string) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.UserEndpoint = &userEndpoint
}
}
func ChangeOAuthScopes(scopes []string) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.Scopes = scopes
}
}
func ChangeOAuthIDAttribute(idAttribute string) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.IDAttribute = &idAttribute
}
}
func ChangeOAuthUsePKCE(usePKCE bool) func(*OAuthIDPChangedEvent) {
return func(e *OAuthIDPChangedEvent) {
e.UsePKCE = &usePKCE
}
}
func (e *OAuthIDPChangedEvent) Payload() interface{} {
return e
}
func (e *OAuthIDPChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func OAuthIDPChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
e := &OAuthIDPChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(e)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IDP-SAf3gw", "unable to unmarshal event")
}
return e, nil
}