forked from mtusi2026/mtusi2026lab2
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type JWT struct {
|
|
AccessTokenLifetime time.Duration
|
|
RefreshTokenLifetime time.Duration
|
|
PublicKeyPath string
|
|
PrivateKeyPath string
|
|
}
|
|
|
|
func (c *JWT) Validate() error {
|
|
switch {
|
|
case c.AccessTokenLifetime == 0:
|
|
return wrapValidationError("jwt", errors.New("access_token_lifetime cannot be empty"))
|
|
case c.RefreshTokenLifetime == 0:
|
|
return wrapValidationError("jwt", errors.New("refresh_token_lifetime cannot be empty"))
|
|
case c.PrivateKeyPath == "":
|
|
return wrapValidationError("jwt", errors.New("private_key_path cannot be empty"))
|
|
case c.PublicKeyPath == "":
|
|
return wrapValidationError("jwt", errors.New("public_key_path cannot be empty"))
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (c *JWT) loadConfig(v *viper.Viper) error {
|
|
v.SetEnvPrefix("JWT")
|
|
c.AccessTokenLifetime = v.GetDuration("ACCESS_TOKEN_LIFETIME")
|
|
c.RefreshTokenLifetime = v.GetDuration("REFRESH_TOKEN_LIFETIME")
|
|
c.PublicKeyPath = v.GetString("PUBLIC_KEY_PATH")
|
|
c.PrivateKeyPath = v.GetString("PRIVATE_KEY_PATH")
|
|
return nil
|
|
}
|
|
|
|
func (c *JWT) copy() *JWT {
|
|
return &JWT{
|
|
AccessTokenLifetime: c.AccessTokenLifetime,
|
|
RefreshTokenLifetime: c.RefreshTokenLifetime,
|
|
PublicKeyPath: c.PublicKeyPath,
|
|
PrivateKeyPath: c.PrivateKeyPath,
|
|
}
|
|
}
|