lib/rainforest/auth.rb in rainforest_auth-0.0.8 vs lib/rainforest/auth.rb in rainforest_auth-0.0.9

- old
+ new

@@ -1,50 +1,50 @@ # # Rainforest Authenitcation # # # @author Russell Smith <russ@rainforestqa.com> -# @copyright CLDRDR Inc, 2013 +# @copyright CLDRDR Inc, 2014 require 'openssl' require 'json' class RainforestAuth attr_reader :key - def initialize key + def initialize(key) @key = key self end - def get_run_callback run_id, callback_type + def get_run_callback(run_id, callback_type) digest = sign(callback_type, {:run_id => run_id}) "https://app.rainforestqa.com/api/1/callback/run/#{run_id}/#{callback_type}/#{digest}" end # Return a signature for a callback_type and specified options - def sign callback_type, options = nil + def sign(callback_type, options = nil) OpenSSL::HMAC.hexdigest(digest, @key, merge_data(callback_type, options)) end # Verify a digest vs callback_type and options - def verify digest, callback_type, options = nil + def verify(digest, callback_type, options = nil) digest == sign(callback_type, options) end # Run a block if valid - def run_if_valid digest, callback_type, options, &block - if verify digest, callback_type, options - block.call callback_type, options + def run_if_valid(digest, callback_type, options, &block) + if verify(digest, callback_type, options) + block.call(callback_type, options) end end private def digest - OpenSSL::Digest::Digest.new 'sha1' + OpenSSL::Digest.new('sha1') end - def merge_data callback_type, options + def merge_data(callback_type, options) {:callback_type => callback_type, :options => options}.to_json end -end \ No newline at end of file +end