Stefan Benz c3b97a91a2
feat: add saml request to link to sessions (#9001)
# Which Problems Are Solved

It is currently not possible to use SAML with the Session API.

# How the Problems Are Solved

Add SAML service, to get and resolve SAML requests.
Add SAML session and SAML request aggregate, which can be linked to the
Session to get back a SAMLResponse from the API directly.

# Additional Changes

Update of dependency zitadel/saml to provide all functionality for
handling of SAML requests and responses.

# Additional Context

Closes #6053

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-12-19 11:11:40 +00:00

140 lines
3.4 KiB
Go

package samlsession
import (
"context"
"time"
"golang.org/x/text/language"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore"
)
const (
samlSessionEventPrefix = "saml_session."
AddedType = samlSessionEventPrefix + "added"
SAMLResponseAddedType = samlSessionEventPrefix + "saml_response.added"
SAMLResponseRevokedType = samlSessionEventPrefix + "saml_response.revoked"
)
type AddedEvent struct {
eventstore.BaseEvent `json:"-"`
UserID string `json:"userID"`
UserResourceOwner string `json:"userResourceOwner"`
SessionID string `json:"sessionID"`
EntityID string `json:"entityID"`
Audience []string `json:"audience"`
AuthMethods []domain.UserAuthMethodType `json:"authMethods"`
AuthTime time.Time `json:"authTime"`
PreferredLanguage *language.Tag `json:"preferredLanguage,omitempty"`
UserAgent *domain.UserAgent `json:"userAgent,omitempty"`
}
func (e *AddedEvent) Payload() interface{} {
return e
}
func (e *AddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func (e *AddedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = *event
}
func NewAddedEvent(ctx context.Context,
aggregate *eventstore.Aggregate,
userID,
userResourceOwner,
sessionID,
entityID string,
audience []string,
authMethods []domain.UserAuthMethodType,
authTime time.Time,
preferredLanguage *language.Tag,
userAgent *domain.UserAgent,
) *AddedEvent {
return &AddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
AddedType,
),
UserID: userID,
UserResourceOwner: userResourceOwner,
SessionID: sessionID,
EntityID: entityID,
Audience: audience,
AuthMethods: authMethods,
AuthTime: authTime,
PreferredLanguage: preferredLanguage,
UserAgent: userAgent,
}
}
type SAMLResponseAddedEvent struct {
eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
Lifetime time.Duration `json:"lifetime,omitempty"`
}
func (e *SAMLResponseAddedEvent) Payload() interface{} {
return e
}
func (e *SAMLResponseAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func (e *SAMLResponseAddedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = *event
}
func NewSAMLResponseAddedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
id string,
lifetime time.Duration,
) *SAMLResponseAddedEvent {
return &SAMLResponseAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
SAMLResponseAddedType,
),
ID: id,
Lifetime: lifetime,
}
}
type SAMLResponseRevokedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *SAMLResponseRevokedEvent) Payload() interface{} {
return e
}
func (e *SAMLResponseRevokedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func (e *SAMLResponseRevokedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = *event
}
func NewSAMLResponseRevokedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
) *SAMLResponseRevokedEvent {
return &SAMLResponseRevokedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
SAMLResponseRevokedType,
),
}
}