MSP-623 reimplement token controller
This commit is contained in:
1 parent
e0fc90e4d4
commit
ed5cb04ed3
1 file changed
+54
-210
+54
-210
@@ -2,114 +2,85 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
rd "github.com/go-redis/redis/v8"
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
redisLib "github.com/integration-system/isp-lib/v2/redis"
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"isp-system-service/conf"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/model"
|
||||
"isp-system-service/redis"
|
||||
"isp-system-service/service"
|
||||
)
|
||||
|
||||
const (
|
||||
SystemIdentityFieldInDb = "1"
|
||||
DomainIdentityFieldInDb = "2"
|
||||
ServiceIdentityFieldInDb = "3"
|
||||
ApplicationIdentityFieldInDb = "4"
|
||||
)
|
||||
type ITokenService interface {
|
||||
GetByAppId(ctx context.Context, appId int) ([]domain.Token, error)
|
||||
Create(ctx context.Context, req domain.TokenCreateRequest) (*domain.ApplicationWithTokens, error)
|
||||
Revoke(ctx context.Context, req domain.TokenRevokeRequest) (*domain.ApplicationWithTokens, error)
|
||||
RevokeByAppId(ctx context.Context, appId int) (*domain.DeleteResponse, error)
|
||||
}
|
||||
|
||||
var Token tokenController
|
||||
type Token struct {
|
||||
service ITokenService
|
||||
}
|
||||
|
||||
type tokenController struct{}
|
||||
func NewToken(service ITokenService) Token {
|
||||
return Token{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateToken godoc
|
||||
// GetByAppId godoc
|
||||
// @Tags token
|
||||
// @Summary Получить токены по идентификатору приложения
|
||||
// @Description Возвращает список токенов, привязанных к приложению
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.Identity true "Идентификатор приложения"
|
||||
// @Success 200 {array} entity.Token
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /token/get_tokens_by_app_id [POST]
|
||||
func (c Token) GetByAppId(ctx context.Context, req domain.Identity) ([]domain.Token, error) {
|
||||
return c.service.GetByAppId(ctx, req.Id)
|
||||
}
|
||||
|
||||
// Create godoc
|
||||
// @Tags token
|
||||
// @Summary Создать токен
|
||||
// @Description Созддает токен и привязывает его к приложению
|
||||
// @Description Создает токен и привязывает его к приложению
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.CreateTokenRequest true "Объект создания токена"
|
||||
// @Success 200 {object} domain.AppWithToken
|
||||
// @Param body body domain.TokenCreateRequest true "Объект создания токена"
|
||||
// @Success 200 {object} domain.ApplicationWithTokens
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /token/create_token [POST]
|
||||
func (c tokenController) CreateToken(req domain.CreateTokenRequest) (*domain.AppWithToken, error) {
|
||||
m, err, app := c.getIdMap(req.AppId)
|
||||
if err != nil {
|
||||
func (c Token) Create(ctx context.Context, req domain.TokenCreateRequest) (*domain.ApplicationWithTokens, error) {
|
||||
result, err := c.service.Create(ctx, req)
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrApplicationNotFound):
|
||||
return nil, status.Errorf(codes.NotFound, "application with id %d not found", req.AppId)
|
||||
case errors.Is(err, domain.ErrServiceNotFound):
|
||||
return nil, status.Errorf(codes.NotFound, "service for app_id id %d not found", req.AppId)
|
||||
case errors.Is(err, domain.ErrDomainNotFound):
|
||||
return nil, status.Errorf(codes.NotFound, "domain for app_id %d not found", req.AppId)
|
||||
case err != nil:
|
||||
return nil, err
|
||||
default:
|
||||
return result, nil
|
||||
}
|
||||
|
||||
expTime := req.ExpireTimeMs
|
||||
if expTime == 0 {
|
||||
expTime = config.GetRemote().(*conf.RemoteConfig).DefaultTokenExpireTime
|
||||
}
|
||||
token, err := service.Jwt.CreateApplication(req.AppId, expTime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tokenInfo := entity.Token{
|
||||
Token: token,
|
||||
ExpireTime: expTime,
|
||||
AppId: req.AppId,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := model.DbClient.RunInTransaction(func(repository model.TokenRepository) error {
|
||||
tokenInfo, err = repository.SaveToken(tokenInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.setIdentityMapForToken(tokenInfo, m)
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
arr, err := Application.enrichWithTokens(*app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return arr[0], nil
|
||||
}
|
||||
|
||||
// RevokeTokens godoc
|
||||
// Revoke godoc
|
||||
// @Tags token
|
||||
// @Summary Отозвать токены
|
||||
// @Description Отвязывает токены от приложений и удялет их
|
||||
// @Description Отвязывает токены от приложений и удаляет их
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.RevokeTokensRequest true "Объект отзыва токенов"
|
||||
// @Success 200 {object} domain.AppWithToken
|
||||
// @Param body body domain.TokenRevokeRequest true "Объект отзыва токенов"
|
||||
// @Success 200 {object} domain.ApplicationWithTokens
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /token/revoke_tokens [POST]
|
||||
func (c tokenController) RevokeTokens(req domain.RevokeTokensRequest) (*domain.AppWithToken, error) {
|
||||
app, err := model.AppRep.GetApplicationById(req.AppId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = c.revokeTokens(req.Tokens)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, err := Application.enrichWithTokens(*app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res[0], nil
|
||||
func (c Token) Revoke(ctx context.Context, req domain.TokenRevokeRequest) (*domain.ApplicationWithTokens, error) {
|
||||
return c.service.Revoke(ctx, req)
|
||||
}
|
||||
|
||||
// RevokeTokensForApp godoc
|
||||
// RevokeForApp godoc
|
||||
// @Tags token
|
||||
// @Summary Отозвать токены для приложения
|
||||
// @Description Отвязывает токены от приложений и удаляет их по идентификатору приложения
|
||||
@@ -119,133 +90,6 @@ func (c tokenController) RevokeTokens(req domain.RevokeTokensRequest) (*domain.A
|
||||
// @Success 200 {object} domain.DeleteResponse
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /token/revoke_tokens_for_app [POST]
|
||||
func (c tokenController) RevokeTokensForApp(identity domain.Identity) (*domain.DeleteResponse, error) {
|
||||
return c.revokeTokensForApp(identity)
|
||||
}
|
||||
|
||||
// GetTokensByAppId godoc
|
||||
// @Tags token
|
||||
// @Summary Получить токены по идентификаотру приложения
|
||||
// @Description Возвращает список токенов, привязанных к приложению
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body domain.Identity true "Идентификатор приложения"
|
||||
// @Success 200 {array} entity.Token
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /token/get_tokens_by_app_id [POST]
|
||||
func (tokenController) GetTokensByAppId(identity domain.Identity) ([]entity.Token, error) {
|
||||
return model.TokenRep.GetTokensByAppId(identity.Id)
|
||||
}
|
||||
|
||||
func (c tokenController) setIdentityMapForToken(token entity.Token, idMap map[string]interface{}) error {
|
||||
return c.SetIdentityMapForTokenV2(token.Token, token.ExpireTime, idMap)
|
||||
}
|
||||
|
||||
func (tokenController) SetIdentityMapForTokenV2(token string, expireTime int64, idMap map[string]interface{}) error {
|
||||
instanceUuid := config.Get().(*conf.Configuration).InstanceUuid
|
||||
_, e := redis.Client.Get().UseDbTx(redisLib.ApplicationTokenDb, func(p rd.Pipeliner) error {
|
||||
key := fmt.Sprintf("%s|%s", token, instanceUuid)
|
||||
stat := p.HMSet(context.Background(), key, idMap)
|
||||
err := stat.Err()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if expireTime > 0 {
|
||||
err = p.Expire(context.Background(), token, time.Duration(expireTime)*time.Millisecond).Err()
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func (tokenController) getIdMap(appId int32) (map[string]interface{}, error, *entity.Application) {
|
||||
appInfo, err := model.AppRep.GetApplicationById(appId)
|
||||
if err != nil {
|
||||
return nil, err, nil
|
||||
}
|
||||
if appInfo == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "Application with id %d not found", appId), nil
|
||||
}
|
||||
|
||||
serviceInfo, err := model.ServiceRep.GetServiceById(appInfo.ServiceId)
|
||||
if err != nil {
|
||||
return nil, err, appInfo
|
||||
}
|
||||
if serviceInfo == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "Service with id %d not found", appInfo.ServiceId), appInfo
|
||||
}
|
||||
|
||||
domainInfo, err := model.DomainRep.GetDomainById(serviceInfo.DomainId)
|
||||
if err != nil {
|
||||
return nil, err, appInfo
|
||||
}
|
||||
if domainInfo == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "Domain with id %d not found", serviceInfo.DomainId), appInfo
|
||||
}
|
||||
|
||||
systemInfo, err := model.SystemRep.GetSystemById(domainInfo.SystemId)
|
||||
if err != nil {
|
||||
return nil, err, appInfo
|
||||
}
|
||||
if systemInfo == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "System with id %d not found", domainInfo.SystemId), appInfo
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
SystemIdentityFieldInDb: systemInfo.Id,
|
||||
DomainIdentityFieldInDb: domainInfo.Id,
|
||||
ServiceIdentityFieldInDb: serviceInfo.Id,
|
||||
ApplicationIdentityFieldInDb: appInfo.Id,
|
||||
}, nil, appInfo
|
||||
}
|
||||
|
||||
func (c tokenController) revokeTokensForApp(identity domain.Identity) (*domain.DeleteResponse, error) {
|
||||
tokens, err := model.TokenRep.GetTokensByAppId(identity.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tokenIdList := make([]string, len(tokens))
|
||||
for i, t := range tokens {
|
||||
tokenIdList[i] = t.Token
|
||||
}
|
||||
|
||||
return c.revokeTokens(tokenIdList)
|
||||
}
|
||||
|
||||
func (tokenController) revokeTokens(tokens []string) (*domain.DeleteResponse, error) {
|
||||
if len(tokens) == 0 {
|
||||
return &domain.DeleteResponse{Deleted: 0}, nil
|
||||
}
|
||||
|
||||
var (
|
||||
count = 0
|
||||
instanceUuid = config.Get().(*conf.Configuration).InstanceUuid
|
||||
keys = make([]string, len(tokens))
|
||||
)
|
||||
for i, token := range tokens {
|
||||
keys[i] = fmt.Sprintf("%s|%s", token, instanceUuid)
|
||||
}
|
||||
err := model.DbClient.RunInTransaction(func(rep model.TokenRepository) error {
|
||||
deleted, err := rep.DeleteTokens(tokens)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
count = deleted
|
||||
|
||||
_, err = redis.Client.Get().UseDbTx(redisLib.ApplicationTokenDb, func(p rd.Pipeliner) error {
|
||||
_, err := p.Del(context.Background(), keys...).Result()
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &domain.DeleteResponse{Deleted: count}, nil
|
||||
func (c Token) RevokeForApp(ctx context.Context, req domain.Identity) (*domain.DeleteResponse, error) {
|
||||
return c.service.RevokeByAppId(ctx, req.Id)
|
||||
}
|
||||
Reference in new issue
Block a user