2023-07-10 13:27:00 +00:00
|
|
|
package authrequest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-07-10 13:27:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
authRequestEventPrefix = "auth_request."
|
|
|
|
AddedType = authRequestEventPrefix + "added"
|
|
|
|
FailedType = authRequestEventPrefix + "failed"
|
|
|
|
CodeAddedType = authRequestEventPrefix + "code.added"
|
|
|
|
SessionLinkedType = authRequestEventPrefix + "session.linked"
|
|
|
|
CodeExchangedType = authRequestEventPrefix + "code.exchanged"
|
|
|
|
SucceededType = authRequestEventPrefix + "succeeded"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
LoginClient string `json:"login_client"`
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
RedirectURI string `json:"redirect_uri"`
|
|
|
|
State string `json:"state,omitempty"`
|
|
|
|
Nonce string `json:"nonce,omitempty"`
|
|
|
|
Scope []string `json:"scope,omitempty"`
|
|
|
|
Audience []string `json:"audience,omitempty"`
|
|
|
|
ResponseType domain.OIDCResponseType `json:"response_type,omitempty"`
|
|
|
|
CodeChallenge *domain.OIDCCodeChallenge `json:"code_challenge,omitempty"`
|
|
|
|
Prompt []domain.Prompt `json:"prompt,omitempty"`
|
|
|
|
UILocales []string `json:"ui_locales,omitempty"`
|
|
|
|
MaxAge *time.Duration `json:"max_age,omitempty"`
|
|
|
|
LoginHint *string `json:"login_hint,omitempty"`
|
|
|
|
HintUserID *string `json:"hint_user_id,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *AddedEvent) Payload() interface{} {
|
2023-07-10 13:27:00 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *AddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-07-10 13:27:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAddedEvent(ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
loginClient,
|
|
|
|
clientID,
|
|
|
|
redirectURI,
|
|
|
|
state,
|
|
|
|
nonce string,
|
|
|
|
scope,
|
|
|
|
audience []string,
|
|
|
|
responseType domain.OIDCResponseType,
|
|
|
|
codeChallenge *domain.OIDCCodeChallenge,
|
|
|
|
prompt []domain.Prompt,
|
|
|
|
uiLocales []string,
|
|
|
|
maxAge *time.Duration,
|
|
|
|
loginHint,
|
|
|
|
hintUserID *string,
|
|
|
|
) *AddedEvent {
|
|
|
|
return &AddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
AddedType,
|
|
|
|
),
|
|
|
|
LoginClient: loginClient,
|
|
|
|
ClientID: clientID,
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
State: state,
|
|
|
|
Nonce: nonce,
|
|
|
|
Scope: scope,
|
|
|
|
Audience: audience,
|
|
|
|
ResponseType: responseType,
|
|
|
|
CodeChallenge: codeChallenge,
|
|
|
|
Prompt: prompt,
|
|
|
|
UILocales: uiLocales,
|
|
|
|
MaxAge: maxAge,
|
|
|
|
LoginHint: loginHint,
|
|
|
|
HintUserID: hintUserID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func AddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
added := &AddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(added)
|
2023-07-10 13:27:00 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "AUTHR-DG4gn", "unable to unmarshal auth request added")
|
2023-07-10 13:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return added, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SessionLinkedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-07-12 12:24:01 +00:00
|
|
|
SessionID string `json:"session_id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
AuthTime time.Time `json:"auth_time"`
|
|
|
|
AuthMethods []domain.UserAuthMethodType `json:"auth_methods"`
|
2023-07-10 13:27:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SessionLinkedEvent) Payload() interface{} {
|
2023-07-10 13:27:00 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SessionLinkedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-07-10 13:27:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSessionLinkedEvent(ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
sessionID,
|
|
|
|
userID string,
|
|
|
|
authTime time.Time,
|
2023-07-12 12:24:01 +00:00
|
|
|
authMethods []domain.UserAuthMethodType,
|
2023-07-10 13:27:00 +00:00
|
|
|
) *SessionLinkedEvent {
|
|
|
|
return &SessionLinkedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SessionLinkedType,
|
|
|
|
),
|
2023-07-12 12:24:01 +00:00
|
|
|
SessionID: sessionID,
|
|
|
|
UserID: userID,
|
|
|
|
AuthTime: authTime,
|
|
|
|
AuthMethods: authMethods,
|
2023-07-10 13:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SessionLinkedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
added := &SessionLinkedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(added)
|
2023-07-10 13:27:00 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "AUTHR-Sfe3w", "unable to unmarshal auth request session linked")
|
2023-07-10 13:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return added, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type FailedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Reason domain.OIDCErrorReason `json:"reason,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *FailedEvent) Payload() interface{} {
|
2023-07-10 13:27:00 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *FailedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-07-10 13:27:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFailedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
reason domain.OIDCErrorReason,
|
|
|
|
) *FailedEvent {
|
|
|
|
return &FailedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
FailedType,
|
|
|
|
),
|
|
|
|
Reason: reason,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func FailedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
added := &FailedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(added)
|
2023-07-10 13:27:00 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "AUTHR-Sfe3w", "unable to unmarshal auth request session linked")
|
2023-07-10 13:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return added, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CodeAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *CodeAddedEvent) Payload() interface{} {
|
2023-07-10 13:27:00 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *CodeAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-07-10 13:27:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodeAddedEvent(ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
) *CodeAddedEvent {
|
|
|
|
return &CodeAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
CodeAddedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func CodeAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
added := &CodeAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(added)
|
2023-07-10 13:27:00 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "AUTHR-Sfe3w", "unable to unmarshal auth request code added")
|
2023-07-10 13:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return added, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CodeExchangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *CodeExchangedEvent) Payload() interface{} {
|
2023-07-10 13:27:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *CodeExchangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-07-10 13:27:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodeExchangedEvent(ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
) *CodeExchangedEvent {
|
|
|
|
return &CodeExchangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
CodeExchangedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func CodeExchangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
return &CodeExchangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SucceededEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SucceededEvent) Payload() interface{} {
|
2023-07-10 13:27:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SucceededEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-07-10 13:27:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSucceededEvent(ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
) *SucceededEvent {
|
|
|
|
return &SucceededEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SucceededType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SucceededEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-07-10 13:27:00 +00:00
|
|
|
return &SucceededEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|