pq_crypto-jwt 0.2.2 → 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.
@@ -1,171 +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
9
12
  KID_STRATEGIES = %i[thumbprint thumbprint_uri].freeze
10
13
  VALIDATION_CACHE_LIMIT = 1024
11
14
  VALIDATION_CACHE_FIELD_BYTES_LIMIT = 8192
12
- VALIDATION_CACHE = {}
13
- VALIDATION_CACHE_MUTEX = Mutex.new
14
- private_constant :VALIDATION_CACHE_FIELD_BYTES_LIMIT, :VALIDATION_CACHE, :VALIDATION_CACHE_MUTEX
15
+ VALIDATION_CACHE = ValidationCache.new(VALIDATION_CACHE_LIMIT).freeze
16
+ private_constant :VALIDATION_CACHE_FIELD_BYTES_LIMIT, :VALIDATION_CACHE, :ValidationCache, :Loader
15
17
 
16
18
  def from_keys(public_keys, kids: nil, kid_strategy: nil)
17
- keys = Array(public_keys)
18
- kids = normalize_kids!(keys, kids, kid_strategy)
19
+ public_keys = Array(public_keys)
20
+ kids = normalize_kids!(public_keys.size, kids, kid_strategy)
19
21
 
20
- jwks = keys.each_with_index.map do |public_key, index|
21
- jwk = PQCrypto::JWT::JWK.from_public_key(public_key, kid: kids&.fetch(index))
22
- apply_kid_strategy(jwk, kid_strategy)
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
23
25
  end
24
26
  { "keys" => jwks }.freeze
25
27
  end
26
28
 
27
29
  def find(jwks, kid: nil, alg: nil, thumbprint: nil)
28
- each_candidate(jwks, kid, alg, thumbprint) do |key|
29
- return key
30
- end
31
- nil
30
+ enum_for(:each_candidate, jwks, kid:, alg:, thumbprint:).first
32
31
  end
33
32
 
34
33
  def find_all(jwks, kid: nil, alg: nil, thumbprint: nil)
35
- matches = []
36
- each_candidate(jwks, kid, alg, thumbprint) { |key| matches << key }
37
- matches
34
+ enum_for(:each_candidate, jwks, kid:, alg:, thumbprint:).to_a
38
35
  end
39
36
 
40
37
  def loader(jwks_hash_or_callable)
41
- cached = CACHE_EMPTY
42
- mutex = Mutex.new
43
-
44
- lambda do |options = {}|
45
- options ||= {}
46
- invalidate = options[:invalidate]
47
- current = cached
48
- return current if !invalidate && !current.equal?(CACHE_EMPTY)
49
-
50
- mutex.synchronize do
51
- cached = CACHE_EMPTY if invalidate
52
- if cached.equal?(CACHE_EMPTY)
53
- cached = jwks_hash_or_callable.respond_to?(:call) ? jwks_hash_or_callable.call(options) : jwks_hash_or_callable
54
- end
55
- cached
56
- end
57
- end
38
+ Loader.new(jwks_hash_or_callable).method(:call).to_proc
58
39
  end
59
40
 
60
- def raw_keys_from(jwks)
61
- source = jwks.respond_to?(:to_hash) ? jwks.to_hash : jwks
62
- keys = source.respond_to?(:[]) ? (source["keys"] || source[:keys]) : nil
63
- Array(keys)
41
+ def clear_validation_cache!
42
+ VALIDATION_CACHE.clear
64
43
  end
65
- private_class_method :raw_keys_from
66
44
 
67
- def each_candidate(jwks, kid, alg, thumbprint)
68
- raw_keys_from(jwks).each do |key|
69
- next unless key.respond_to?(:to_hash)
45
+ private
70
46
 
71
- normalized = normalize_jwk_hash(key.to_hash)
72
- next unless cheap_match?(normalized, kid, alg)
73
- next unless cached_valid_public_jwk?(normalized)
74
- next unless thumbprint.nil? || PQCrypto::JWT::JWK.thumbprint(normalized) == thumbprint
75
-
76
- yield normalized
77
- rescue PQCrypto::JWT::Error, ArgumentError, TypeError
78
- next
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
79
53
  end
80
54
  end
81
- private_class_method :each_candidate
82
55
 
83
- def normalize_jwk_hash(hash)
84
- hash.each_with_object({}) { |(key, value), out| out[String(key)] = value }
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)
85
61
  end
86
- private_class_method :normalize_jwk_hash
87
62
 
88
- def value_for(hash, key) = hash[key] || hash[key.to_sym]
89
- private_class_method :value_for
63
+ def raw_keys_from(jwks)
64
+ jwks = jwks.to_hash if jwks.respond_to?(:to_hash)
65
+ return [] unless jwks.is_a?(Hash)
90
66
 
91
- def cheap_match?(key, kid, alg)
92
- (kid.nil? || value_for(key, "kid") == kid) &&
93
- (alg.nil? || value_for(key, "alg") == alg)
67
+ Array(jwks["keys"] || jwks[:keys])
94
68
  end
95
- private_class_method :cheap_match?
96
-
97
- def normalize_kids!(public_keys, kids, kid_strategy)
98
- raise ArgumentError, "kid_strategy must be one of #{KID_STRATEGIES.inspect}" if kid_strategy && !KID_STRATEGIES.include?(kid_strategy)
99
- raise ArgumentError, "kids and kid_strategy are mutually exclusive" if kids && kid_strategy
100
- return nil unless kids
101
69
 
102
- unless kids.respond_to?(:to_ary)
103
- raise ArgumentError, "kids must be an Array with one kid per public key"
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}"
104
73
  end
74
+ raise ArgumentError, "kids and kid_strategy are mutually exclusive" if kids && kid_strategy
75
+ return unless kids
105
76
 
106
- normalized = kids.to_ary
107
- unless normalized.is_a?(Array)
108
- raise ArgumentError, "kids must be an Array with one kid per public key"
109
- end
110
- raise ArgumentError, "kids.size must match public_keys.size" unless normalized.size == public_keys.size
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
111
80
 
112
- normalized
81
+ kids
113
82
  end
114
- private_class_method :normalize_kids!
115
83
 
116
- def apply_kid_strategy(jwk, kid_strategy)
117
- return jwk unless kid_strategy
84
+ def valid_public_jwk?(jwk)
85
+ cache_key = validation_cache_key(jwk)
86
+ return public_jwk?(jwk) unless cache_key
118
87
 
119
- kid = case kid_strategy
120
- when :thumbprint then PQCrypto::JWT::JWK.thumbprint(jwk)
121
- when :thumbprint_uri then PQCrypto::JWT::JWK.thumbprint_uri(jwk)
122
- end
123
- jwk.merge("kid" => kid).freeze
88
+ VALIDATION_CACHE.fetch(cache_key) { public_jwk?(jwk) }
124
89
  end
125
- private_class_method :apply_kid_strategy
126
-
127
- def cached_valid_public_jwk?(key)
128
- cache_key = validation_cache_key(key)
129
- return valid_public_jwk?(key) unless cache_key
130
-
131
- VALIDATION_CACHE_MUTEX.synchronize do
132
- cached = VALIDATION_CACHE[cache_key]
133
- return cached unless cached.nil?
134
- end
135
90
 
136
- result = valid_public_jwk?(key)
137
- VALIDATION_CACHE_MUTEX.synchronize do
138
- VALIDATION_CACHE.shift if VALIDATION_CACHE.size >= VALIDATION_CACHE_LIMIT
139
- VALIDATION_CACHE[cache_key] = result
140
- end
141
- result
142
- end
143
- private_class_method :cached_valid_public_jwk?
144
-
145
- def validation_cache_key(key)
146
- kty = key["kty"]
147
- alg = key["alg"]
148
- pub = key["pub"]
149
- return nil unless kty.is_a?(String) && alg.is_a?(String) && pub.is_a?(String)
150
- return nil if kty.bytesize > VALIDATION_CACHE_FIELD_BYTES_LIMIT
151
- return nil if alg.bytesize > VALIDATION_CACHE_FIELD_BYTES_LIMIT
152
- return nil if pub.bytesize > VALIDATION_CACHE_FIELD_BYTES_LIMIT
153
-
154
- [kty.dup.freeze, alg.dup.freeze, pub.dup.freeze].freeze
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
155
95
  end
156
- private_class_method :validation_cache_key
157
96
 
158
- def valid_public_jwk?(key)
159
- PQCrypto::JWT::JWK.public_key_from_jwk(key)
97
+ def public_jwk?(jwk)
98
+ JWK.public_key_from_jwk(jwk)
160
99
  true
161
- rescue PQCrypto::JWT::Error, ArgumentError, TypeError
100
+ rescue Error, ArgumentError, TypeError
162
101
  false
163
102
  end
164
- private_class_method :valid_public_jwk?
165
-
166
- def clear_validation_cache!
167
- VALIDATION_CACHE_MUTEX.synchronize { VALIDATION_CACHE.clear }
168
- end
169
103
  end
170
104
  end
171
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.2"
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.2
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.2/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: []