2021-01-07 15:06:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-01 07:48:50 +00:00
|
|
|
"github.com/caos/logging"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-07 15:06:45 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
usr_repo "github.com/caos/zitadel/internal/repository/user"
|
2021-01-07 15:06:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) getHumanU2FTokens(ctx context.Context, userID, resourceowner string) ([]*domain.WebAuthNToken, error) {
|
2021-01-15 08:32:59 +00:00
|
|
|
tokenReadModel := NewHumanU2FTokensReadModel(userID, resourceowner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, tokenReadModel)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tokenReadModel.UserState == domain.UserStateDeleted {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-4M0ds", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
return readModelToU2FTokens(tokenReadModel), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) getHumanPasswordlessTokens(ctx context.Context, userID, resourceowner string) ([]*domain.WebAuthNToken, error) {
|
2021-01-15 08:32:59 +00:00
|
|
|
tokenReadModel := NewHumanPasswordlessTokensReadModel(userID, resourceowner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, tokenReadModel)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tokenReadModel.UserState == domain.UserStateDeleted {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-Mv9sd", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
return readModelToPasswordlessTokens(tokenReadModel), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) getHumanU2FLogin(ctx context.Context, userID, authReqID, resourceowner string) (*domain.WebAuthNLogin, error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
tokenReadModel := NewHumanU2FLoginReadModel(userID, authReqID, resourceowner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, tokenReadModel)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tokenReadModel.State == domain.UserStateDeleted {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-5m88U", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
return &domain.WebAuthNLogin{
|
|
|
|
Challenge: tokenReadModel.Challenge,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) getHumanPasswordlessLogin(ctx context.Context, userID, authReqID, resourceowner string) (*domain.WebAuthNLogin, error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
tokenReadModel := NewHumanPasswordlessLoginReadModel(userID, authReqID, resourceowner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, tokenReadModel)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tokenReadModel.State == domain.UserStateDeleted {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-fm84R", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
return &domain.WebAuthNLogin{
|
|
|
|
Challenge: tokenReadModel.Challenge,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanAddU2FSetup(ctx context.Context, userID, resourceowner string, isLoginUI bool) (*domain.WebAuthNToken, error) {
|
|
|
|
u2fTokens, err := c.getHumanU2FTokens(ctx, userID, resourceowner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
addWebAuthN, userAgg, webAuthN, err := c.addHumanWebAuthN(ctx, userID, resourceowner, isLoginUI, u2fTokens)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
events, err := c.eventstore.PushEvents(ctx, usr_repo.NewHumanU2FAddedEvent(ctx, userAgg, addWebAuthN.WebauthNTokenID, webAuthN.Challenge))
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addWebAuthN, events...)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
createdWebAuthN := writeModelToWebAuthN(addWebAuthN)
|
|
|
|
createdWebAuthN.CredentialCreationData = webAuthN.CredentialCreationData
|
|
|
|
createdWebAuthN.AllowedCredentialIDs = webAuthN.AllowedCredentialIDs
|
|
|
|
createdWebAuthN.UserVerification = webAuthN.UserVerification
|
|
|
|
return createdWebAuthN, nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanAddPasswordlessSetup(ctx context.Context, userID, resourceowner string, isLoginUI bool) (*domain.WebAuthNToken, error) {
|
|
|
|
passwordlessTokens, err := c.getHumanPasswordlessTokens(ctx, userID, resourceowner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
addWebAuthN, userAgg, webAuthN, err := c.addHumanWebAuthN(ctx, userID, resourceowner, isLoginUI, passwordlessTokens)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
events, err := c.eventstore.PushEvents(ctx, usr_repo.NewHumanPasswordlessAddedEvent(ctx, userAgg, addWebAuthN.WebauthNTokenID, webAuthN.Challenge))
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addWebAuthN, events...)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
createdWebAuthN := writeModelToWebAuthN(addWebAuthN)
|
|
|
|
createdWebAuthN.CredentialCreationData = webAuthN.CredentialCreationData
|
|
|
|
createdWebAuthN.AllowedCredentialIDs = webAuthN.AllowedCredentialIDs
|
|
|
|
createdWebAuthN.UserVerification = webAuthN.UserVerification
|
|
|
|
return createdWebAuthN, nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) addHumanWebAuthN(ctx context.Context, userID, resourceowner string, isLoginUI bool, tokens []*domain.WebAuthNToken) (*HumanWebAuthNWriteModel, *eventstore.Aggregate, *domain.WebAuthNToken, error) {
|
2021-01-15 08:32:59 +00:00
|
|
|
if userID == "" || resourceowner == "" {
|
|
|
|
return nil, nil, nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-3M0od", "Errors.IDMissing")
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
user, err := c.getHuman(ctx, userID, resourceowner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
org, err := c.getOrg(ctx, user.ResourceOwner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
orgPolicy, err := c.getOrgIAMPolicy(ctx, org.AggregateID)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
accountName := domain.GenerateLoginName(user.GetUsername(), org.PrimaryDomain, orgPolicy.UserLoginMustBeDomain)
|
|
|
|
if accountName == "" {
|
|
|
|
accountName = user.EmailAddress
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
webAuthN, err := c.webauthn.BeginRegistration(user, accountName, domain.AuthenticatorAttachmentUnspecified, domain.UserVerificationRequirementDiscouraged, isLoginUI, tokens...)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
tokenID, err := c.idGenerator.Next()
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
addWebAuthN, err := c.webauthNWriteModelByID(ctx, userID, tokenID, resourceowner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userAgg := UserAggregateFromWriteModel(&addWebAuthN.WriteModel)
|
|
|
|
return addWebAuthN, userAgg, webAuthN, nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanVerifyU2FSetup(ctx context.Context, userID, resourceowner, tokenName, userAgentID string, credentialData []byte) error {
|
|
|
|
u2fTokens, err := c.getHumanU2FTokens(ctx, userID, resourceowner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
userAgg, webAuthN, verifyWebAuthN, err := c.verifyHumanWebAuthN(ctx, userID, resourceowner, tokenName, userAgentID, credentialData, u2fTokens)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx,
|
2021-01-15 08:32:59 +00:00
|
|
|
usr_repo.NewHumanU2FVerifiedEvent(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
userAgg,
|
2021-03-01 07:48:50 +00:00
|
|
|
verifyWebAuthN.WebauthNTokenID,
|
2021-01-15 08:32:59 +00:00
|
|
|
webAuthN.WebAuthNTokenName,
|
|
|
|
webAuthN.AttestationType,
|
|
|
|
webAuthN.KeyID,
|
|
|
|
webAuthN.PublicKey,
|
|
|
|
webAuthN.AAGUID,
|
|
|
|
webAuthN.SignCount,
|
|
|
|
),
|
|
|
|
)
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanHumanPasswordlessSetup(ctx context.Context, userID, resourceowner, tokenName, userAgentID string, credentialData []byte) error {
|
|
|
|
u2fTokens, err := c.getHumanPasswordlessTokens(ctx, userID, resourceowner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
userAgg, webAuthN, verifyWebAuthN, err := c.verifyHumanWebAuthN(ctx, userID, resourceowner, tokenName, userAgentID, credentialData, u2fTokens)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx,
|
2021-02-08 10:30:30 +00:00
|
|
|
usr_repo.NewHumanPasswordlessVerifiedEvent(
|
2021-01-15 08:32:59 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
userAgg,
|
2021-03-01 07:48:50 +00:00
|
|
|
verifyWebAuthN.WebauthNTokenID,
|
2021-01-15 08:32:59 +00:00
|
|
|
webAuthN.WebAuthNTokenName,
|
|
|
|
webAuthN.AttestationType,
|
|
|
|
webAuthN.KeyID,
|
|
|
|
webAuthN.PublicKey,
|
|
|
|
webAuthN.AAGUID,
|
|
|
|
webAuthN.SignCount,
|
|
|
|
),
|
|
|
|
)
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) verifyHumanWebAuthN(ctx context.Context, userID, resourceowner, tokenName, userAgentID string, credentialData []byte, tokens []*domain.WebAuthNToken) (*eventstore.Aggregate, *domain.WebAuthNToken, *HumanWebAuthNWriteModel, error) {
|
2021-01-15 08:32:59 +00:00
|
|
|
if userID == "" || resourceowner == "" {
|
|
|
|
return nil, nil, nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-3M0od", "Errors.IDMissing")
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
user, err := c.getHuman(ctx, userID, resourceowner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
_, token := domain.GetTokenToVerify(tokens)
|
2021-02-24 10:17:39 +00:00
|
|
|
webAuthN, err := c.webauthn.FinishRegistration(user, token, tokenName, credentialData, userAgentID != "")
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
verifyWebAuthN, err := c.webauthNWriteModelByID(ctx, userID, token.WebAuthNTokenID, resourceowner)
|
2021-01-15 08:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userAgg := UserAggregateFromWriteModel(&verifyWebAuthN.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
return userAgg, webAuthN, verifyWebAuthN, nil
|
2021-01-15 08:32:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanBeginU2FLogin(ctx context.Context, userID, resourceOwner string, authRequest *domain.AuthRequest, isLoginUI bool) (*domain.WebAuthNLogin, error) {
|
|
|
|
u2fTokens, err := c.getHumanU2FTokens(ctx, userID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
userAgg, webAuthNLogin, err := c.beginWebAuthNLogin(ctx, userID, resourceOwner, u2fTokens, isLoginUI)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx,
|
2021-02-08 10:30:30 +00:00
|
|
|
usr_repo.NewHumanU2FBeginLoginEvent(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
userAgg,
|
2021-02-08 10:30:30 +00:00
|
|
|
webAuthNLogin.Challenge,
|
|
|
|
authRequestDomainToAuthRequestInfo(authRequest),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return webAuthNLogin, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanBeginPasswordlessLogin(ctx context.Context, userID, resourceOwner string, authRequest *domain.AuthRequest, isLoginUI bool) (*domain.WebAuthNLogin, error) {
|
|
|
|
u2fTokens, err := c.getHumanPasswordlessTokens(ctx, userID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
userAgg, webAuthNLogin, err := c.beginWebAuthNLogin(ctx, userID, resourceOwner, u2fTokens, isLoginUI)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx,
|
2021-02-08 10:30:30 +00:00
|
|
|
usr_repo.NewHumanPasswordlessBeginLoginEvent(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
userAgg,
|
2021-02-08 10:30:30 +00:00
|
|
|
webAuthNLogin.Challenge,
|
|
|
|
authRequestDomainToAuthRequestInfo(authRequest),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return webAuthNLogin, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) beginWebAuthNLogin(ctx context.Context, userID, resourceOwner string, tokens []*domain.WebAuthNToken, isLoginUI bool) (*eventstore.Aggregate, *domain.WebAuthNLogin, error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
if userID == "" {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-hh8K9", "Errors.IDMissing")
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
human, err := c.getHuman(ctx, userID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, nil, err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
webAuthNLogin, err := c.webauthn.BeginLogin(human, domain.UserVerificationRequirementDiscouraged, isLoginUI, tokens...)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, nil, err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
writeModel, err := c.webauthNWriteModelByID(ctx, userID, "", resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, nil, err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&writeModel.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
|
|
|
|
return userAgg, webAuthNLogin, nil
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanFinishU2FLogin(ctx context.Context, userID, resourceOwner string, credentialData []byte, authRequest *domain.AuthRequest, isLoginUI bool) error {
|
|
|
|
webAuthNLogin, err := c.getHumanU2FLogin(ctx, userID, authRequest.ID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
u2fTokens, err := c.getHumanU2FTokens(ctx, userID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
userAgg, token, signCount, err := c.finishWebAuthNLogin(ctx, userID, resourceOwner, credentialData, webAuthNLogin, u2fTokens, isLoginUI)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
2021-03-01 07:48:50 +00:00
|
|
|
_, pushErr := c.eventstore.PushEvents(ctx,
|
|
|
|
usr_repo.NewHumanU2FCheckFailedEvent(
|
|
|
|
ctx,
|
|
|
|
userAgg,
|
|
|
|
authRequestDomainToAuthRequestInfo(authRequest),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
logging.Log("EVENT-33M9f").OnError(pushErr).WithField("userID", userID).Warn("could not push failed passwordless check event")
|
2021-02-08 10:30:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx,
|
2021-03-01 07:48:50 +00:00
|
|
|
usr_repo.NewHumanU2FCheckSucceededEvent(
|
|
|
|
ctx,
|
|
|
|
userAgg,
|
|
|
|
authRequestDomainToAuthRequestInfo(authRequest),
|
|
|
|
),
|
2021-02-08 10:30:30 +00:00
|
|
|
usr_repo.NewHumanU2FSignCountChangedEvent(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
userAgg,
|
2021-02-08 10:30:30 +00:00
|
|
|
token.WebAuthNTokenID,
|
|
|
|
signCount,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanFinishPasswordlessLogin(ctx context.Context, userID, resourceOwner string, credentialData []byte, authRequest *domain.AuthRequest, isLoginUI bool) error {
|
|
|
|
webAuthNLogin, err := c.getHumanPasswordlessLogin(ctx, userID, authRequest.ID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
passwordlessTokens, err := c.getHumanPasswordlessTokens(ctx, userID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
userAgg, token, signCount, err := c.finishWebAuthNLogin(ctx, userID, resourceOwner, credentialData, webAuthNLogin, passwordlessTokens, isLoginUI)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
2021-03-01 07:48:50 +00:00
|
|
|
_, pushErr := c.eventstore.PushEvents(ctx,
|
|
|
|
usr_repo.NewHumanPasswordlessCheckFailedEvent(
|
|
|
|
ctx,
|
|
|
|
userAgg,
|
|
|
|
authRequestDomainToAuthRequestInfo(authRequest),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
logging.Log("EVENT-33M9f").OnError(pushErr).WithField("userID", userID).Warn("could not push failed passwordless check event")
|
2021-02-08 10:30:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx,
|
2021-03-01 07:48:50 +00:00
|
|
|
usr_repo.NewHumanU2FCheckSucceededEvent(
|
|
|
|
ctx,
|
|
|
|
userAgg,
|
|
|
|
authRequestDomainToAuthRequestInfo(authRequest),
|
|
|
|
),
|
2021-02-08 10:30:30 +00:00
|
|
|
usr_repo.NewHumanPasswordlessSignCountChangedEvent(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
userAgg,
|
2021-02-08 10:30:30 +00:00
|
|
|
token.WebAuthNTokenID,
|
|
|
|
signCount,
|
|
|
|
),
|
|
|
|
)
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) finishWebAuthNLogin(ctx context.Context, userID, resourceOwner string, credentialData []byte, webAuthN *domain.WebAuthNLogin, tokens []*domain.WebAuthNToken, isLoginUI bool) (*eventstore.Aggregate, *domain.WebAuthNToken, uint32, error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
if userID == "" {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, nil, 0, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-hh8K9", "Errors.IDMissing")
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
human, err := c.getHuman(ctx, userID, resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, nil, 0, err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2021-02-24 10:17:39 +00:00
|
|
|
keyID, signCount, err := c.webauthn.FinishLogin(human, webAuthN, credentialData, isLoginUI, tokens...)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil && keyID == nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, nil, 0, err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, token := domain.GetTokenByKeyID(tokens, keyID)
|
|
|
|
if token == nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, nil, 0, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-3b7zs", "Errors.User.WebAuthN.NotFound")
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
writeModel, err := c.webauthNWriteModelByID(ctx, userID, "", resourceOwner)
|
2021-02-08 10:30:30 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, nil, 0, err
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
userAgg := UserAggregateFromWriteModel(&writeModel.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
|
|
|
|
return userAgg, token, signCount, nil
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanRemoveU2F(ctx context.Context, userID, webAuthNID, resourceOwner string) error {
|
2021-02-18 13:48:27 +00:00
|
|
|
event := usr_repo.PrepareHumanU2FRemovedEvent(ctx, webAuthNID)
|
2021-02-24 10:17:39 +00:00
|
|
|
return c.removeHumanWebAuthN(ctx, userID, webAuthNID, resourceOwner, event)
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) HumanRemovePasswordless(ctx context.Context, userID, webAuthNID, resourceOwner string) error {
|
2021-02-18 13:48:27 +00:00
|
|
|
event := usr_repo.PrepareHumanPasswordlessRemovedEvent(ctx, webAuthNID)
|
2021-02-24 10:17:39 +00:00
|
|
|
return c.removeHumanWebAuthN(ctx, userID, webAuthNID, resourceOwner, event)
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) removeHumanWebAuthN(ctx context.Context, userID, webAuthNID, resourceOwner string, preparedEvent func(*eventstore.Aggregate) eventstore.EventPusher) error {
|
2021-01-07 15:06:45 +00:00
|
|
|
if userID == "" || webAuthNID == "" {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-6M9de", "Errors.IDMissing")
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
existingWebAuthN, err := c.webauthNWriteModelByID(ctx, userID, webAuthNID, resourceOwner)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-15 08:32:59 +00:00
|
|
|
if existingWebAuthN.State == domain.MFAStateUnspecified || existingWebAuthN.State == domain.MFAStateRemoved {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-2M9ds", "Errors.User.ExternalIDP.NotFound")
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
userAgg := UserAggregateFromWriteModel(&existingWebAuthN.WriteModel)
|
2021-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, preparedEvent(userAgg))
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) webauthNWriteModelByID(ctx context.Context, userID, webAuthNID, resourceOwner string) (writeModel *HumanWebAuthNWriteModel, err error) {
|
2021-01-07 15:06:45 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
writeModel = NewHumanWebAuthNWriteModel(userID, webAuthNID, resourceOwner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
2021-01-07 15:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|