From 4251ca3da960fade37cb1b52575fb14f0b595fdb Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 18 Jun 2024 14:12:15 +0300 Subject: [PATCH] MSP-3427 added logout after password change --- assembly/locator.go | 1 + docs/docs.go | 1718 +++++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 1109 ++++++++++++++++++++++++++++ repository/user.go | 2 +- service/user.go | 16 +- tests/user_test.go | 11 + 6 files changed, 2853 insertions(+), 4 deletions(-) create mode 100644 docs/docs.go create mode 100644 docs/swagger.yaml diff --git a/assembly/locator.go b/assembly/locator.go index e7f6a14..b9d23b8 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -77,6 +77,7 @@ func (l Locator) Config(ctx context.Context, ldapRepoSupplier ldap.RepoSupplier, auditService, txManager, ldapService, + tokenService, cfg.IdleTimeoutMs, l.logger, ) diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 0000000..9d63722 --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,1718 @@ +// Package docs Code generated by swaggo/swag. DO NOT EDIT +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "contact": {}, + "license": { + "name": "GNU GPL v3.0" + }, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/auth/login": { + "post": { + "description": "Авторизация с получением токена администратора", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "auth" + ], + "summary": "Авторизация по логину и паролю", + "parameters": [ + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.LoginRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.LoginResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "401": { + "description": "Данные для авторизации не верны", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/auth/login_with_sudir": { + "post": { + "description": "Авторизация с получением токена администратора", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "auth" + ], + "summary": "Авторизация по авторизационному коду от СУДИР", + "parameters": [ + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.LoginSudirRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.LoginResponse" + } + }, + "401": { + "description": "Некорректный код для авторизации", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "412": { + "description": "Авторизация СУДИР не настроена на сервере", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/auth/logout": { + "post": { + "description": "Выход из авторизованной сессии администрирования", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "auth" + ], + "summary": "Выход из авторизованной сессии", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Невалидный токен", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/log/all": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "log" + ], + "summary": "Получение списка логов", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.PageRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.AuditResponse" + } + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/log/events": { + "post": { + "description": "Возвращает полный список доступных событий аудита", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "log" + ], + "summary": "Получение списка логов", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.AuditResponse" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/domain.AuditEvent" + } + } + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/log/set_events": { + "post": { + "description": "Всегда возвращает полный список доступных событий аудита", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "log" + ], + "summary": "Получение списка логов", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.SetAuditEvent" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/role/all": { + "post": { + "description": "Получить список ролей", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "role" + ], + "summary": "Список ролей", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/domain.Role" + } + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/role/create": { + "post": { + "description": "Создать роль", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Создать роль", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.CreateRoleRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.User" + } + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "409": { + "description": "Роль с указанным именем уже существует", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/role/delete": { + "post": { + "description": "Удалить существующую роль", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "role" + ], + "summary": "Удалить роль", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.DeleteRoleRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/role/update": { + "post": { + "description": "Обновить данные существующую роль", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "role" + ], + "summary": "Обновить роль", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.UpdateRoleRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.Role" + } + }, + "404": { + "description": "Роль с указанным id не существует", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "409": { + "description": "Роль с указанным именем уже существует", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/secure/authenticate": { + "post": { + "description": "Проверяет токен и возвращает идентификатор администратора", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "secure" + ], + "summary": "Метод аутентификации токена", + "parameters": [ + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.SecureAuthRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.SecureAuthResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/secure/authorize": { + "post": { + "description": "Проверяет наличие у администратора необходимого разрешения", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "secure" + ], + "summary": "Метод авторизации для администратора", + "parameters": [ + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.SecureAuthzRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.SecureAuthzResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/session/all": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "session" + ], + "summary": "Получение списка сессий", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.SessionRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.PageRequest" + } + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/session/revoke": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "session" + ], + "summary": "Отзыв сессии", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.RevokeRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/user/block_user": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Метод блокировки/разблокировки пользователя", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.IdRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/user/change_password": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Метод изменения пароля пользователя", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.ChangePasswordRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/user/create_user": { + "post": { + "description": "Создать пользователя", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Создать пользователя", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.CreateUserRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.User" + } + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "409": { + "description": "Пользователь с указанным email уже существует", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/user/delete_user": { + "post": { + "description": "Удалить существующего пользователя", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Удалить пользователя", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.IdentitiesRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.DeleteResponse" + } + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/user/get_by_id": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Метод получения данных по пользователю", + "parameters": [ + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.IdRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.User" + } + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/user/get_design": { + "post": { + "description": "Получение внешнего вида (палитра и наименование) админ-интерфейса", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Получение внешнего вида", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/conf.UIDesign" + } + }, + "400": { + "description": "Невалидный токен", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/user/get_profile": { + "post": { + "description": "Получить данные профиля", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Получить профиль", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.AdminUserShort" + } + }, + "400": { + "description": "Невалидный токен", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "404": { + "description": "Пользователя не существует", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/user/get_users": { + "post": { + "description": "Получить список пользователей", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Список пользователей", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.UsersRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.UsersResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + }, + "/user/update_user": { + "post": { + "description": "Обновить данные существующего пользователя", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Обновить пользователя", + "parameters": [ + { + "type": "string", + "description": "Токен администратора", + "name": "X-AUTH-ADMIN", + "in": "header", + "required": true + }, + { + "description": "Тело запроса", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/domain.UpdateUserRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/domain.User" + } + }, + "400": { + "description": "Невалидное тело запроса", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "404": { + "description": "Пользователь с указанным id не существует", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "409": { + "description": "Пользователь с указанным email уже существует", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/domain.GrpcError" + } + } + } + } + } + }, + "definitions": { + "conf.UIDesign": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "primaryColor": { + "type": "string" + } + } + }, + "domain.AdminUserShort": { + "type": "object", + "required": [ + "email" + ], + "properties": { + "email": { + "type": "string" + }, + "firstName": { + "type": "string" + }, + "idleTimeoutMs": { + "type": "integer" + }, + "lastName": { + "type": "string" + }, + "permissions": { + "type": "array", + "items": { + "type": "string" + } + }, + "role": { + "type": "string" + }, + "roles": { + "type": "array", + "items": { + "type": "integer" + } + } + } + }, + "domain.Audit": { + "type": "object", + "properties": { + "createdAt": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "message": { + "type": "string" + }, + "userId": { + "type": "integer" + } + } + }, + "domain.AuditEvent": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "event": { + "type": "string" + }, + "name": { + "type": "string" + } + } + }, + "domain.AuditResponse": { + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/definitions/domain.Audit" + } + }, + "totalCount": { + "type": "integer" + } + } + }, + "domain.ChangePasswordRequest": { + "type": "object", + "required": [ + "newPassword", + "oldPassword" + ], + "properties": { + "newPassword": { + "type": "string" + }, + "oldPassword": { + "type": "string" + } + } + }, + "domain.CreateRoleRequest": { + "type": "object", + "properties": { + "changeMessage": { + "type": "string" + }, + "externalGroup": { + "type": "string" + }, + "name": { + "type": "string" + }, + "permissions": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "domain.CreateUserRequest": { + "type": "object", + "required": [ + "email", + "password" + ], + "properties": { + "description": { + "type": "string" + }, + "email": { + "type": "string" + }, + "firstName": { + "type": "string" + }, + "lastName": { + "type": "string" + }, + "password": { + "type": "string" + }, + "roles": { + "type": "array", + "items": { + "type": "integer" + } + } + } + }, + "domain.DeleteResponse": { + "type": "object", + "properties": { + "deleted": { + "type": "integer" + } + } + }, + "domain.DeleteRoleRequest": { + "type": "object", + "properties": { + "id": { + "type": "integer" + } + } + }, + "domain.GrpcError": { + "type": "object", + "properties": { + "details": { + "type": "array", + "items": {} + }, + "errorCode": { + "type": "string" + }, + "errorMessage": { + "type": "string" + } + } + }, + "domain.IdRequest": { + "type": "object", + "required": [ + "userId" + ], + "properties": { + "userId": { + "type": "integer" + } + } + }, + "domain.IdentitiesRequest": { + "type": "object", + "required": [ + "ids" + ], + "properties": { + "ids": { + "type": "array", + "items": { + "type": "integer" + } + } + } + }, + "domain.LoginRequest": { + "type": "object", + "required": [ + "email", + "password" + ], + "properties": { + "email": { + "type": "string" + }, + "password": { + "type": "string" + } + } + }, + "domain.LoginResponse": { + "type": "object", + "properties": { + "expired": { + "type": "string" + }, + "headerName": { + "type": "string" + }, + "token": { + "type": "string" + } + } + }, + "domain.LoginSudirRequest": { + "type": "object", + "required": [ + "authCode" + ], + "properties": { + "authCode": { + "type": "string" + } + } + }, + "domain.PageRequest": { + "type": "object", + "required": [ + "limit" + ], + "properties": { + "limit": { + "type": "integer" + }, + "offset": { + "type": "integer" + } + } + }, + "domain.Permission": { + "type": "object", + "properties": { + "key": { + "type": "string" + }, + "name": { + "type": "string" + } + } + }, + "domain.RevokeRequest": { + "type": "object", + "required": [ + "id" + ], + "properties": { + "id": { + "type": "integer" + } + } + }, + "domain.Role": { + "type": "object", + "properties": { + "changeMessage": { + "type": "string" + }, + "createdAt": { + "type": "string" + }, + "exclusive": { + "type": "boolean" + }, + "externalGroup": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "immutable": { + "type": "boolean" + }, + "name": { + "type": "string" + }, + "permissions": { + "type": "array", + "items": { + "type": "string" + } + }, + "updatedAt": { + "type": "string" + } + } + }, + "domain.SecureAuthRequest": { + "type": "object", + "properties": { + "token": { + "type": "string" + } + } + }, + "domain.SecureAuthResponse": { + "type": "object", + "properties": { + "adminId": { + "type": "integer" + }, + "authenticated": { + "type": "boolean" + }, + "errorReason": { + "type": "string" + } + } + }, + "domain.SecureAuthzRequest": { + "type": "object", + "properties": { + "adminId": { + "type": "integer" + }, + "permission": { + "type": "string" + } + } + }, + "domain.SecureAuthzResponse": { + "type": "object", + "properties": { + "authorized": { + "type": "boolean" + } + } + }, + "domain.SessionRequest": { + "type": "object", + "properties": { + "limit": { + "type": "integer" + }, + "offset": { + "type": "integer" + } + } + }, + "domain.SetAuditEvent": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "event": { + "type": "string" + } + } + }, + "domain.UpdateRoleRequest": { + "type": "object", + "properties": { + "changeMessage": { + "type": "string" + }, + "externalGroup": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "permissions": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "domain.UpdateUserRequest": { + "type": "object", + "required": [ + "id" + ], + "properties": { + "blocked": { + "type": "boolean" + }, + "description": { + "type": "string" + }, + "email": { + "type": "string" + }, + "firstName": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "lastName": { + "type": "string" + }, + "roles": { + "type": "array", + "items": { + "type": "integer" + } + } + } + }, + "domain.User": { + "type": "object", + "properties": { + "blocked": { + "type": "boolean" + }, + "createdAt": { + "type": "string" + }, + "description": { + "type": "string" + }, + "email": { + "type": "string" + }, + "firstName": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "lastName": { + "type": "string" + }, + "lastSessionCreatedAt": { + "type": "string" + }, + "roles": { + "type": "array", + "items": { + "type": "integer" + } + }, + "updatedAt": { + "type": "string" + } + } + }, + "domain.UsersRequest": { + "type": "object", + "properties": { + "email": { + "type": "string" + }, + "ids": { + "type": "array", + "items": { + "type": "integer" + } + }, + "limit": { + "type": "integer" + }, + "offset": { + "type": "integer" + } + } + }, + "domain.UsersResponse": { + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/definitions/domain.User" + } + } + } + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "1.0.0", + Host: "localhost:9000", + BasePath: "/api/admin", + Schemes: []string{}, + Title: "msp-admin-service", + Description: "сервис управления администраторами", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/docs/swagger.yaml b/docs/swagger.yaml new file mode 100644 index 0000000..e1c4670 --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,1109 @@ +basePath: /api/admin +definitions: + conf.UIDesign: + properties: + name: + type: string + primaryColor: + type: string + type: object + domain.AdminUserShort: + properties: + email: + type: string + firstName: + type: string + idleTimeoutMs: + type: integer + lastName: + type: string + permissions: + items: + type: string + type: array + role: + type: string + roles: + items: + type: integer + type: array + required: + - email + type: object + domain.Audit: + properties: + createdAt: + type: string + id: + type: integer + message: + type: string + userId: + type: integer + type: object + domain.AuditEvent: + properties: + enabled: + type: boolean + event: + type: string + name: + type: string + type: object + domain.AuditResponse: + properties: + items: + items: + $ref: '#/definitions/domain.Audit' + type: array + totalCount: + type: integer + type: object + domain.ChangePasswordRequest: + properties: + newPassword: + type: string + oldPassword: + type: string + required: + - newPassword + - oldPassword + type: object + domain.CreateRoleRequest: + properties: + changeMessage: + type: string + externalGroup: + type: string + name: + type: string + permissions: + items: + type: string + type: array + type: object + domain.CreateUserRequest: + properties: + description: + type: string + email: + type: string + firstName: + type: string + lastName: + type: string + password: + type: string + roles: + items: + type: integer + type: array + required: + - email + - password + type: object + domain.DeleteResponse: + properties: + deleted: + type: integer + type: object + domain.DeleteRoleRequest: + properties: + id: + type: integer + type: object + domain.GrpcError: + properties: + details: + items: {} + type: array + errorCode: + type: string + errorMessage: + type: string + type: object + domain.IdRequest: + properties: + userId: + type: integer + required: + - userId + type: object + domain.IdentitiesRequest: + properties: + ids: + items: + type: integer + type: array + required: + - ids + type: object + domain.LoginRequest: + properties: + email: + type: string + password: + type: string + required: + - email + - password + type: object + domain.LoginResponse: + properties: + expired: + type: string + headerName: + type: string + token: + type: string + type: object + domain.LoginSudirRequest: + properties: + authCode: + type: string + required: + - authCode + type: object + domain.PageRequest: + properties: + limit: + type: integer + offset: + type: integer + required: + - limit + type: object + domain.Permission: + properties: + key: + type: string + name: + type: string + type: object + domain.RevokeRequest: + properties: + id: + type: integer + required: + - id + type: object + domain.Role: + properties: + changeMessage: + type: string + createdAt: + type: string + exclusive: + type: boolean + externalGroup: + type: string + id: + type: integer + immutable: + type: boolean + name: + type: string + permissions: + items: + type: string + type: array + updatedAt: + type: string + type: object + domain.SecureAuthRequest: + properties: + token: + type: string + type: object + domain.SecureAuthResponse: + properties: + adminId: + type: integer + authenticated: + type: boolean + errorReason: + type: string + type: object + domain.SecureAuthzRequest: + properties: + adminId: + type: integer + permission: + type: string + type: object + domain.SecureAuthzResponse: + properties: + authorized: + type: boolean + type: object + domain.SessionRequest: + properties: + limit: + type: integer + offset: + type: integer + type: object + domain.SetAuditEvent: + properties: + enabled: + type: boolean + event: + type: string + type: object + domain.UpdateRoleRequest: + properties: + changeMessage: + type: string + externalGroup: + type: string + id: + type: integer + name: + type: string + permissions: + items: + type: string + type: array + type: object + domain.UpdateUserRequest: + properties: + blocked: + type: boolean + description: + type: string + email: + type: string + firstName: + type: string + id: + type: integer + lastName: + type: string + roles: + items: + type: integer + type: array + required: + - id + type: object + domain.User: + properties: + blocked: + type: boolean + createdAt: + type: string + description: + type: string + email: + type: string + firstName: + type: string + id: + type: integer + lastName: + type: string + lastSessionCreatedAt: + type: string + roles: + items: + type: integer + type: array + updatedAt: + type: string + type: object + domain.UsersRequest: + properties: + email: + type: string + ids: + items: + type: integer + type: array + limit: + type: integer + offset: + type: integer + type: object + domain.UsersResponse: + properties: + items: + items: + $ref: '#/definitions/domain.User' + type: array + type: object +host: localhost:9000 +info: + contact: {} + description: сервис управления администраторами + license: + name: GNU GPL v3.0 + title: msp-admin-service + version: 1.0.0 +paths: + /auth/login: + post: + consumes: + - application/json + description: Авторизация с получением токена администратора + parameters: + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.LoginRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.LoginResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/domain.GrpcError' + "401": + description: Данные для авторизации не верны + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Авторизация по логину и паролю + tags: + - auth + /auth/login_with_sudir: + post: + consumes: + - application/json + description: Авторизация с получением токена администратора + parameters: + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.LoginSudirRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.LoginResponse' + "401": + description: Некорректный код для авторизации + schema: + $ref: '#/definitions/domain.GrpcError' + "412": + description: Авторизация СУДИР не настроена на сервере + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Авторизация по авторизационному коду от СУДИР + tags: + - auth + /auth/logout: + post: + consumes: + - application/json + description: Выход из авторизованной сессии администрирования + parameters: + - 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: + - application/json + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.PageRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.AuditResponse' + "400": + description: Невалидное тело запроса + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Получение списка логов + tags: + - log + /log/events: + post: + consumes: + - application/json + description: Возвращает полный список доступных событий аудита + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.AuditResponse' + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/domain.AuditEvent' + type: array + "400": + description: Невалидное тело запроса + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Получение списка логов + tags: + - log + /log/set_events: + post: + consumes: + - application/json + description: Всегда возвращает полный список доступных событий аудита + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.SetAuditEvent' + 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: + - log + /role/all: + post: + consumes: + - application/json + description: Получить список ролей + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/domain.Role' + type: array + "400": + description: Bad Request + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Список ролей + tags: + - role + /role/create: + post: + consumes: + - application/json + description: Создать роль + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.CreateRoleRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.User' + "400": + description: Невалидное тело запроса + schema: + $ref: '#/definitions/domain.GrpcError' + "409": + description: Роль с указанным именем уже существует + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Создать роль + tags: + - user + /role/delete: + post: + consumes: + - application/json + description: Удалить существующую роль + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.DeleteRoleRequest' + 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: + - role + /role/update: + post: + consumes: + - application/json + description: Обновить данные существующую роль + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.UpdateRoleRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.Role' + "404": + description: Роль с указанным id не существует + schema: + $ref: '#/definitions/domain.GrpcError' + "409": + description: Роль с указанным именем уже существует + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Обновить роль + tags: + - role + /secure/authenticate: + post: + consumes: + - application/json + description: Проверяет токен и возвращает идентификатор администратора + parameters: + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.SecureAuthRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.SecureAuthResponse' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Метод аутентификации токена + tags: + - secure + /secure/authorize: + post: + consumes: + - application/json + description: Проверяет наличие у администратора необходимого разрешения + parameters: + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.SecureAuthzRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.SecureAuthzResponse' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Метод авторизации для администратора + tags: + - secure + /session/all: + post: + consumes: + - application/json + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.SessionRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.PageRequest' + "400": + description: Невалидное тело запроса + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Получение списка сессий + tags: + - session + /session/revoke: + post: + consumes: + - application/json + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.RevokeRequest' + 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: + - session + /user/block_user: + post: + consumes: + - application/json + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.IdRequest' + 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: + - user + /user/change_password: + post: + consumes: + - application/json + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.ChangePasswordRequest' + 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: + - user + /user/create_user: + post: + consumes: + - application/json + description: Создать пользователя + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.CreateUserRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.User' + "400": + description: Невалидное тело запроса + schema: + $ref: '#/definitions/domain.GrpcError' + "409": + description: Пользователь с указанным email уже существует + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Создать пользователя + tags: + - user + /user/delete_user: + post: + consumes: + - application/json + description: Удалить существующего пользователя + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.IdentitiesRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.DeleteResponse' + "400": + description: Невалидное тело запроса + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Удалить пользователя + tags: + - user + /user/get_by_id: + post: + consumes: + - application/json + parameters: + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.IdRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.User' + "400": + description: Невалидное тело запроса + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Метод получения данных по пользователю + tags: + - user + /user/get_design: + post: + consumes: + - application/json + description: Получение внешнего вида (палитра и наименование) админ-интерфейса + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/conf.UIDesign' + "400": + description: Невалидный токен + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Получение внешнего вида + tags: + - user + /user/get_profile: + post: + consumes: + - application/json + description: Получить данные профиля + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.AdminUserShort' + "400": + description: Невалидный токен + schema: + $ref: '#/definitions/domain.GrpcError' + "404": + description: Пользователя не существует + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Получить профиль + tags: + - user + /user/get_users: + post: + consumes: + - application/json + description: Получить список пользователей + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.UsersRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.UsersResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Список пользователей + tags: + - user + /user/update_user: + post: + consumes: + - application/json + description: Обновить данные существующего пользователя + parameters: + - description: Токен администратора + in: header + name: X-AUTH-ADMIN + required: true + type: string + - description: Тело запроса + in: body + name: body + required: true + schema: + $ref: '#/definitions/domain.UpdateUserRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/domain.User' + "400": + description: Невалидное тело запроса + schema: + $ref: '#/definitions/domain.GrpcError' + "404": + description: Пользователь с указанным id не существует + schema: + $ref: '#/definitions/domain.GrpcError' + "409": + description: Пользователь с указанным email уже существует + schema: + $ref: '#/definitions/domain.GrpcError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/domain.GrpcError' + summary: Обновить пользователя + tags: + - user +swagger: "2.0" diff --git a/repository/user.go b/repository/user.go index 7194adf..0543979 100644 --- a/repository/user.go +++ b/repository/user.go @@ -246,7 +246,7 @@ func (u User) ChangePassword(ctx context.Context, userId int64, newPassword stri _, err = u.db.Exec(ctx, q, args...) if err != nil { - return errors.WithMessage(err, "user.repo.ChangePassword: exec query") + return errors.WithMessagef(err, "user.repo.ChangePassword: exec query: %s", q) } return nil diff --git a/service/user.go b/service/user.go index 3e20027..6d40213 100644 --- a/service/user.go +++ b/service/user.go @@ -62,6 +62,7 @@ type User struct { auditService auditService txRunner UserTransactionRunner ldapService LdapService + tokenService tokenService idleTimeoutMs int logger log.Logger } @@ -74,6 +75,7 @@ func NewUser( service auditService, txRunner UserTransactionRunner, ldapService LdapService, + tokenService tokenService, idleTimeoutMs int, logger log.Logger, ) User { @@ -85,6 +87,7 @@ func NewUser( auditService: service, txRunner: txRunner, ldapService: ldapService, + tokenService: tokenService, idleTimeoutMs: idleTimeoutMs, logger: logger, } @@ -469,9 +472,9 @@ func (u User) ChangePassword(ctx context.Context, adminId int64, oldPassword str return domain.ErrInvalidPassword } - encryptedPassword, errNew := u.cryptPassword(newPassword) - if errNew != nil { - return errors.WithMessage(errNew, "user.service.ChangePassword: crypt new password") + encryptedPassword, err := u.cryptPassword(newPassword) + if err != nil { + return errors.WithMessage(err, "user.service.ChangePassword: crypt new password") } err = u.userRepo.ChangePassword(ctx, adminId, encryptedPassword) @@ -479,5 +482,12 @@ func (u User) ChangePassword(ctx context.Context, adminId int64, oldPassword str return errors.WithMessage(err, "user.service.ChangePassword: change password ") } + err = u.tokenService.RevokeAllByUserId(ctx, adminId) + if err != nil { + return errors.WithMessage(err, "revoke all tokens by user id") + } + + u.auditService.SaveAuditAsync(ctx, adminId, "Выход", entity.EventSuccessLogout) + return nil } diff --git a/tests/user_test.go b/tests/user_test.go index d33a91f..2fd7ca7 100644 --- a/tests/user_test.go +++ b/tests/user_test.go @@ -345,6 +345,14 @@ func (s *UserTestSuite) TestChangePasswordUser() { // insert user with old password adminId := InsertUser(s.db, entity.User{Email: "a_del@a.ru", Password: "password"}) + InsertTokenEntity(s.db, entity.Token{ + Token: "token-841297641213", + UserId: adminId, + Status: entity.TokenStatusAllowed, + CreatedAt: time.Time{}, + ExpiredAt: time.Time{}, + }) + // check for err when invalid data invalidReq := domain.ChangePasswordRequest{OldPassword: "invalid", NewPassword: "new_password"} err := s.grpcCli.Invoke("admin/user/change_password"). @@ -369,4 +377,7 @@ func (s *UserTestSuite) TestChangePasswordUser() { notEqualErr := bcrypt.CompareHashAndPassword([]byte(newPassword), []byte(changePswReq.NewPassword)) s.Require().NoError(notEqualErr) + + tokenInfo := SelectTokenEntityByToken(s.db, "token-841297641213") + s.Require().Equal(entity.TokenStatusRevoked, tokenInfo.Status) }