mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
aabefb9382
# Which Problems Are Solved The session API was designed to be flexible enough for multiple use cases / login scenarios, where the login could respect the login policy or not. The session API itself does not have a corresponding policy and would not check for a required MFA or alike. It therefore also did not yet respect the lockout policy and would leave it to the login UI to handle that. Since the lockout policy is related to the user and not the login itself, we decided to handle the lockout also on calls of the session API. # How the Problems Are Solved If a lockout policy is set for either password or (T)OTP checks, the corresponding check on the session API be run against the lockout check. This means that any failed check, regardless if occurred in the session API or the current hosted login will be counted against the maximum allowed checks of that authentication mechanism. TOTP, OTP SMS and OTP Email are each treated as a separate mechanism. For implementation: - The existing lockout check functions were refactored to be usable for session API calls. - `SessionCommand` type now returns not only an error, but also `[]eventstore.Command` - these will be executed in case of an error # Additional Changes None. # Additional Context Closes #7967 --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
91 lines
3.5 KiB
Go
91 lines
3.5 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
type humanWebAuthNTokens struct {
|
|
human *domain.Human
|
|
tokens []*domain.WebAuthNToken
|
|
}
|
|
|
|
func (s *SessionCommands) getHumanWebAuthNTokens(ctx context.Context, userVerification domain.UserVerificationRequirement) (*humanWebAuthNTokens, error) {
|
|
humanWritemodel, err := s.gethumanWriteModel(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tokenReadModel, err := s.getHumanWebAuthNTokenReadModel(ctx, userVerification)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &humanWebAuthNTokens{
|
|
human: writeModelToHuman(humanWritemodel),
|
|
tokens: readModelToWebAuthNTokens(tokenReadModel),
|
|
}, nil
|
|
}
|
|
|
|
func (s *SessionCommands) getHumanWebAuthNTokenReadModel(ctx context.Context, userVerification domain.UserVerificationRequirement) (readModel HumanWebAuthNTokensReadModel, err error) {
|
|
readModel = NewHumanU2FTokensReadModel(s.sessionWriteModel.UserID, s.sessionWriteModel.UserResourceOwner)
|
|
if userVerification == domain.UserVerificationRequirementRequired {
|
|
readModel = NewHumanPasswordlessTokensReadModel(s.sessionWriteModel.UserID, s.sessionWriteModel.UserResourceOwner)
|
|
}
|
|
err = s.eventstore.FilterToQueryReducer(ctx, readModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return readModel, nil
|
|
}
|
|
|
|
func (c *Commands) CreateWebAuthNChallenge(userVerification domain.UserVerificationRequirement, rpid string, dst json.Unmarshaler) SessionCommand {
|
|
return func(ctx context.Context, cmd *SessionCommands) ([]eventstore.Command, error) {
|
|
humanPasskeys, err := cmd.getHumanWebAuthNTokens(ctx, userVerification)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
webAuthNLogin, err := c.webauthnConfig.BeginLogin(ctx, humanPasskeys.human, userVerification, rpid, humanPasskeys.tokens...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err = json.Unmarshal(webAuthNLogin.CredentialAssertionData, dst); err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "COMMAND-Yah6A", "Errors.Internal")
|
|
}
|
|
|
|
cmd.WebAuthNChallenged(ctx, webAuthNLogin.Challenge, webAuthNLogin.AllowedCredentialIDs, webAuthNLogin.UserVerification, rpid)
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
func (c *Commands) CheckWebAuthN(credentialAssertionData json.Marshaler) SessionCommand {
|
|
return func(ctx context.Context, cmd *SessionCommands) ([]eventstore.Command, error) {
|
|
credentialAssertionData, err := json.Marshal(credentialAssertionData)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "COMMAND-ohG2o", "Errors.Internal")
|
|
}
|
|
challenge := cmd.sessionWriteModel.WebAuthNChallenge
|
|
if challenge == nil {
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-Ioqu5", "Errors.Session.WebAuthN.NoChallenge")
|
|
}
|
|
webAuthNTokens, err := cmd.getHumanWebAuthNTokens(ctx, challenge.UserVerification)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
webAuthN := challenge.WebAuthNLogin(webAuthNTokens.human, credentialAssertionData)
|
|
|
|
credential, err := c.webauthnConfig.FinishLogin(ctx, webAuthNTokens.human, webAuthN, credentialAssertionData, webAuthNTokens.tokens...)
|
|
if err != nil && (credential == nil || credential.ID == nil) {
|
|
return nil, err
|
|
}
|
|
_, token := domain.GetTokenByKeyID(webAuthNTokens.tokens, credential.ID)
|
|
if token == nil {
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-Aej7i", "Errors.User.WebAuthN.NotFound")
|
|
}
|
|
cmd.WebAuthNChecked(ctx, cmd.now(), token.WebAuthNTokenID, credential.Authenticator.SignCount, credential.Flags.UserVerified)
|
|
return nil, nil
|
|
}
|
|
}
|