Adding basic password strategy.
This commit is contained in:
Родитель
a9ef69754d
Коммит
9b5ce21488
1
Rakefile
1
Rakefile
|
@ -15,6 +15,7 @@ begin
|
|||
gem.add_dependency 'oauth'
|
||||
gem.add_dependency 'nokogiri'
|
||||
gem.add_dependency 'json'
|
||||
gem.add_dependency 'rack-openid'
|
||||
gem.add_development_dependency "rspec", ">= 1.2.9"
|
||||
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
||||
end
|
||||
|
|
|
@ -56,7 +56,7 @@ module OmniAuth
|
|||
|
||||
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
||||
return "OAuth" if lower_case_and_underscored_word.to_s == 'oauth'
|
||||
return "OpenID" if lower_case_and_underscored_word.to_s == 'open_id'
|
||||
return "OpenID" if ['open_id', 'openid'].include? lower_case_and_underscored_word.to_s
|
||||
|
||||
if first_letter_in_uppercase
|
||||
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
||||
|
@ -68,7 +68,7 @@ module OmniAuth
|
|||
end
|
||||
|
||||
require 'omni_auth/strategy'
|
||||
%w(oauth http_basic linked_in gowalla twitter open_id).each do |s|
|
||||
%w(oauth http_basic linked_in gowalla twitter open_id password).each do |s|
|
||||
require "omni_auth/strategies/#{s}"
|
||||
end
|
||||
require 'omni_auth/builder'
|
|
@ -16,7 +16,7 @@ module OmniAuth
|
|||
def request_phase
|
||||
@response = RestClient.get(endpoint, request_headers)
|
||||
request.POST['auth'] = auth_hash
|
||||
@env['HTTP_METHOD'] = 'GET'
|
||||
@env['REQUEST_METHOD'] = 'GET'
|
||||
@env['PATH_INFO'] = "#{OmniAuth.config.path_prefix}/#{name}/callback"
|
||||
|
||||
@app.call(@env)
|
||||
|
|
|
@ -43,10 +43,10 @@ module OmniAuth
|
|||
end
|
||||
|
||||
def auth_hash(response)
|
||||
{
|
||||
OmniAuth::Utils.deep_merge(super(), {
|
||||
'uid' => response.display_identifier,
|
||||
'user_info' => user_info(response.display_identifier, ::OpenID::SReg::Response.from_success_response(response))
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
def user_info(identifier, sreg)
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
require 'digest/sha1'
|
||||
module OmniAuth
|
||||
module Strategies
|
||||
class Password
|
||||
include OmniAuth::Strategy
|
||||
|
||||
def initialize(app, secret = 'changethisappsecret', options = {})
|
||||
@options = options
|
||||
@options[:identifier_key] ||= 'nickname'
|
||||
@secret = secret
|
||||
super(app, :password)
|
||||
end
|
||||
|
||||
attr_reader :secret
|
||||
|
||||
def request_phase
|
||||
return fail!(:missing_information) unless request[:identifier] && request[:password]
|
||||
return fail!(:password_mismatch) if request[:password_confirmation] && request[:password_confirmation] != '' && request[:password] != request[:password_confirmation]
|
||||
|
||||
env['REQUEST_METHOD'] = 'GET'
|
||||
env['PATH_INFO'] = request.path + '/callback'
|
||||
request['auth'] = auth_hash(encrypt(request[:identifier], request[:password]))
|
||||
@app.call(env)
|
||||
end
|
||||
|
||||
def auth_hash(crypted_password)
|
||||
OmniAuth::Utils.deep_merge(super(), {
|
||||
'uid' => crypted_password,
|
||||
'user_info' => {
|
||||
@options[:identifier_key] => request[:identifier]
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
def callback_phase
|
||||
@app.call(env)
|
||||
end
|
||||
|
||||
def encrypt(identifier, password)
|
||||
Digest::SHA1.hexdigest([identifier, password, secret].join('::'))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -67,6 +67,6 @@ describe 'OmniAuth::Strategies::Twitter' do
|
|||
end
|
||||
|
||||
it 'should initialize with just consumer key and secret' do
|
||||
OmniAuth::Strategies::Twitter.new({},'abc','def')
|
||||
lambda{OmniAuth::Strategies::Twitter.new({},'abc','def')}.should_not raise_error
|
||||
end
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
require File.dirname(__FILE__) + '/../../spec_helper'
|
||||
|
||||
describe OmniAuth::Strategies::Password do
|
||||
before(:each) do
|
||||
FakeAdapter.reset!
|
||||
end
|
||||
|
||||
it do
|
||||
FakeAdapter.authenticate('mbleigh','dude').should be_false
|
||||
FakeAdapter.register('mbleigh','dude')
|
||||
FakeAdapter.authenticate('mbleigh','dude').should be_true
|
||||
end
|
||||
end
|
Загрузка…
Ссылка в новой задаче