Class: Zipmark::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/zipmark/callback.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, options = {}) ⇒ Callback

Returns a new instance of Callback.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/zipmark/callback.rb', line 15

def initialize(request, options = {})
  raise ArgumentError, "Request cannot be nil" unless request
  @request = request
  @errors = {}
  @client = options[:client]
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



13
14
15
# File 'lib/zipmark/callback.rb', line 13

def client
  @client
end

#errorsObject

Returns the value of attribute errors.



13
14
15
# File 'lib/zipmark/callback.rb', line 13

def errors
  @errors
end

#requestObject

Returns the value of attribute request.



13
14
15
# File 'lib/zipmark/callback.rb', line 13

def request
  @request
end

Instance Method Details

#allowable_intervalObject



70
71
72
73
# File 'lib/zipmark/callback.rb', line 70

def allowable_interval
  # 15 minutes
  15 * 60
end

#application_identifierObject



34
35
36
# File 'lib/zipmark/callback.rb', line 34

def application_identifier
  client.adapter.username if client
end

#authorization_headerObject



100
101
102
# File 'lib/zipmark/callback.rb', line 100

def authorization_header
  @request.headers["Authorization"]
end

#bodyObject



22
23
24
# File 'lib/zipmark/callback.rb', line 22

def body
  @request.raw_post
end

#dateObject



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

Returns:

  • (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

#eventObject



42
43
44
# File 'lib/zipmark/callback.rb', line 42

def event
  parsed_body["callback"]["event"]
end

#hashed_contentObject



54
55
56
# File 'lib/zipmark/callback.rb', line 54

def hashed_content
  Digest::MD5.hexdigest(body) if body
end

#identifierObject



30
31
32
# File 'lib/zipmark/callback.rb', line 30

def identifier
  client.identifier if client
end

#objectObject



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_typeObject



50
51
52
# File 'lib/zipmark/callback.rb', line 50

def object_type
  parsed_body["callback"]["object_type"]
end

#parsed_bodyObject



26
27
28
# File 'lib/zipmark/callback.rb', line 26

def parsed_body
  @parsed_body ||= JSON.parse(body)
end

#secretObject



38
39
40
# File 'lib/zipmark/callback.rb', line 38

def secret
  client.adapter.password if client
end

#uriObject



62
63
64
# File 'lib/zipmark/callback.rb', line 62

def uri
  @request.path
end

#valid?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/zipmark/callback.rb', line 75

def valid?
  validate_date && validate_authorization
end

#validate_authorizationObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/zipmark/callback.rb', line 79

def validate_authorization
  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
  valid_authorization = "ZM #{Base64.strict_encode64(identifier).chomp}:#{signed_string}"
  if authorization_header == valid_authorization
    return true
  else
    errors[:authorization] = "Signature does not match."
    return false
  end
end

#validate_dateObject



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