MSP-1070 implement logout
This commit is contained in:
1 parent
39911e3a09
commit
ca4554f514
5 files changed
+60
-2
No files matched your search
+20
-1
@@ -2,10 +2,13 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/integration-system/isp-kit/grpc"
|
||||
"github.com/integration-system/isp-kit/log"
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"msp-admin-service/domain"
|
||||
)
|
||||
@@ -13,6 +16,7 @@ import (
|
||||
type authService interface {
|
||||
Login(ctx context.Context, request domain.LoginRequest) (*domain.LoginResponse, error)
|
||||
LoginWithSudir(ctx context.Context, request domain.LoginSudirRequest) (*domain.LoginResponse, error)
|
||||
Logout(ctx context.Context, adminId int64) error
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
@@ -38,7 +42,22 @@ func NewAuth(authService authService, logger log.Logger) Auth {
|
||||
// @Failure 400 {object} domain.GrpcError "Невалидный токен"
|
||||
// @Failure 500 {object} domain.GrpcError
|
||||
// @Router /auth/logout [POST]
|
||||
func (a Auth) Logout() error {
|
||||
func (a Auth) Logout(ctx context.Context, authData grpc.AuthData) error {
|
||||
token, err := grpc.StringFromMd(domain.AdminAuthIdHeader, metadata.MD(authData))
|
||||
if err != nil {
|
||||
return status.Error(codes.InvalidArgument, "Отсутствует идентификатор")
|
||||
}
|
||||
|
||||
adminId, err := strconv.Atoi(token)
|
||||
if err != nil {
|
||||
return status.Error(codes.InvalidArgument, "Недействительный идентификатор")
|
||||
}
|
||||
|
||||
err = a.authService.Logout(ctx, int64(adminId))
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "logout")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -1,6 +1,9 @@
|
||||
package domain
|
||||
|
||||
const AdminAuthHeaderName = "x-auth-admin"
|
||||
const (
|
||||
AdminAuthHeaderName = "x-auth-admin"
|
||||
AdminAuthIdHeader = "x-admin-id"
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
Email string `valid:"required"`
|
||||
|
||||
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/integration-system/isp-kit/db"
|
||||
"github.com/pkg/errors"
|
||||
@@ -51,3 +52,17 @@ func (r Token) GetEntity(ctx context.Context, token string) (*entity.Token, erro
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (r Token) RevokeByUserId(ctx context.Context, userId int64, updatedAt time.Time) error {
|
||||
q := `
|
||||
UPDATE tokens
|
||||
SET status = $1, updated_at = $2
|
||||
WHERE user_id = $3 AND status = $4;
|
||||
`
|
||||
_, err := r.db.Exec(ctx, q, entity.TokenStatusRevoked, updatedAt, userId, entity.TokenStatusAllowed)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update token status")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -18,6 +18,7 @@ type userRepository interface {
|
||||
|
||||
type tokenService interface {
|
||||
GenerateToken(ctx context.Context, id int64) (string, string, error)
|
||||
RevokeAllByUserId(ctx context.Context, userId int64) error
|
||||
}
|
||||
|
||||
type sudirService interface {
|
||||
@@ -108,3 +109,12 @@ func (a Auth) LoginWithSudir(ctx context.Context, request domain.LoginSudirReque
|
||||
HeaderName: domain.AdminAuthHeaderName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a Auth) Logout(ctx context.Context, adminId int64) error {
|
||||
err := a.tokenService.RevokeAllByUserId(ctx, adminId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "revoke all tokens by user id")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
type TokenRep interface {
|
||||
Save(ctx context.Context, token entity.Token) error
|
||||
GetEntity(ctx context.Context, token string) (*entity.Token, error)
|
||||
RevokeByUserId(ctx context.Context, userId int64, updatedAt time.Time) error
|
||||
}
|
||||
|
||||
type Token struct {
|
||||
@@ -67,3 +68,13 @@ func (s Token) GetUserId(ctx context.Context, token string) (int64, error) {
|
||||
|
||||
return tokenInfo.UserId, nil
|
||||
}
|
||||
|
||||
func (s Token) RevokeAllByUserId(ctx context.Context, userId int64) error {
|
||||
updatedAt := time.Now().UTC()
|
||||
err := s.tokenRep.RevokeByUserId(ctx, userId, updatedAt)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "set revoked status")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user