Skip to content

Commit 29ccc7a

Browse files
feat: make leeway configurable on the initalization side.
1 parent a9bf5b6 commit 29ccc7a

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

pkg/nodeauth/jwt/node_jwt_authenticator.go

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

17-
const (
18-
// JWTLeeway defines the time leeway for JWT validation to address clock skew between systems
19-
JWTLeeway = 5 * time.Second
20-
)
21-
2217
// NodeJWTAuthenticator is designed to be used by the server-side service to authenticate the JWT token generated by the Node.
2318
type NodeJWTAuthenticator struct {
2419
nodeAuthProvider NodeAuthProvider // Source of truth to validate public key in the JWT claim.
2520
parser *jwt.Parser // JWT parser to parse the JWT token.
2621
logger *slog.Logger
2722
}
2823

29-
func NewNodeJWTAuthenticator(nodeAuthProvider NodeAuthProvider, logger *slog.Logger) *NodeJWTAuthenticator {
30-
// Configure parser with validation options
31-
parser := jwt.NewParser(
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 {
35+
parserOpts := []jwt.ParserOption{
3236
jwt.WithIssuedAt(),
3337
jwt.WithExpirationRequired(),
34-
jwt.WithLeeway(JWTLeeway),
35-
)
38+
}
39+
40+
// Apply optional configurations (e.g. leeway)
41+
for _, opt := range opts {
42+
opt(&parserOpts)
43+
}
44+
45+
parser := jwt.NewParser(parserOpts...)
3646

3747
return &NodeJWTAuthenticator{
3848
nodeAuthProvider: nodeAuthProvider,

pkg/nodeauth/jwt/node_jwt_authenticator_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestNodeJWTAuthenticator_AuthenticateJWT_LeewayHandlesClockSkew(t *testing.
151151
privateKey, csaPubKey := createValidatorTestKeys()
152152
mockProvider := &mocks.NodeAuthProvider{}
153153
mockProvider.On("IsNodePubKeyTrusted", mock.Anything, csaPubKey).Return(true, nil)
154-
authenticator := NewNodeJWTAuthenticator(mockProvider, createTestLogger())
154+
authenticator := NewNodeJWTAuthenticator(mockProvider, createTestLogger(), WithLeeway(5*time.Second))
155155

156156
testRequest := testRequest{Field: "test-request"}
157157
digest := utils.CalculateRequestDigest(testRequest)
@@ -186,7 +186,7 @@ func TestNodeJWTAuthenticator_AuthenticateJWT_LeewayHandlesClockSkew(t *testing.
186186
// Given
187187
privateKey, csaPubKey := createValidatorTestKeys()
188188
mockProvider := &mocks.NodeAuthProvider{}
189-
authenticator := NewNodeJWTAuthenticator(mockProvider, createTestLogger())
189+
authenticator := NewNodeJWTAuthenticator(mockProvider, createTestLogger(), WithLeeway(5*time.Second))
190190

191191
testRequest := testRequest{Field: "test-request"}
192192
digest := utils.CalculateRequestDigest(testRequest)
@@ -222,7 +222,7 @@ func TestNodeJWTAuthenticator_AuthenticateJWT_LeewayHandlesClockSkew(t *testing.
222222
privateKey, csaPubKey := createValidatorTestKeys()
223223
mockProvider := &mocks.NodeAuthProvider{}
224224
mockProvider.On("IsNodePubKeyTrusted", mock.Anything, csaPubKey).Return(true, nil)
225-
authenticator := NewNodeJWTAuthenticator(mockProvider, createTestLogger())
225+
authenticator := NewNodeJWTAuthenticator(mockProvider, createTestLogger(), WithLeeway(5*time.Second))
226226

227227
testRequest := testRequest{Field: "test-request"}
228228
digest := utils.CalculateRequestDigest(testRequest)
@@ -257,7 +257,7 @@ func TestNodeJWTAuthenticator_AuthenticateJWT_LeewayHandlesClockSkew(t *testing.
257257
// Given
258258
privateKey, csaPubKey := createValidatorTestKeys()
259259
mockProvider := &mocks.NodeAuthProvider{}
260-
authenticator := NewNodeJWTAuthenticator(mockProvider, createTestLogger())
260+
authenticator := NewNodeJWTAuthenticator(mockProvider, createTestLogger(), WithLeeway(5*time.Second))
261261

262262
testRequest := testRequest{Field: "test-request"}
263263
digest := utils.CalculateRequestDigest(testRequest)

0 commit comments

Comments
 (0)