From 494ffac97c57af8eb990daf3a8dd9043e1b702eb Mon Sep 17 00:00:00 2001 From: yakovlevav Date: Wed, 10 Sep 2025 13:33:33 +0300 Subject: [PATCH] MSP-6148 add idle logout --- controller/auth.go | 30 ++++++++++++++++++++++++++++-- docs/swagger.yaml | 41 ++++++++++++++++++++++++++++++++++++++--- domain/login.go | 4 ++++ routes/routes.go | 5 +++++ service/auth.go | 7 ++++++- 5 files changed, 81 insertions(+), 6 deletions(-) diff --git a/controller/auth.go b/controller/auth.go index fa8b59e..4797e60 100644 --- a/controller/auth.go +++ b/controller/auth.go @@ -16,7 +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 + Logout(ctx context.Context, adminId int64, request *domain.LogoutRequest) error } type Auth struct { @@ -48,7 +48,33 @@ func (a Auth) Logout(ctx context.Context, authData grpc.AuthData) error { return err } - err = a.authService.Logout(ctx, adminId) + err = a.authService.Logout(ctx, adminId, nil) + if err != nil { + return errors.WithMessage(err, "logout") + } + + return nil +} + +// LogoutWithReason +// @Tags auth +// @Summary Выход по бездействию из авторизованной сессии +// @Description Выход по бездействию из авторизованной сессии администрирования +// @Accept json +// @Produce json +// @Param body body domain.LogoutRequest true "Тело запроса" +// @Param X-AUTH-ADMIN header string true "Токен администратора" +// @Success 200 +// @Failure 400 {object} domain.GrpcError "Невалидный токен" +// @Failure 500 {object} domain.GrpcError +// @Router /auth/logout_with_reason [POST] +func (a Auth) LogoutWithReason(ctx context.Context, authData grpc.AuthData, logoutRequest domain.LogoutRequest) error { + adminId, err := getAdminId(authData) + if err != nil { + return err + } + + err = a.authService.Logout(ctx, adminId, &logoutRequest) if err != nil { return errors.WithMessage(err, "logout") } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 951d977..1946721 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -174,6 +174,11 @@ definitions: required: - authCode type: object + domain.LogoutRequest: + properties: + reason: + type: string + type: object domain.PageRequest: properties: limit: @@ -228,7 +233,6 @@ definitions: domain.SecureAuthResponse: properties: adminId: - format: int64 type: integer authenticated: type: boolean @@ -310,7 +314,6 @@ definitions: firstName: type: string id: - format: int64 type: integer lastName: type: string @@ -329,7 +332,6 @@ definitions: type: string ids: items: - format: int64 type: integer type: array limit: @@ -448,6 +450,39 @@ paths: summary: Выход из авторизованной сессии tags: - auth + /auth/logout_with_reason: + post: + consumes: + - application/json + description: Выход по бездействию из авторизованной сессии администрирования + parameters: + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.LogoutRequest' + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + "400": + description: Невалидный токен + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Выход по бездействию из авторизованной сессии + tags: + - auth /log/all: post: consumes: diff --git a/domain/login.go b/domain/login.go index 216425a..abc5090 100644 --- a/domain/login.go +++ b/domain/login.go @@ -6,6 +6,10 @@ const ( AdminAuthIdHeader = "x-admin-id" ) +type LogoutRequest struct { + Reason string +} + type LoginRequest struct { Email string `validate:"required"` Password string ` validate:"required"` diff --git a/routes/routes.go b/routes/routes.go index c96ce9a..636d312 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -49,6 +49,11 @@ func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor { Inner: true, Handler: c.Auth.Logout, }, + { + Path: "admin/auth/logout_with_reason", + Inner: true, + Handler: c.Auth.LogoutWithReason, + }, { Path: "admin/user/get_profile", Inner: true, diff --git a/service/auth.go b/service/auth.go index ec10d21..d77707c 100644 --- a/service/auth.go +++ b/service/auth.go @@ -213,7 +213,7 @@ func (a Auth) LoginWithSudir(ctx context.Context, request domain.LoginSudirReque }, nil } -func (a Auth) Logout(ctx context.Context, adminId int64) error { +func (a Auth) Logout(ctx context.Context, adminId int64, request *domain.LogoutRequest) error { err := a.txRunner.AuthTransaction(ctx, func(ctx context.Context, tx AuthTransaction) error { err := a.tokenService.RevokeAllByUserId(ctx, adminId) if err != nil { @@ -226,6 +226,11 @@ func (a Auth) Logout(ctx context.Context, adminId int64) error { return errors.WithMessage(err, "update user last_active_at") } + if request != nil && request.Reason == "idle" { + a.auditService.SaveAuditAsync(ctx, adminId, "Выход по бездействию", entity.EventSuccessLogout) + return nil + } + a.auditService.SaveAuditAsync(ctx, adminId, "Выход", entity.EventSuccessLogout) return nil })