From f7aed1c864f4b185b41df002f25b5575d407bef7 Mon Sep 17 00:00:00 2001 From: Fabi <38692350+fgerschwiler@users.noreply.github.com> Date: Wed, 24 Jun 2020 10:47:11 +0200 Subject: [PATCH] feat: my user mfas (#264) --- .../eventsourcing/eventstore/user.go | 4 +++ internal/auth/repository/user.go | 1 + pkg/auth/api/grpc/user.go | 8 +++--- pkg/auth/api/grpc/user_converter.go | 26 +++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/internal/auth/repository/eventsourcing/eventstore/user.go b/internal/auth/repository/eventsourcing/eventstore/user.go index ae55f3e0a0..f50e001422 100644 --- a/internal/auth/repository/eventsourcing/eventstore/user.go +++ b/internal/auth/repository/eventsourcing/eventstore/user.go @@ -170,6 +170,10 @@ func (repo *UserRepo) ChangePassword(ctx context.Context, userID, old, new strin return err } +func (repo *UserRepo) MyUserMfas(ctx context.Context) ([]*model.MultiFactor, error) { + return repo.View.UserMfas(auth.GetCtxData(ctx).UserID) +} + func (repo *UserRepo) AddMfaOTP(ctx context.Context, userID string) (*model.OTP, error) { return repo.UserEvents.AddOTP(ctx, userID) } diff --git a/internal/auth/repository/user.go b/internal/auth/repository/user.go index 31cd7f39f0..6b341f527b 100644 --- a/internal/auth/repository/user.go +++ b/internal/auth/repository/user.go @@ -49,6 +49,7 @@ type myUserRepo interface { ChangeMyPassword(ctx context.Context, old, new string) error + MyUserMfas(ctx context.Context) ([]*model.MultiFactor, error) AddMyMfaOTP(ctx context.Context) (*model.OTP, error) VerifyMyMfaOTPSetup(ctx context.Context, code string) error RemoveMyMfaOTP(ctx context.Context) error diff --git a/pkg/auth/api/grpc/user.go b/pkg/auth/api/grpc/user.go index 1da3b0ea83..434d463d5b 100644 --- a/pkg/auth/api/grpc/user.go +++ b/pkg/auth/api/grpc/user.go @@ -4,8 +4,6 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" - - "github.com/caos/zitadel/internal/errors" ) func (s *Server) GetMyUser(ctx context.Context, _ *empty.Empty) (*UserView, error) { @@ -49,7 +47,11 @@ func (s *Server) GetMyUserAddress(ctx context.Context, _ *empty.Empty) (*UserAdd } func (s *Server) GetMyMfas(ctx context.Context, _ *empty.Empty) (*MultiFactors, error) { - return nil, errors.ThrowUnimplemented(nil, "GRPC-vkl9i", "Not implemented") + mfas, err := s.repo.MyUserMfas(ctx) + if err != nil { + return nil, err + } + return &MultiFactors{Mfas: mfasFromModel(mfas)}, nil } func (s *Server) UpdateMyUserProfile(ctx context.Context, request *UpdateUserProfileRequest) (*UserProfile, error) { diff --git a/pkg/auth/api/grpc/user_converter.go b/pkg/auth/api/grpc/user_converter.go index 88a4f7705a..668bff8eb0 100644 --- a/pkg/auth/api/grpc/user_converter.go +++ b/pkg/auth/api/grpc/user_converter.go @@ -309,3 +309,29 @@ func mfaStateFromModel(state usr_model.MfaState) MFAState { return MFAState_MFASTATE_UNSPECIFIED } } + +func mfasFromModel(mfas []*usr_model.MultiFactor) []*MultiFactor { + converted := make([]*MultiFactor, len(mfas)) + for i, mfa := range mfas { + converted[i] = mfaFromModel(mfa) + } + return converted +} + +func mfaFromModel(mfa *usr_model.MultiFactor) *MultiFactor { + return &MultiFactor{ + State: mfaStateFromModel(mfa.State), + Type: mfaTypeFromModel(mfa.Type), + } +} + +func mfaTypeFromModel(mfatype usr_model.MfaType) MfaType { + switch mfatype { + case usr_model.MfaTypeOTP: + return MfaType_MFATYPE_OTP + case usr_model.MfaTypeSMS: + return MfaType_MFATYPE_SMS + default: + return MfaType_MFATYPE_UNSPECIFIED + } +}