pq_crypto-jwt 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PQCrypto
4
+ module JWT
5
+ module JWKS
6
+ class ValidationCache
7
+ MISSING = Object.new.freeze
8
+
9
+ def initialize(limit)
10
+ @limit = limit
11
+ @entries = {}
12
+ @mutex = Mutex.new
13
+ end
14
+
15
+ def fetch(key)
16
+ cached = mutex.synchronize { entries.fetch(key, MISSING) }
17
+ return cached unless cached.equal?(MISSING)
18
+
19
+ result = yield
20
+ mutex.synchronize { store(key, result) }
21
+ result
22
+ end
23
+
24
+ def clear
25
+ mutex.synchronize { entries.clear }
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :limit, :entries, :mutex
31
+
32
+ def store(key, result)
33
+ entries.shift if entries.size >= limit && !entries.key?(key)
34
+ entries[key] = result
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,55 +1,105 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "jwks/validation_cache"
4
+ require_relative "jwks/loader"
5
+
3
6
  module PQCrypto
4
7
  module JWT
5
8
  module JWKS
6
- module_function
9
+ extend self
7
10
 
8
11
  CACHE_EMPTY = Object.new.freeze
12
+ KID_STRATEGIES = %i[thumbprint thumbprint_uri].freeze
13
+ VALIDATION_CACHE_LIMIT = 1024
14
+ VALIDATION_CACHE_FIELD_BYTES_LIMIT = 8192
15
+ VALIDATION_CACHE = ValidationCache.new(VALIDATION_CACHE_LIMIT).freeze
16
+ private_constant :VALIDATION_CACHE_FIELD_BYTES_LIMIT, :VALIDATION_CACHE, :ValidationCache, :Loader
17
+
18
+ def from_keys(public_keys, kids: nil, kid_strategy: nil)
19
+ public_keys = Array(public_keys)
20
+ kids = normalize_kids!(public_keys.size, kids, kid_strategy)
9
21
 
10
- def from_keys(public_keys, kids: nil)
11
- keys = Array(public_keys).each_with_index.map do |public_key, index|
12
- PQCrypto::JWT::JWK.from_public_key(public_key, kid: kids&.fetch(index, nil))
22
+ jwks = public_keys.zip(kids || []).map do |public_key, kid|
23
+ jwk = JWK.from_public_key(public_key, kid:)
24
+ kid_strategy ? jwk.merge("kid" => JWK.public_send(kid_strategy, jwk)).freeze : jwk
13
25
  end
14
- { "keys" => keys }.freeze
26
+ { "keys" => jwks }.freeze
15
27
  end
16
28
 
17
29
  def find(jwks, kid: nil, alg: nil, thumbprint: nil)
18
- keys_from(jwks).find { |key| match?(key, kid, alg, thumbprint) }
30
+ enum_for(:each_candidate, jwks, kid:, alg:, thumbprint:).first
19
31
  end
20
32
 
21
33
  def find_all(jwks, kid: nil, alg: nil, thumbprint: nil)
22
- keys_from(jwks).select { |key| match?(key, kid, alg, thumbprint) }
34
+ enum_for(:each_candidate, jwks, kid:, alg:, thumbprint:).to_a
23
35
  end
24
36
 
25
37
  def loader(jwks_hash_or_callable)
26
- cached = CACHE_EMPTY
27
- lambda do |options = {}|
28
- options ||= {}
29
- cached = CACHE_EMPTY if options[:invalidate]
30
- if cached.equal?(CACHE_EMPTY)
31
- cached = jwks_hash_or_callable.respond_to?(:call) ? jwks_hash_or_callable.call(options) : jwks_hash_or_callable
32
- end
33
- cached
38
+ Loader.new(jwks_hash_or_callable).method(:call).to_proc
39
+ end
40
+
41
+ def clear_validation_cache!
42
+ VALIDATION_CACHE.clear
43
+ end
44
+
45
+ private
46
+
47
+ def each_candidate(jwks, **filters)
48
+ raw_keys_from(jwks).each do |key|
49
+ jwk = JWK.normalize_hash!(key)
50
+ yield jwk if match?(jwk, **filters)
51
+ rescue Error, ArgumentError, TypeError
52
+ nil
34
53
  end
35
54
  end
36
55
 
37
- def keys_from(jwks)
38
- source = jwks.respond_to?(:to_hash) ? jwks.to_hash : jwks
39
- keys = source.respond_to?(:[]) ? (source["keys"] || source[:keys]) : nil
40
- Array(keys).map { |key| key.to_hash.each_with_object({}) { |(k, v), out| out[String(k)] = v } }
56
+ def match?(jwk, kid:, alg:, thumbprint:)
57
+ (kid.nil? || jwk["kid"] == kid) &&
58
+ (alg.nil? || jwk["alg"] == alg) &&
59
+ valid_public_jwk?(jwk) &&
60
+ (thumbprint.nil? || JWK.thumbprint(jwk) == thumbprint)
61
+ end
62
+
63
+ def raw_keys_from(jwks)
64
+ jwks = jwks.to_hash if jwks.respond_to?(:to_hash)
65
+ return [] unless jwks.is_a?(Hash)
66
+
67
+ Array(jwks["keys"] || jwks[:keys])
41
68
  end
42
- private_class_method :keys_from
43
69
 
44
- def value_for(hash, key) = hash[key] || hash[key.to_sym]
45
- private_class_method :value_for
70
+ def normalize_kids!(count, kids, kid_strategy)
71
+ if kid_strategy && !KID_STRATEGIES.include?(kid_strategy)
72
+ raise ArgumentError, "kid_strategy must be one of #{KID_STRATEGIES.inspect}"
73
+ end
74
+ raise ArgumentError, "kids and kid_strategy are mutually exclusive" if kids && kid_strategy
75
+ return unless kids
76
+
77
+ kids = kids.to_ary if kids.respond_to?(:to_ary)
78
+ raise ArgumentError, "kids must be an Array with one kid per public key" unless kids.is_a?(Array)
79
+ raise ArgumentError, "kids.size must match public_keys.size" unless kids.size == count
80
+
81
+ kids
82
+ end
83
+
84
+ def valid_public_jwk?(jwk)
85
+ cache_key = validation_cache_key(jwk)
86
+ return public_jwk?(jwk) unless cache_key
87
+
88
+ VALIDATION_CACHE.fetch(cache_key) { public_jwk?(jwk) }
89
+ end
90
+
91
+ def validation_cache_key(jwk)
92
+ fields = jwk.values_at("kty", "alg", "pub")
93
+ cacheable = fields.all? { |field| field.is_a?(String) && field.bytesize <= VALIDATION_CACHE_FIELD_BYTES_LIMIT }
94
+ fields.map { |field| field.dup.freeze }.freeze if cacheable
95
+ end
46
96
 
47
- def match?(key, kid, alg, thumbprint)
48
- (kid.nil? || value_for(key, "kid") == kid) &&
49
- (alg.nil? || value_for(key, "alg") == alg) &&
50
- (thumbprint.nil? || PQCrypto::JWT::JWK.thumbprint(key) == thumbprint)
97
+ def public_jwk?(jwk)
98
+ JWK.public_key_from_jwk(jwk)
99
+ true
100
+ rescue Error, ArgumentError, TypeError
101
+ false
51
102
  end
52
- private_class_method :match?
53
103
  end
54
104
  end
55
105
  end
@@ -3,83 +3,68 @@
3
3
  module PQCrypto
4
4
  module JWT
5
5
  module Keys
6
- module_function
6
+ extend self
7
7
 
8
- EXPECT_VALUES = [:auto, :signature].freeze
8
+ EXPECT_VALUES = %i[auto signature].freeze
9
9
  LOAD_ERRORS = [PQCrypto::Error, ArgumentError].freeze
10
10
 
11
11
  def generate(alg)
12
12
  algorithm = PQCrypto::JWT.algorithm_for(alg)
13
13
  raise ArgumentError, "Unsupported PQCrypto JOSE algorithm: #{alg.inspect}" unless algorithm
14
- raise ArgumentError, "Unsupported key kind for #{alg.inspect}" unless algorithm.key_kind == :signature
15
14
 
16
15
  PQCrypto::Signature.generate(algorithm.pq_crypto_algorithm)
17
16
  end
18
17
 
19
18
  def public_from_pem(pem, expect: :auto)
20
- validate_expect!(expect)
21
-
22
- key = begin
23
- expect == :signature ? PQCrypto::Signature.public_key_from_spki_pem(pem) : PQCrypto::Key.from_pem(pem)
24
- rescue *LOAD_ERRORS => e
25
- raise PQCrypto::JWT::Error, e.message
19
+ load_key(PQCrypto::Signature::PublicKey, expect) do |signature_only|
20
+ signature_only ? PQCrypto::Signature.public_key_from_spki_pem(pem) : PQCrypto::Key.from_pem(pem)
26
21
  end
27
- as_public!(key)
28
22
  end
29
23
 
30
24
  def public_from_der(der, expect: :auto)
31
- validate_expect!(expect)
32
-
33
- key = begin
34
- expect == :signature ? PQCrypto::Signature.public_key_from_spki_der(der) : PQCrypto::Key.from_der(der)
35
- rescue *LOAD_ERRORS => e
36
- raise PQCrypto::JWT::Error, e.message
25
+ load_key(PQCrypto::Signature::PublicKey, expect) do |signature_only|
26
+ signature_only ? PQCrypto::Signature.public_key_from_spki_der(der) : PQCrypto::Key.from_der(der)
37
27
  end
38
- as_public!(key)
39
28
  end
40
29
 
41
30
  def secret_from_pem(pem, expect: :auto, passphrase: nil)
42
- validate_expect!(expect)
43
-
44
- key = begin
45
- expect == :signature ? PQCrypto::Signature.secret_key_from_pkcs8_pem(pem, passphrase: passphrase) : PQCrypto::Key.from_pem(pem, passphrase: passphrase)
46
- rescue *LOAD_ERRORS => e
47
- raise PQCrypto::JWT::Error, e.message
31
+ load_key(PQCrypto::Signature::SecretKey, expect) do |signature_only|
32
+ if signature_only
33
+ PQCrypto::Signature.secret_key_from_pkcs8_pem(pem, passphrase:)
34
+ else
35
+ PQCrypto::Key.from_pem(pem, passphrase:)
36
+ end
48
37
  end
49
- as_secret!(key)
50
38
  end
51
39
 
52
40
  def secret_from_der(der, expect: :auto, passphrase: nil)
53
- validate_expect!(expect)
54
-
55
- key = begin
56
- expect == :signature ? PQCrypto::Signature.secret_key_from_pkcs8_der(der, passphrase: passphrase) : PQCrypto::Key.from_der(der, passphrase: passphrase)
57
- rescue *LOAD_ERRORS => e
58
- raise PQCrypto::JWT::Error, e.message
41
+ load_key(PQCrypto::Signature::SecretKey, expect) do |signature_only|
42
+ if signature_only
43
+ PQCrypto::Signature.secret_key_from_pkcs8_der(der, passphrase:)
44
+ else
45
+ PQCrypto::Key.from_der(der, passphrase:)
46
+ end
59
47
  end
60
- as_secret!(key)
61
48
  end
62
49
 
63
- def validate_expect!(expect)
64
- return if EXPECT_VALUES.include?(expect)
50
+ private
65
51
 
66
- raise ArgumentError, "expect: must be one of #{EXPECT_VALUES.map(&:inspect).join(', ')}"
67
- end
68
- private_class_method :validate_expect!
52
+ def load_key(klass, expect, &)
53
+ unless EXPECT_VALUES.include?(expect)
54
+ raise ArgumentError, "expect: must be one of #{EXPECT_VALUES.map(&:inspect).join(', ')}"
55
+ end
69
56
 
70
- def as_public!(key)
71
- return key if key.is_a?(PQCrypto::Signature::PublicKey)
57
+ key = decode_key(expect == :signature, &)
58
+ return key if key.is_a?(klass)
72
59
 
73
- raise PQCrypto::JWT::KeyTypeError, "Expected PQCrypto::Signature::PublicKey, got #{key.class}"
60
+ raise KeyTypeError, "Expected #{klass}, got #{key.class}"
74
61
  end
75
- private_class_method :as_public!
76
-
77
- def as_secret!(key)
78
- return key if key.is_a?(PQCrypto::Signature::SecretKey)
79
62
 
80
- raise PQCrypto::JWT::KeyTypeError, "Expected PQCrypto::Signature::SecretKey, got #{key.class}"
63
+ def decode_key(signature_only)
64
+ yield signature_only
65
+ rescue *LOAD_ERRORS => error
66
+ raise Error, error.message
81
67
  end
82
- private_class_method :as_secret!
83
68
  end
84
69
  end
85
70
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PQCrypto
4
4
  module JWT
5
- VERSION = "0.2.1"
5
+ VERSION = "0.2.3"
6
6
  end
7
7
  end
data/lib/pq_crypto/jwt.rb CHANGED
@@ -6,10 +6,8 @@ require "pq_crypto"
6
6
 
7
7
  require_relative "jwt/version"
8
8
  require_relative "jwt/errors"
9
+ require_relative "jwt/base64url"
9
10
  require_relative "jwt/jwa"
10
- require_relative "jwt/jwa/ml_dsa_44"
11
- require_relative "jwt/jwa/ml_dsa_65"
12
- require_relative "jwt/jwa/ml_dsa_87"
13
11
  require_relative "jwt/keys"
14
12
  require_relative "jwt/jwk"
15
13
  require_relative "jwt/jwk/akp"
@@ -21,7 +19,7 @@ module PQCrypto
21
19
  MLDSA65 = JWA::MLDSA65
22
20
  MLDSA87 = JWA::MLDSA87
23
21
 
24
- SIGNING_ALGORITHMS = [JWA::MLDSA44, JWA::MLDSA65, JWA::MLDSA87].freeze
22
+ SIGNING_ALGORITHMS = [MLDSA44, MLDSA65, MLDSA87].freeze
25
23
  KEM_ALGORITHMS = [].freeze
26
24
  ALGORITHMS_BY_JOSE = SIGNING_ALGORITHMS.to_h { |algorithm| [algorithm.alg, algorithm] }.freeze
27
25
 
@@ -31,19 +29,23 @@ module PQCrypto
31
29
  def algorithms = SIGNING_ALGORITHMS
32
30
  def signing_algorithms = SIGNING_ALGORITHMS
33
31
  def kem_algorithms = KEM_ALGORITHMS
34
- def algorithm_for(alg_string) = ALGORITHMS_BY_JOSE[alg_string]
35
- def registered? = @registered == true
32
+ def algorithm_for(alg) = ALGORITHMS_BY_JOSE[alg]
33
+ def registered? = registered == true
36
34
 
37
35
  def register!
38
36
  REGISTRATION_MUTEX.synchronize do
39
37
  return true if registered?
40
38
 
41
39
  SIGNING_ALGORITHMS.each { |algorithm| ::JWT::JWA.register_algorithm(algorithm) }
42
- ::JWT::JWK.classes << ::JWT::JWK::AKP unless ::JWT::JWK.classes.include?(::JWT::JWK::AKP)
43
- @registered = true
40
+ classes = ::JWT::JWK.classes
41
+ classes << ::JWT::JWK::AKP unless classes.include?(::JWT::JWK::AKP)
42
+ self.registered = true
44
43
  end
45
- true
46
44
  end
45
+
46
+ private
47
+
48
+ attr_accessor :registered
47
49
  end
48
50
  end
49
51
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pq_crypto-jwt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Haydarov
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2026-05-20 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: pq_crypto
@@ -84,15 +83,21 @@ files:
84
83
  - LICENSE.txt
85
84
  - README.md
86
85
  - lib/pq_crypto/jwt.rb
86
+ - lib/pq_crypto/jwt/base64url.rb
87
87
  - lib/pq_crypto/jwt/errors.rb
88
88
  - lib/pq_crypto/jwt/jwa.rb
89
+ - lib/pq_crypto/jwt/jwa/detached_signing_input_io.rb
90
+ - lib/pq_crypto/jwt/jwa/ml_dsa.rb
89
91
  - lib/pq_crypto/jwt/jwa/ml_dsa_44.rb
90
92
  - lib/pq_crypto/jwt/jwa/ml_dsa_65.rb
91
93
  - lib/pq_crypto/jwt/jwa/ml_dsa_87.rb
92
94
  - lib/pq_crypto/jwt/jwa/ml_dsa_streaming.rb
95
+ - lib/pq_crypto/jwt/jwa/streaming.rb
93
96
  - lib/pq_crypto/jwt/jwk.rb
94
97
  - lib/pq_crypto/jwt/jwk/akp.rb
95
98
  - lib/pq_crypto/jwt/jwks.rb
99
+ - lib/pq_crypto/jwt/jwks/loader.rb
100
+ - lib/pq_crypto/jwt/jwks/validation_cache.rb
96
101
  - lib/pq_crypto/jwt/keys.rb
97
102
  - lib/pq_crypto/jwt/version.rb
98
103
  homepage: https://github.com/roman-haidarov/pq_crypto-jwt
@@ -100,9 +105,8 @@ licenses:
100
105
  - MIT
101
106
  metadata:
102
107
  source_code_uri: https://github.com/roman-haidarov/pq_crypto-jwt
103
- changelog_uri: https://github.com/roman-haidarov/pq_crypto-jwt/blob/v0.2.1/CHANGELOG.md
108
+ changelog_uri: https://github.com/roman-haidarov/pq_crypto-jwt/blob/v0.2.3/CHANGELOG.md
104
109
  bug_tracker_uri: https://github.com/roman-haidarov/pq_crypto-jwt/issues
105
- post_install_message:
106
110
  rdoc_options: []
107
111
  require_paths:
108
112
  - lib
@@ -117,8 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
121
  - !ruby/object:Gem::Version
118
122
  version: '0'
119
123
  requirements: []
120
- rubygems_version: 3.3.27
121
- signing_key:
124
+ rubygems_version: 3.6.7
122
125
  specification_version: 4
123
126
  summary: ML-DSA JWS algorithms for ruby-jwt backed by pq_crypto
124
127
  test_files: []