appoxy_api 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ module Appoxy
14
14
  module ApiController
15
15
 
16
16
  def verify_signature
17
-
17
+ params2 = nil
18
18
  if request.put? || request.post?
19
19
  # We'll extract params from body instead here
20
20
  # todo: maybe check for json format first in case this is a file or something?
@@ -25,28 +25,47 @@ module Appoxy
25
25
  params.merge! params2
26
26
  end
27
27
 
28
- operation = "#{controller_name}/#{action_name}"
29
- puts "XXX " + operation
28
+ #operation = "#{controller_name}/#{action_name}"
29
+ #operation = request.env["PATH_INFO"].gsub(/\/api\//, "")# here we're getting original request url'
30
+
31
+ # #getting clean params (without parsed via routes)
32
+ # params_for_signature = params2||request.query_parameters
33
+ # #removing mandatory params
34
+ # params_for_signature = params_for_signature.delete_if {|key, value| ["access_key", "sigv", "sig", "timestamp"].include? key}
30
35
 
31
- # puts 'params in base=' + params.inspect
32
36
 
37
+ #puts "params " +operation+Appoxy::Api::Signatures.hash_to_s(params_for_signature)
33
38
  access_key = params["access_key"]
34
39
  sigv = params["sigv"]
35
40
  timestamp = params["timestamp"]
36
41
  sig = params["sig"]
37
-
42
+ signature = ""
43
+ case sigv
44
+ when "0.1"
45
+ puts "outdated version of client"
46
+ signature = "#{controller_name}/#{action_name}"
47
+ when "0.2"
48
+ puts "new version of client"
49
+ operation = request.env["PATH_INFO"].gsub(/\/api\//, "")# here we're getting original request url'
50
+ params_for_signature = params2||request.query_parameters
51
+ params_for_signature = params_for_signature.delete_if {|key, value| ["access_key", "sigv", "sig", "timestamp"].include? key}
52
+ signature = operation+Appoxy::Api::Signatures.hash_to_s(params_for_signature)
53
+ end
54
+ # puts "signature " + signature
38
55
  raise Appoxy::Api::ApiError, "No access_key" if access_key.nil?
39
56
  raise Appoxy::Api::ApiError, "No sigv" if sigv.nil?
40
57
  raise Appoxy::Api::ApiError, "No timestamp" if timestamp.nil?
41
58
  raise Appoxy::Api::ApiError, "No sig" if sig.nil?
42
-
43
- sig2 = Appoxy::Api::Signatures.generate_signature(operation, timestamp, secret_key_for_signature(access_key))
59
+ timestamp2 = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
60
+ raise Appoxy::Api::ApiError, "Request timed out!" unless (Time.parse(timestamp2)-Time.parse(timestamp))<60 # deny all requests older than 60 seconds
61
+ sig2 = Appoxy::Api::Signatures.generate_signature(signature, timestamp, secret_key_for_signature(access_key))
44
62
  raise Appoxy::Api::ApiError, "Invalid signature!" unless sig == sig2
45
63
 
46
- puts 'Verified OK'
64
+ puts 'Signature OK'
47
65
 
48
66
  end
49
67
 
68
+
50
69
  def sig_should
51
70
  raise "You didn't define a sig_should method in your controller!"
52
71
  end
@@ -56,8 +75,8 @@ module Appoxy
56
75
  response_as_string = '' # in case we want to add debugging or something
57
76
  # respond_to do |format|
58
77
  # format.json { render :json=>msg }
59
- response_as_string = render_to_string :json => msg
60
- render :json => response_as_string
78
+ # response_as_string = render_to_string :json => msg
79
+ render :json => msg
61
80
  # end
62
81
  true
63
82
  end
@@ -79,6 +98,7 @@ module Appoxy
79
98
 
80
99
  end
81
100
 
101
+
82
102
  class ApiError < StandardError
83
103
 
84
104
  def initialize(msg=nil)
data/lib/api/client.rb CHANGED
@@ -9,12 +9,14 @@ module Appoxy
9
9
 
10
10
  attr_accessor :host, :access_key, :secret_key
11
11
 
12
+
12
13
  def initialize(host, access_key, secret_key, options={})
13
14
  @host = host
14
15
  @access_key = access_key
15
16
  @secret_key = secret_key
16
17
  end
17
18
 
19
+
18
20
  def get(method, params={}, options={})
19
21
  begin
20
22
  # ClientHelper.run_http(host, access_key, secret_key, :get, method, nil, params)
@@ -25,6 +27,7 @@ module Appoxy
25
27
  end
26
28
  end
27
29
 
30
+
28
31
  def post(method, params={}, options={})
29
32
  begin
30
33
  parse_response RestClient.post(url(method), add_params(method, params).to_json, headers)
@@ -36,6 +39,7 @@ module Appoxy
36
39
 
37
40
  end
38
41
 
42
+
39
43
  def put(method, body, options={})
40
44
  begin
41
45
  parse_response RestClient.put(url(method), add_params(method, body).to_json, headers)
@@ -46,21 +50,32 @@ module Appoxy
46
50
  end
47
51
  end
48
52
 
53
+
54
+ def delete(method, params={}, options={})
55
+ begin
56
+ parse_response RestClient.delete(append_params(url(method), add_params(method, params)))
57
+ rescue RestClient::BadRequest => ex
58
+ raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s
59
+ end
60
+ end
61
+
62
+
49
63
  def url(command_path)
50
64
  url = host + command_path
51
65
  url
52
66
  end
53
67
 
68
+
54
69
  def add_params(command_path, hash)
55
70
  ts = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
56
- # puts 'timestamp = ' + ts
57
- sig = Appoxy::Api::Signatures.generate_signature(command_path, ts, secret_key)
58
-
59
- extra_params = {'sigv'=>"0.1", 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
71
+ #p "hash_to s" + command_path + Appoxy::Api::Signatures.hash_to_s(hash)
72
+ sig = Appoxy::Api::Signatures.generate_signature(command_path + Appoxy::Api::Signatures.hash_to_s(hash), ts, secret_key)
73
+ extra_params = {'sigv'=>"0.2", 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
60
74
  hash.merge!(extra_params)
61
75
 
62
76
  end
63
77
 
78
+
64
79
  def append_params(host, params)
65
80
  host += "?"
66
81
  i = 0
@@ -72,11 +87,13 @@ module Appoxy
72
87
  return host
73
88
  end
74
89
 
90
+
75
91
  def headers
76
92
  user_agent = "Appoxy API Ruby Client"
77
93
  headers = {'User-Agent' => user_agent}
78
94
  end
79
95
 
96
+
80
97
  def parse_response(response)
81
98
  begin
82
99
  return ActiveSupport::JSON.decode(response.to_s)
@@ -90,4 +107,4 @@ module Appoxy
90
107
  end
91
108
 
92
109
  end
93
- end
110
+ end
@@ -7,12 +7,20 @@ module Appoxy
7
7
  return gmtime.strftime("%Y-%m-%dT%H:%M:%SZ")
8
8
  end
9
9
 
10
+
10
11
  def self.generate_signature(operation, timestamp, secret_key)
11
12
  my_sha_hmac = Digest::HMAC.digest(operation + timestamp, secret_key, Digest::SHA1)
12
13
  my_b64_hmac_digest = Base64.encode64(my_sha_hmac).strip
13
14
  return my_b64_hmac_digest
14
15
  end
15
16
 
17
+
18
+ def self.hash_to_s(hash)
19
+ str = ""
20
+ hash.each_pair {|key, value| str+= "#{key}#{value}" }
21
+ #removing all characters that could differ after parsing with rails
22
+ return str.delete "\"\/:{}[]\' T"
23
+ end
16
24
  end
17
25
  end
18
26
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 5
9
- version: 0.0.5
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Travis Reeder
@@ -14,13 +14,14 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-17 00:00:00 -07:00
17
+ date: 2010-12-29 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rest-client
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ">="
26
27
  - !ruby/object:Gem::Version
@@ -49,11 +50,12 @@ homepage: http://www.appoxy.com
49
50
  licenses: []
50
51
 
51
52
  post_install_message:
52
- rdoc_options:
53
- - --charset=UTF-8
53
+ rdoc_options: []
54
+
54
55
  require_paths:
55
56
  - lib
56
57
  required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
57
59
  requirements:
58
60
  - - ">="
59
61
  - !ruby/object:Gem::Version
@@ -61,6 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
63
  - 0
62
64
  version: "0"
63
65
  required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
64
67
  requirements:
65
68
  - - ">="
66
69
  - !ruby/object:Gem::Version
@@ -70,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
73
  requirements: []
71
74
 
72
75
  rubyforge_project:
73
- rubygems_version: 1.3.6
76
+ rubygems_version: 1.3.7
74
77
  signing_key:
75
78
  specification_version: 3
76
79
  summary: Appoxy API Helper gem