2023-05-05 15:34:53 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/session"
|
|
|
|
)
|
|
|
|
|
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-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
|
|
|
|
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-11 15:36:18 +00:00
|
|
|
WebAuthNUserVerified bool
|
|
|
|
Metadata map[string][]byte
|
|
|
|
State domain.SessionState
|
2023-05-05 15:34:53 +00:00
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
WebAuthNChallenge *WebAuthNChallengeModel
|
2023-06-07 15:28:42 +00:00
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
aggregate *eventstore.Aggregate
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSessionWriteModel(sessionID string, resourceOwner string) *SessionWriteModel {
|
|
|
|
return &SessionWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: sessionID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
Metadata: make(map[string][]byte),
|
|
|
|
aggregate: &session.NewAggregate(sessionID, resourceOwner).Aggregate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-05 15:34:53 +00:00
|
|
|
case *session.TokenSetEvent:
|
|
|
|
wm.reduceTokenSet(e)
|
|
|
|
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-05-05 15:34:53 +00:00
|
|
|
session.TokenSetType,
|
|
|
|
session.MetadataSetType,
|
|
|
|
session.TerminateType,
|
|
|
|
).
|
|
|
|
Builder()
|
|
|
|
|
|
|
|
if wm.ResourceOwner != "" {
|
|
|
|
query.ResourceOwner(wm.ResourceOwner)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) reduceAdded(e *session.AddedEvent) {
|
|
|
|
wm.State = domain.SessionStateActive
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) reduceUserChecked(e *session.UserCheckedEvent) {
|
|
|
|
wm.UserID = e.UserID
|
|
|
|
wm.UserCheckedAt = e.CheckedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-05 15:34:53 +00:00
|
|
|
func (wm *SessionWriteModel) reduceTokenSet(e *session.TokenSetEvent) {
|
|
|
|
wm.TokenID = e.TokenID
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02 16:57:53 +00:00
|
|
|
// TODO: add OTP (sms and email) check https://github.com/zitadel/zitadel/issues/6224
|
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-02 16:57:53 +00:00
|
|
|
// TODO: add checks with https://github.com/zitadel/zitadel/issues/6224
|
|
|
|
/*
|
|
|
|
if !wm.TOTPFactor.OTPSMSCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeOTPSMS)
|
|
|
|
}
|
|
|
|
if !wm.TOTPFactor.OTPEmailCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeOTPEmail)
|
|
|
|
}
|
|
|
|
*/
|
2023-07-12 12:24:01 +00:00
|
|
|
return types
|
|
|
|
}
|