diff --git a/assembly/locator.go b/assembly/locator.go index f3cd3db..3b28c53 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -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, ) diff --git a/conf/default_remote_config.json b/conf/default_remote_config.json index 08285a4..dd794af 100644 --- a/conf/default_remote_config.json +++ b/conf/default_remote_config.json @@ -7,6 +7,5 @@ "username": "{{ msp_pgsql_user }}", "host": "{{ msp_pgsql_address }}", "port": 5432 - }, - "applicationSecret": "{{ msp_app_secret }}" + } } diff --git a/conf/remote.go b/conf/remote.go index afe9b0c..d6120ad 100644 --- a/conf/remote.go +++ b/conf/remote.go @@ -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"` } diff --git a/service/jwt.go b/service/jwt.go deleted file mode 100644 index ad99482..0000000 --- a/service/jwt.go +++ /dev/null @@ -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 -} diff --git a/service/token.go b/service/token.go index 53d5b46..2771201 100644 --- a/service/token.go +++ b/service/token.go @@ -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 { diff --git a/service/token_source.go b/service/token_source.go new file mode 100644 index 0000000..5f4ed27 --- /dev/null +++ b/service/token_source.go @@ -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 +}