Permalink
Browse files

Update library to use HMAC authentication

  • Loading branch information...
1 parent 5601e2c commit 6d547b8e6ff36f5fb8df081dcc5204d28ffcf5e9 @jeremyboles committed Jan 29, 2013
Showing with 40 additions and 17 deletions.
  1. +6 −6 Gemfile.lock
  2. +31 −8 lib/snapshot/connection.rb
  3. +3 −3 lib/snapshot/image.rb
View
@@ -1,18 +1,18 @@
PATH
remote: .
specs:
- snapshot-ruby (0.1)
- json (= 1.4.6)
- rest-client (= 1.6.1)
+ snapshot-ruby (0.2.1)
+ json (= 1.6.5)
+ rest-client (= 1.6.7)
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
fakeweb (1.3.0)
- json (1.4.6)
- mime-types (1.16)
- rest-client (1.6.1)
+ json (1.6.5)
+ mime-types (1.19)
+ rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.4.0)
rspec-core (~> 2.4.0)
View
@@ -1,4 +1,8 @@
+require 'base64'
+require 'openssl'
+require 'restclient'
require 'uri'
+require 'time'
module Snapshot
class Connection
@@ -22,19 +26,38 @@ def initialize(opts={})
end
end
- # Returns an authenticated RestClient::Resource
- #
- def resource
- @resource ||= RestClient::Resource.new(self.url, {
- :user => self.access_key,
- :password => self.secret_key
- })
+ def delete(path)
+ request(:delete, path)
end
+ def get(path)
+ request(:get, path)
+ end
+
+ def post(path, payload={})
+ request(:post, path, payload)
+ end
+
# Returns a URL to Snapshot for the connection
#
def url
- "http://#{domain}/"
+ "http://#{domain}"
+ end
+
+ private
+
+ def request(method, path, payload = nil)
+ date = Time.now.httpdate
+
+ sha1 = OpenSSL::Digest::Digest.new('sha1')
+ message = "#{method.to_s.upcase}\n#{path}\n#{date}"
+ digest = Base64.encode64(OpenSSL::HMAC.digest(sha1, secret_key, message))
+
+ headers = { authorization: "HMAC #{access_key}:#{digest}", date: date }
+
+ params = { method: method, url: "#{url}#{path}", headers: headers }
+ params[:payload] = payload unless payload.nil?
+ RestClient::Request.execute(params)
end
end
end
View
@@ -27,7 +27,7 @@ class << self
#
def create(file)
file = File.open(file) if file.is_a?(String)
- result = Snapshot.connection.resource.post(:file => file)
+ result = Snapshot.connection.post('/', file: file)
info = JSON.parse(result)['file']
self.new.tap do |img|
@@ -65,7 +65,7 @@ def destroy(id)
# Snapshot::Image.find('a45af9b9e6ebab9c685faef4e72e7a14f3e24e1a')
#
def find(id)
- result = Snapshot.connection.resource["#{id}"].get
+ result = Snapshot.connection.get("/#{id}")
info = JSON.parse(result)
self.new.tap do |img|
@@ -90,7 +90,7 @@ def find(id)
# img.destroy
#
def destroy
- Snapshot.connection.resource["#{id}"].delete
+ Snapshot.connection.delete("/#{id}")
true
rescue RestClient::ResourceNotFound
nil

0 comments on commit 6d547b8

Please sign in to comment.