Class: Zipmark::Callback
- Inherits:
-
Object
- Object
- Zipmark::Callback
- Defined in:
- lib/zipmark/callback.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#request ⇒ Object
Returns the value of attribute request.
Instance Method Summary collapse
- #allowable_interval ⇒ Object
- #application_identifier ⇒ Object
- #authorization_header ⇒ Object
- #body ⇒ Object
- #date ⇒ Object
- #date_within_range? ⇒ Boolean
- #event ⇒ Object
- #hashed_content ⇒ Object
- #identifier ⇒ Object
-
#initialize(request, options = {}) ⇒ Callback
constructor
A new instance of Callback.
- #object ⇒ Object
- #object_type ⇒ Object
- #parsed_body ⇒ Object
- #secret ⇒ Object
- #uri ⇒ Object
- #valid? ⇒ Boolean
- #validate_authorization ⇒ Object
- #validate_date ⇒ Object
Constructor Details
#initialize(request, options = {}) ⇒ Callback
Returns a new instance of Callback.
15 16 17 18 19 20 |
# File 'lib/zipmark/callback.rb', line 15 def initialize(request, = {}) raise ArgumentError, "Request cannot be nil" unless request @request = request @errors = {} @client = [:client] end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
13 14 15 |
# File 'lib/zipmark/callback.rb', line 13 def client @client end |
#errors ⇒ Object
Returns the value of attribute errors.
13 14 15 |
# File 'lib/zipmark/callback.rb', line 13 def errors @errors end |
#request ⇒ Object
Returns the value of attribute request.
13 14 15 |
# File 'lib/zipmark/callback.rb', line 13 def request @request end |
Instance Method Details
#allowable_interval ⇒ Object
70 71 72 73 |
# File 'lib/zipmark/callback.rb', line 70 def allowable_interval # 15 minutes 15 * 60 end |
#application_identifier ⇒ Object
34 35 36 |
# File 'lib/zipmark/callback.rb', line 34 def application_identifier client.adapter.username if client end |
#authorization_header ⇒ Object
100 101 102 |
# File 'lib/zipmark/callback.rb', line 100 def @request.headers["Authorization"] end |
#body ⇒ Object
22 23 24 |
# File 'lib/zipmark/callback.rb', line 22 def body @request.raw_post end |
#date ⇒ Object
58 59 60 |
# File 'lib/zipmark/callback.rb', line 58 def date Time.parse(@request.headers["Date"]) if @request.headers["Date"] end |
#date_within_range? ⇒ Boolean
66 67 68 |
# File 'lib/zipmark/callback.rb', line 66 def date_within_range? date && date < Time.now + allowable_interval && date > Time.now - allowable_interval end |
#event ⇒ Object
42 43 44 |
# File 'lib/zipmark/callback.rb', line 42 def event parsed_body["callback"]["event"] end |
#hashed_content ⇒ Object
54 55 56 |
# File 'lib/zipmark/callback.rb', line 54 def hashed_content Digest::MD5.hexdigest(body) if body end |
#identifier ⇒ Object
30 31 32 |
# File 'lib/zipmark/callback.rb', line 30 def identifier client.identifier if client end |
#object ⇒ Object
46 47 48 |
# File 'lib/zipmark/callback.rb', line 46 def object Zipmark::Entity.new(parsed_body["callback"]["object"].merge(:client => @client, :resource_type => object_type.downcase)) end |
#object_type ⇒ Object
50 51 52 |
# File 'lib/zipmark/callback.rb', line 50 def object_type parsed_body["callback"]["object_type"] end |
#parsed_body ⇒ Object
26 27 28 |
# File 'lib/zipmark/callback.rb', line 26 def parsed_body @parsed_body ||= JSON.parse(body) end |
#secret ⇒ Object
38 39 40 |
# File 'lib/zipmark/callback.rb', line 38 def secret client.adapter.password if client end |
#uri ⇒ Object
62 63 64 |
# File 'lib/zipmark/callback.rb', line 62 def uri @request.path end |
#valid? ⇒ Boolean
75 76 77 |
# File 'lib/zipmark/callback.rb', line 75 def valid? validate_date && end |
#validate_authorization ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/zipmark/callback.rb', line 79 def string_to_sign = ["POST",hashed_content,'application/json',date.rfc2822,uri,application_identifier].join("\n") signed_string = Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret, string_to_sign)).chomp = "ZM #{Base64.strict_encode64(identifier).chomp}:#{signed_string}" if == return true else errors[:authorization] = "Signature does not match." return false end end |
#validate_date ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/zipmark/callback.rb', line 91 def validate_date if date_within_range? return true else errors[:date] = "Date is not within bounds." return false end end |