Skip to content

Commit c1b7651

Browse files
feat: refactor the implementation to potentially address sonarCube issues.
1 parent 4436444 commit c1b7651

File tree

2 files changed

+76
-445
lines changed

2 files changed

+76
-445
lines changed

pkg/nodeauth/jwt/node_jwt_authenticator.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,30 @@ import (
1414
"github.com/smartcontractkit/chainlink-common/pkg/nodeauth/utils"
1515
)
1616

17+
// NodeJWTAuthenticatorConfig holds configuration for NodeJWTAuthenticator
18+
type NodeJWTAuthenticatorConfig struct {
19+
// Leeway is the time leeway for JWT validation to address clock skew between systems
20+
Leeway time.Duration
21+
}
22+
1723
// NodeJWTAuthenticator is designed to be used by the server-side service to authenticate the JWT token generated by the Node.
1824
type NodeJWTAuthenticator struct {
1925
nodeAuthProvider NodeAuthProvider // Source of truth to validate public key in the JWT claim.
2026
parser *jwt.Parser // JWT parser to parse the JWT token.
2127
logger *slog.Logger
2228
}
2329

24-
// NodeJWTAuthenticatorOption is a functional option for configuring NodeJWTAuthenticator
25-
type NodeJWTAuthenticatorOption func(*[]jwt.ParserOption)
26-
27-
// WithLeeway sets a custom leeway duration for JWT validation to address clock skew between systems
28-
func WithLeeway(leeway time.Duration) NodeJWTAuthenticatorOption {
29-
return func(parserOpts *[]jwt.ParserOption) {
30-
*parserOpts = append(*parserOpts, jwt.WithLeeway(leeway))
31-
}
32-
}
33-
34-
func NewNodeJWTAuthenticator(nodeAuthProvider NodeAuthProvider, logger *slog.Logger, opts ...NodeJWTAuthenticatorOption) *NodeJWTAuthenticator {
30+
func NewNodeJWTAuthenticator(nodeAuthProvider NodeAuthProvider, logger *slog.Logger, config ...*NodeJWTAuthenticatorConfig) *NodeJWTAuthenticator {
3531
parserOpts := []jwt.ParserOption{
3632
jwt.WithIssuedAt(),
3733
jwt.WithExpirationRequired(),
3834
}
3935

40-
// Apply optional configurations (e.g. leeway)
41-
for _, opt := range opts {
42-
opt(&parserOpts)
36+
// Apply optional configuration
37+
if len(config) > 0 && config[0] != nil {
38+
if config[0].Leeway > 0 {
39+
parserOpts = append(parserOpts, jwt.WithLeeway(config[0].Leeway))
40+
}
4341
}
4442

4543
parser := jwt.NewParser(parserOpts...)

0 commit comments

Comments
 (0)