MSP-2370 implement admin/secure/authorize
This commit is contained in:
1 parent
db7a668875
commit
3b3fc9dcfb
20 files changed
+509
-177
No files matched your search
+24
-3
@@ -3,12 +3,14 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/integration-system/isp-kit/grpc/apierrors"
|
||||
"github.com/pkg/errors"
|
||||
"msp-admin-service/domain"
|
||||
)
|
||||
|
||||
type SecureService interface {
|
||||
GetUserId(ctx context.Context, token string) (int64, error)
|
||||
Authenticate(ctx context.Context, token string) (int64, error)
|
||||
Authorize(ctx context.Context, adminId int, permission string) (bool, error)
|
||||
}
|
||||
|
||||
type Secure struct {
|
||||
@@ -27,13 +29,12 @@ func NewSecure(service SecureService) Secure {
|
||||
// @Description Проверяет токен и возвращает идентификатор администратора
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param X-AUTH-ADMIN header string true "Токен администратора"
|
||||
// @Param body body domain.SecureAuthRequest true "Тело запроса"
|
||||
// @Success 200 {object} domain.SecureAuthResponse
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /secure/authenticate [POST]
|
||||
func (s Secure) Authenticate(ctx context.Context, req domain.SecureAuthRequest) (*domain.SecureAuthResponse, error) {
|
||||
adminId, err := s.service.GetUserId(ctx, req.Token)
|
||||
adminId, err := s.service.Authenticate(ctx, req.Token)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrTokenExpired):
|
||||
return &domain.SecureAuthResponse{
|
||||
@@ -57,3 +58,23 @@ func (s Secure) Authenticate(ctx context.Context, req domain.SecureAuthRequest)
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Authorize
|
||||
// @Tags secure
|
||||
// @Summary Метод авторизации для администратора
|
||||
// @Description Проверяет наличие у администратора необходимого разрешения
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.SecureAuthzRequest true "Тело запроса"
|
||||
// @Success 200 {object} domain.SecureAuthzResponse
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /secure/authorize [POST]
|
||||
func (s Secure) Authorize(ctx context.Context, req domain.SecureAuthzRequest) (*domain.SecureAuthzResponse, error) {
|
||||
ok, err := s.service.Authorize(ctx, req.AdminId, req.Permission)
|
||||
if err != nil {
|
||||
return nil, apierrors.NewInternalServiceError(err)
|
||||
}
|
||||
return &domain.SecureAuthzResponse{
|
||||
Authorized: ok,
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user