From e5d73d91de3e0073e1689715e89ca04a9cb99a1a Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Mon, 1 Aug 2022 18:01:57 +0300 Subject: [PATCH] MSP-634 add func get_auth_data to token repository --- entity/token.go | 9 +++++++++ repository/token.go | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/entity/token.go b/entity/token.go index 50a553f..c4277f1 100644 --- a/entity/token.go +++ b/entity/token.go @@ -10,3 +10,12 @@ type Token struct { ExpireTime int CreatedAt time.Time } + +type AuthData struct { + SystemId int + DomainId int + ServiceId int + AppId int + ExpireTime int + CreatedAt time.Time +} diff --git a/repository/token.go b/repository/token.go index 83165b7..5c5a455 100644 --- a/repository/token.go +++ b/repository/token.go @@ -2,11 +2,13 @@ package repository import ( "context" + "database/sql" "github.com/Masterminds/squirrel" "github.com/integration-system/isp-kit/db" "github.com/integration-system/isp-kit/db/query" "github.com/pkg/errors" + "isp-system-service/domain" "isp-system-service/entity" ) @@ -92,3 +94,27 @@ func (r Token) DeleteToken(ctx context.Context, tokens []string) (int, error) { return int(rowsAffected), nil } + +func (r Token) AuthDataByToken(ctx context.Context, token string) (*entity.AuthData, error) { + q := ` +SELECT system_id, domain_id, service_id, app_id, token.expire_time, token.created_at +FROM token + LEFT JOIN application + ON token.app_id = application.id + LEFT JOIN service + ON application.service_id = service.id + LEFT JOIN domain + ON service.domain_id = domain.id +WHERE token = $1 +` + result := entity.AuthData{} + err := r.db.SelectRow(ctx, &result, q, token) + if err != nil { + if err == sql.ErrNoRows { + return nil, domain.ErrTokenNotFound + } + return nil, errors.WithMessage(err, "select row db") + } + + return &result, nil +}