MSP-3427 password change realized
This commit is contained in:
1 parent
03bbdd1761
commit
d799b5be60
14 files changed
+136
-10
No files matched your search
@@ -33,7 +33,7 @@ type Assembly struct {
|
||||
func New(boot *bootstrap.Bootstrap) (*Assembly, error) {
|
||||
server := grpc.NewServer()
|
||||
httpCli := httpclix.Default(httpcli.WithMiddlewares(httpclix.Log(boot.App.Logger())))
|
||||
db := dbrx.New(dbx.WithMigration(boot.MigrationsDir))
|
||||
db := dbrx.New(dbx.WithMigrationRunner(boot.MigrationsDir, boot.App.Logger()))
|
||||
bgjobCli := bgjobx.NewClient(db, boot.App.Logger())
|
||||
return &Assembly{
|
||||
boot: boot,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
configServiceAddress:
|
||||
ip: 10.2.4.244
|
||||
port: 9001
|
||||
grpcOuterAddress:
|
||||
#ip: 127.0.0.1
|
||||
port: 9007
|
||||
grpcInnerAddress:
|
||||
ip: 0.0.0.0
|
||||
port: 9007
|
||||
moduleName: msp-admin-service
|
||||
infraServerPort: 9557
|
||||
|
||||
logfile:
|
||||
path: /var/log/msp-admin-service/runtime.log
|
||||
maxSizeMb: 512
|
||||
maxBackups: 4
|
||||
compress: true
|
||||
+1
-1
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
// nolint: gochecknoinits
|
||||
func init() { //
|
||||
schema.CustomGenerators.Register("logLevel", func(field reflect.StructField, t *jsonschema.Type) {
|
||||
schema.CustomGenerators.Register("logLevel", func(field reflect.StructField, t *jsonschema.Schema) {
|
||||
t.Type = "string"
|
||||
t.Enum = []interface{}{"debug", "info", "error", "fatal"}
|
||||
})
|
||||
|
||||
+28
-2
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/txix-open/isp-kit/grpc/apierrors"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/grpc"
|
||||
@@ -18,6 +19,7 @@ type userService interface {
|
||||
DeleteUsers(ctx context.Context, ids []int64, adminId int64) (int, error)
|
||||
Block(ctx context.Context, adminId int64, userId int) error
|
||||
GetById(ctx context.Context, userId int) (*domain.User, error)
|
||||
ChangePassword(ctx context.Context, adminId int64, oldPassword string, newPassword string) error
|
||||
}
|
||||
|
||||
type User struct {
|
||||
@@ -211,6 +213,30 @@ func (u User) GetById(ctx context.Context, identities domain.IdRequest) (*domain
|
||||
return u.userService.GetById(ctx, identities.UserId)
|
||||
}
|
||||
|
||||
func (u User) ChangePassword(ctx context.Context, req domain.ChangePasswordRequest) error {
|
||||
return u.userService.ChangePassword(ctx, req)
|
||||
// ChangePassword
|
||||
// @Tags user
|
||||
// @Summary Метод изменения пароля пользователя
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param X-AUTH-ADMIN header string true "Токен администратора"
|
||||
// @Param body body domain.ChangePasswordRequest true "Тело запроса"
|
||||
// @Success 200
|
||||
// @Failure 400 {object} domain.GrpcError "Невалидное тело запроса"
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /user/change_password [POST]
|
||||
func (u User) ChangePassword(ctx context.Context, authData grpc.AuthData, req domain.ChangePasswordRequest) error {
|
||||
adminId, err := getAdminId(authData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = u.userService.ChangePassword(ctx, adminId, req.OldPassword, req.NewPassword)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrInvalidPassword):
|
||||
return apierrors.NewBusinessError(1001, "invalid password", err)
|
||||
case err != nil:
|
||||
return apierrors.NewInternalServiceError(err)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ var (
|
||||
ErrTooManyLoginRequests = errors.New("too many login requests")
|
||||
ErrUserIsBlocked = errors.New("user is blocked")
|
||||
ErrNoActionRequired = errors.New("no action required")
|
||||
ErrInvalidPassword = errors.New("invalid password")
|
||||
)
|
||||
|
||||
type UnknownAuditEventError struct {
|
||||
|
||||
@@ -231,3 +231,23 @@ func (u User) Block(ctx context.Context, userId int) (*entity.User, error) {
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (u User) ChangePassword(ctx context.Context, userId int64, newPassword string) error {
|
||||
ctx = sql_metrics.OperationLabelToContext(ctx, "User.ChangePassword")
|
||||
|
||||
q, args, err := query.New().
|
||||
Update("users").
|
||||
Where(squirrel.Eq{"id": userId}).
|
||||
Set("password", newPassword).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.repo.ChangePassword: build query")
|
||||
}
|
||||
|
||||
_, err = u.db.Exec(ctx, q, args...)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.repo.ChangePassword: exec query")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -93,6 +93,11 @@ func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor {
|
||||
Inner: true,
|
||||
Handler: c.User.GetById,
|
||||
},
|
||||
{
|
||||
Path: "admin/user/change_password",
|
||||
Inner: true,
|
||||
Handler: c.User.ChangePassword,
|
||||
},
|
||||
{
|
||||
Path: "admin/role/all",
|
||||
Inner: true,
|
||||
|
||||
@@ -33,6 +33,7 @@ type UserRepo interface {
|
||||
DeleteUser(ctx context.Context, ids []int64) (int, error)
|
||||
Insert(ctx context.Context, user entity.User) (int, error)
|
||||
ChangeBlockStatus(ctx context.Context, userId int) (bool, error)
|
||||
ChangePassword(ctx context.Context, userId int64, newPassword string) error
|
||||
}
|
||||
|
||||
type TokenRepo interface {
|
||||
@@ -457,3 +458,26 @@ func diffToString(a map[string]any, b map[string]any) string {
|
||||
result := builder.String()
|
||||
return result[:len(result)-1]
|
||||
}
|
||||
|
||||
func (u User) ChangePassword(ctx context.Context, adminId int64, oldPassword string, newPassword string) error {
|
||||
admin, err := u.userRepo.GetUserById(ctx, adminId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.service.ChangePassword: get user by id")
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(admin.Password), []byte(oldPassword)); err != nil {
|
||||
return domain.ErrInvalidPassword
|
||||
}
|
||||
|
||||
encryptedPassword, errOld := u.cryptPassword(newPassword)
|
||||
if errOld != nil {
|
||||
return errors.WithMessage(errOld, "user.service.ChangePassword: crypt old password")
|
||||
}
|
||||
|
||||
err = u.userRepo.ChangePassword(ctx, adminId, encryptedPassword)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.service.ChangePassword: change password ")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+1
-1
@@ -35,7 +35,7 @@ type AuditSuite struct {
|
||||
func (t *AuditSuite) SetupTest() {
|
||||
testInstance, _ := test.New(t.T())
|
||||
t.test = testInstance
|
||||
t.db = dbt.New(testInstance, dbx.WithMigration("../migrations"))
|
||||
t.db = dbt.New(testInstance, dbx.WithMigrationRunner("../migrations", testInstance.Logger()))
|
||||
|
||||
remote := conf.Remote{
|
||||
Audit: conf.Audit{
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ type AuthTestSuite struct {
|
||||
func (s *AuthTestSuite) SetupTest() {
|
||||
testInstance, _ := test.New(s.T())
|
||||
s.test = testInstance
|
||||
s.db = dbt.New(testInstance, dbx.WithMigration("../migrations"))
|
||||
s.db = dbt.New(testInstance, dbx.WithMigrationRunner("../migrations", testInstance.Logger()))
|
||||
s.httpCli = httpcli.New()
|
||||
|
||||
mocksrv, host := s.initMockSudir()
|
||||
|
||||
@@ -29,7 +29,7 @@ type CustomizationTestSuite struct {
|
||||
func (s *CustomizationTestSuite) SetupTest() {
|
||||
testInstance, _ := test.New(s.T())
|
||||
s.test = testInstance
|
||||
s.db = dbt.New(testInstance, dbx.WithMigration("../migrations"))
|
||||
s.db = dbt.New(testInstance, dbx.WithMigrationRunner("../migrations", testInstance.Logger()))
|
||||
|
||||
remote := conf.Remote{
|
||||
UiDesign: conf.UIDesign{
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestInactiveWorker(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
test, require := test.New(t)
|
||||
db := dbt.New(test, dbx.WithMigration("../migrations"))
|
||||
db := dbt.New(test, dbx.WithMigrationRunner("../migrations", test.Logger()))
|
||||
|
||||
userId := InsertUser(db, entity.User{Email: "a@test"})
|
||||
InsertUser(db, entity.User{Email: "b@test"})
|
||||
|
||||
@@ -34,7 +34,7 @@ type SecureSuite struct {
|
||||
|
||||
func (s *SecureSuite) SetupTest() {
|
||||
s.test, s.require = test.New(s.T())
|
||||
s.db = dbt.New(s.test, dbx.WithMigration("../migrations"))
|
||||
s.db = dbt.New(s.test, dbx.WithMigrationRunner("../migrations", s.test.Logger()))
|
||||
httpCli := httpcli.New()
|
||||
remote := conf.Remote{
|
||||
ExpireSec: 3600,
|
||||
|
||||
+34
-1
@@ -2,6 +2,8 @@ package tests_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/txix-open/isp-kit/grpc/apierrors"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -45,7 +47,7 @@ type UserTestSuite struct {
|
||||
func (s *UserTestSuite) SetupTest() {
|
||||
testInstance, _ := test.New(s.T())
|
||||
s.test = testInstance
|
||||
s.db = dbt.New(testInstance, dbx.WithMigration("../migrations"))
|
||||
s.db = dbt.New(testInstance, dbx.WithMigrationRunner("../migrations", testInstance.Logger()))
|
||||
s.httpCli = httpcli.New()
|
||||
|
||||
remote := conf.Remote{
|
||||
@@ -338,3 +340,34 @@ func (s *UserTestSuite) TestBlockUser() {
|
||||
s.Require().NoError(err)
|
||||
s.EqualValues(entity.TokenStatusRevoked, t.Status)
|
||||
}
|
||||
|
||||
func (s *UserTestSuite) TestChangePasswordUser() {
|
||||
// insert user with old password
|
||||
adminId := InsertUser(s.db, entity.User{Email: "a_del@a.ru", Password: "password"})
|
||||
|
||||
// check for err when invalid data
|
||||
invalidReq := domain.ChangePasswordRequest{OldPassword: "invalid", NewPassword: "new_password"}
|
||||
err := s.grpcCli.Invoke("admin/user/change_password").
|
||||
AppendMetadata(domain.AdminAuthIdHeader, strconv.Itoa(int(adminId))).
|
||||
JsonRequestBody(invalidReq).
|
||||
Do(context.Background())
|
||||
s.Require().Error(err)
|
||||
err = apierrors.FromError(err)
|
||||
s.Require().Contains(err.Error(), "1001")
|
||||
|
||||
// change password
|
||||
changePswReq := domain.ChangePasswordRequest{OldPassword: "password", NewPassword: "new_password"}
|
||||
err = s.grpcCli.Invoke("admin/user/change_password").
|
||||
AppendMetadata(domain.AdminAuthIdHeader, strconv.Itoa(int(adminId))).
|
||||
JsonRequestBody(changePswReq).
|
||||
Do(context.Background())
|
||||
|
||||
s.Require().NoError(err)
|
||||
|
||||
var newPassword string
|
||||
s.db.Must().SelectRow(&newPassword, "select password from users where id = $1", adminId)
|
||||
|
||||
notEqualErr := bcrypt.CompareHashAndPassword([]byte(newPassword), []byte(changePswReq.NewPassword))
|
||||
s.Require().NoError(notEqualErr)
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user