From 208dbe7b585df345d7ff31691a8e814702d87246 Mon Sep 17 00:00:00 2001 From: novikovmu Date: Wed, 5 Oct 2022 11:33:18 +0300 Subject: [PATCH] MSP-1068 init auth controller --- controller/auth.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 controller/auth.go diff --git a/controller/auth.go b/controller/auth.go new file mode 100644 index 0000000..f636e66 --- /dev/null +++ b/controller/auth.go @@ -0,0 +1,98 @@ +package controller + +import ( + "context" + + "github.com/integration-system/isp-kit/log" + "github.com/pkg/errors" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "msp-admin-service/domain" +) + +type authService interface { + Login(ctx context.Context, authRequest domain.AuthRequest) (*domain.Auth, error) + LoginWithSudir(ctx context.Context, request domain.SudirAuthRequest) (*domain.Auth, error) +} + +type Auth struct { + authService authService + logger log.Logger +} + +func NewAuth(authService authService, logger log.Logger) Auth { + return Auth{ + authService: authService, + logger: logger, + } +} + +// Logout +// @Tags auth +// @Summary Выход из авторизованной сессии +// @Description Выход из авторизованной сессии администрирования +// @Accept json +// @Produce json +// @Param X-AUTH-ADMIN header string true "Токен администратора" +// @Success 200 +// @Failure 400 {object} domain.GrpcError "Невалидный токен" +// @Failure 500 {object} domain.GrpcError +// @Router /auth/logout [POST] +func (a Auth) Logout() error { + return nil +} + +// Login +// @Tags auth +// @Summary Авторизация по логину и паролю +// @Description Авторизация с получением токена администратора +// @Accept json +// @Produce json +// @Param body body domain.AuthRequest true "Тело запроса" +// @Success 200 {object} domain.Auth +// @Failure 400 {object} domain.GrpcError +// @Failure 401 {object} domain.GrpcError "Данные для авторизации не верны" +// @Failure 500 {object} domain.GrpcError +// @Router /auth/login [POST] +func (a Auth) Login(ctx context.Context, authRequest domain.AuthRequest) (*domain.Auth, error) { + auth, err := a.authService.Login(ctx, authRequest) + + switch { + case errors.Is(err, domain.ErrSudirAuthorization): + return nil, status.Error(codes.InvalidArgument, "Пользователь имеет только авторизацию СУДИР") + case errors.Is(err, domain.ErrUnauthenticated): + a.logger.Error(ctx, err.Error()) + return nil, status.Error(codes.Unauthenticated, "Данные для авторизации не верны") + case err != nil: + return nil, errors.WithMessage(err, "login") + default: + return auth, nil + } +} + +// LoginWithSudir +// @Tags auth +// @Summary Авторизация по авторизационному коду от СУДИР +// @Description Авторизация с получением токена администратора +// @Accept json +// @Produce json +// @Param body body domain.SudirAuthRequest true "Тело запроса" +// @Success 200 {object} domain.Auth +// @Failure 401 {object} domain.GrpcError "Некорректный код для авторизации" +// @Failure 412 {object} domain.GrpcError "Авторизация СУДИР не настроена на сервере" +// @Failure 500 {object} domain.GrpcError +// @Router /auth/login_with_sudir [POST] +func (a Auth) LoginWithSudir(ctx context.Context, request domain.SudirAuthRequest) (*domain.Auth, error) { + auth, err := a.authService.LoginWithSudir(ctx, request) + + switch { + case errors.Is(err, domain.ErrSudirAuthIsMissed): + return nil, status.Error(codes.FailedPrecondition, "Авторизация СУДИР не настроена на сервере") + case errors.Is(err, domain.ErrUnauthenticated): + return nil, status.Error(codes.Unauthenticated, "Некорректный код для авторизации") + case err != nil: + return nil, errors.WithMessage(err, "login with sudir") + default: + return auth, nil + } +}