Forked from herestomwiththeweather/oauth2_mac_client_example.rb
Created
October 31, 2016 17:15
-
-
Save zeroc0d3/498ceb6e98dd7e254f3c5310af5dd478 to your computer and use it in GitHub Desktop.
Example code to make hmac signed oauth2 requests
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 'uri' | |
require 'cgi' | |
require 'net/https' | |
require 'json' | |
require 'oauth2_mac_client' | |
# oauth2_mac_client gem at https://github.com/herestomwiththeweather/oauth2_mac_client | |
# usage using this test class: | |
# > client = OAuth2Client.new | |
# > client.get_token('refresh_token') # get a new MAC type access token | |
# > client.list_payments # GET | |
# > client.test_payment # POST | |
class OAuth2Client | |
attr_accessor :refresh_token, :access_token, :access_secret, :issued_at | |
def initialize | |
@access_token = "" | |
@access_secret = "" | |
@issued_at = nil | |
@refresh_token = refresh_token | |
end | |
def provider_url | |
'https://evening-ocean-4975.herokuapp.com' | |
#ENV['OAUTH_PROVIDER_URL'] || "" | |
end | |
def client_id | |
'Y0UrvQT9mhmlr8mwsAV09w==' | |
#ENV['OAUTH_CLIENT_ID'] || "" | |
end | |
def client_secret | |
'OwGJBrKY23dbEnpo5wGPsTz5N57htGIFn5/yqEO+55F7dReK0bK3kpj/lMk3gq7LpKiU0rsTXXd4L24InXiolQ==' | |
#ENV['OAUTH_CLIENT_SECRET'] || "" | |
end | |
def code | |
ENV['OAUTH_CLIENT_CODE'] || "" | |
end | |
def refresh_token | |
'nKuK5wZTkHkgPZ9mhS7l9G4n0SpJi8UDG0jxqy5fKOA5yMDST7jjr8hVitfrzhEjXiygeW3I14sNxmA85CU9dg==' || '' | |
#ENV['OAUTH_CLIENT_REFRESH_TOKEN'] || "" | |
end | |
def redirect_uri | |
'http://ubuntu.local:3001/oauth/callback' | |
#ENV['OAUTH_REDIRECT_URI'] || "" | |
end | |
def test_payment | |
access_token=Oauth2MacClient::Token.new(access_token:@access_token,mac_key:@access_secret,mac_algorithm:'hmac-sha-256',issued_at:@issued_at) | |
payment_url=provider_url+'/transacts/credits' | |
uri=URI.parse(payment_url) | |
http=Net::HTTP.new(uri.host,uri.port) | |
http.set_debug_output(Logger.new(STDOUT)) | |
http.use_ssl=true | |
http.ca_path = '/etc/ssl/certs' | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
body={to:'[email protected]',amount:12.50,note:'guitar lessons'} | |
request=Net::HTTP::Post.new(uri.request_uri) | |
request.set_form_data(body) | |
request['Authorization']=access_token.construct_authorization_header(payment_url,'post',request.body) | |
request['Accept']='application/json' | |
response=http.request(request) | |
end | |
def list_payments | |
access_token=Oauth2MacClient::Token.new(access_token:@access_token,mac_key:@access_secret,mac_algorithm:'hmac-sha-256',issued_at:@issued_at) | |
payment_url=provider_url+'/transacts/credits' | |
uri=URI.parse(payment_url) | |
http=Net::HTTP.new(uri.host,uri.port) | |
http.set_debug_output(Logger.new(STDOUT)) | |
http.use_ssl=true | |
http.ca_path = '/etc/ssl/certs' | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
request=Net::HTTP::Get.new(uri.request_uri) | |
request['Authorization']=access_token.construct_authorization_header(payment_url,'get') | |
request['Accept']='application/json' | |
response=http.request(request) | |
end | |
def code_or_refresh_token(grant_type) | |
if('authorization_code'==grant_type) | |
"code=#{CGI::escape code}" | |
else # refresh_token | |
"refresh_token=#{CGI::escape refresh_token}" | |
end | |
end | |
def get_token(grant_type) | |
u = URI.parse(provider_url) | |
http = ::Net::HTTP.new(u.host,u.port) | |
http.use_ssl = true | |
http.ca_path = '/etc/ssl/certs' | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
body = '' | |
body += "client_id=#{CGI::escape client_id}" | |
body += "&client_secret=#{CGI::escape client_secret}" | |
body += "&#{code_or_refresh_token(grant_type)}" | |
body += "&grant_type=#{grant_type}" | |
body += "&redirect_uri=#{CGI::escape redirect_uri}" | |
response = http.post('/oauth/token',body,"Content-Type" => "application/x-www-form-urlencoded") | |
@body = JSON.parse(response.body) | |
if @body['error'].nil? | |
@access_secret = '' | |
@access_token = @body['access_token'] | |
if 'mac' == @body['token_type'] | |
@access_secret = @body['mac_key'] | |
end | |
log_tokens | |
@issued_at = Time.now | |
if('authorization_code'==grant_type) | |
@refresh_token = @body['refresh_token'] | |
end | |
else | |
puts "***#{@body['error']}*** #{@body['error_description']}" | |
false | |
end | |
end | |
def log_tokens | |
token_type = @body['token_type'] | |
expires_in = @body['expires_in'] | |
puts "[#{token_type}] token expires in #{expires_in} sec" | |
puts "access_token: #{@access_token}" | |
if 'mac' == @body['token_type'] | |
puts "access_secret: #{@access_secret}" | |
puts "mac algorithm: #{@body['mac_algorithm']}" | |
end | |
puts "refresh_token: #{@refresh_token}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment