http_signatures 0.0.1 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d470a2f4d0e20f91439770b757fec53dfe6e0cf7
4
- data.tar.gz: 44e3fc97e52f403f4a71c7c0d9aead5440128409
3
+ metadata.gz: 2f089f43abace9a9b86c3d19ba394ca4029260c2
4
+ data.tar.gz: d3f055bf4eb80034756d585770be2bffd778b798
5
5
  SHA512:
6
- metadata.gz: 6ae9785147e0d0d07deec0af632caafdc67ea901eea51ead27c990ef0bc98e9c39b0b465e7961898271f231aa035fd823ff2a9b026f0ed6c6a3a2344d5142fec
7
- data.tar.gz: 69334e7da16049f64d3ccba567a4737c75071290c698699111bfae112449d20bdd570295f9a8053948b6dca023502d395df37f435475f682f407907b21b600f3
6
+ metadata.gz: 6d8a718a78fed672fb5c61b1a43f9d01fc983abf08f5fa97f7156f1d14ceaba5bcd95413f2b47dec8bc154880a7cfa4530b68ffa52cb63a48866a67309249065
7
+ data.tar.gz: 00be41245a855a9441a0414c1a2c10c5523df24b879f32c3b9c55d2327bb9dfe56b7382304a8e36818e870e0e4479e78830446bcef84daf2b79a410d69639671
data/README.md CHANGED
@@ -15,10 +15,13 @@ require "http_signatures"
15
15
  $context = HttpSignatures::Context.new(
16
16
  keys: {"examplekey" => "secret-key-here"},
17
17
  algorithm: "hmac-sha256",
18
- headers: %w{(request-target) Date Content-Length},
18
+ headers: ["(request-target)", "Date", "Content-Length"],
19
19
  )
20
20
  ```
21
21
 
22
+ If there's only one key in the `keys` hash, that will be used for signing.
23
+ Otherwise, specify one via `signing_key_id: "examplekey"`.
24
+
22
25
  ### Messages
23
26
 
24
27
  A message is an HTTP request or response. A subset of the interface of
@@ -29,6 +32,7 @@ of `message#method` and `message#path` for `(request-target)` support.
29
32
  ```rb
30
33
  require "net/http"
31
34
  require "time"
35
+
32
36
  message = Net::HTTP::Get.new(
33
37
  "/path?query=123",
34
38
  "Date" => Time.now.rfc822,
@@ -39,7 +43,7 @@ message = Net::HTTP::Get.new(
39
43
  ### Signing a message
40
44
 
41
45
  ```rb
42
- $context.signer("examplekey").sign(message)
46
+ $context.signer.sign(message)
43
47
  ```
44
48
 
45
49
  Now `message` contains the signature headers:
@@ -1,19 +1,30 @@
1
1
  module HttpSignatures
2
2
  class Context
3
3
 
4
- def initialize(keys: {}, algorithm: nil, headers: nil)
4
+ def initialize(keys: {}, signing_key_id: nil, algorithm: nil, headers: nil)
5
5
  @key_store = KeyStore.new(keys)
6
+ @signing_key_id = signing_key_id
6
7
  @algorithm_name = algorithm
7
8
  @headers = headers
8
9
  end
9
10
 
10
- def signer(key_id)
11
+ def signer
11
12
  Signer.new(
12
- key: @key_store.fetch(key_id),
13
+ key: signing_key,
13
14
  algorithm: Algorithm.create(@algorithm_name),
14
15
  header_list: HeaderList.new(@headers),
15
16
  )
16
17
  end
17
18
 
19
+ private
20
+
21
+ def signing_key
22
+ if @signing_key_id
23
+ @key_store.fetch(@signing_key_id)
24
+ else
25
+ @key_store.only_key
26
+ end
27
+ end
28
+
18
29
  end
19
30
  end
@@ -10,6 +10,14 @@ module HttpSignatures
10
10
  @keys.fetch(id)
11
11
  end
12
12
 
13
+ def only_key
14
+ if @keys.one?
15
+ @keys.values.first
16
+ else
17
+ raise KeyError, "Expected 1 key, found #{@keys.size}"
18
+ end
19
+ end
20
+
13
21
  private
14
22
 
15
23
  def []=(id, secret)
@@ -1,3 +1,3 @@
1
1
  module HttpSignatures
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/spec/context_spec.rb CHANGED
@@ -2,28 +2,56 @@ require "net/http"
2
2
 
3
3
  RSpec.describe HttpSignatures::Context do
4
4
 
5
- subject(:context) do
6
- HttpSignatures::Context.new(
7
- keys: {"hello" => "world"},
8
- algorithm: "null",
9
- headers: %w{(request-target) date content-length},
10
- )
11
- end
12
-
13
5
  let(:message) { Net::HTTP::Get.new("/", "date" => "x", "content-length" => "0") }
14
6
 
15
- describe "#signer" do
16
- it "instantiates Signer with key, algorithm, headers" do
17
- expect(HttpSignatures::Signer).to receive(:new) do |args|
18
- expect(args[:key]).to eq(HttpSignatures::Key.new(id: "hello", secret: "world"))
19
- expect(args[:algorithm].name).to eq("null")
20
- expect(args[:header_list].to_a).to eq(%w{(request-target) date content-length})
7
+ context "with one key in KeyStore, no signing_key_id specified" do
8
+ subject(:context) do
9
+ HttpSignatures::Context.new(
10
+ keys: {"hello" => "world"},
11
+ algorithm: "null",
12
+ headers: %w{(request-target) date content-length},
13
+ )
14
+ end
15
+
16
+ describe "#signer" do
17
+ it "instantiates Signer with key, algorithm, headers" do
18
+ expect(HttpSignatures::Signer).to receive(:new) do |args|
19
+ expect(args[:key]).to eq(HttpSignatures::Key.new(id: "hello", secret: "world"))
20
+ expect(args[:algorithm].name).to eq("null")
21
+ expect(args[:header_list].to_a).to eq(%w{(request-target) date content-length})
22
+ end
23
+ context.signer
24
+ end
25
+
26
+ it "signs without errors" do
27
+ context.signer.sign(message)
21
28
  end
22
- context.signer("hello")
29
+ end
30
+ end
31
+
32
+ context "with two keys in KeyStore, signing_key_id specified" do
33
+ subject(:context) do
34
+ HttpSignatures::Context.new(
35
+ keys: {"hello" => "world", "another" => "key"},
36
+ signing_key_id: "another",
37
+ algorithm: "null",
38
+ headers: %w{(request-target) date content-length},
39
+ )
23
40
  end
24
41
 
25
- it "signs without errors" do
26
- context.signer("hello").sign(message)
42
+ describe "#signer" do
43
+ it "instantiates Signer with key, algorithm, headers" do
44
+ expect(HttpSignatures::Signer).to receive(:new) do |args|
45
+ expect(args[:key]).to eq(HttpSignatures::Key.new(id: "another", secret: "key"))
46
+ expect(args[:algorithm].name).to eq("null")
47
+ expect(args[:header_list].to_a).to eq(%w{(request-target) date content-length})
48
+ end
49
+ context.signer
50
+ end
51
+
52
+ it "signs without errors" do
53
+ context.signer.sign(message)
54
+ end
27
55
  end
28
56
  end
29
57
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_signatures
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Annesley