Skip to content

Commit 2cbd30d

Browse files
authored
chore(cryptojwt): remove unused WithCache API (Closes #99)
chore(cryptojwt): remove unused WithCache API (Closes #99)
1 parent 8bad559 commit 2cbd30d

4 files changed

Lines changed: 6 additions & 478 deletions

File tree

‎pkg/cryptojwt/esjwt.go‎

Lines changed: 0 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ type esjwtEncoderWithPrivateKeyFile struct {
1717
method jwt.SigningMethod
1818
}
1919

20-
type esjwtEncoderWithCachedPrivateKey struct {
21-
encoder encoder
22-
privateKey crypto.PrivateKey
23-
method jwt.SigningMethod
24-
}
25-
2620
type esjwtDecoderWithPrivateKeyFile struct {
2721
decoder decoder
2822
privateKeyFile string
@@ -35,12 +29,6 @@ type esjwtDecoderWithPublicKeyFile struct {
3529
method jwt.SigningMethod
3630
}
3731

38-
type esjwtDecoderWithCachedPublicKey struct {
39-
decoder decoder
40-
publicKey crypto.PublicKey
41-
method jwt.SigningMethod
42-
}
43-
4432
// NewES256Encoder creates a new ECDSA-SHA256 JWT encoder with a private key file.
4533
//
4634
// Parameters:
@@ -151,79 +139,6 @@ func NewES256DecoderWithPublicKeyFileAndValidation(publicKeyFile string, validat
151139
}
152140
}
153141

154-
// NewES256EncoderWithCache creates a new ECDSA-SHA256 JWT encoder with cached private key.
155-
//
156-
// The private key is loaded once at creation time, improving performance for repeated
157-
// operations. This is recommended for high-throughput scenarios.
158-
//
159-
// Security: The cached key remains in memory for the lifetime of the encoder. Private
160-
// key files should have strict file permissions (0600).
161-
//
162-
// Performance: Eliminates repeated file reads and key parsing for encoding many tokens.
163-
//
164-
// Example:
165-
//
166-
// encoder, err := cryptojwt.NewES256EncoderWithCache("ec-private.pem")
167-
// if err != nil {
168-
// log.Fatal(err)
169-
// }
170-
// for i := 0; i < 1000; i++ {
171-
// token, _ := encoder.Encode(fmt.Sprintf(`{"id":%d}`, i))
172-
// fmt.Println(token)
173-
// }
174-
func NewES256EncoderWithCache(privateKeyFile string) (Encoder, error) {
175-
privateKey, _, err := readECDSAPrivateKey(privateKeyFile)
176-
if err != nil {
177-
return nil, err
178-
}
179-
return &esjwtEncoderWithCachedPrivateKey{
180-
method: jwt.SigningMethodES256,
181-
privateKey: privateKey,
182-
}, nil
183-
}
184-
185-
// NewES256DecoderWithPrivateKeyFileAndCache creates a new ECDSA-SHA256 JWT decoder with cached public key from private key file.
186-
// The key is loaded once at creation time, improving performance for repeated operations.
187-
func NewES256DecoderWithPrivateKeyFileAndCache(privateKeyFile string) (Decoder, error) {
188-
return NewES256DecoderWithPrivateKeyFileAndCacheAndValidation(privateKeyFile, ValidationOptions{})
189-
}
190-
191-
// NewES256DecoderWithPrivateKeyFileAndCacheAndValidation creates a new ECDSA-SHA256 JWT decoder with cached public key and validation options.
192-
func NewES256DecoderWithPrivateKeyFileAndCacheAndValidation(privateKeyFile string, validationOpts ValidationOptions) (Decoder, error) {
193-
_, publicKey, err := readECDSAPrivateKey(privateKeyFile)
194-
if err != nil {
195-
return nil, err
196-
}
197-
return &esjwtDecoderWithCachedPublicKey{
198-
method: jwt.SigningMethodES256,
199-
publicKey: publicKey,
200-
decoder: decoder{validationOpts: validationOpts},
201-
}, nil
202-
}
203-
204-
// NewES256DecoderWithPublicKeyFileAndCache creates a new ECDSA-SHA256 JWT decoder with cached public key from public key file.
205-
// The key is loaded once at creation time, improving performance for repeated operations.
206-
func NewES256DecoderWithPublicKeyFileAndCache(publicKeyFile string) (Decoder, error) {
207-
return NewES256DecoderWithPublicKeyFileAndCacheAndValidation(publicKeyFile, ValidationOptions{})
208-
}
209-
210-
// NewES256DecoderWithPublicKeyFileAndCacheAndValidation creates a new ECDSA-SHA256 JWT decoder with cached public key and validation options.
211-
func NewES256DecoderWithPublicKeyFileAndCacheAndValidation(publicKeyFile string, validationOpts ValidationOptions) (Decoder, error) {
212-
publicKeyBytes, err := os.ReadFile(publicKeyFile) // #nosec G304 -- user-provided file path
213-
if err != nil {
214-
return nil, fmt.Errorf("error reading public key file: %w", err)
215-
}
216-
publicKey, err := jwt.ParseECPublicKeyFromPEM(publicKeyBytes)
217-
if err != nil {
218-
return nil, fmt.Errorf("error parsing EC public key: %w", err)
219-
}
220-
return &esjwtDecoderWithCachedPublicKey{
221-
method: jwt.SigningMethodES256,
222-
publicKey: publicKey,
223-
decoder: decoder{validationOpts: validationOpts},
224-
}, nil
225-
}
226-
227142
// NewES384DecoderWithPublicKeyFile creates a new ECDSA-SHA384 JWT decoder with a public key file.
228143
func NewES384DecoderWithPublicKeyFile(publicKeyFile string) Decoder {
229144
return NewES384DecoderWithPublicKeyFileAndValidation(publicKeyFile, ValidationOptions{})
@@ -238,61 +153,6 @@ func NewES384DecoderWithPublicKeyFileAndValidation(publicKeyFile string, validat
238153
}
239154
}
240155

241-
// NewES384EncoderWithCache creates a new ECDSA-SHA384 JWT encoder with cached private key.
242-
// The private key is loaded once at creation time, improving performance for repeated operations.
243-
func NewES384EncoderWithCache(privateKeyFile string) (Encoder, error) {
244-
privateKey, _, err := readECDSAPrivateKey(privateKeyFile)
245-
if err != nil {
246-
return nil, err
247-
}
248-
return &esjwtEncoderWithCachedPrivateKey{
249-
method: jwt.SigningMethodES384,
250-
privateKey: privateKey,
251-
}, nil
252-
}
253-
254-
// NewES384DecoderWithPrivateKeyFileAndCache creates a new ECDSA-SHA384 JWT decoder with cached public key from private key file.
255-
// The key is loaded once at creation time, improving performance for repeated operations.
256-
func NewES384DecoderWithPrivateKeyFileAndCache(privateKeyFile string) (Decoder, error) {
257-
return NewES384DecoderWithPrivateKeyFileAndCacheAndValidation(privateKeyFile, ValidationOptions{})
258-
}
259-
260-
// NewES384DecoderWithPrivateKeyFileAndCacheAndValidation creates a new ECDSA-SHA384 JWT decoder with cached public key and validation options.
261-
func NewES384DecoderWithPrivateKeyFileAndCacheAndValidation(privateKeyFile string, validationOpts ValidationOptions) (Decoder, error) {
262-
_, publicKey, err := readECDSAPrivateKey(privateKeyFile)
263-
if err != nil {
264-
return nil, err
265-
}
266-
return &esjwtDecoderWithCachedPublicKey{
267-
method: jwt.SigningMethodES384,
268-
publicKey: publicKey,
269-
decoder: decoder{validationOpts: validationOpts},
270-
}, nil
271-
}
272-
273-
// NewES384DecoderWithPublicKeyFileAndCache creates a new ECDSA-SHA384 JWT decoder with cached public key from public key file.
274-
// The key is loaded once at creation time, improving performance for repeated operations.
275-
func NewES384DecoderWithPublicKeyFileAndCache(publicKeyFile string) (Decoder, error) {
276-
return NewES384DecoderWithPublicKeyFileAndCacheAndValidation(publicKeyFile, ValidationOptions{})
277-
}
278-
279-
// NewES384DecoderWithPublicKeyFileAndCacheAndValidation creates a new ECDSA-SHA384 JWT decoder with cached public key and validation options.
280-
func NewES384DecoderWithPublicKeyFileAndCacheAndValidation(publicKeyFile string, validationOpts ValidationOptions) (Decoder, error) {
281-
publicKeyBytes, err := os.ReadFile(publicKeyFile) // #nosec G304 -- user-provided file path
282-
if err != nil {
283-
return nil, fmt.Errorf("error reading public key file: %w", err)
284-
}
285-
publicKey, err := jwt.ParseECPublicKeyFromPEM(publicKeyBytes)
286-
if err != nil {
287-
return nil, fmt.Errorf("error parsing EC public key: %w", err)
288-
}
289-
return &esjwtDecoderWithCachedPublicKey{
290-
method: jwt.SigningMethodES384,
291-
publicKey: publicKey,
292-
decoder: decoder{validationOpts: validationOpts},
293-
}, nil
294-
}
295-
296156
// NewES512DecoderWithPublicKeyFile creates a new ECDSA-SHA512 JWT decoder with a public key file.
297157
func NewES512DecoderWithPublicKeyFile(publicKeyFile string) Decoder {
298158
return NewES512DecoderWithPublicKeyFileAndValidation(publicKeyFile, ValidationOptions{})
@@ -307,61 +167,6 @@ func NewES512DecoderWithPublicKeyFileAndValidation(publicKeyFile string, validat
307167
}
308168
}
309169

310-
// NewES512EncoderWithCache creates a new ECDSA-SHA512 JWT encoder with cached private key.
311-
// The private key is loaded once at creation time, improving performance for repeated operations.
312-
func NewES512EncoderWithCache(privateKeyFile string) (Encoder, error) {
313-
privateKey, _, err := readECDSAPrivateKey(privateKeyFile)
314-
if err != nil {
315-
return nil, err
316-
}
317-
return &esjwtEncoderWithCachedPrivateKey{
318-
method: jwt.SigningMethodES512,
319-
privateKey: privateKey,
320-
}, nil
321-
}
322-
323-
// NewES512DecoderWithPrivateKeyFileAndCache creates a new ECDSA-SHA512 JWT decoder with cached public key from private key file.
324-
// The key is loaded once at creation time, improving performance for repeated operations.
325-
func NewES512DecoderWithPrivateKeyFileAndCache(privateKeyFile string) (Decoder, error) {
326-
return NewES512DecoderWithPrivateKeyFileAndCacheAndValidation(privateKeyFile, ValidationOptions{})
327-
}
328-
329-
// NewES512DecoderWithPrivateKeyFileAndCacheAndValidation creates a new ECDSA-SHA512 JWT decoder with cached public key and validation options.
330-
func NewES512DecoderWithPrivateKeyFileAndCacheAndValidation(privateKeyFile string, validationOpts ValidationOptions) (Decoder, error) {
331-
_, publicKey, err := readECDSAPrivateKey(privateKeyFile)
332-
if err != nil {
333-
return nil, err
334-
}
335-
return &esjwtDecoderWithCachedPublicKey{
336-
method: jwt.SigningMethodES512,
337-
publicKey: publicKey,
338-
decoder: decoder{validationOpts: validationOpts},
339-
}, nil
340-
}
341-
342-
// NewES512DecoderWithPublicKeyFileAndCache creates a new ECDSA-SHA512 JWT decoder with cached public key from public key file.
343-
// The key is loaded once at creation time, improving performance for repeated operations.
344-
func NewES512DecoderWithPublicKeyFileAndCache(publicKeyFile string) (Decoder, error) {
345-
return NewES512DecoderWithPublicKeyFileAndCacheAndValidation(publicKeyFile, ValidationOptions{})
346-
}
347-
348-
// NewES512DecoderWithPublicKeyFileAndCacheAndValidation creates a new ECDSA-SHA512 JWT decoder with cached public key and validation options.
349-
func NewES512DecoderWithPublicKeyFileAndCacheAndValidation(publicKeyFile string, validationOpts ValidationOptions) (Decoder, error) {
350-
publicKeyBytes, err := os.ReadFile(publicKeyFile) // #nosec G304 -- user-provided file path
351-
if err != nil {
352-
return nil, fmt.Errorf("error reading public key file: %w", err)
353-
}
354-
publicKey, err := jwt.ParseECPublicKeyFromPEM(publicKeyBytes)
355-
if err != nil {
356-
return nil, fmt.Errorf("error parsing EC public key: %w", err)
357-
}
358-
return &esjwtDecoderWithCachedPublicKey{
359-
method: jwt.SigningMethodES512,
360-
publicKey: publicKey,
361-
decoder: decoder{validationOpts: validationOpts},
362-
}, nil
363-
}
364-
365170
func readECDSAPrivateKey(privateKeyFile string) (crypto.PrivateKey, crypto.PublicKey, error) {
366171
contentKeyFile, err := os.ReadFile(privateKeyFile) // #nosec G304 -- user-provided file path
367172
if err != nil {
@@ -409,11 +214,3 @@ func (j *esjwtDecoderWithPublicKeyFile) Decode(token string) (string, error) {
409214
}
410215
return j.decoder.DecodeJWT(key, token)
411216
}
412-
413-
func (j *esjwtEncoderWithCachedPrivateKey) Encode(payload string) (string, error) {
414-
return j.encoder.EncodeJWT(j.privateKey, j.method, payload)
415-
}
416-
417-
func (j *esjwtDecoderWithCachedPublicKey) Decode(token string) (string, error) {
418-
return j.decoder.DecodeJWT(j.publicKey, token)
419-
}

‎pkg/cryptojwt/esjwt_test.go‎

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,9 @@ func TestECDSAComplexPayloads(t *testing.T) {
501501
})
502502
}
503503

504-
// Benchmarks comparing cached vs uncached performance
504+
// Benchmarks for the encode and decode paths used by the CLI
505505

506-
func BenchmarkES256EncodeWithoutCache(b *testing.B) {
506+
func BenchmarkES256Encode(b *testing.B) {
507507
privateKeyPath, _ := generateECDSAKeyPair(b, elliptic.P256())
508508
encoder := cryptojwt.NewES256Encoder(privateKeyPath)
509509

@@ -513,20 +513,7 @@ func BenchmarkES256EncodeWithoutCache(b *testing.B) {
513513
}
514514
}
515515

516-
func BenchmarkES256EncodeWithCache(b *testing.B) {
517-
privateKeyPath, _ := generateECDSAKeyPair(b, elliptic.P256())
518-
encoder, err := cryptojwt.NewES256EncoderWithCache(privateKeyPath)
519-
if err != nil {
520-
b.Fatalf("Failed to create cached encoder: %v", err)
521-
}
522-
523-
b.ResetTimer()
524-
for i := 0; i < b.N; i++ {
525-
_, _ = encoder.Encode(validPayload)
526-
}
527-
}
528-
529-
func BenchmarkES256DecodeWithoutCache(b *testing.B) {
516+
func BenchmarkES256Decode(b *testing.B) {
530517
privateKeyPath, publicKeyPath := generateECDSAKeyPair(b, elliptic.P256())
531518
encoder := cryptojwt.NewES256Encoder(privateKeyPath)
532519
token, err := encoder.Encode(validPayload)
@@ -540,21 +527,3 @@ func BenchmarkES256DecodeWithoutCache(b *testing.B) {
540527
_, _ = decoder.Decode(token)
541528
}
542529
}
543-
544-
func BenchmarkES256DecodeWithCache(b *testing.B) {
545-
privateKeyPath, publicKeyPath := generateECDSAKeyPair(b, elliptic.P256())
546-
encoder := cryptojwt.NewES256Encoder(privateKeyPath)
547-
token, err := encoder.Encode(validPayload)
548-
if err != nil {
549-
b.Fatalf("Failed to encode: %v", err)
550-
}
551-
decoder, err := cryptojwt.NewES256DecoderWithPublicKeyFileAndCache(publicKeyPath)
552-
if err != nil {
553-
b.Fatalf("Failed to create cached decoder: %v", err)
554-
}
555-
556-
b.ResetTimer()
557-
for i := 0; i < b.N; i++ {
558-
_, _ = decoder.Decode(token)
559-
}
560-
}

0 commit comments

Comments
 (0)