MSP-1070 rename auth struct to login struct

This commit is contained in:
DeniskaRediska committed 2022-10-07 17:00:29 +03:00
1 parent bb4c8cd8ad
commit ece61a0e87
4 files changed
+28 -28

No files matched your search

+8 -8
View File
@@ -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 {
+8 -8
View File
@@ -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
}
+6 -6
View File
@@ -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,
+6 -6
View File
@@ -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).