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 +4 -4
- data/lib/signed_json/version.rb +1 -1
- data/lib/signed_json.rb +18 -11
- data/signed_json.gemspec +1 -1
- data/spec/signed_json_spec.rb +13 -13
- data/spec/spec_helper.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f87c5b0f1a4ece38890c06ce7ea92402ac1051f
|
4
|
+
data.tar.gz: 0f74718e9382c444ded4db1a5fccbced5fcc8430
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b1a8f253f94571aec508af9fd4202627bf0f15a9e8aedf4f1c5b38bca11440f02d13444a3a9161d8da3947cdc4ffaec1fa7ef0a134900cc96092fbf5c6a9814
|
7
|
+
data.tar.gz: 7320c16827347b01d0bcc9416d8ab2e801ce1c2cdd680e892934f3a83d1b32b2f6359b5e3e47d94443913ef45eb4c7a8888d1988946fb9725980937b6ceb0b4a
|
data/lib/signed_json/version.rb
CHANGED
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]
|
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 =
|
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
|
29
|
+
OpenSSL::HMAC.hexdigest(digest, @secret, json_generate(input))
|
29
30
|
end
|
30
31
|
|
31
32
|
private
|
32
33
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
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
|
-
|
41
|
-
|
42
|
+
def json_parse(json)
|
43
|
+
JSON.parse(json)
|
44
|
+
rescue TypeError, JSON::ParserError
|
45
|
+
raise InputError
|
46
|
+
end
|
42
47
|
|
43
|
-
|
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
data/spec/signed_json_spec.rb
CHANGED
@@ -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".
|
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,
|
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
|
-
{
|
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
|
-
[
|
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
|
-
{
|
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.
|
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).
|
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
|
-
|
47
|
+
expect {
|
48
48
|
SignedJson::Signer.new('wrong').decode(encoded)
|
49
|
-
}.
|
49
|
+
}.to raise_error(SignedJson::SignatureError)
|
50
50
|
end
|
51
51
|
|
52
52
|
it "raises InputError for invalid input" do
|
53
|
-
|
53
|
+
expect {
|
54
54
|
SignedJson::Signer.new('key').decode('blarg')
|
55
|
-
}.
|
55
|
+
}.to raise_error(SignedJson::InputError)
|
56
56
|
end
|
57
57
|
|
58
58
|
it "raises InputError for nil input" do
|
59
|
-
|
59
|
+
expect {
|
60
60
|
SignedJson::Signer.new('key').decode(nil)
|
61
|
-
}.
|
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
|
-
|
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:
|
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-
|
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: '
|
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: '
|
40
|
+
version: '3.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|