1
0
Fork 0
mirror of https://github.com/omniauth/omniauth.git synced 2022-11-09 12:31:49 -05:00

Everything can now be initialized with a block instead of arguments. Closes #56

This commit is contained in:
Michael Bleigh 2010-11-29 12:59:20 -06:00
parent 4ec1060961
commit c9f3375a18
25 changed files with 64 additions and 47 deletions

View file

@ -6,7 +6,7 @@ module OmniAuth
class HttpBasic class HttpBasic
include OmniAuth::Strategy include OmniAuth::Strategy
def initialize(app, name, endpoint, headers = {}) def initialize(app, name, endpoint = nil, headers = {}, &block)
super super
@endpoint = endpoint @endpoint = endpoint
@request_headers = headers @request_headers = headers

View file

@ -6,10 +6,10 @@ module OmniAuth
class Password class Password
include OmniAuth::Strategy include OmniAuth::Strategy
def initialize(app, secret = 'changethisappsecret', options = {}) def initialize(app, secret = 'changethisappsecret', options = {}, &block)
@options = options @options = options
@secret = secret @secret = secret
super(app, :password) super(app, :password, &block)
end end
attr_reader :secret attr_reader :secret

View file

@ -11,10 +11,12 @@ module OmniAuth
end end
end end
def initialize(app, name, *args) def initialize(app, name, *args, &block)
@app = app @app = app
@name = name.to_sym @name = name.to_sym
@options = args.last.is_a?(Hash) ? args.pop : {} @options = args.last.is_a?(Hash) ? args.pop : {}
yield self if block_given?
end end
def call(env) def call(env)

View file

@ -29,7 +29,7 @@ Use the LDAP strategy as a middleware in your applicaiton:
require 'omniauth/enterprise' require 'omniauth/enterprise'
use OmniAuth::Strategies::LDAP, use OmniAuth::Strategies::LDAP,
"My LDAP", :title => "My LDAP",
:host => '10.101.10.1', :host => '10.101.10.1',
:port => 389, :port => 389,
:method => :plain, :method => :plain,

View file

@ -8,8 +8,8 @@ module OmniAuth
autoload :Configuration, 'omniauth/strategies/cas/configuration' autoload :Configuration, 'omniauth/strategies/cas/configuration'
autoload :ServiceTicketValidator, 'omniauth/strategies/cas/service_ticket_validator' autoload :ServiceTicketValidator, 'omniauth/strategies/cas/service_ticket_validator'
def initialize(app, options = {}) def initialize(app, options = {}, &block)
super(app, options.delete(:name) || :cas, options) super(app, options.delete(:name) || :cas, options, &block)
@configuration = OmniAuth::Strategies::CAS::Configuration.new(options) @configuration = OmniAuth::Strategies::CAS::Configuration.new(options)
end end

View file

@ -27,9 +27,8 @@ module OmniAuth
# #
# @param [Rack Application] app Standard Rack middleware argument. # @param [Rack Application] app Standard Rack middleware argument.
# @option options [String, 'LDAP Authentication'] :title A title for the authentication form. # @option options [String, 'LDAP Authentication'] :title A title for the authentication form.
def initialize(app, title, options = {}) def initialize(app, options = {}, &block)
super(app, options[:name] || :ldap, options.dup) super(app, options[:name] || :ldap, options.dup, &block)
@title = title
@name_proc = (@options.delete(:name_proc) || Proc.new {|name| name}) @name_proc = (@options.delete(:name_proc) || Proc.new {|name| name})
@adaptor = OmniAuth::Strategies::LDAP::Adaptor.new(options) @adaptor = OmniAuth::Strategies::LDAP::Adaptor.new(options)
end end

View file

@ -30,6 +30,6 @@ If you want to allow multiple providers, use the OmniAuth Builder:
use OmniAuth::Builder do use OmniAuth::Builder do
provider :twitter, 'consumer_key', 'consumer_secret' provider :twitter, 'consumer_key', 'consumer_secret'
provider :facebook, 'app_id', 'app_secret' provider :facebook, 'client_id', 'client_secret'
end end

View file

@ -13,7 +13,7 @@ module OmniAuth
# Initialize the Dopplr strategy. # Initialize the Dopplr strategy.
# #
# @option options [Hash, {}] :client_options Options to be passed directly to the OAuth Consumer # @option options [Hash, {}] :client_options Options to be passed directly to the OAuth Consumer
def initialize(app, consumer_key, consumer_secret, options = {}) def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
client_options = { client_options = {
:site => 'https://www.dopplr.com', :site => 'https://www.dopplr.com',
:request_token_path => "/oauth/request_token", :request_token_path => "/oauth/request_token",
@ -21,7 +21,7 @@ module OmniAuth
:authorize_path => "/oauth/authorize" :authorize_path => "/oauth/authorize"
} }
super(app, :dopplr, consumer_key, consumer_secret, client_options, options) super(app, :dopplr, consumer_key, consumer_secret, client_options, options, &block)
end end
def user_data def user_data

View file

@ -7,14 +7,14 @@ module OmniAuth
# basic user information. # basic user information.
# #
# @example Basic Usage # @example Basic Usage
# use OmniAuth::Strategies::Facebook, 'app_id', 'app_secret' # use OmniAuth::Strategies::Facebook, 'client_id', 'client_secret'
class Facebook < OAuth2 class Facebook < OAuth2
# @param [Rack Application] app standard middleware application parameter # @param [Rack Application] app standard middleware application parameter
# @param [String] app_id the application id as [registered on Facebook](http://www.facebook.com/developers/) # @param [String] client_id the application id as [registered on Facebook](http://www.facebook.com/developers/)
# @param [String] app_secret the application secret as registered on Facebook # @param [String] client_secret the application secret as registered on Facebook
# @option options [String] :scope ('email,offline_access') comma-separated extended permissions such as `email` and `manage_pages` # @option options [String] :scope ('email,offline_access') comma-separated extended permissions such as `email` and `manage_pages`
def initialize(app, app_id, app_secret, options = {}) def initialize(app, client_id = nil, client_secret = nil, options = {}, &block)
super(app, :facebook, app_id, app_secret, {:site => 'https://graph.facebook.com/'}, options) super(app, :facebook, client_id, client_secret, {:site => 'https://graph.facebook.com/'}, options, &block)
end end
def user_data def user_data

View file

@ -8,7 +8,7 @@ module OmniAuth
# #
# @option options [Boolean, true] :sign_in When true, use a sign-in flow instead of the authorization flow. # @option options [Boolean, true] :sign_in When true, use a sign-in flow instead of the authorization flow.
# @option options [Boolean, false] :mobile When true, use the mobile sign-in interface. # @option options [Boolean, false] :mobile When true, use the mobile sign-in interface.
def initialize(app, consumer_key, consumer_secret, options = {}) def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
client_options = {:site => 'http://foursquare.com'} client_options = {:site => 'http://foursquare.com'}
auth_path = (options[:sign_in] == false) ? '/oauth/authorize' : '/oauth/authenticate' auth_path = (options[:sign_in] == false) ? '/oauth/authorize' : '/oauth/authenticate'
@ -16,7 +16,7 @@ module OmniAuth
client_options[:authorize_path] = auth_path client_options[:authorize_path] = auth_path
super(app, :foursquare, consumer_key, consumer_secret, client_options, options) super(app, :foursquare, consumer_key, consumer_secret, client_options, options, &block)
end end
def auth_hash def auth_hash

View file

@ -8,16 +8,16 @@ module OmniAuth
# and provide the proper credentials to this middleware. # and provide the proper credentials to this middleware.
class GitHub < OAuth2 class GitHub < OAuth2
# @param [Rack Application] app standard middleware application argument # @param [Rack Application] app standard middleware application argument
# @param [String] app_id the application ID for your client # @param [String] client_id the application ID for your client
# @param [String] app_secret the application secret # @param [String] client_secret the application secret
def initialize(app, app_id, app_secret, options = {}) def initialize(app, client_id = nil, client_secret = nil, options = {}, &block)
client_options = { client_options = {
:site => 'https://github.com/', :site => 'https://github.com/',
:authorize_path => '/login/oauth/authorize', :authorize_path => '/login/oauth/authorize',
:access_token_path => '/login/oauth/access_token' :access_token_path => '/login/oauth/access_token'
} }
super(app, :github, app_id, app_secret, client_options, options) super(app, :github, client_id, client_secret, client_options, options, &block)
end end
protected protected

View file

@ -14,14 +14,14 @@ module OmniAuth
# @param [String] api_key the application id as [registered on Gowalla](http://gowalla.com/api/keys) # @param [String] api_key the application id as [registered on Gowalla](http://gowalla.com/api/keys)
# @param [String] secret_key the application secret as [registered on Gowalla](http://gowalla.com/api/keys) # @param [String] secret_key the application secret as [registered on Gowalla](http://gowalla.com/api/keys)
# @option options ['read','read-write'] :scope ('read') the scope of your authorization request; must be `read` or `read-write` # @option options ['read','read-write'] :scope ('read') the scope of your authorization request; must be `read` or `read-write`
def initialize(app, api_key, secret_key, options = {}) def initialize(app, api_key = nil, secret_key = nil, options = {}, &block)
client_options = { client_options = {
:site => 'https://api.gowalla.com/api/oauth', :site => 'https://api.gowalla.com/api/oauth',
:authorize_url => 'https://gowalla.com/api/oauth/new', :authorize_url => 'https://gowalla.com/api/oauth/new',
:access_token_url => 'https://api.gowalla.com/api/oauth/token' :access_token_url => 'https://api.gowalla.com/api/oauth/token'
} }
super(app, :gowalla, api_key, secret_key, client_options, options) super(app, :gowalla, api_key, secret_key, client_options, options, &block)
end end
protected protected

View file

@ -12,12 +12,12 @@ module OmniAuth
# use OmniAuth::Strategies::Identica, 'consumerkey', 'consumersecret' # use OmniAuth::Strategies::Identica, 'consumerkey', 'consumersecret'
# #
class Identica < OmniAuth::Strategies::OAuth class Identica < OmniAuth::Strategies::OAuth
def initialize(app, consumer_key, consumer_secret, options = {}) def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
super(app, :identica, consumer_key, consumer_secret, super(app, :identica, consumer_key, consumer_secret,
{:site => 'http://identi.ca', {:site => 'http://identi.ca',
:request_token_path => "/api/oauth/request_token", :request_token_path => "/api/oauth/request_token",
:access_token_path => "/api/oauth/access_token", :access_token_path => "/api/oauth/access_token",
:authorize_path => "/api/oauth/authorize"}, options) :authorize_path => "/api/oauth/authorize"}, options, &block)
end end
def auth_hash def auth_hash

View file

@ -4,13 +4,13 @@ require 'omniauth/oauth'
module OmniAuth module OmniAuth
module Strategies module Strategies
class LinkedIn < OmniAuth::Strategies::OAuth class LinkedIn < OmniAuth::Strategies::OAuth
def initialize(app, consumer_key, consumer_secret, options = {}) def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
super(app, :linked_in, consumer_key, consumer_secret, super(app, :linked_in, consumer_key, consumer_secret,
{:site => 'https://api.linkedin.com', {:site => 'https://api.linkedin.com',
:request_token_path => '/uas/oauth/requestToken', :request_token_path => '/uas/oauth/requestToken',
:access_token_path => '/uas/oauth/accessToken', :access_token_path => '/uas/oauth/accessToken',
:authorize_path => '/uas/oauth/authorize', :authorize_path => '/uas/oauth/authorize',
:scheme => :header}, options) :scheme => :header}, options, &block)
end end
def auth_hash def auth_hash

View file

@ -11,7 +11,7 @@ module OmniAuth
# use OmniAuth::Strategies::Meetup, 'consumerkey', 'consumersecret' # use OmniAuth::Strategies::Meetup, 'consumerkey', 'consumersecret'
# #
class Meetup < OmniAuth::Strategies::OAuth class Meetup < OmniAuth::Strategies::OAuth
def initialize(app, consumer_key, consumer_secret, options = {}) def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
super(app, :meetup, consumer_key, consumer_secret, super(app, :meetup, consumer_key, consumer_secret,
# :site => 'https://api.meetup.com', # :site => 'https://api.meetup.com',
{:request_token_path => "https://api.meetup.com/oauth/request", {:request_token_path => "https://api.meetup.com/oauth/request",

View file

@ -6,7 +6,9 @@ module OmniAuth
class OAuth class OAuth
include OmniAuth::Strategy include OmniAuth::Strategy
def initialize(app, name, consumer_key, consumer_secret, consumer_options = {}, 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 self.consumer_options = consumer_options
super super
end end

View file

@ -16,7 +16,7 @@ module OmniAuth
# The options passed in to the strategy. # The options passed in to the strategy.
attr_accessor :options attr_accessor :options
# The `OAuth2::Client` for this strategy. # The `OAuth2::Client` for this strategy.
attr_accessor :client attr_accessor :client_id, :client_secret, :client_options
# An error that is indicated in the OAuth 2.0 callback. # An error that is indicated in the OAuth 2.0 callback.
# This could be a `redirect_uri_mismatch` or other # This could be a `redirect_uri_mismatch` or other
@ -37,9 +37,15 @@ module OmniAuth
# @param [String] client_id the client/application ID of this provider # @param [String] client_id the client/application ID of this provider
# @param [String] client_secret the client/application secret of this provider # @param [String] client_secret the client/application secret of this provider
# @param [Hash] options that will be passed through to the OAuth2::Client (see [oauth2 docs](http://rubydoc.info/gems/oauth2)) # @param [Hash] options that will be passed through to the OAuth2::Client (see [oauth2 docs](http://rubydoc.info/gems/oauth2))
def initialize(app, name, client_id, client_secret, client_options = {}, options = {}) def initialize(app, name, client_id = nil, client_secret = nil, client_options = {}, options = {}, &block)
self.client_id = client_id
self.client_secret = client_secret
self.client_options = client_options
super super
self.client = ::OAuth2::Client.new(client_id, client_secret, client_options.merge(options[:client_options] || {})) end
def client
::OAuth2::Client.new(client_id, client_secret, client_options.merge(options[:client_options] || {}))
end end
protected protected

View file

@ -9,12 +9,12 @@ module OmniAuth
# use OmniAuth::Strategies::SmugMug, 'consumerkey', 'consumersecret' # use OmniAuth::Strategies::SmugMug, 'consumerkey', 'consumersecret'
# #
class SmugMug < OmniAuth::Strategies::OAuth class SmugMug < OmniAuth::Strategies::OAuth
def initialize(app, consumer_key, consumer_secret, options = {}) def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
super(app, :smugmug, consumer_key, consumer_secret, super(app, :smugmug, consumer_key, consumer_secret,
{:site => 'http://api.smugmug.com', {:site => 'http://api.smugmug.com',
:request_token_path => "/services/oauth/getRequestToken.mg", :request_token_path => "/services/oauth/getRequestToken.mg",
:access_token_path => "/services/oauth/getAccessToken.mg", :access_token_path => "/services/oauth/getAccessToken.mg",
:authorize_path => "/services/oauth/authorize.mg"}, options) :authorize_path => "/services/oauth/authorize.mg"}, options, &block)
end end
def auth_hash def auth_hash

View file

@ -13,7 +13,7 @@ module OmniAuth
# #
class SoundCloud < OmniAuth::Strategies::OAuth class SoundCloud < OmniAuth::Strategies::OAuth
def initialize(app, consumer_key, consumer_secret, options = {}) def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
super(app, :soundcloud, consumer_key, consumer_secret, {:site => 'https://api.soundcloud.com'}, options) super(app, :soundcloud, consumer_key, consumer_secret, {:site => 'https://api.soundcloud.com'}, options)
end end

View file

@ -4,14 +4,14 @@ require 'multi_json'
module OmniAuth module OmniAuth
module Strategies module Strategies
class ThirtySevenSignals < OAuth2 class ThirtySevenSignals < OAuth2
def initialize(app, app_id, app_secret, options = {}) def initialize(app, client_id = nil, client_secret = nil, options = {}, &block)
client_options = { client_options = {
:site => 'https://launchpad.37signals.com/', :site => 'https://launchpad.37signals.com/',
:authorize_path => '/authorization/new', :authorize_path => '/authorization/new',
:access_token_path => '/authorization/token' :access_token_path => '/authorization/token'
} }
super(app, :thirty_seven_signals, app_id, app_secret, client_options, options) super(app, :thirty_seven_signals, client_id, client_secret, client_options, options, &block)
end end
def user_data def user_data

View file

@ -10,12 +10,12 @@ module OmniAuth
# use OmniAuth::Strategies::TripIt, 'consumerkey', 'consumersecret' # use OmniAuth::Strategies::TripIt, 'consumerkey', 'consumersecret'
# #
class TripIt < OmniAuth::Strategies::OAuth class TripIt < OmniAuth::Strategies::OAuth
def initialize(app, consumer_key, consumer_secret, options = {}) def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
super(app, :tripit, consumer_key, consumer_secret, super(app, :tripit, consumer_key, consumer_secret,
{:site => 'https://api.tripit.com', {:site => 'https://api.tripit.com',
:request_token_path => "/oauth/request_token", :request_token_path => "/oauth/request_token",
:access_token_path => "/oauth/access_token", :access_token_path => "/oauth/access_token",
:authorize_url => "https://www.tripit.com/oauth/authorize"}, options) :authorize_url => "https://www.tripit.com/oauth/authorize"}, options, &block)
end end
def request_phase def request_phase

View file

@ -15,7 +15,7 @@ module OmniAuth
# Initialize the middleware # Initialize the middleware
# #
# @option options [Boolean, true] :sign_in When true, use the "Sign in with Twitter" flow instead of the authorization flow. # @option options [Boolean, true] :sign_in When true, use the "Sign in with Twitter" flow instead of the authorization flow.
def initialize(app, consumer_key, consumer_secret, options = {}) def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
client_options = { client_options = {
:site => 'https://api.twitter.com' :site => 'https://api.twitter.com'
} }

View file

@ -2,6 +2,10 @@ shared_examples_for "an oauth strategy" do
it 'should be initializable with only three arguments' do it 'should be initializable with only three arguments' do
lambda{ strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}, 'key', 'secret') }.should_not raise_error lambda{ strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}, 'key', 'secret') }.should_not raise_error
end end
it 'should be initializable with a block' do
lambda{ strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}){|s| s.consumer_key = 'abc'} }.should_not raise_error
end
it 'should handle the setting of client options' do it 'should handle the setting of client options' do
s = strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}, 'key', 'secret', :client_options => {:abc => 'def'}) s = strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}, 'key', 'secret', :client_options => {:abc => 'def'})
@ -13,6 +17,10 @@ shared_examples_for "an oauth2 strategy" do
it 'should be initializable with only three arguments' do it 'should be initializable with only three arguments' do
lambda{ strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}, 'key', 'secret') }.should_not raise_error lambda{ strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}, 'key', 'secret') }.should_not raise_error
end end
it 'should be initializable with a block' do
lambda{ strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}){|s| s.client_id = 'abc'} }.should_not raise_error
end
it 'should handle the setting of client options' do it 'should handle the setting of client options' do
s = strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}, 'key', 'secret', :client_options => {:abc => 'def'}) s = strategy_class.new(lambda{|env| [200, {}, ['Hello World']]}, 'key', 'secret', :client_options => {:abc => 'def'})

View file

@ -3,9 +3,9 @@ require 'omniauth/openid'
module OmniAuth module OmniAuth
module Strategies module Strategies
class GoogleApps < OmniAuth::Strategies::OpenID class GoogleApps < OmniAuth::Strategies::OpenID
def initialize(app, store = nil, options = {}) def initialize(app, store = nil, options = {}, &block)
options[:name] ||= 'google_apps' options[:name] ||= 'google_apps'
super(app, store, options) super(app, store, options, &block)
end end
def get_identifier def get_identifier

View file

@ -35,8 +35,8 @@ module OmniAuth
# @option options [Array] :optional The optional attributes for the OpenID request. May # @option options [Array] :optional The optional attributes for the OpenID request. May
# be ActiveExchange or sreg. # be ActiveExchange or sreg.
# @option options [Symbol, :open_id] :name The URL segment name for this provider. # @option options [Symbol, :open_id] :name The URL segment name for this provider.
def initialize(app, store = nil, options = {}) def initialize(app, store = nil, options = {}, &block)
super(app, options[:name] || :open_id) super(app, (options[:name] || :open_id), &block)
@options = options @options = options
@options[:required] ||= [AX[:email], AX[:name], AX[:first_name], AX[:last_name], 'email', 'fullname'] @options[:required] ||= [AX[:email], AX[:name], AX[:first_name], AX[:last_name], 'email', 'fullname']
@options[:optional] ||= [AX[:nickname], AX[:city], AX[:state], AX[:website], AX[:image], 'postcode', 'nickname'] @options[:optional] ||= [AX[:nickname], AX[:city], AX[:state], AX[:website], AX[:image], 'postcode', 'nickname']