MSP-634 replace jwt to simple crypto rand

This commit is contained in:
d1slike committed 2022-08-04 22:13:28 +03:00
1 parent 2ea518d902
commit 231792aefa
6 files changed
+34 -66

No files matched your search

+1 -1
View File
@@ -45,7 +45,7 @@ func (l Locator) Handler(cfg conf.Remote) isp.BackendServiceServer {
domainService := service.NewDomain(domainRep)
serviceService := service.NewService(domainRep, serviceRep)
jwtService := service.NewJwt(cfg.ApplicationSecret)
jwtService := service.NewTokenSource()
tokenService := service.NewToken(cfg.DefaultTokenExpireTime, jwtService, applicationService, txManager,
applicationRep, domainRep, serviceRep, tokenRep,
)
+1 -2
View File
@@ -7,6 +7,5 @@
"username": "{{ msp_pgsql_user }}",
"host": "{{ msp_pgsql_address }}",
"port": 5432
},
"applicationSecret": "{{ msp_app_secret }}"
}
}
-1
View File
@@ -20,5 +20,4 @@ type Remote struct {
Database dbx.Config `schema:"Настройка базы данных"`
LogLevel log.Level `schemaGen:"logLevel" schema:"Уровень логирования"`
DefaultTokenExpireTime int `schema:"Время жизни токена по умолчанию,время жизни токена в миллисекундах с момента его создания. если = -1 - время жизни неограниченно"`
ApplicationSecret string `schema:"Ключ для подписи application токенов" valid:"required~Required"`
}
-56
View File
@@ -1,56 +0,0 @@
package service
import (
"crypto/rand"
"encoding/hex"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
)
type Jwt struct {
secret string
}
func NewJwt(secret string) Jwt {
return Jwt{
secret: secret,
}
}
func (s Jwt) CreateApplicationToken(appId int, expireTime int) (string, error) {
random, err := s.generateSalt()
if err != nil {
return "", errors.WithMessage(err, "generate salt")
}
created := time.Now().UTC()
claims := jwt.MapClaims{
"appId": appId,
"iat": created.Unix(),
"salt": random,
}
if expireTime > 0 {
claims["exp"] = created.Add(time.Millisecond * time.Duration(expireTime)).Unix()
}
token, err := jwt.NewWithClaims(jwt.SigningMethodHS512, claims).
SignedString([]byte(s.secret))
if err != nil {
return "", errors.WithMessage(err, "signed token")
}
return token, nil
}
func (Jwt) generateSalt() (string, error) {
cryptoRand := make([]byte, 16) //nolint:gomnd
_, err := rand.Read(cryptoRand)
if err != nil {
return "", errors.WithMessage(err, "crypto/rand read")
}
random := hex.EncodeToString(cryptoRand)
return random, nil
}
+6 -6
View File
@@ -8,8 +8,8 @@ import (
"isp-system-service/entity"
)
type ITokenJwt interface {
CreateApplicationToken(appId int, expireTime int) (string, error)
type ITokenSource interface {
CreateApplicationToken() (string, error)
}
type ITokenAppEnrich interface {
@@ -47,7 +47,7 @@ type ITokenTxRunner interface {
type Token struct {
defaultExpireTime int
jwt ITokenJwt
jwt ITokenSource
appEnrich ITokenAppEnrich
tx ITokenTxRunner
appRep ITokenApplicationRep
@@ -58,7 +58,7 @@ type Token struct {
func NewToken(
defaultExpireTime int,
jwtGenerate ITokenJwt,
jwtGenerate ITokenSource,
appEnrich ITokenAppEnrich,
tx ITokenTxRunner,
appRep ITokenApplicationRep,
@@ -113,9 +113,9 @@ func (s Token) Create(ctx context.Context, req domain.TokenCreateRequest) (*doma
expTime = s.defaultExpireTime
}
token, err := s.jwt.CreateApplicationToken(req.AppId, expTime)
token, err := s.jwt.CreateApplicationToken()
if err != nil {
return nil, errors.WithMessagef(err, "jwt create application token")
return nil, errors.WithMessagef(err, "create application token")
}
err = s.tx.TokenCreateTx(ctx, func(ctx context.Context, tx ITokenCreateTx) error {
+26
View File
@@ -0,0 +1,26 @@
package service
import (
"crypto/rand"
"encoding/hex"
"github.com/pkg/errors"
)
type TokenSource struct {
}
func NewTokenSource() TokenSource {
return TokenSource{}
}
func (s TokenSource) CreateApplicationToken() (string, error) {
cryptoRand := make([]byte, 128) //nolint:gomnd
_, err := rand.Read(cryptoRand)
if err != nil {
return "", errors.WithMessage(err, "crypto/rand read")
}
random := hex.EncodeToString(cryptoRand)
return random, nil
}