json-jwt 0.1.2 → 0.1.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.

Potentially problematic release.


This version of json-jwt might be problematic. Click here for more details.

data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json-jwt (0.0.7)
4
+ json-jwt (0.1.2)
5
5
  activesupport (>= 2.3)
6
6
  i18n
7
7
  json (>= 1.4.3)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/lib/json/jwk.rb CHANGED
@@ -1,20 +1,25 @@
1
1
  module JSON
2
- class JWK
3
- class << self
4
- def encode(public_key, kid = nil)
5
- json = case public_key
6
- when OpenSSL::PKey::RSA
7
- {
8
- alg: :RSA,
9
- exp: UrlSafeBase64.encode64(public_key.e.to_s(2)),
10
- mod: UrlSafeBase64.encode64(public_key.n.to_s(2))
11
- }
12
- else
13
- raise "Only OpenSSL::PKey::RSA is supported now"
14
- end
15
- json[:kid] = kid if kid.present?
16
- json
2
+ class JWK < Hash
3
+ def initialize(public_key, options = {})
4
+ replace encode(public_key, options)
5
+ end
6
+
7
+ private
8
+
9
+ def encode(public_key, options = {})
10
+ hash = case public_key
11
+ when OpenSSL::PKey::RSA
12
+ {
13
+ alg: :RSA,
14
+ exp: UrlSafeBase64.encode64(public_key.e.to_s(2)),
15
+ mod: UrlSafeBase64.encode64(public_key.n.to_s(2))
16
+ }
17
+ else
18
+ raise "Only RSA is supported now"
17
19
  end
20
+ hash.merge(options)
18
21
  end
19
22
  end
20
- end
23
+ end
24
+
25
+ require 'json/jwk/set'
@@ -0,0 +1,11 @@
1
+ module JSON
2
+ class JWK::Set < Array
3
+ def initialize(*jwks)
4
+ replace Array(jwks).flatten
5
+ end
6
+
7
+ def as_json
8
+ {:keys => self}
9
+ end
10
+ end
11
+ end
data/lib/json/jws.rb CHANGED
@@ -80,5 +80,14 @@ module JSON
80
80
  raise InvalidFormat.new('Unknown Signature Algorithm')
81
81
  end
82
82
  end
83
+
84
+ def replace(hash_or_jwt)
85
+ super
86
+ if hash_or_jwt.is_a? JSON::JWT
87
+ self.header = hash_or_jwt.header
88
+ self.signature = hash_or_jwt.signature
89
+ end
90
+ self
91
+ end
83
92
  end
84
93
  end
data/lib/json/jwt.rb CHANGED
@@ -37,10 +37,6 @@ module JSON
37
37
  end
38
38
  end
39
39
 
40
- def [](key)
41
- super
42
- end
43
-
44
40
  def to_s
45
41
  [
46
42
  header.to_json,
@@ -66,17 +62,6 @@ module JSON
66
62
  raise InvalidFormat.new("Invalid JSON Format")
67
63
  end
68
64
  end
69
-
70
- private
71
-
72
- def replace(hash_or_jwt)
73
- super hash_or_jwt
74
- if hash_or_jwt.is_a? JSON::JWT
75
- self.header = hash_or_jwt.header
76
- self.signature = hash_or_jwt.signature
77
- end
78
- self
79
- end
80
65
  end
81
66
  end
82
67
 
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSON::JWK::Set do
4
+ let(:jwk) { JSON::JWK.new public_key }
5
+
6
+ context 'when single JWK given' do
7
+ let(:set) { JSON::JWK::Set.new jwk }
8
+
9
+ it 'should become proper JWK set format' do
10
+ _set_ = set.as_json
11
+ _set_.should include :keys
12
+ _set_[:keys].should == [jwk]
13
+ end
14
+ end
15
+
16
+ context 'when multiple JWKs given' do
17
+ let(:set) { JSON::JWK::Set.new jwk, jwk }
18
+
19
+ it 'should become proper JWK set format' do
20
+ _set_ = set.as_json
21
+ _set_.should include :keys
22
+ _set_[:keys].should == [jwk, jwk]
23
+ end
24
+ end
25
+
26
+ context 'when an Array of JWKs given' do
27
+ let(:set) { JSON::JWK::Set.new [jwk, jwk] }
28
+
29
+ it 'should become proper JWK set format' do
30
+ _set_ = set.as_json
31
+ _set_.should include :keys
32
+ _set_[:keys].should == [jwk, jwk]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSON::JWK do
4
+ let(:rsa_public_key) { public_key }
5
+
6
+ context 'when RSA public key given' do
7
+ let(:jwk) { JSON::JWK.new rsa_public_key }
8
+ it { jwk.should include :alg, :exp, :mod }
9
+ its(:alg) { jwk[:alg].should == :RSA }
10
+ its(:exp) { jwk[:exp].should == UrlSafeBase64.encode64(rsa_public_key.e.to_s(2)) }
11
+ its(:mod) { jwk[:mod].should == UrlSafeBase64.encode64(rsa_public_key.n.to_s(2)) }
12
+
13
+ context 'when kid/use options given' do
14
+ let(:jwk) { JSON::JWK.new rsa_public_key, :kid => '12345', :use => :sig }
15
+ it { jwk.should include :kid, :use }
16
+ its(:kid) { jwk[:kid].should == '12345' }
17
+ its(:use) { jwk[:use].should == :sig }
18
+ end
19
+ end
20
+ end
@@ -79,7 +79,7 @@ describe JSON::JWS do
79
79
  describe '#verify' do
80
80
  shared_examples_for :succes_signature_verification do
81
81
  it do
82
- expect { decoded }.should_not raise_error
82
+ expect { decoded }.not_to raise_error
83
83
  decoded.should be_a JSON::JWT
84
84
  end
85
85
 
@@ -49,7 +49,7 @@ describe JSON::JWT do
49
49
  it do
50
50
  expect do
51
51
  jwt.verify(no_signed, '', 'secret')
52
- end.should raise_error JSON::JWT::UnexpectedAlgorighm
52
+ end.to raise_error JSON::JWT::UnexpectedAlgorighm
53
53
  end
54
54
  end
55
55
 
@@ -57,7 +57,7 @@ describe JSON::JWT do
57
57
  it do
58
58
  expect do
59
59
  jwt.verify(no_signed, 'signature')
60
- end.should raise_error JSON::JWT::VerificationFailed
60
+ end.to raise_error JSON::JWT::VerificationFailed
61
61
  end
62
62
  end
63
63
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-jwt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -143,11 +143,14 @@ files:
143
143
  - json-jwt.gemspec
144
144
  - lib/json/jwe.rb
145
145
  - lib/json/jwk.rb
146
+ - lib/json/jwk/set.rb
146
147
  - lib/json/jws.rb
147
148
  - lib/json/jwt.rb
148
149
  - spec/fixtures/rsa/private_key.pem
149
150
  - spec/fixtures/rsa/public_key.pem
150
151
  - spec/helpers/sign_key_fixture_helper.rb
152
+ - spec/json/jwk/set_spec.rb
153
+ - spec/json/jwk_spec.rb
151
154
  - spec/json/jws_spec.rb
152
155
  - spec/json/jwt_spec.rb
153
156
  - spec/spec_helper.rb
@@ -180,6 +183,8 @@ test_files:
180
183
  - spec/fixtures/rsa/private_key.pem
181
184
  - spec/fixtures/rsa/public_key.pem
182
185
  - spec/helpers/sign_key_fixture_helper.rb
186
+ - spec/json/jwk/set_spec.rb
187
+ - spec/json/jwk_spec.rb
183
188
  - spec/json/jws_spec.rb
184
189
  - spec/json/jwt_spec.rb
185
190
  - spec/spec_helper.rb