Module: OpenQq::Signature

Included in:
Gateway
Defined in:
lib/open_qq/signature.rb

Instance Method Summary collapse

Instance Method Details

#make_callback_source(http_method, url, params) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/open_qq/signature.rb', line 30

def make_callback_source(http_method, url, params)
  escape_opt = params.sort_by{|k,v| k.to_s}.map do |kv|
    value = URI.escape(kv.last, /[^0-9a-zA-Z!*()]/)
    "#{kv.first}=#{value}"
  end.join('&')
  "#{http_method}&#{url_escape(url)}&#{url_escape(escape_opt)}"
end

#make_source(http_method, url, params) ⇒ Object


25
26
27
28
# File 'lib/open_qq/signature.rb', line 25

def make_source(http_method, url, params)
  escape_opt = url_escape( params.sort_by{|k,v| k.to_s}.map{|kv| "#{kv.first}=#{kv.last}" }.join('&') )
  "#{http_method}&#{url_escape(url)}&#{escape_opt}"
end

#parsed_params(params) ⇒ Object


21
22
23
# File 'lib/open_qq/signature.rb', line 21

def parsed_params(params)
  params.map{|k,v| "#{url_escape(k.to_s)}=#{url_escape(v.to_s)}"}.join('&')
end

#signature(key, source) ⇒ Object


13
14
15
# File 'lib/open_qq/signature.rb', line 13

def signature(key, source)
  Base64.encode64( OpenSSL::HMAC.digest('sha1', key, source) ).rstrip
end

#url_escape(url) ⇒ Object


17
18
19
# File 'lib/open_qq/signature.rb', line 17

def url_escape(url)
  URI.escape(url, /[^\.\-_\da-zA-Z]/)
end

#verify_sig(sig, key, http_method, url, params) ⇒ Object


38
39
40
# File 'lib/open_qq/signature.rb', line 38

def verify_sig(sig, key, http_method, url, params)
  sig == url_escape( signature(key, make_callback_source(http_method, url, params)) )
end