Skip to content

Commit

Permalink
Adding basic password strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Apr 4, 2010
1 parent a9ef697 commit 9b5ce21
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/omni_auth.rb
Expand Up @@ -56,7 +56,7 @@ def deep_merge(hash, other_hash)

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 }
Expand All @@ -68,7 +68,7 @@ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
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'
2 changes: 1 addition & 1 deletion lib/omni_auth/strategies/http_basic.rb
Expand Up @@ -16,7 +16,7 @@ def initialize(app, name, endpoint, headers = {})
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)
Expand Down
4 changes: 2 additions & 2 deletions lib/omni_auth/strategies/open_id.rb
Expand Up @@ -43,10 +43,10 @@ def callback_phase
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)
Expand Down
44 changes: 44 additions & 0 deletions lib/omni_auth/strategies/password.rb
@@ -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
2 changes: 1 addition & 1 deletion spec/omni_auth/strategies/oauth_spec.rb
Expand Up @@ -67,6 +67,6 @@ def session
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
Empty file.
13 changes: 13 additions & 0 deletions spec/omni_auth/strategies/password_spec.rb
@@ -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

0 comments on commit 9b5ce21

Please sign in to comment.