signed_json 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f40f32c57443555eeb355a349ff0cc13d15fdea
4
- data.tar.gz: 4d2cb908d2373eea85da54dad795a6080764b32d
3
+ metadata.gz: 7f87c5b0f1a4ece38890c06ce7ea92402ac1051f
4
+ data.tar.gz: 0f74718e9382c444ded4db1a5fccbced5fcc8430
5
5
  SHA512:
6
- metadata.gz: 329bdc5c2246244cfca4b3c704b7099d81af8338e8059498857238284772c1ed26d856b08d3ff46aeee5a171e9f0aec29ebe41d6da615b16cbf3d5fc4d7d891c
7
- data.tar.gz: 50971730f9c7c370067c67fb5c14e44536e691192a309ad1f30e3460fcf9707a45bb7d1ea73f48090b1fb1ce771e5d4b2aa59b3633f1e41f5d0ef9e1b4940369
6
+ metadata.gz: 0b1a8f253f94571aec508af9fd4202627bf0f15a9e8aedf4f1c5b38bca11440f02d13444a3a9161d8da3947cdc4ffaec1fa7ef0a134900cc96092fbf5c6a9814
7
+ data.tar.gz: 7320c16827347b01d0bcc9416d8ab2e801ce1c2cdd680e892934f3a83d1b32b2f6359b5e3e47d94443913ef45eb4c7a8888d1988946fb9725980937b6ceb0b4a
@@ -1,3 +1,3 @@
1
1
  module SignedJson
2
- VERSION = "1.0.1"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/signed_json.rb CHANGED
@@ -10,11 +10,12 @@ module SignedJson
10
10
  end
11
11
 
12
12
  def encode(input)
13
- [digest_for(input), input].to_json
13
+ data_to_encode = [digest_for(input), input]
14
+ json_generate(data_to_encode)
14
15
  end
15
16
 
16
17
  def decode(input)
17
- digest, data = json_decode(input)
18
+ digest, data = decode_digest_and_data(input)
18
19
  raise SignatureError unless digest === digest_for(data)
19
20
  data
20
21
  end
@@ -25,23 +26,29 @@ module SignedJson
25
26
  def digest_for(input)
26
27
  require 'openssl' unless defined?(OpenSSL) # from ActiveSupport::MessageVerifier
27
28
  digest = OpenSSL::Digest.const_get(@digest).new
28
- OpenSSL::HMAC.hexdigest(digest, @secret, input.to_json)
29
+ OpenSSL::HMAC.hexdigest(digest, @secret, json_generate(input))
29
30
  end
30
31
 
31
32
  private
32
33
 
33
- def json_decode(input)
34
- begin
35
- parts = JSON.parse(input)
36
- rescue TypeError, JSON::ParserError
34
+ def decode_digest_and_data(json)
35
+ parts = json_parse(json)
36
+ unless parts.instance_of?(Array) && parts.length == 2
37
37
  raise InputError
38
38
  end
39
+ parts
40
+ end
39
41
 
40
- raise InputError unless
41
- parts.instance_of?(Array) && parts.length == 2
42
+ def json_parse(json)
43
+ JSON.parse(json)
44
+ rescue TypeError, JSON::ParserError
45
+ raise InputError
46
+ end
42
47
 
43
- parts
48
+ def json_generate(data)
49
+ # Use JSON.dump; JSON.generate only handles top-level object/array.
50
+ JSON.dump(data)
44
51
  end
45
- end
46
52
 
53
+ end
47
54
  end
data/signed_json.gemspec CHANGED
@@ -20,6 +20,6 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_dependency('json')
22
22
 
23
- s.add_development_dependency('rspec', ['~> 2.3'])
23
+ s.add_development_dependency('rspec', ['~> 3.1'])
24
24
  s.add_development_dependency('rake')
25
25
  end
@@ -5,23 +5,23 @@ describe SignedJson do
5
5
  describe "round trip encoding/decoding" do
6
6
 
7
7
  it "round-trips a string" do
8
- "a string".should round_trip_as_signed_json
8
+ expect("a string").to round_trip_as_signed_json
9
9
  end
10
10
 
11
11
  it "round-trips an array of strings and ints" do
12
- [1, 'a', 2, 'b'].should round_trip_as_signed_json
12
+ expect([1, "a", 2, "b"]).to round_trip_as_signed_json
13
13
  end
14
14
 
15
15
  it "round-trips a hash with string keys, string and int values" do
16
- { 'a' => 'b', 'b' => 2 }.should round_trip_as_signed_json
16
+ expect({"a" => "b", "b" => 2}).to round_trip_as_signed_json
17
17
  end
18
18
 
19
19
  it "round-trips a nested array" do
20
- [ 'a', [ 'b', [ 'c', 'd' ], 'e' ], 'f' ].should round_trip_as_signed_json
20
+ expect(["a", ["b", ["c", "d"], "e"], "f"]).to round_trip_as_signed_json
21
21
  end
22
22
 
23
23
  it "round-trips a hash/array/string/int structure" do
24
- { 'a' => [ 'b' ], 'd' => { 'e' => 'f' }, 'g' => 10 }.should round_trip_as_signed_json
24
+ expect({"a" => ["b"], "d" => {"e" => "f"}, "g" => 10}).to round_trip_as_signed_json
25
25
  end
26
26
 
27
27
  end
@@ -30,12 +30,12 @@ describe SignedJson do
30
30
 
31
31
  it "returns a string" do
32
32
  encoded = SignedJson::Signer.new('right').encode('test')
33
- encoded.should be_instance_of(String)
33
+ expect(encoded).to be_instance_of(String)
34
34
  end
35
35
 
36
36
  it "returns a valid JSON-encoded array" do
37
37
  encoded = SignedJson::Signer.new('right').encode('test')
38
- JSON.parse(encoded).should be_instance_of(Array)
38
+ expect(JSON.parse(encoded)).to be_instance_of(Array)
39
39
  end
40
40
 
41
41
  end
@@ -44,21 +44,21 @@ describe SignedJson do
44
44
 
45
45
  it "raises SignatureError for incorrect key/signature" do
46
46
  encoded = SignedJson::Signer.new('right').encode('test')
47
- lambda {
47
+ expect {
48
48
  SignedJson::Signer.new('wrong').decode(encoded)
49
- }.should raise_error(SignedJson::SignatureError)
49
+ }.to raise_error(SignedJson::SignatureError)
50
50
  end
51
51
 
52
52
  it "raises InputError for invalid input" do
53
- lambda {
53
+ expect {
54
54
  SignedJson::Signer.new('key').decode('blarg')
55
- }.should raise_error(SignedJson::InputError)
55
+ }.to raise_error(SignedJson::InputError)
56
56
  end
57
57
 
58
58
  it "raises InputError for nil input" do
59
- lambda {
59
+ expect {
60
60
  SignedJson::Signer.new('key').decode(nil)
61
- }.should raise_error(SignedJson::InputError)
61
+ }.to raise_error(SignedJson::InputError)
62
62
  end
63
63
 
64
64
  end
data/spec/spec_helper.rb CHANGED
@@ -23,7 +23,7 @@ RSpec::Matchers.define :round_trip_as_signed_json do
23
23
  false
24
24
  end
25
25
 
26
- failure_message_for_should do |actual|
26
+ failure_message do |actual|
27
27
  if @reason == :not_encoded
28
28
  "Expected encoded to be different to original input: #{actual}"
29
29
  elsif @reason == :mismatch
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signed_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Annesley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-11 00:00:00.000000000 Z
11
+ date: 2014-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.3'
33
+ version: '3.1'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.3'
40
+ version: '3.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement