Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks email after identity, then returns nil and fixes depreciated init #15

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ source "http://rubygems.org"

gem "rspec", ">= 2.0.0.beta.22"
gem "rails", "3.0.0"
gem "ruby-debug"
gem "jeweler"

gem "jeweler"
10 changes: 7 additions & 3 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ In @config/application.rb@ or @config/environments/YOUR_ENV.rb@ (to set a differ
</pre>

*Migrations*

Your model needs one attribute/column to store the RPX identifier. By default, this identifier is @rpx_identifier@.
So don't forget to add that field to your model (using a migration or whatever...).
Run the following command
<pre>rails g model identity user_id:integer identifier:string</pre>
Add the following to your user model
<pre>has_many :identities</pre>
Then commit the model by running
<pre>rake db:migrate</pre>

*Views*

Expand Down Expand Up @@ -145,6 +148,7 @@ h2. Contributors
* "Michael Bumann":http://github.com/bumi
* "Arek Flinik":http://github.com/blax
* "Nader Akhnoukh":http://github.com/iamnader
* "Mohammad El-Abid":http://github.com/TheEmpty

h2. Thanks

Expand Down
2 changes: 1 addition & 1 deletion devise_rpx_connectable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |s|
"lib/devise_rpx_connectable/strategy.rb",
"lib/devise_rpx_connectable/version.rb",
"lib/devise_rpx_connectable/view_helpers.rb",
"rails/init.rb"
"init.rb"
]
s.homepage = %q{http://github.com/slainer68/devise_rpx_connectable}
s.rdoc_options = ["--charset=UTF-8"]
Expand Down
11 changes: 11 additions & 0 deletions devise_rpx_connectable_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
puts "loading generator..."

class DeviseRpxConnectableGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)

def copy_migration
migration_template "migration.rb", "db/migrate/devise_add_identifiers.rb"
end
end

puts "loaded"
5 changes: 5 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# encoding: utf-8
require 'devise_rpx_connectable'
puts "trying to require generator"
require '../devise_rpx_connectable_generator'
puts "required"
1 change: 0 additions & 1 deletion lib/devise_rpx_connectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
require 'devise_rpx_connectable/strategy'
Warden::Strategies.add(:rpx_connectable, Devise::RpxConnectable::Strategies::RpxConnectable)

require 'devise_rpx_connectable/schema'
require 'devise_rpx_connectable/view_helpers'

module Devise
Expand Down
27 changes: 22 additions & 5 deletions lib/devise_rpx_connectable/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ def self.included(base) #:nodoc:
# Store RPX account/session credentials.
#
def store_rpx_credentials!(attributes = {})
self.send(:"#{self.class.rpx_identifier_field}=", attributes[:identifier])

# Confirm without e-mail - if confirmable module is loaded.
self.skip_confirmation! if self.respond_to?(:skip_confirmation!)

Expand Down Expand Up @@ -135,8 +133,22 @@ def rpx_auto_create_account?
# Authenticate a user based on RPX Identifier.
#
def authenticate_with_rpx(attributes = {})
if attributes[:identifier].present?
self.find_for_rpx(attributes[:identifier])
begin
if attributes[:identifier].present?
user = self.find_for_rpx(attributes[:identifier])
end

if !user and attributes[:email]
if user = self.find_by_email(attributes[:email])
user.identities.new(:identifier => attributes[:identifier]) #build_identity?
user.save!
end
end

return user

rescue
raise StandardError, "Error in authenticate_with_rpx() -> #{$!}"
end
end

Expand All @@ -147,7 +159,12 @@ def authenticate_with_rpx(attributes = {})
# namedscope to filter records while authenticating.
#
def find_for_rpx(identifier)
self.first(:conditions => { rpx_identifier_field => identifier })
#self.first(:conditions => { rpx_identifier_field => identifier })
@identity = Identity.first(:conditions => ["identifier = ?", identifier])
if @identity
return self.find @identity.user_id
end
return false
end

# Contains the logic used in authentication. Overwritten by other devise modules.
Expand Down
21 changes: 0 additions & 21 deletions lib/devise_rpx_connectable/schema.rb

This file was deleted.

30 changes: 17 additions & 13 deletions lib/devise_rpx_connectable/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,32 @@ def valid?
def authenticate!
klass = mapping.to
raise StandardError, "RPXNow API key is not defined, please see the documentation of RPXNow gem to setup it." unless RPXNow.api_key.present?
begin
rpx_user = (RPXNow.user_data(params[:token], :extended => klass.rpx_extended_user_data, :additional => klass.rpx_additional_user_data) rescue nil)
fail!(:rpx_invalid) and return unless rpx_user

if user = klass.authenticate_with_rpx(:identifier => rpx_user["identifier"])
user.on_before_rpx_success(rpx_user)
success!(user)
return
end
rpx_data = (RPXNow.user_data(params[:token], :extended => klass.rpx_extended_user_data, :additional => klass.rpx_additional_user_data) rescue nil)
fail!(:rpx_invalid) and return unless rpx_data

if user = klass.authenticate_with_rpx(rpx_data)
user.on_before_rpx_success(rpx_data)
success!(user) and return
end

begin
fail!(:rpx_invalid) and return unless klass.rpx_auto_create_account?

user = klass.new
user.store_rpx_credentials!(rpx_user)
user.on_before_rpx_auto_create(rpx_user)
user.store_rpx_credentials!(rpx_data)
user.on_before_rpx_auto_create(rpx_data)

# TODO: create a random password here and email the user if we can so that we can check the validations
# if the user doesn't have an email, set it to [email protected] and blank the password and salt
user.save(:validate => false)
user.on_before_rpx_success(rpx_user)
success!(user)
i = Identity.new(:identifier => rpx_data[:identifer])
i.save! if i.save # if we got an error with this, it's fine; we can work through it.
user.on_before_rpx_success(rpx_data)
success!(user) and return

rescue
fail!(:rpx_invalid)
fail!(:email_taken) and return
end
end

Expand Down
2 changes: 0 additions & 2 deletions rails/init.rb

This file was deleted.

2 changes: 2 additions & 0 deletions spec/devise_rpx_connectable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class RPXNow
@user.should_receive(:on_before_rpx_auto_create).with(RPX_USER_DATA).and_return(true)
@user.should_receive(:save).with({ :validate => false }).and_return(true)
@user.should_receive(:on_before_rpx_success).with(RPX_USER_DATA).and_return(true)

Identity.should_recieve(:new).and_return(@identity)

@strategy.should_receive(:"success!").with(@user).and_return(true)

Expand Down