Skip to content

Commit 4a8df1c

Browse files
committed
[jwt] pass issuer from config and revoked TTL
1 parent 2a31e3d commit 4a8df1c

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

internal/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type PubSub struct {
9191
type JWT struct {
9292
Secret string `yaml:"secret" envconfig:"JWT__SECRET"`
9393
TTL Duration `yaml:"ttl" envconfig:"JWT__TTL"`
94+
Issuer string `yaml:"issuer" envconfig:"JWT__ISSUER"`
9495
}
9596

9697
var defaultConfig = Config{
@@ -128,6 +129,7 @@ var defaultConfig = Config{
128129
URL: "memory://",
129130
},
130131
JWT: JWT{
131-
TTL: Duration(time.Hour * 24 * 365), // 1 year
132+
TTL: Duration(time.Hour * 24),
133+
Issuer: "sms-gate.app",
132134
},
133135
}

internal/config/module.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ var Module = fx.Module(
129129
return jwt.Config{
130130
Secret: cfg.JWT.Secret,
131131
TTL: time.Duration(cfg.JWT.TTL),
132+
Issuer: cfg.JWT.Issuer,
132133
}
133134
}),
134135
)

internal/sms-gateway/jwt/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
type Config struct {
99
Secret string
1010
TTL time.Duration
11+
Issuer string
1112
}
1213

1314
func (c Config) Validate() error {
@@ -16,7 +17,7 @@ func (c Config) Validate() error {
1617
}
1718

1819
if c.TTL == 0 {
19-
return fmt.Errorf("%w: ttl is required", ErrInvalidConfig)
20+
return fmt.Errorf("%w: ttl must be positive", ErrInvalidConfig)
2021
}
2122

2223
return nil

internal/sms-gateway/jwt/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "errors"
44

55
var (
66
ErrDisabled = errors.New("jwt disabled")
7+
ErrInitFailed = errors.New("failed to initialize jwt")
78
ErrInvalidConfig = errors.New("invalid config")
89
ErrTokenRevoked = errors.New("token revoked")
910
ErrUnexpectedSigningMethod = errors.New("unexpected signing method")

internal/sms-gateway/jwt/revoked.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"time"
78

89
"github.com/android-sms-gateway/server/pkg/cache"
910
)
@@ -30,6 +31,6 @@ func (r *revokedStorage) IsRevoked(ctx context.Context, token string) (bool, err
3031
return true, nil
3132
}
3233

33-
func (r *revokedStorage) Revoke(ctx context.Context, token string) error {
34-
return r.storage.Set(ctx, token, nil)
34+
func (r *revokedStorage) Revoke(ctx context.Context, token string, ttl time.Duration) error {
35+
return r.storage.Set(ctx, token, nil, cache.WithTTL(ttl))
3536
}

internal/sms-gateway/jwt/service.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func New(config Config, revoked *revokedStorage) (Service, error) {
3030
return nil, err
3131
}
3232

33+
if revoked == nil {
34+
return nil, fmt.Errorf("%w: revoked storage is required", ErrInitFailed)
35+
}
36+
3337
idFactory, err := nanoid.Standard(jtiLength)
3438
if err != nil {
3539
return nil, fmt.Errorf("can't create id factory: %w", err)
@@ -45,6 +49,10 @@ func New(config Config, revoked *revokedStorage) (Service, error) {
4549
}
4650

4751
func (s *service) GenerateToken(userID string, scopes []string, ttl time.Duration) (string, error) {
52+
if ttl < 0 {
53+
return "", fmt.Errorf("%w: ttl must be non-negative", ErrInvalidConfig)
54+
}
55+
4856
if ttl == 0 {
4957
ttl = s.config.TTL
5058
}
@@ -53,7 +61,7 @@ func (s *service) GenerateToken(userID string, scopes []string, ttl time.Duratio
5361
claims := &Claims{
5462
RegisteredClaims: jwt.RegisteredClaims{
5563
ID: s.idFactory(),
56-
Issuer: "sms-gate.app",
64+
Issuer: s.config.Issuer,
5765
Subject: userID,
5866
IssuedAt: jwt.NewNumericDate(now),
5967
ExpiresAt: jwt.NewNumericDate(now.Add(min(ttl, s.config.TTL))),
@@ -73,7 +81,7 @@ func (s *service) GenerateToken(userID string, scopes []string, ttl time.Duratio
7381

7482
func (s *service) ParseToken(ctx context.Context, token string) (*Claims, error) {
7583
parsedToken, err := jwt.ParseWithClaims(token, new(Claims), func(t *jwt.Token) (any, error) {
76-
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
84+
if t.Method != jwt.SigningMethodHS256 {
7785
return nil, fmt.Errorf("%w: %v", ErrUnexpectedSigningMethod, t.Header["alg"])
7886
}
7987
return []byte(s.config.Secret), nil
@@ -99,5 +107,5 @@ func (s *service) ParseToken(ctx context.Context, token string) (*Claims, error)
99107
}
100108

101109
func (s *service) RevokeToken(ctx context.Context, jti string) error {
102-
return s.revoked.Revoke(ctx, jti)
110+
return s.revoked.Revoke(ctx, jti, s.config.TTL)
103111
}

0 commit comments

Comments
 (0)