Skip to content

add on_validation options for validation something like captcha #86

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

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 19 additions & 4 deletions lib/omniauth/strategies/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Identity
option :on_login, nil
option :on_registration, nil
option :on_failed_registration, nil
option :on_validation, nil
option :locate_conditions, lambda{|req| {model.auth_key => req['auth_key']} }

def request_phase
Expand Down Expand Up @@ -60,13 +61,27 @@ def registration_form

def registration_phase
attributes = (options[:fields] + [:password, :password_confirmation]).inject({}){|h,k| h[k] = request[k.to_s]; h}
@identity = model.create(attributes)
if @identity.persisted?
env['PATH_INFO'] = callback_path
@identity = model.new(attributes)
self.env['omniauth.identity'] = @identity

if options[:on_validation]
unless options[:on_validation].call(env: self.env)
failed_registration =
if options[:on_failed_registration]
options[:on_failed_registration].call(self.env)
else
registration_form
end

return failed_registration
end
end

if @identity.save && @identity.persisted?
self.env['PATH_INFO'] = callback_path
callback_phase
else
if options[:on_failed_registration]
self.env['omniauth.identity'] = @identity
options[:on_failed_registration].call(self.env)
else
registration_form
Expand Down
23 changes: 22 additions & 1 deletion spec/omniauth/strategies/identity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,32 @@ def set_app!(identity_options = {})
post '/auth/identity/register', properties
auth_hash['uid'].should == 'abc'
end

context 'custom validation stage' do

context 'validation method returns true' do
it 'should register the user normally' do
set_app!(:on_validation => lambda{|env| true})

post '/auth/identity/register', properties
identity_hash.should_not be_nil
end
end

context 'validation method returns false' do
it 'should not register the user' do
set_app!(:on_validation => lambda{|env| false})

post '/auth/identity/register', properties
identity_hash.should be_nil
end
end
end
end

context 'with invalid identity' do
let(:properties) { {
:name => 'Awesome Dude',
:name => 'Awesome Dude',
:email => 'awesome@example.com',
:password => 'NOT',
:password_confirmation => 'MATCHING'
Expand Down