Created
October 3, 2013 14:17
-
-
Save dschneider/6810568 to your computer and use it in GitHub Desktop.
A small Sinatra app that can be used to catch a service provider callback (e. g. from Facebook, Twitter or Google) using OmniAuth.
This file contains hidden or 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 'omniauth' | |
# A small Sinatra app that can be used to catch a service provider | |
# callback (e. g. from Facebook, Twitter or Google) using OmniAuth. | |
# It can be mounted in the routes. This Sinatra app can be useful | |
# if you have a Rails API app without any cookies / sessions. | |
# | |
# Examples | |
# | |
# # Mount the app in Rails' routes file | |
# mount ProviderCallbackApp, :at => "/" | |
# | |
# # This will catch anything that goes to: | |
# # * /auth/:provider/callback # :provider could be 'facebook' | |
# # * /auth/failure | |
# # (see Sinatra routes below) | |
# | |
# It's strongly coupled with OmniAuth. If you route to the | |
# '/auth/:provider/callback' URL the OmniAuth middleware will kick in and | |
# provide you with the provider details. | |
class ProviderCallbackApp < Sinatra::Base | |
use Rack::Session::Cookie | |
use OmniAuth::Builder do | |
provider :facebook, 'APP_ID', 'APP_SECRET', scope: 'APP_SCOPE' | |
end | |
# Points to '/auth/failure' (overwrites thrown exception in | |
# development mode). | |
OmniAuth.config.on_failure = Proc.new do |env| | |
if env['omniauth.error'] | |
error_type = CGI::escape(env['omniauth.error'].error) | |
error_reason = CGI::escape(env['omniauth.error'].error_reason) | |
endpoint = | |
"/auth/failure?error_type=#{error_type}&error_reason=#{error_reason}" | |
else | |
endpoint = '/auth/failure' | |
end | |
[302, {'Location' => endpoint, 'Content-Type'=> 'text/html'}, []] | |
end | |
# Catches the callback from a service provider. Details can | |
# then be used to create a user for example or save the service | |
# credentials in the database. | |
# | |
# Returns the user's token in a JSON object. | |
get "/auth/:provider/callback" do | |
if omniauth_details = request.env["omniauth.auth"] | |
# TODO: Your app magic goes here! | |
content_type :json | |
{ data: 'GOES HERE' }.to_json | |
else | |
content_type :json | |
status 400 | |
{ errors: { message: 'No valid provider authentication given' } }.to_json | |
end | |
end | |
# This endpoint will be called if there is an error on the | |
# provider side (app access is denied or token is expired). | |
# | |
# Returns JSON object with error message. | |
get "/auth/failure" do | |
content_type :json | |
status 400 | |
{ | |
errors: { | |
type: params[:error_type], | |
message: (params[:message] || params[:error_reason]) | |
} | |
}.to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment