Skip to content

Instantly share code, notes, and snippets.

@chirauki
Last active July 11, 2017 10:27
Show Gist options
  • Save chirauki/2453b7a6d6429fa54dd68c098933cee7 to your computer and use it in GitHub Desktop.
Save chirauki/2453b7a6d6429fa54dd68c098933cee7 to your computer and use it in GitHub Desktop.
OAuth app creation and authorisation
require 'abiquo-api'
require 'json'
require 'oauth'
require 'uri'
require 'faraday'
def client(api_url, creds={})
connection_params = {
:abiquo_api_url => api_url,
:connection_options => {
:ssl => {
:verify => false
}
}
}
credentials = {}
if creds.has_key? :abiquo_username
# Basic Auth
credentials = {
:abiquo_username => creds[:abiquo_username],
:abiquo_password => creds[:abiquo_password]
}
elsif creds.has_key? :abiquo_api_key
# OAuth App
credentials = {
:abiquo_api_key => creds[:abiquo_api_key],
:abiquo_api_secret => creds[:abiquo_api_secret],
:abiquo_token_key => creds[:abiquo_token_key],
:abiquo_token_secret => creds[:abiquo_token_secret]
}
end
connection_params.merge! credentials
return AbiquoAPI.new(connection_params)
end
def register_app(api_url, creds={}, app_name)
abq = client(api_url, creds)
user_info_lnk = AbiquoAPI::Link.new :href => '/api/login',
:type => 'application/vnd.abiquo.user+json',
:client => abq
user_info = user_info_lnk.get
apps_lnk = user_info.link(:applications)
app_payload = { 'name' => app_name }
abq.post(apps_lnk, app_payload.to_json, :accept => 'application/vnd.abiquo.application+json',
:content => 'application/vnd.abiquo.application+json')
end
def get_access_token(site, app, creds={})
consumer = OAuth::Consumer.new(app.apiKey, app.apiSecret, :site => site)
request_token = consumer.get_request_token
connection = Faraday.new(:url => request_token.authorize_url, :ssl => { :verify => false } ) do |c|
if creds[:abiquo_api_key]
credentials = {
:consumer_key => creds[:abiquo_api_key],
:consumer_secret => creds[:abiquo_api_secret],
:token => creds[:abiquo_token_key],
:token_secret => creds[:abiquo_token_secret]
}
c.request :oauth, credentials
elsif creds[:abiquo_username]
c.basic_auth(creds[:abiquo_username], creds[:abiquo_password])
elsif creds[:access_token]
c.authorization :Bearer, creds[:access_token]
end
c.adapter :excon
end
resp = connection.run_request(:get, request_token.authorize_url, "", {})
location = resp.headers['location']
verifier = location.scan(/oauth_verifier=.*/)[0].split("=").last
access_token = request_token.get_access_token(:oauth_verifier => verifier)
return access_token.token, access_token.secret
end
API_URL = "https://chirauki401.bcn.abiquo.com/api"
API_USER = "admin"
API_PASS = "xabiquo"
APP_NAME = "TEST-#{Time.now.to_i}"
basic_auth_creds = {
:abiquo_username => API_USER,
:abiquo_password => API_PASS
}
app = register_app(API_URL, basic_auth_creds, APP_NAME)
token, secret = get_access_token(API_URL, app, basic_auth_creds)
puts "App #{APP_NAME}"
puts "App Key: #{app.apiKey}"
puts "App Secret: #{app.apiSecret}"
puts "Access token: #{token}"
puts "Access token secret: #{secret}"
oauth_creds = {
:abiquo_api_key => app.apiKey,
:abiquo_api_secret => app.apiSecret,
:abiquo_token_key => token,
:abiquo_token_secret => secret
}
NEW_APP_NAME = "OAUTH-#{APP_NAME}"
app2 = register_app(API_URL, oauth_creds, NEW_APP_NAME)
token2, secret2 = get_access_token(API_URL, app, oauth_creds)
puts "App #{NEW_APP_NAME}"
puts "App Key: #{app2.apiKey}"
puts "App Secret: #{app2.apiSecret}"
puts "Access token: #{token2}"
puts "Access token secret: #{secret2}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment