Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement SSO authentication, status and logout API calls.
- Loading branch information
Showing
8 changed files
with
255 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Changelog | ||
|
||
## 0.0.1 - 11/11/11 | ||
## 0.0.1 - 05/07/2013 | ||
* Initial version | ||
|
||
* Initial version |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,142 @@ | ||
require 'rubygems' | ||
require 'hashie' | ||
require 'httparty' | ||
require 'json' | ||
|
||
#require 'punchtab/auth' | ||
|
||
|
||
module Punchtab | ||
# Your code goes here... | ||
class API | ||
include HTTParty | ||
|
||
BASE_API_URL = 'https://api.punchtab.com/v1' | ||
|
||
base_uri BASE_API_URL | ||
format :json | ||
|
||
attr_reader :access_token | ||
|
||
def initialize(options = {}) | ||
@client_id = options[:client_id] # required | ||
@access_key = options[:access_key] # required | ||
@secret_key = options[:secret_key] # required | ||
@domain = options[:domain] # required | ||
@user_info = options[:user_info] # optional | ||
|
||
unless @client_id && @access_key && @secret_key && @domain | ||
raise Exception.new('Client Id, Access Key, Secret Key and Domain are required to authenticate, before using PunchTab services.') | ||
end | ||
end | ||
|
||
# https://api.punchtab.com/v1/auth/sso | ||
def authenticate | ||
# setup the user data structure | ||
user_data = {:id => @client_id} | ||
if @user_info | ||
user_data.merge!(@user_info) | ||
end | ||
|
||
# prepare authentication params | ||
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' | ||
Punchtab::API.headers 'Referer' => "http://#{@domain}" | ||
|
||
# setup the post params | ||
post_data = { | ||
:client_id => @client_id, | ||
:key => @access_key, | ||
:auth_request => auth_request, | ||
:timestamp => time_stamp, | ||
:signature => signature | ||
} | ||
raw_response = Punchtab::API.post(path, :body => post_data) | ||
response = Hashie::Mash.new(raw_response) | ||
if response.status == 'connected' | ||
@access_token = response.authResponse.accessToken | ||
return response | ||
else | ||
if response.error | ||
raise Exception.new(raw_response) | ||
end | ||
end | ||
end | ||
|
||
# https://api.punchtab.com/v1/auth/logout | ||
def logout | ||
# make the POST call | ||
path = '/auth/logout' | ||
Punchtab::API.headers 'Referer' => "http://#{@domain}" | ||
|
||
# setup the post params | ||
post_data = { | ||
:token => @access_token, | ||
:key => @access_key | ||
} | ||
raw_response = Punchtab::API.post(path, :body => post_data) | ||
response = Hashie::Mash.new(raw_response) | ||
if response.status == 'disconnected' | ||
return response | ||
else | ||
if response.error | ||
raise Exception.new(raw_response) | ||
end | ||
end | ||
end | ||
|
||
# https://api.punchtab.com/v1/auth/status | ||
def status | ||
# make the POST call | ||
path = '/auth/status' | ||
Punchtab::API.headers 'Referer' => "http://#{@domain}" | ||
|
||
# setup the post params | ||
post_data = { | ||
:token => @access_token, | ||
:key => @access_key | ||
} | ||
raw_response = Punchtab::API.post(path, :body => post_data) | ||
response = Hashie::Mash.new(raw_response) | ||
if response.status == 'connected' | ||
return response | ||
else | ||
if response.error | ||
raise Exception.new(raw_response) | ||
end | ||
end | ||
end | ||
end | ||
|
||
class Client | ||
|
||
attr_reader :access_token | ||
|
||
def initialize(options = {}) | ||
# initialize the API | ||
@api = Punchtab::API.new(options) | ||
|
||
# authenticate | ||
response = @api.authenticate | ||
if response | ||
# get the access token | ||
@access_token = @api.access_token | ||
end | ||
|
||
end | ||
|
||
def status | ||
@api.status | ||
end | ||
|
||
def logout | ||
@api.logout | ||
end | ||
|
||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#module Punchtab | ||
# class Auth | ||
# include HTTParty | ||
# | ||
# base_uri BASE_API_URL | ||
# format :json | ||
# | ||
# 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 | ||
# } | ||
# response = Hashie::Mash.new(Punchtab::Auth.post(path, :body => post_data)) | ||
# if response.status == 'connected' | ||
# response.authResponse.accessToken | ||
# else | ||
# if response.error | ||
# 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 | ||
#end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Punchtab | ||
VERSION = "0.0.1" | ||
VERSION = '0.0.1' | ||
end |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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