Skip to content

Instantly share code, notes, and snippets.

@nacx
Last active December 20, 2018 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nacx/8581621 to your computer and use it in GitHub Desktop.
Save nacx/8581621 to your computer and use it in GitHub Desktop.
Example webapp that uses OAuth to access the Abiquo API
#!/usr/bin/env python
from flask import Flask, redirect, request, session, url_for
from functools import wraps
from requests_oauthlib import OAuth1Session
import json
import os
# Application tokens (returned when the user registers an app)
API_HOST = '192.168.1.187'
API_KEY = 'a740ffb0-ba0a-4fab-a263-9bd909c311b7'
API_SECRET = 'CtI1VTfr6nTQqjPCwsmKUy9IRvCYxGZ8yvArdOUA'
# OAuth endpoints
API = 'http://%s/api' % API_HOST
REQUEST_TOKEN = '%s/oauth/request_token' % API
ACCESS_TOKEN = '%s/oauth/access_token' % API
AUTH_URL = 'http://%s/ui/#/authorize' % API_HOST
app = Flask(__name__)
_oauth_session = None
def protected(f):
@wraps(f)
def decorated(*args, **kwargs):
oauth = None
if 'access_token' in session:
token = session['access_token']
oauth = OAuth1Session(API_KEY, API_SECRET, token['oauth_token'], token['oauth_token_secret'])
kwargs['oauth'] = oauth
return f(*args, **kwargs)
return decorated
@app.route("/")
@protected
def index(oauth=None):
if not oauth:
return redirect(url_for('authorize'))
# Access a protected resource using the access token
headers = {'Accept': 'application/vnd.abiquo.user+json'}
usr = oauth.get(API + "/login", headers=headers).json()
return "<pre>%s</pre>" % json.dumps(usr, indent=4)
@app.route("/authorize")
def authorize():
# Request an OAuth token and the authorization url
global _oauth_session
_oauth_session = OAuth1Session(API_KEY, API_SECRET, callback_uri=url_for('callback', _external=True))
keys = _oauth_session.fetch_request_token(REQUEST_TOKEN)
auth_url = '%s?oauth_token=%s' % (AUTH_URL, keys.get('oauth_token'))
# Redirect to the provider to authorize the token. As it is an unauthenticated request,
# the provider will ask for the credentials and redirect back to the configured callback
# once the application has been authorized
return redirect(auth_url)
@app.route("/callback")
def callback():
# Parse the authorization response to extract the oauth_verifier
res = _oauth_session.parse_authorization_response(request.url)
# Request the access token that can be used to access the protected resources
session['access_token'] = _oauth_session.fetch_access_token(ACCESS_TOKEN)
return redirect(url_for('index'))
if __name__ == "__main__":
app.secret_key = os.urandom(24)
app.run(debug=True)
require 'json'
require 'rubygems'
require 'securerandom'
require 'sinatra'
require 'oauth'
configure do
# Application tokens (returned when the user registers an app)
API_HOST = '192.168.1.187'
API_KEY = 'a740ffb0-ba0a-4fab-a263-9bd909c311b7'
API_SECRET = 'CtI1VTfr6nTQqjPCwsmKUy9IRvCYxGZ8yvArdOUA'
enable :sessions
set :session_secret, SecureRandom.hex(24)
end
before do
session[:oauth] ||= {}
# Configure the OAuth endpoints
@consumer ||= OAuth::Consumer.new(API_KEY, API_SECRET, {
:site => "http://#{API_HOST}/api",
:request_token_path => "/oauth/request_token",
:access_token_path => "/oauth/access_token",
:authorize_path => "http://#{API_HOST}/ui/#/authorize"
})
if !session[:oauth][:request_token_secret].nil?
@request_token = OAuth::RequestToken.new(@consumer, session[:oauth][:request_token], session[:oauth][:request_token_secret])
end
if !session[:oauth][:access_token_secret].nil?
@access_token = OAuth::AccessToken.new(@consumer, session[:oauth][:access_token], session[:oauth][:access_token_secret])
end
end
get '/' do
if !@access_token
return redirect to('/authorize')
end
# Access a protected resource using the access token
headers = {'Accept' => 'application/vnd.abiquo.user+json'}
user = @access_token.get('/login', headers)
"<pre>#{JSON.pretty_generate(JSON.parse(user.body))}</pre>"
end
get '/authorize' do
# Request an OAuth token and the authorization url
@request_token = @consumer.get_request_token(:oauth_callback => to('/callback'))
session[:oauth][:request_token] = @request_token.token
session[:oauth][:request_token_secret] = @request_token.secret
# Redirect to the provider to authorize the token. As it is an unauthenticated request,
# the provider will ask for the credentials and redirect back to the configured callback
# once the application has been authorized
redirect @consumer.authorize_path + '?oauth_token=' + @request_token.token
end
get '/callback' do
# Request the access token that can be used to access the protected resources
@access_token = @request_token.get_access_token(:oauth_verifier => "#{params[:oauth_verifier]}")
session[:oauth][:access_token] = @access_token.token
session[:oauth][:access_token_secret] = @access_token.secret
redirect to('/')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment