diff --git a/controller/auth.go b/controller/auth.go index f636e66..ef54558 100644 --- a/controller/auth.go +++ b/controller/auth.go @@ -11,8 +11,8 @@ import ( ) type authService interface { - Login(ctx context.Context, authRequest domain.AuthRequest) (*domain.Auth, error) - LoginWithSudir(ctx context.Context, request domain.SudirAuthRequest) (*domain.Auth, error) + Login(ctx context.Context, request domain.LoginRequest) (*domain.LoginResponse, error) + LoginWithSudir(ctx context.Context, request domain.LoginSudirRequest) (*domain.LoginResponse, error) } type Auth struct { @@ -48,13 +48,13 @@ func (a Auth) Logout() error { // @Description Авторизация с получением токена администратора // @Accept json // @Produce json -// @Param body body domain.AuthRequest true "Тело запроса" -// @Success 200 {object} domain.Auth +// @Param body body domain.LoginRequest true "Тело запроса" +// @Success 200 {object} domain.LoginResponse // @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) { +func (a Auth) Login(ctx context.Context, authRequest domain.LoginRequest) (*domain.LoginResponse, error) { auth, err := a.authService.Login(ctx, authRequest) switch { @@ -76,13 +76,13 @@ func (a Auth) Login(ctx context.Context, authRequest domain.AuthRequest) (*domai // @Description Авторизация с получением токена администратора // @Accept json // @Produce json -// @Param body body domain.SudirAuthRequest true "Тело запроса" -// @Success 200 {object} domain.Auth +// @Param body body domain.LoginSudirRequest true "Тело запроса" +// @Success 200 {object} domain.LoginResponse // @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) { +func (a Auth) LoginWithSudir(ctx context.Context, request domain.LoginSudirRequest) (*domain.LoginResponse, error) { auth, err := a.authService.LoginWithSudir(ctx, request) switch { diff --git a/domain/auth.go b/domain/login.go similarity index 74% rename from domain/auth.go rename to domain/login.go index 8db055a..07331c2 100644 --- a/domain/auth.go +++ b/domain/login.go @@ -2,17 +2,17 @@ package domain const AdminAuthHeaderName = "x-auth-admin" -type Auth struct { - Token string - Expired string `json:",omitempty"` - HeaderName string -} - -type AuthRequest struct { +type LoginRequest struct { Email string `valid:"required"` Password string ` valid:"required"` } -type SudirAuthRequest struct { +type LoginSudirRequest struct { AuthCode string `valid:"required"` } + +type LoginResponse struct { + Token string + Expired string `json:",omitempty"` + HeaderName string +} diff --git a/service/auth.go b/service/auth.go index b1a9124..18dc9a6 100644 --- a/service/auth.go +++ b/service/auth.go @@ -40,8 +40,8 @@ func NewAuth(userRepository userRepository, tokenService tokenService, sudirServ } } -func (a Auth) Login(ctx context.Context, authRequest domain.AuthRequest) (*domain.Auth, error) { - user, err := a.userRepository.GetUserByEmail(ctx, authRequest.Email) +func (a Auth) Login(ctx context.Context, request domain.LoginRequest) (*domain.LoginResponse, error) { + user, err := a.userRepository.GetUserByEmail(ctx, request.Email) switch { case errors.Is(err, domain.ErrNotFound): return nil, errors.WithMessage(domain.ErrUnauthenticated, "wrong email") @@ -51,7 +51,7 @@ func (a Auth) Login(ctx context.Context, authRequest domain.AuthRequest) (*domai return nil, domain.ErrSudirAuthorization } - err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(authRequest.Password)) + err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(request.Password)) if err != nil { return nil, errors.WithMessage(domain.ErrUnauthenticated, "wrong password") } @@ -61,14 +61,14 @@ func (a Auth) Login(ctx context.Context, authRequest domain.AuthRequest) (*domai return nil, errors.WithMessage(err, "generate token") } - return &domain.Auth{ + return &domain.LoginResponse{ Token: tokenString, Expired: expired, HeaderName: domain.AdminAuthHeaderName, }, nil } -func (a Auth) LoginWithSudir(ctx context.Context, request domain.SudirAuthRequest) (*domain.Auth, error) { +func (a Auth) LoginWithSudir(ctx context.Context, request domain.LoginSudirRequest) (*domain.LoginResponse, error) { sudirUser, err := a.sudirService.Authenticate(ctx, request.AuthCode) var authErr *entity.SudirAuthError @@ -102,7 +102,7 @@ func (a Auth) LoginWithSudir(ctx context.Context, request domain.SudirAuthReques return nil, errors.WithMessage(err, "generate token") } - return &domain.Auth{ + return &domain.LoginResponse{ Token: tokenString, Expired: expired, HeaderName: domain.AdminAuthHeaderName, diff --git a/tests/auth_test.go b/tests/auth_test.go index 50d2634..b88d151 100644 --- a/tests/auth_test.go +++ b/tests/auth_test.go @@ -80,9 +80,9 @@ func (s *AuthTestSuite) TestLoginHappyPath() { Password: "password", }) - response := domain.Auth{} + response := domain.LoginResponse{} err := s.grpcCli.Invoke("admin/auth/login"). - JsonRequestBody(domain.AuthRequest{ + JsonRequestBody(domain.LoginRequest{ Email: "a@a.ru", Password: "password", }). @@ -97,7 +97,7 @@ func (s *AuthTestSuite) TestLoginHappyPath() { func (s *AuthTestSuite) TestLoginNotFound() { err := s.grpcCli.Invoke("admin/auth/login"). - JsonRequestBody(domain.AuthRequest{ + JsonRequestBody(domain.LoginRequest{ Email: "a1@a.ru", Password: "password", }). @@ -118,7 +118,7 @@ func (s *AuthTestSuite) TestLoginWrongPassword() { }) err := s.grpcCli.Invoke("admin/auth/login"). - JsonRequestBody(domain.AuthRequest{ + JsonRequestBody(domain.LoginRequest{ Email: "a@a.ru", Password: "WrongPassword", }). @@ -130,10 +130,10 @@ func (s *AuthTestSuite) TestLoginWrongPassword() { } func (s *AuthTestSuite) TestSudirLoginHappyPath() { - response := domain.Auth{} + response := domain.LoginResponse{} err := s.grpcCli.Invoke("admin/auth/login_with_sudir"). - JsonRequestBody(domain.SudirAuthRequest{ + JsonRequestBody(domain.LoginSudirRequest{ AuthCode: "code", }). ReadJsonResponse(&response).