2023-05-05 15:34:53 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-06-07 15:28:42 +00:00
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
2023-05-05 15:34:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/session"
|
|
|
|
)
|
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
type PasskeyChallengeModel struct {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PasskeyChallengeModel) WebAuthNLogin(human *domain.Human, credentialAssertionData []byte) (*domain.WebAuthNLogin, error) {
|
|
|
|
if p == nil {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-Ioqu5", "Errors.Session.Passkey.NoChallenge")
|
|
|
|
}
|
|
|
|
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-06-07 15:28:42 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
type SessionWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
TokenID string
|
|
|
|
UserID string
|
|
|
|
UserCheckedAt time.Time
|
|
|
|
PasswordCheckedAt time.Time
|
2023-06-21 14:06:18 +00:00
|
|
|
IntentCheckedAt time.Time
|
2023-06-07 15:28:42 +00:00
|
|
|
PasskeyCheckedAt time.Time
|
2023-05-05 15:34:53 +00:00
|
|
|
Metadata map[string][]byte
|
2023-06-27 12:36:07 +00:00
|
|
|
Domain string
|
2023-05-05 15:34:53 +00:00
|
|
|
State domain.SessionState
|
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
PasskeyChallenge *PasskeyChallengeModel
|
|
|
|
|
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-06-07 15:28:42 +00:00
|
|
|
case *session.PasskeyChallengedEvent:
|
|
|
|
wm.reducePasskeyChallenged(e)
|
|
|
|
case *session.PasskeyCheckedEvent:
|
|
|
|
wm.reducePasskeyChecked(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-06-07 15:28:42 +00:00
|
|
|
session.PasskeyChallengedType,
|
|
|
|
session.PasskeyCheckedType,
|
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) {
|
2023-06-27 12:36:07 +00:00
|
|
|
wm.Domain = e.Domain
|
2023-05-05 15:34:53 +00:00
|
|
|
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-06-07 15:28:42 +00:00
|
|
|
func (wm *SessionWriteModel) reducePasskeyChallenged(e *session.PasskeyChallengedEvent) {
|
|
|
|
wm.PasskeyChallenge = &PasskeyChallengeModel{
|
|
|
|
Challenge: e.Challenge,
|
|
|
|
AllowedCrentialIDs: e.AllowedCrentialIDs,
|
|
|
|
UserVerification: e.UserVerification,
|
2023-06-27 12:36:07 +00:00
|
|
|
RPID: wm.Domain,
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *SessionWriteModel) reducePasskeyChecked(e *session.PasskeyCheckedEvent) {
|
|
|
|
wm.PasskeyChallenge = nil
|
|
|
|
wm.PasskeyCheckedAt = 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,
|
|
|
|
wm.PasskeyCheckedAt,
|
|
|
|
wm.IntentCheckedAt,
|
|
|
|
// TODO: add U2F and OTP check https://github.com/zitadel/zitadel/issues/5477
|
|
|
|
} {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if !wm.PasskeyCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypePasswordless)
|
|
|
|
}
|
|
|
|
if !wm.IntentCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeIDP)
|
|
|
|
}
|
|
|
|
// TODO: add checks with https://github.com/zitadel/zitadel/issues/5477
|
|
|
|
/*
|
|
|
|
if !wm.TOTPCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeOTP)
|
|
|
|
}
|
|
|
|
if !wm.U2FCheckedAt.IsZero() {
|
|
|
|
types = append(types, domain.UserAuthMethodTypeU2F)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return types
|
|
|
|
}
|