Created
February 16, 2017 05:38
-
-
Save jimmyz/5da294ce275c3aaad93ee338b0045b39 to your computer and use it in GitHub Desktop.
Omniauth Example
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 'omniauth-oauth2' | |
require 'faraday' | |
module OmniAuth | |
module Strategies | |
class FamilySearch < OmniAuth::Strategies::OAuth2 | |
option :client_options, { | |
:site => 'https://ident.familysearch.org', | |
:authorize_url => '/cis-web/oauth2/v3/authorization', | |
:token_url => '/cis-web/oauth2/v3/token' | |
} | |
option :api_site, "https://familysearch.org" | |
def request_phase | |
super | |
end | |
def authorize_params | |
super | |
end | |
uid { user_info['id'] } | |
info do | |
{ | |
:name => user_name, | |
:email => user_email | |
} | |
end | |
extra do | |
{ :raw_info => raw_info } | |
end | |
def raw_info | |
@raw_info ||= get_user_info | |
end | |
def callback_url | |
full_host + script_name + callback_path | |
end | |
def user_info | |
@user_info ||= raw_info['users'] ? raw_info['users'].first : {} | |
end | |
def user_name | |
user_info['contactName'] | |
end | |
def user_email | |
user_info['email'] | |
end | |
protected | |
def get_user_info | |
conn = Faraday.new(:url => options.api_site) | |
response = conn.get do |req| | |
req.url '/platform/users/current' | |
req.headers['Accept'] = 'application/x-fs-v1+json' | |
req.headers['Authorization'] = "Bearer #{access_token.token}" | |
end | |
return JSON.parse response.body | |
end | |
end | |
end | |
end | |
OmniAuth.config.add_camelization 'familysearch', 'FamilySearch' |
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
# frozen_string_literal: true | |
source "https://rubygems.org" | |
# gem "rails" | |
gem 'sinatra', '~> 1.4', '>= 1.4.7' | |
gem 'omniauth-familysearch' |
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
# myapp.rb | |
require 'sinatra' | |
require 'omniauth' | |
require 'omniauth-familysearch' | |
use Rack::Session::Cookie | |
use OmniAuth::Builder do | |
provider :familysearch, 'a0T3000000BfM3mEAF', '', | |
:client_options => { :site => 'https://identbeta.familysearch.org' }, | |
:api_site => "https://beta.familysearch.org" | |
end | |
get '/' do | |
'<a href="/auth/familysearch">Login</a>' | |
end | |
get '/auth/:name/callback' do | |
auth = request.env['omniauth.auth'] | |
"#{auth.uid}, #{auth.info.email} #{auth.info.name} #{auth.credentials.token}" | |
# do whatever you want with the information! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment