2023-05-05 15:34:53 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2024-05-16 05:07:56 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2023-05-05 15:34:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/session"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-05-05 15:34:53 +00:00
|
|
|
)
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
type WebAuthNChallengeModel struct {
|
2023-06-07 15:28:42 +00:00
|
|
|
Challenge string
|
|
|
|
AllowedCrentialIDs [][]byte
|
|
|
|
UserVerification domain.UserVerificationRequirement
|
2023-06-27 12:36:07 +00:00
|
|
|
RPID string
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
type OTPCode struct {
|
|
|
|
Code *crypto.CryptoValue
|
|
|
|
Expiry time.Duration
|
|
|
|
CreationDate time.Time
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
func (p *WebAuthNChallengeModel) WebAuthNLogin(human *domain.Human, credentialAssertionData []byte) *domain.WebAuthNLogin {
|
2023-06-07 15:28:42 +00:00
|
|
|
return &domain.WebAuthNLogin{
|
|
|
|
ObjectRoot: human.ObjectRoot,
|
|
|
|
CredentialAssertionData: credentialAssertionData,
|
|
|
|
Challenge: p.Challenge,
|
|
|
|
AllowedCredentialIDs: p.AllowedCrentialIDs,
|
|
|
|
UserVerification: p.UserVerification,
|
2023-06-27 12:36:07 +00:00
|
|
|
RPID: p.RPID,
|
2023-08-11 15:36:18 +00:00
|
|
|
}
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
type SessionWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
TokenID string
|
|
|
|
UserID string
|
2023-11-16 06:35:50 +00:00
|
|
|
UserResourceOwner string
|
2024-05-16 05:07:56 +00:00
|
|
|
PreferredLanguage *language.Tag
|
2023-08-11 15:36:18 +00:00
|
|
|
UserCheckedAt time.Time
|
|
|
|
PasswordCheckedAt time.Time
|
|
|
|
IntentCheckedAt time.Time
|
|
|
|
WebAuthNCheckedAt time.Time
|
2023-08-15 09:50:42 +00:00
|
|
|
TOTPCheckedAt time.Time
|
2023-08-24 09:41:52 +00:00
|
|
|
OTPSMSCheckedAt time.Time
|
|
|
|
OTPEmailCheckedAt time.Time
|
2023-08-11 15:36:18 +00:00
|
|
|
WebAuthNUserVerified bool
|
|
|
|
Metadata map[string][]byte
|
|
|
|
State domain.SessionState
|
2024-05-16 05:07:56 +00:00
|
|
|
UserAgent *domain.UserAgent
|
2023-11-06 09:48:28 +00:00
|
|
|
Expiration time.Time
|
2023-05-05 15:34:53 +00:00
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
WebAuthNChallenge *WebAuthNChallengeModel
|
|
|
|
OTPSMSCodeChallenge *OTPCode
|
|
|
|
OTPEmailCodeChallenge *OTPCode
|
2023-06-07 15:28:42 +00:00
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
aggregate *eventstore.Aggregate
|
|
|
|
}
|
|
|
|
|
2023-11-16 06:35:50 +00:00
|
|
|
func NewSessionWriteModel(sessionID string, instanceID string) *SessionWriteModel {
|
2023-05-05 15:34:53 +00:00
|
|
|
return &SessionWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2023-12-09 08:59:51 +00:00
|
|
|
AggregateID: sessionID,
|
2023-05-05 15:34:53 +00:00
|
|
|
},
|
|
|
|
Metadata: make(map[string][]byte),
|
2023-11-16 06:35:50 +00:00
|
|
|
aggregate: &session.NewAggregate(sessionID, instanceID).Aggregate,
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *session.AddedEvent:
|
|
|
|
wm.reduceAdded(e)
|
|
|
|
case *session.UserCheckedEvent:
|
|
|
|
wm.reduceUserChecked(e)
|
|
|
|
case *session.PasswordCheckedEvent:
|
|
|
|
wm.reducePasswordChecked(e)
|
2023-06-21 14:06:18 +00:00
|
|
|
case *session.IntentCheckedEvent:
|
|
|
|
wm.reduceIntentChecked(e)
|
2023-08-11 15:36:18 +00:00
|
|
|
case *session.WebAuthNChallengedEvent:
|
|
|
|
wm.reduceWebAuthNChallenged(e)
|
|
|
|
case *session.WebAuthNCheckedEvent:
|
|
|
|
wm.reduceWebAuthNChecked(e)
|
2023-08-15 09:50:42 +00:00
|
|
|
case *session.TOTPCheckedEvent:
|
|
|
|
wm.reduceTOTPChecked(e)
|
2023-08-24 09:41:52 +00:00
|
|
|
case *session.OTPSMSChallengedEvent:
|
|
|
|
wm.reduceOTPSMSChallenged(e)
|
|
|
|
case *session.OTPSMSCheckedEvent:
|
|
|
|
wm.reduceOTPSMSChecked(e)
|
|
|
|
case *session.OTPEmailChallengedEvent:
|
|
|
|
wm.reduceOTPEmailChallenged(e)
|
|
|
|
case *session.OTPEmailCheckedEvent:
|
|
|
|
wm.reduceOTPEmailChecked(e)
|
2023-05-05 15:34:53 +00:00
|
|
|
case *session.TokenSetEvent:
|
|
|
|
wm.reduceTokenSet(e)
|
2023-11-06 09:48:28 +00:00
|
|
|
case *session.LifetimeSetEvent:
|
|
|
|
wm.reduceLifetimeSet(e)
|
2023-05-05 15:34:53 +00:00
|
|
|
case *session.TerminateEvent:
|
|
|
|
wm.reduceTerminate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(session.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(
|
|
|
|
session.AddedType,
|
|
|
|
session.UserCheckedType,
|
|
|
|
session.PasswordCheckedType,
|
2023-06-21 14:06:18 +00:00
|
|
|
session.IntentCheckedType,
|
2023-08-11 15:36:18 +00:00
|
|
|
session.WebAuthNChallengedType,
|
|
|
|
session.WebAuthNCheckedType,
|
2023-08-15 09:50:42 +00:00
|
|
|
session.TOTPCheckedType,
|
2023-08-24 09:41:52 +00:00
|
|
|
session.OTPSMSChallengedType,
|
|
|
|
session.OTPSMSCheckedType,
|
|
|
|
session.OTPEmailChallengedType,
|
|
|
|
session.OTPEmailCheckedType,
|
2023-05-05 15:34:53 +00:00
|
|
|
session.TokenSetType,
|
|
|
|
session.MetadataSetType,
|
2023-11-06 09:48:28 +00:00
|
|
|
session.LifetimeSetType,
|
2023-05-05 15:34:53 +00:00
|
|
|
session.TerminateType,
|
|
|
|
).
|
|
|
|
Builder()
|
|
|
|
|
|
|
|
if wm.ResourceOwner != "" {
|
|
|
|
query.ResourceOwner(wm.ResourceOwner)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) reduceAdded(e *session.AddedEvent) {
|
|
|
|
wm.State = domain.SessionStateActive
|
2024-05-16 05:07:56 +00:00
|
|
|
wm.UserAgent = e.UserAgent
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) reduceUserChecked(e *session.UserCheckedEvent) {
|
|
|
|
wm.UserID = e.UserID
|
2023-11-16 06:35:50 +00:00
|
|
|
wm.UserResourceOwner = e.UserResourceOwner
|
2023-05-05 15:34:53 +00:00
|
|
|
wm.UserCheckedAt = e.CheckedAt
|
2024-05-16 05:07:56 +00:00
|
|
|
wm.PreferredLanguage = e.PreferredLanguage
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) reducePasswordChecked(e *session.PasswordCheckedEvent) {
|
|
|
|
wm.PasswordCheckedAt = e.CheckedAt
|
|
|
|
}
|
|
|
|
|
2023-06-21 14:06:18 +00:00
|
|
|
func (wm *SessionWriteModel) reduceIntentChecked(e *session.IntentCheckedEvent) {
|
|
|
|
wm.IntentCheckedAt = e.CheckedAt
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
func (wm *SessionWriteModel) reduceWebAuthNChallenged(e *session.WebAuthNChallengedEvent) {
|
|
|
|
wm.WebAuthNChallenge = &WebAuthNChallengeModel{
|
2023-06-07 15:28:42 +00:00
|
|
|
Challenge: e.Challenge,
|
|
|
|
AllowedCrentialIDs: e.AllowedCrentialIDs,
|
|
|
|
UserVerification: e.UserVerification,
|
2023-08-11 15:36:18 +00:00
|
|
|
RPID: e.RPID,
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
func (wm *SessionWriteModel) reduceWebAuthNChecked(e *session.WebAuthNCheckedEvent) {
|
|
|
|
wm.WebAuthNChallenge = nil
|
|
|
|
wm.WebAuthNCheckedAt = e.CheckedAt
|
|
|
|
wm.WebAuthNUserVerified = e.UserVerified
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 09:50:42 +00:00
|
|
|
func (wm *SessionWriteModel) reduceTOTPChecked(e *session.TOTPCheckedEvent) {
|
|
|
|
wm.TOTPCheckedAt = e.CheckedAt
|
|
|
|
}
|
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
func (wm *SessionWriteModel) reduceOTPSMSChallenged(e *session.OTPSMSChallengedEvent) {
|
|
|
|
wm.OTPSMSCodeChallenge = &OTPCode{
|
|
|
|
Code: e.Code,
|
|
|
|
Expiry: e.Expiry,
|
|
|
|
CreationDate: e.CreationDate(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) reduceOTPSMSChecked(e *session.OTPSMSCheckedEvent) {
|
|
|
|
wm.OTPSMSCodeChallenge = nil
|
|
|
|
wm.OTPSMSCheckedAt = e.CheckedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) reduceOTPEmailChallenged(e *session.OTPEmailChallengedEvent) {
|
|
|
|
wm.OTPEmailCodeChallenge = &OTPCode{
|
|
|
|
Code: e.Code,
|
|
|
|
Expiry: e.Expiry,
|
|
|
|
CreationDate: e.CreationDate(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) reduceOTPEmailChecked(e *session.OTPEmailCheckedEvent) {
|
|
|
|
wm.OTPEmailCodeChallenge = nil
|
|
|
|
wm.OTPEmailCheckedAt = e.CheckedAt
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
func (wm *SessionWriteModel) reduceTokenSet(e *session.TokenSetEvent) {
|
|
|
|
wm.TokenID = e.TokenID
|
|
|
|
}
|
|
|
|
|
2023-11-06 09:48:28 +00:00
|
|
|
func (wm *SessionWriteModel) reduceLifetimeSet(e *session.LifetimeSetEvent) {
|
|
|
|
wm.Expiration = e.CreationDate().Add(e.Lifetime)
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
func (wm *SessionWriteModel) reduceTerminate() {
|
|
|
|
wm.State = domain.SessionStateTerminated
|
|
|
|
}
|
|
|
|
|
2023-07-10 13:27:00 +00:00
|
|
|
// AuthenticationTime returns the time the user authenticated using the latest time of all checks
|
|
|
|
func (wm *SessionWriteModel) AuthenticationTime() time.Time {
|
|
|
|
var authTime time.Time
|
|
|
|
for _, check := range []time.Time{
|
|
|
|
wm.PasswordCheckedAt,
|
2023-08-11 15:36:18 +00:00
|
|
|
wm.WebAuthNCheckedAt,
|
2023-08-15 09:50:42 +00:00
|
|
|
wm.TOTPCheckedAt,
|
2023-07-10 13:27:00 +00:00
|
|
|
wm.IntentCheckedAt,
|
2023-08-24 09:41:52 +00:00
|
|
|
wm.OTPSMSCheckedAt,
|
|
|
|
wm.OTPEmailCheckedAt,
|
2023-07-10 13:27:00 +00:00
|
|
|
} {
|
|
|
|
if check.After(authTime) {
|
|
|
|
authTime = check
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return authTime
|
|
|
|
}
|
2023-07-12 12:24:01 +00:00
|
|
|
|
|
|
|
// AuthMethodTypes returns a list of UserAuthMethodTypes based on succeeded checks
|
|
|
|
func (wm *SessionWriteModel) AuthMethodTypes() []domain.UserAuthMethodType {
|
|
|
|
types := make([]domain.UserAuthMethodType, 0, domain.UserAuthMethodTypeIDP)
|
|
|
|
if !wm.PasswordCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypePassword)
|
|
|
|
}
|
2023-08-11 15:36:18 +00:00
|
|
|
if !wm.WebAuthNCheckedAt.IsZero() {
|
|
|
|
if wm.WebAuthNUserVerified {
|
|
|
|
types = append(types, domain.UserAuthMethodTypePasswordless)
|
|
|
|
} else {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeU2F)
|
|
|
|
}
|
2023-07-12 12:24:01 +00:00
|
|
|
}
|
|
|
|
if !wm.IntentCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeIDP)
|
|
|
|
}
|
2023-08-15 09:50:42 +00:00
|
|
|
if !wm.TOTPCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeTOTP)
|
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
if !wm.OTPSMSCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeOTPSMS)
|
|
|
|
}
|
|
|
|
if !wm.OTPEmailCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeOTPEmail)
|
|
|
|
}
|
2023-07-12 12:24:01 +00:00
|
|
|
return types
|
|
|
|
}
|
2023-11-06 09:48:28 +00:00
|
|
|
|
|
|
|
// CheckNotInvalidated checks that the session was not invalidated either manually ([session.TerminateType])
|
|
|
|
// or automatically (expired).
|
|
|
|
func (wm *SessionWriteModel) CheckNotInvalidated() error {
|
|
|
|
if wm.State == domain.SessionStateTerminated {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowPreconditionFailed(nil, "COMMAND-Hewfq", "Errors.Session.Terminated")
|
2023-11-06 09:48:28 +00:00
|
|
|
}
|
|
|
|
if !wm.Expiration.IsZero() && wm.Expiration.Before(time.Now()) {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowPreconditionFailed(nil, "COMMAND-Hkl3d", "Errors.Session.Expired")
|
2023-11-06 09:48:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckIsActive checks that the session was not invalidated ([CheckNotInvalidated]) and actually already exists.
|
|
|
|
func (wm *SessionWriteModel) CheckIsActive() error {
|
|
|
|
if wm.State == domain.SessionStateUnspecified {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowPreconditionFailed(nil, "COMMAND-Flk38", "Errors.Session.NotExisting")
|
2023-11-06 09:48:28 +00:00
|
|
|
}
|
|
|
|
return wm.CheckNotInvalidated()
|
|
|
|
}
|