Created
September 20, 2012 19:50
-
-
Save eedrummer/3757955 to your computer and use it in GitHub Desktop.
Sinatra-based OAuth 2 client (I don't remember where I found this...)
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 'sinatra' | |
require 'rack/oauth2' | |
require 'json' | |
require 'json/jwt' | |
def client | |
# app id, app secret, and site are all test-env specific | |
Rack::OAuth2::Client.new( | |
:identifier => 'd7633f880b4771b4f91f107a1a27835e', | |
:secret => '40dc03bf175980bccd15df10f9db1aa2', | |
:redirect_uri => redirect_uri, | |
:scheme => 'http', | |
:host => 'growing-spring-4857.herokuapp.com', | |
:token_endpoint => '/oauth2/token' | |
) | |
end | |
get '/' do | |
'working' | |
end | |
get '/logout' do | |
response.set_cookie 'access_token', '' | |
'logged out' | |
end | |
# get '/auth' do | |
# redirect client.web_server.authorize_url( | |
# :redirect_uri => redirect_uri | |
# ) | |
# end | |
# get '/auth/callback' do | |
# access_token = client.web_server.get_access_token params[:code], :redirect_uri => redirect_uri | |
# user = JSON.parse access_token.get('/current_user.json') | |
# user.inspect | |
# end | |
get '/access' do | |
c = client | |
c.resource_owner_credentials = params[:username], params[:password] | |
access_token = c.access_token! | |
access_token.get('http://growing-spring-4857.herokuapp.com/records/1/root.xml').body | |
JSON::JWT.decode(access_token.access_token) | |
end | |
def redirect_uri | |
uri = URI.parse(request.url) | |
uri.path = '/auth/callback' | |
uri.query = nil | |
uri.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment