Skip to content

Commit

Permalink
Implement SSO authentication, status and logout API calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
rupakg committed May 10, 2013
1 parent 84efc65 commit de07d71
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 15 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
@@ -1,5 +1,5 @@
# Changelog

## 0.0.1 - 11/11/11
## 0.0.1 - 05/07/2013
* Initial version

* Initial version
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2011 Rupak Ganguly
Copyright (c) 2013 Rupak Ganguly

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
50 changes: 47 additions & 3 deletions README.md
@@ -1,10 +1,54 @@
# punchtab

Ruby wrapper for the [PunchTab](http://www.punchtab.com/developer-docs) API, the world's first instant loyalty platform.
Ruby wrapper for [PunchTab API](http://www.punchtab.com/developer-docs), the world's first instant loyalty platform.

## Installation

gem install punchtab
```
gem install punchtab
```

## Pre-requistes

1. Get a developer account at [PunchTab](http://www.punchtab.com).
2. Make sure your PunchTab account is enabled for SSO authentication. Do so, by going to the developer account page,
and checking the 'Single Sign On (SSO)' checkbox.

![Enable SSO authentication for PunchTab account](punchtab-enable-sso.png "Enable SSO authentication for PunchTab account")

## Getting Started

1. Authenticate using Single Sign On (SSO):

```ruby
# authenticate with PunchTab
client = Punchtab::Client.new(
:client_id => 'your client_id',
:access_key => 'your access_key',
:secret_key => 'your secret_key',
:domain => 'www.mydomain.com',
:user_info => {
:first_name => 'Rupak',
:last_name => 'Ganguly',
:email => 'me@domain.com'}
)
# if authentication is successful, you should get an access token back
puts "Access Token: #{client.access_token}"

# if authentication fails, an exception is thrown

```
> Note: You can get all of the above values from your Punchtab [developer account page](https://www.punchtab.com/account/).
2. Call other API methods:

```
# check status
client.status
# logout
client.logout
```

## Features

Expand All @@ -23,4 +67,4 @@ Ruby wrapper for the [PunchTab](http://www.punchtab.com/developer-docs) API, the

## Copyright

Copyright (c) 2011 [Rupak Ganguly](http://rails.webintellix.com). See LICENSE for details.
Copyright (c) 2013 [Rupak Ganguly](http://rails.webintellix.com). See [LICENSE](https://github.com/rupakg/punchtab/blob/master/LICENSE) for details.
139 changes: 138 additions & 1 deletion lib/punchtab.rb
@@ -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
49 changes: 49 additions & 0 deletions lib/punchtab/auth.rb
@@ -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
2 changes: 1 addition & 1 deletion lib/punchtab/version.rb
@@ -1,3 +1,3 @@
module Punchtab
VERSION = "0.0.1"
VERSION = '0.0.1'
end
Binary file added punchtab-enable-sso.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 17 additions & 7 deletions punchtab.gemspec
@@ -1,14 +1,14 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "punchtab/version"
$:.push File.expand_path('../lib', __FILE__)
require 'punchtab/version'

Gem::Specification.new do |s|
s.name = "punchtab"
s.name = 'punchtab'
s.version = Punchtab::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Rupak Ganguly"]
s.date = %q{2011-11-11}
s.email = ["rupakg@gmail.com"]
s.authors = ['Rupak Ganguly']
s.date = %q{2013-05-07}
s.email = ['rupakg@gmail.com']
s.homepage = %q{http://github.com/rupakg/ruby-punchtab}
s.summary = %q{Ruby wrapper for PunchTab API}
s.description = %q{Ruby wrapper for PunchTab API. PunchTab is the world's first instant loyalty platform.}
Expand All @@ -17,5 +17,15 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.require_paths = ['lib']

s.add_runtime_dependency('json', '~> 1.7.7')
s.add_runtime_dependency('hashie', '~> 2.0')
s.add_runtime_dependency('httparty', '~> 0.11')

#s.add_development_dependency(%q<shoulda>, ['>= 2.10.1'])
#s.add_development_dependency(%q<jnunemaker-matchy>, ['= 0.4.0'])
#s.add_development_dependency(%q<mocha>, ['~> 0.9.12'])
#s.add_development_dependency(%q<fakeweb>, ['~> 1.3.0'])

end

0 comments on commit de07d71

Please sign in to comment.