lib/omniauth/strategies/open_id.rb in oa-openid-0.0.2 vs lib/omniauth/strategies/open_id.rb in oa-openid-0.0.4
- old
+ new
@@ -1,12 +1,20 @@
require 'rack/openid'
+require 'gapps_openid'
+require 'omniauth/openid'
module OmniAuth
module Strategies
class OpenID
include OmniAuth::Strategy
+ attr_accessor :options
+
+ # Should be 'openid_url'
+ # @see http://github.com/intridea/omniauth/issues/issue/13
+ IDENTIFIER_URL_PARAMETER = 'identifier'
+
AX = {
:email => 'http://axschema.org/contact/email',
:name => 'http://axschema.org/namePerson',
:nickname => 'http://axschema.org/namePerson/friendly',
:first_name => 'http://axschema.org/namePerson/first',
@@ -18,12 +26,12 @@
}
def initialize(app, store = nil, options = {})
super(app, options[:name] || :open_id)
@options = options
- @options[:required] ||= [AX[:email], AX[:name], 'email', 'fullname']
- @options[:optional] ||= [AX[:first_name], AX[:last_name], AX[:nickname], AX[:city], AX[:state], AX[:website], AX[:image], 'postcode', 'nickname']
+ @options[:required] ||= [AX[:email], AX[:first_name], AX[:last_name], 'email', 'fullname']
+ @options[:optional] ||= [AX[:nickname], AX[:city], AX[:state], AX[:website], AX[:image], 'postcode', 'nickname']
@store = store
end
def dummy_app
lambda{|env| [401, {"WWW-Authenticate" => Rack::OpenID.build_header(
@@ -39,11 +47,11 @@
uri.path += '/callback'
uri.to_s
end
def identifier
- request[:identifier]
+ request[IDENTIFIER_URL_PARAMETER]
end
def request_phase
identifier ? start : get_identifier
end
@@ -51,39 +59,34 @@
def start
openid = Rack::OpenID.new(dummy_app, @store)
response = openid.call(env)
case env['rack.openid.response']
when Rack::OpenID::MissingResponse, Rack::OpenID::TimeoutResponse
- fail :connection_failed
+ fail!(:connection_failed)
else
response
end
end
def get_identifier
- response = app.call(env)
- if response[0] < 400
- response
- else
- OmniAuth::Form.build('OpenID Authentication') do
- text_field('OpenID Identifier', 'identifier')
- end.to_response
- end
+ OmniAuth::Form.build('OpenID Authentication') do
+ label_field('OpenID Identifier', IDENTIFIER_URL_PARAMETER)
+ input_field('url', IDENTIFIER_URL_PARAMETER)
+ end.to_response
end
def callback_phase
env['REQUEST_METHOD'] = 'GET'
openid = Rack::OpenID.new(lambda{|env| [200,{},[]]}, @store)
openid.call(env)
resp = env.delete('rack.openid.response')
- case resp.status
- when :failure
- fail!(:invalid_credentials)
- when :success
+ if resp && resp.status == :success
request['auth'] = auth_hash(resp)
@app.call(env)
+ else
+ fail!(:invalid_credentials)
end
end
def auth_hash(response)
OmniAuth::Utils.deep_merge(super(), {
@@ -118,6 +121,6 @@
'urls' => ({'Website' => Array(ax[AX[:website]]).first} if Array(ax[AX[:website]]).any?)
}.inject({}){|h,(k,v)| h[k] = Array(v).first; h}.reject{|k,v| v.nil? || v == ''}
end
end
end
-end
\ No newline at end of file
+end