notarize 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +10 -17
  2. data/lib/notarize/version.rb +1 -1
  3. data/lib/notarize.rb +32 -30
  4. metadata +3 -3
data/README.md CHANGED
@@ -21,45 +21,38 @@ Or install it with:
21
21
 
22
22
  ## As the client
23
23
 
24
- include Notarize::Client
24
+ Just instantiate a Notary object with your service config and call #send_request with the path and a parameter list.
25
25
 
26
- Implement a #config method that returns a hash with :host, :public_key, and :private_key values for the service you're using. Then just call #send_request with the path and a parameter list.
27
-
28
- def config
29
- { host: "http://www.example-service.com/", public_key: "yourname", private_key: "secret" }
30
- end
31
-
32
- ...
33
-
34
- send_request("/example/path/42/", { foo: "Foo", bar: "Bar" })
26
+ notary = Notarize::Notary.new("http://www.example.com", "public_key", "private_key")
27
+ notary.send_request("/example/path/42/", { foo: "Foo", bar: "Bar" })
35
28
 
36
29
  Optionally you can also pass in an alternate HTTP verb for non-GET requests. Accepted values are :get (the default), :post, :put, and :delete.
37
30
 
38
- send_request("/example/path/42/", { foo: "Foo", bar: "Bar" }, :post)
31
+ response = notary.send_request("/example/path/42/", { foo: "Foo", bar: "Bar" }, :post)
39
32
 
40
33
  send_request returns a hash with two values. :body with the parsed json response, and :code with the HTTP status code.
41
34
 
42
35
  ## As the server
43
36
 
44
- Notarize provides a generate_signature helper method that takes a hash of the incoming params, and the private key of the client making the request. Result should match the value in the incoming 'signature' parameter. For example, in a before_filter:
37
+ Notarize provides a matching_signature? class method that takes a hash of the incoming params, and the private key of the client making the request. The result is checked against params[:signature].
45
38
 
46
- include Notarize::Helper
47
-
48
39
  before_filter :authenticate_request!
49
40
  ...
50
41
 
51
42
  def authenticate_request!
52
43
  client = ApiClient.where(public_key: params[:public_key]).first # Or however your app works.
53
44
 
54
- if generate_signature(params, client.private_key) == params[:signature]
45
+ if Notarize::Notary.matching_signature?(params, client.private_key)
55
46
  # It's ok!
56
47
  else
57
48
  # Get outta town!
58
49
  end
59
50
  end
60
51
 
61
- Notarize doesn't manage your list of authorized clients for you.
52
+ This ApiClient object is just an example; Notarize doesn't manage your list of authorized clients for you.
62
53
 
63
54
  ## Parties Responsible
64
55
 
65
- Author: Aaron Klaassen (aaron@outerspacehero.com)
56
+ Aaron Klaassen
57
+ aaron@outerspacehero.com
58
+ http://www.outerspacehero.com/
@@ -1,3 +1,3 @@
1
1
  module Notarize
2
- VERSION = "1.0"
2
+ VERSION = "1.1"
3
3
  end
data/lib/notarize.rb CHANGED
@@ -2,48 +2,50 @@ require "notarize/version"
2
2
 
3
3
  module Notarize
4
4
 
5
- module Helper
6
- protected
5
+ class Notary
7
6
 
8
- def sorted_query_string(params, reject_sig = true)
9
- params = params.reject { |k, v| k.to_s == 'signature' } if reject_sig
10
-
11
- qs = params.keys.sort_by { |k,v| k.to_s }.collect do |key|
12
- "#{key}=#{params[key]}"
13
- end.join('&')
14
- end
15
-
16
- def generate_signature(params, salt)
17
- Digest::SHA256.hexdigest(sorted_query_string(params) + salt)
18
- end
19
- end
20
-
21
- module Client
22
- include Notarize::Helper
23
-
24
- protected
25
-
26
- def signed_url(path, params)
27
- "#{config[:host]}#{path}?#{sorted_query_string(params)}&signature=#{generate_signature(params, config[:private_key])}"
7
+ def initialize(host, public_key, private_key)
8
+ @host = host
9
+ @public_key = public_key
10
+ @private_key = private_key
28
11
  end
29
12
 
30
13
  def send_request(path, params = {}, method = :get)
31
14
  raise ArgumentError.new("Invalid HTTP verb #{method}") if ![:get, :post, :put, :delete].include?(method)
32
15
 
33
16
  params ||= {}
34
- params.merge!({ public_key: config[:public_key] })
17
+ params.merge!({ public_key: @public_key })
35
18
  response = HTTParty.send(method, signed_url(path, params))
36
19
 
37
20
  { body: JSON.parse(response.body), code: response.code }
38
21
  end
39
22
 
40
- def config
41
- raise NotImplementedError.new "Notarize#config not implemented."
42
- # {
43
- # host: "example.com"
44
- # public_key: "username"
45
- # private_key: "secret"
46
- # }
23
+ def signed_url(path, params)
24
+ sorted_params = Notarize::Notary.sorted_query_string(params)
25
+ sig = Notarize::Notary.generate_signature(params, @private_key)
26
+ "#{@host}#{path}?#{sorted_params}&signature=#{sig}"
47
27
  end
28
+
29
+ class << self
30
+ def generate_signature(params, salt)
31
+ Digest::SHA256.hexdigest(Notarize::Notary.sorted_query_string(params) + salt)
32
+ end
33
+
34
+ def sorted_query_string(params, reject_sig = true)
35
+ params = params.reject { |k, v| k.to_s == 'signature' } if reject_sig
36
+
37
+ qs = params.keys.sort_by { |k,v| k.to_s }.collect do |key|
38
+ "#{key}=#{params[key]}"
39
+ end.join('&')
40
+ end
41
+
42
+ # For the service-side
43
+ def matching_signature?(params, private_key)
44
+ generate_signature(params, private_key) == params[:signature]
45
+ end
46
+
47
+ end
48
+
48
49
  end
50
+
49
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notarize
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-06 00:00:00.000000000 Z
12
+ date: 2013-04-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  version: '0'
76
76
  requirements: []
77
77
  rubyforge_project:
78
- rubygems_version: 1.8.23
78
+ rubygems_version: 1.8.24
79
79
  signing_key:
80
80
  specification_version: 3
81
81
  summary: A simple library for generating signed http requests.