Created
May 10, 2013 05:22
-
-
Save rupakg/5552544 to your computer and use it in GitHub Desktop.
PunchTab SSO auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'hashie' | |
require 'httparty' | |
require 'json' | |
#require 'punchtab/auth' | |
BASE_API_URL = 'https://api.punchtab.com/v1' | |
module Punchtab | |
class Auth | |
include HTTParty | |
base_uri BASE_API_URL | |
format :json | |
headers 'Referer' => 'http://www.webintellix.com' | |
def self.authenticate(options = {}) | |
@client_id = options[:client_id] # required | |
@access_key = options[:access_key] # required | |
@secret_key = options[:secret_key] # required | |
if @client_id && @access_key && @secret_key | |
user_data = [ | |
'id' => @client_id, | |
'first_name' => 'Rupak', | |
'last_name' => 'Ganguly', | |
'email' => 'rupakg@gmail.com' | |
] | |
time_stamp = Time.now.to_i | |
auth_request = Base64.encode64(JSON.dump(user_data)) | |
string_to_sign = "#{auth_request} #{time_stamp}" | |
hmac = OpenSSL::HMAC.new(@secret_key, OpenSSL::Digest::SHA1.new) | |
signature = hmac.update(string_to_sign).hexdigest | |
# make the POST call | |
path = '/auth/sso' | |
post_data = { | |
:client_id => @client_id, | |
:key => @access_key, | |
:auth_request => auth_request, | |
:timestamp => time_stamp, | |
:signature => signature | |
} | |
raw_response = Punchtab::Auth.post(path, :body => post_data) | |
response = Hashie::Mash.new(raw_response) | |
if response.status == 'connected' | |
return response | |
else | |
if response.error | |
puts "Raw Error Response: #{raw_response}" | |
raise Exception.new("Authentication Failed: '#{response.error.description}', Status: '#{response.status}'") | |
end | |
end | |
else | |
raise ArgumentError.new('Client Id, Access Key and Secret Key are required to authenticate, before using PunchTab services.') | |
end | |
end | |
end | |
class Client | |
def initialize(options = {}) | |
@client_id = options[:client_id] # required | |
@access_key = options[:access_key] # required | |
@secret_key = options[:secret_key] # required | |
response = Punchtab::Auth.authenticate(options) | |
puts "Successfully Authenticated: #{response}" unless response | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment