mirror of
https://github.com/omniauth/omniauth.git
synced 2022-11-09 12:31:49 -05:00
Adds Instapaper and xAuth strategies. Closes #213
This commit is contained in:
parent
a29a5e6d1f
commit
f87f3fc772
4 changed files with 111 additions and 1 deletions
|
@ -15,7 +15,7 @@ To install OmniAuth, simply install the gem:
|
|||
|
||||
OmniAuth currently supports the following external providers:
|
||||
|
||||
* via OAuth
|
||||
* via OAuth (OAuth 1.0, OAuth 2, and xAuth)
|
||||
* 37signals ID
|
||||
* Bit.ly (credit: [philnash](https://github.com/philnash))
|
||||
* Dopplr (credit: [flextrip](https://github.com/flextrip))
|
||||
|
@ -41,6 +41,7 @@ OmniAuth currently supports the following external providers:
|
|||
* Mixi (credit: [kiyoshi](https://github.com/kiyoshi))
|
||||
* Evernote (credit: [szimek](https://github.com/szimek))
|
||||
* Doit.im (credit: [chouti](https://github.com/chouti))
|
||||
* Instapaper (credit: [micpringle](https://github.com/micpringle))
|
||||
* Flickr (credit: [pchilton](https://github.com/pchilton))
|
||||
* OpenID
|
||||
* Google Apps (via OpenID)
|
||||
|
|
|
@ -4,6 +4,7 @@ module OmniAuth
|
|||
module Strategies
|
||||
autoload :OAuth, 'omniauth/strategies/oauth'
|
||||
autoload :OAuth2, 'omniauth/strategies/oauth2'
|
||||
autoload :XAuth, 'omniauth/strategies/xauth'
|
||||
|
||||
autoload :Twitter, 'omniauth/strategies/twitter'
|
||||
autoload :LinkedIn, 'omniauth/strategies/linked_in'
|
||||
|
@ -33,5 +34,6 @@ module OmniAuth
|
|||
autoload :Mixi, 'omniauth/strategies/mixi'
|
||||
autoload :Evernote, 'omniauth/strategies/evernote'
|
||||
autoload :Doit, 'omniauth/strategies/doit'
|
||||
autoload :Instapaper, 'omniauth/strategies/instapaper'
|
||||
end
|
||||
end
|
||||
|
|
40
oa-oauth/lib/omniauth/strategies/instapaper.rb
Normal file
40
oa-oauth/lib/omniauth/strategies/instapaper.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
require 'omniauth/oauth'
|
||||
require 'multi_json'
|
||||
|
||||
module OmniAuth
|
||||
module Strategies
|
||||
class Instapaper < OmniAuth::Strategies::XAuth
|
||||
|
||||
def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
|
||||
client_options = {
|
||||
:title => 'Instapaper',
|
||||
:site => 'https://www.instapaper.com',
|
||||
:access_token_path => '/api/1/oauth/access_token'
|
||||
}
|
||||
super(app, :instapaper, consumer_key, consumer_secret, client_options, options, &block)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def user_data
|
||||
@data ||= MultiJson.decode(@access_token.get('/api/1/account/verify_credentials').body)[0]
|
||||
end
|
||||
|
||||
def user_info
|
||||
{
|
||||
'nickname' => user_data['username'],
|
||||
'name' => user_data['username']
|
||||
}
|
||||
end
|
||||
|
||||
def auth_hash
|
||||
OmniAuth::Utils.deep_merge(super, {
|
||||
'uid' => user_data['user_id'],
|
||||
'user_info' => user_info
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
67
oa-oauth/lib/omniauth/strategies/xauth.rb
Normal file
67
oa-oauth/lib/omniauth/strategies/xauth.rb
Normal file
|
@ -0,0 +1,67 @@
|
|||
require 'omniauth/oauth'
|
||||
require 'multi_json'
|
||||
|
||||
module OmniAuth
|
||||
module Strategies
|
||||
class XAuth
|
||||
include OmniAuth::Strategy
|
||||
|
||||
attr_reader :name
|
||||
attr_accessor :consumer_key, :consumer_secret, :consumer_options
|
||||
|
||||
def initialize(app, name, consumer_key = nil, consumer_secret = nil, consumer_options = {}, options = {}, &block)
|
||||
self.consumer_key = consumer_key
|
||||
self.consumer_secret = consumer_secret
|
||||
self.consumer_options = consumer_options
|
||||
super
|
||||
end
|
||||
|
||||
def request_phase
|
||||
session['oauth'] ||= {}
|
||||
if env['REQUEST_METHOD'] == 'GET'
|
||||
get_credentials
|
||||
else
|
||||
session['omniauth.xauth'] = { 'x_auth_mode' => 'client_auth', 'x_auth_username' => request['username'], 'x_auth_password' => request['password'] }
|
||||
redirect callback_path
|
||||
end
|
||||
end
|
||||
|
||||
def get_credentials
|
||||
OmniAuth::Form.build(consumer_options[:title] || "xAuth Credentials") do
|
||||
text_field 'Username', 'username'
|
||||
password_field 'Password', 'password'
|
||||
end.to_response
|
||||
end
|
||||
|
||||
def consumer
|
||||
::OAuth::Consumer.new(consumer_key, consumer_secret, consumer_options.merge(options[:client_options] || options[:consumer_options] || {}))
|
||||
end
|
||||
|
||||
def callback_phase
|
||||
@access_token = consumer.get_access_token(nil, {}, session['omniauth.xauth'])
|
||||
super
|
||||
rescue ::Net::HTTPFatalError => e
|
||||
fail!(:service_unavailable, e)
|
||||
rescue ::OAuth::Unauthorized => e
|
||||
fail!(:invalid_credentials, e)
|
||||
rescue ::MultiJson::DecodeError => e
|
||||
fail!(:invalid_response, e)
|
||||
ensure
|
||||
session['omniauth.xauth'] = nil
|
||||
end
|
||||
|
||||
def auth_hash
|
||||
OmniAuth::Utils.deep_merge(super, {
|
||||
'credentials' => {
|
||||
'token' => @access_token.token,
|
||||
'secret' => @access_token.secret
|
||||
}, 'extra' => {
|
||||
'access_token' => @access_token
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in a new issue