mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
ef012d0081
* feat: add phone change and code verification for user v2 api * feat: add phone change and code verification for user v2 api * fix: add ignored phone.proto * fix: integration tests * Update proto/zitadel/user/v2alpha/user_service.proto * Update idp_template.go --------- Co-authored-by: Livio Spring <livio.a@gmail.com>
201 lines
7.6 KiB
Go
201 lines
7.6 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/logging"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
)
|
|
|
|
// ChangeUserPhone sets a user's phone number, generates a code
|
|
// and triggers a notification sms.
|
|
func (c *Commands) ChangeUserPhone(ctx context.Context, userID, resourceOwner, phone string, alg crypto.EncryptionAlgorithm) (*domain.Phone, error) {
|
|
return c.changeUserPhoneWithCode(ctx, userID, resourceOwner, phone, alg, false)
|
|
}
|
|
|
|
// ChangeUserPhoneReturnCode sets a user's phone number, generates a code and does not send a notification sms.
|
|
// The generated plain text code will be set in the returned Phone object.
|
|
func (c *Commands) ChangeUserPhoneReturnCode(ctx context.Context, userID, resourceOwner, phone string, alg crypto.EncryptionAlgorithm) (*domain.Phone, error) {
|
|
return c.changeUserPhoneWithCode(ctx, userID, resourceOwner, phone, alg, true)
|
|
}
|
|
|
|
// ChangeUserPhoneVerified sets a user's phone number and marks it is verified.
|
|
// No code is generated and no confirmation sms is send.
|
|
func (c *Commands) ChangeUserPhoneVerified(ctx context.Context, userID, resourceOwner, phone string) (*domain.Phone, error) {
|
|
cmd, err := c.NewUserPhoneEvents(ctx, userID, resourceOwner)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err = c.checkPermission(ctx, domain.PermissionUserWrite, cmd.aggregate.ResourceOwner, userID); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = cmd.Change(ctx, domain.PhoneNumber(phone)); err != nil {
|
|
return nil, err
|
|
}
|
|
cmd.SetVerified(ctx)
|
|
return cmd.Push(ctx)
|
|
}
|
|
|
|
func (c *Commands) changeUserPhoneWithCode(ctx context.Context, userID, resourceOwner, phone string, alg crypto.EncryptionAlgorithm, returnCode bool) (*domain.Phone, error) {
|
|
config, err := secretGeneratorConfig(ctx, c.eventstore.Filter, domain.SecretGeneratorTypeVerifyPhoneCode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
gen := crypto.NewEncryptionGenerator(*config, alg)
|
|
return c.changeUserPhoneWithGenerator(ctx, userID, resourceOwner, phone, gen, returnCode)
|
|
}
|
|
|
|
// changeUserPhoneWithGenerator set a user's phone number.
|
|
// returnCode controls if the plain text version of the code will be set in the return object.
|
|
// When the plain text code is returned, no notification sms will be send to the user.
|
|
func (c *Commands) changeUserPhoneWithGenerator(ctx context.Context, userID, resourceOwner, phone string, gen crypto.Generator, returnCode bool) (*domain.Phone, error) {
|
|
cmd, err := c.NewUserPhoneEvents(ctx, userID, resourceOwner)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if authz.GetCtxData(ctx).UserID != userID {
|
|
if err = c.checkPermission(ctx, domain.PermissionUserWrite, cmd.aggregate.ResourceOwner, userID); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if err = cmd.Change(ctx, domain.PhoneNumber(phone)); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = cmd.AddGeneratedCode(ctx, gen, returnCode); err != nil {
|
|
return nil, err
|
|
}
|
|
return cmd.Push(ctx)
|
|
}
|
|
|
|
func (c *Commands) VerifyUserPhone(ctx context.Context, userID, resourceOwner, code string, alg crypto.EncryptionAlgorithm) (*domain.ObjectDetails, error) {
|
|
config, err := secretGeneratorConfig(ctx, c.eventstore.Filter, domain.SecretGeneratorTypeVerifyPhoneCode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
gen := crypto.NewEncryptionGenerator(*config, alg)
|
|
return c.verifyUserPhoneWithGenerator(ctx, userID, resourceOwner, code, gen)
|
|
}
|
|
|
|
func (c *Commands) verifyUserPhoneWithGenerator(ctx context.Context, userID, resourceOwner, code string, gen crypto.Generator) (*domain.ObjectDetails, error) {
|
|
cmd, err := c.NewUserPhoneEvents(ctx, userID, resourceOwner)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = cmd.VerifyCode(ctx, code, gen)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err = cmd.Push(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return writeModelToObjectDetails(&cmd.model.WriteModel), nil
|
|
}
|
|
|
|
// UserPhoneEvents allows step-by-step additions of events,
|
|
// operating on the Human Phone Model.
|
|
type UserPhoneEvents struct {
|
|
eventstore *eventstore.Eventstore
|
|
aggregate *eventstore.Aggregate
|
|
events []eventstore.Command
|
|
model *HumanPhoneWriteModel
|
|
|
|
plainCode *string
|
|
}
|
|
|
|
// NewUserPhoneEvents constructs a UserPhoneEvents with a Human Phone Write Model,
|
|
// filtered by userID and resourceOwner.
|
|
// If a model cannot be found, or it's state is invalid and error is returned.
|
|
func (c *Commands) NewUserPhoneEvents(ctx context.Context, userID, resourceOwner string) (*UserPhoneEvents, error) {
|
|
if userID == "" {
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-xP292j", "Errors.User.Phone.IDMissing")
|
|
}
|
|
|
|
model, err := c.phoneWriteModelByID(ctx, userID, resourceOwner)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if model.UserState == domain.UserStateUnspecified || model.UserState == domain.UserStateDeleted {
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-ieJ2e", "Errors.User.Phone.NotFound")
|
|
}
|
|
if model.UserState == domain.UserStateInitial {
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-uz0Uu", "Errors.User.NotInitialised")
|
|
}
|
|
return &UserPhoneEvents{
|
|
eventstore: c.eventstore,
|
|
aggregate: UserAggregateFromWriteModel(&model.WriteModel),
|
|
model: model,
|
|
}, nil
|
|
}
|
|
|
|
// Change sets a new phone number.
|
|
// The generated event unsets any previously generated code and verified flag.
|
|
func (c *UserPhoneEvents) Change(ctx context.Context, phone domain.PhoneNumber) error {
|
|
phone, err := phone.Normalize()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
event, hasChanged := c.model.NewChangedEvent(ctx, c.aggregate, phone)
|
|
if !hasChanged {
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-Uch5e", "Errors.User.Phone.NotChanged")
|
|
}
|
|
c.events = append(c.events, event)
|
|
return nil
|
|
}
|
|
|
|
// SetVerified sets the phone number to verified.
|
|
func (c *UserPhoneEvents) SetVerified(ctx context.Context) {
|
|
c.events = append(c.events, user.NewHumanPhoneVerifiedEvent(ctx, c.aggregate))
|
|
}
|
|
|
|
// AddGeneratedCode generates a new encrypted code and sets it to the phone number.
|
|
// When returnCode a plain text of the code will be returned from Push.
|
|
func (c *UserPhoneEvents) AddGeneratedCode(ctx context.Context, gen crypto.Generator, returnCode bool) error {
|
|
value, plain, err := crypto.NewCode(gen)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c.events = append(c.events, user.NewHumanPhoneCodeAddedEventV2(ctx, c.aggregate, value, gen.Expiry(), returnCode))
|
|
if returnCode {
|
|
c.plainCode = &plain
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *UserPhoneEvents) VerifyCode(ctx context.Context, code string, gen crypto.Generator) error {
|
|
if code == "" {
|
|
return caos_errs.ThrowInvalidArgument(nil, "COMMAND-Fia4a", "Errors.User.Code.Empty")
|
|
}
|
|
|
|
err := crypto.VerifyCode(c.model.CodeCreationDate, c.model.CodeExpiry, c.model.Code, code, gen)
|
|
if err == nil {
|
|
c.events = append(c.events, user.NewHumanPhoneVerifiedEvent(ctx, c.aggregate))
|
|
return nil
|
|
}
|
|
_, err = c.eventstore.Push(ctx, user.NewHumanPhoneVerificationFailedEvent(ctx, c.aggregate))
|
|
logging.WithFields("id", "COMMAND-Zoo6b", "userID", c.aggregate.ID).OnError(err).Error("NewHumanPhoneVerificationFailedEvent push failed")
|
|
return caos_errs.ThrowInvalidArgument(err, "COMMAND-eis9R", "Errors.User.Code.Invalid")
|
|
}
|
|
|
|
// Push all events to the eventstore and Reduce them into the Model.
|
|
func (c *UserPhoneEvents) Push(ctx context.Context) (*domain.Phone, error) {
|
|
pushedEvents, err := c.eventstore.Push(ctx, c.events...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = AppendAndReduce(c.model, pushedEvents...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
phone := writeModelToPhone(c.model)
|
|
phone.PlainCode = c.plainCode
|
|
|
|
return phone, nil
|
|
}
|