Code Monkey home page Code Monkey logo

authlogic_openid's Introduction

Authlogic OpenID

Authlogic OpenID is an extension of the Authlogic library to add OpenID support. Authlogic v2.0 introduced an enhanced API that makes “plugging in” alternate authentication methods as easy as installing a gem.

* Documentation: authlogic-oid.rubyforge.org * Authlogic: github.com/binarylogic/authlogic * Live example: authlogicexample.binarylogic.com

Install and use

1. Make some simple changes to your database:

class AddUsersOpenidField < ActiveRecord::Migration
  def self.up
    add_column :users, :openid_identifier, :string
    add_index :users, :openid_identifier

    change_column :users, :login, :string, :default => nil, :null => true
    change_column :users, :crypted_password, :string, :default => nil, :null => true
    change_column :users, :password_salt, :string, :default => nil, :null => true
  end

  def self.down
    remove_column :users, :openid_identifier

    [:login, :crypted_password, :password_salt].each do |field|
      User.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
      change_column :users, field, :string, :default => "", :null => false
    end
  end
end

2. Install the openid_authentication plugin

$ script/plugin install git://github.com/rails/open_id_authentication.git
$ rake open_id_authentication:db:create

For more information on how to configure the plugin, checkout it’s README: github.com/rails/open_id_authentication/tree/master

3. Install the Authlogic Openid gem

$ sudo gem install authlogic-oid

Now add the gem dependency in your config:

config.gem "authlogic-oid", :lib => "authlogic_openid"

Or for older version of rails, install it as a plugin:

$ script/plugin install git://github.com/binarylogic/authlogic_openid.git

4. Make sure you save your objects properly

You only need to save your objects this way if you want the user to authenticate with their OpenID provider.

That being said, you probably want to do this in your controllers. You should do this for BOTH your User objects and UserSession objects (assuming you are authenticating users). It should look something like this:

@user_session.save do |result|
  if result
    flash[:notice] = "Login successful!"
    redirect_back_or_default account_url
  else
    render :action => :new
  end
end

You should save your @user objects this way as well, because you also want the user to verify that they own the OpenID identifier that they supplied.

Notice we are saving with a block. Why? Because we need to redirect the user to their OpenID provider so that they can authenticate. When we do this, we don’t want to execute that block of code, because if we do, we will get a DoubleRender error. This lets us skip that entire block and send the user along their way without any problems.

That’s it! The rest is taken care of for you.

Redirecting from the models?

If you are interested, I explain myself below. Regardless, if you don’t feel comfortable with the organization of the logic,you can easily do this using the traditional method. As you saw in the setup instructions, this library leverages the open_id_authentication rails plugin. After the user has been authenticated just do this:

UserSession.create(@user)

It’s that simple. For more information there is a great OpenID tutorial at: railscasts.com/episodes/68-openid-authentication

Now, here are my thoughts on the subject:

You are probably thinking: “Ben, you can’t handle controller responsibilities in models”. I agree with you on that comment, but my personal opinion is that these are not controller responsibilities. The fact that OpenID authentication requires a redirect should not effect the location of the logic / code. It’s all part of the authentication process, which is the entire purpose of this library. This library is not one big module of code, its a collection of modules that all deal with OpenID authentication. These modules get included wherever it makes sense. That’s the whole idea behind modules. To group common logic.

Let’s take a step back and look at the traditional method of OpenID authentication in rails. What if you wanted to authenticate with OpenID in multiple controllers in your application (Ex: registration and loggin in)? You would probably pull out the common code into a module and include it in the respective controllers. Even better, you might create a class that elegantly handles this process and then place it in your lib directory. Then, if you really wanted to be slick, you might take it another step further and have your models trigger this class during certain actions. Then what do we have? This exact library, that’s exactly what this is.

The last thing I will leave you with, to get you thinking, is… where do sweepers lie in the MVC pattern? Without this, things like caching would be extremely difficult. There is a big difference between misplacing code / logic, and organizing logic into a separate module and hooking it in using the API provided by your models. Especially when the logic needs to be triggered by actions invoked on models.

Regardless, if I still haven’t convinced you, I hope this library is of some benefit to you. At the very least an example of how to extend Authlogic.

Copyright © 2009 Ben Johnson of [Binary Logic](www.binarylogic.com), released under the MIT license

authlogic_openid's People

Contributors

binarylogic avatar pelle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

authlogic_openid's Issues

Session is a reserved word in oracle

Is there a way to alter the table name used?
Or can the code be changed to quote the session table?

ActiveRecord::StatementInvalid (OCIError: ORA-00903:
invalid table name: SELECT data FROM session WHERE id = 3 FOR UPDATE)

ActionView::Template::Error ("DESC session" failed; does it exist?)

Thanks,

Gem of new version and small issue when using in test/console..

Can this Github repos be turned into gem? Now I only seem to find the 1.0.4 version :(

Also I get a 'nil.params?' error when using in console / test.
Please see below for fix:

# This fixes the following error:
#  You have a nil object when you didn't expect it!
#  The error occurred while evaluating nil.params
module AuthlogicOpenid
  module ActsAsAuthentic
    module Methods

      def openid_complete?
        session_class.controller && session_class.controller.params[:open_id_complete] && session_class.controller.params[:for_model]
      end

    end
  end
end

uninitialized constant Rack::OpenID

So I followed the railscast tutorial and used the old version of the plugin from Ryan's git file. I can now successfuly create/register a user using OpenID (Google), but I cannot log in with this user. When I submit the OpenID that has been registered, I get "uninitialized constant Rack::OpenID". Any ideas?

Thanks!

Missing template user_sessions/create.erb in view path app/views

authlogic v2.0.13 and authlogic-oid v1.0.4

When I submit an invalid open id to a new session ("goodopenid.com" in my case) I get the following error:

Missing template user_sessions/create.erb in view path app/views

Here's my user_sessions_controller create method (copied from examples)

def create
@user_session = UserSession.new(params[:user_session])
# We are saving with a block to accomodate for OpenID authentication
# If you are not using OpenID you can save without a block:
#
# if @user_session.save
# # ... successful login
# else
# # ... unsuccessful login
# end
@user_session.save do |result|
if result
flash[:notice] = "Login successful!"
redirect_back_or_default root_path
else
render :action => :new
end
end
end

For some reason its attempting to render "create" rather than rendering "new" with errors.

Incidentally, I see the same problem in the sample app here:

http://authlogicexample.binarylogic.com/

There, when I use "goodopenid.com" I get a crash.

Ideas?

possible "remember_me" typo in validate_by_openid method

In the validate_by_openid method, you're setting the variable "self.remember_me", while a few lines below, you're using "remember_me?" (notice the question mark).

I'm not completely sure if this is a typo/bug, but it looks like it. Sorry if I'm wrong.

Doc needs: How to save @user correctly

Hi there,

I need help writing the create() method in the users_controller.

So perhaps this could label this as a "documentation bug".

Anyway, in my users controller, I wrote this:

def create
@user = User.new(params[:user])
debugger
@user.save do |result|
if result
flash[:notice] = "Account registered!"
redirect_back_or_default account_url
else
render :action => :new
end
end
end

When control hits

@user.save do |result|

control transfers to this file:

http://github.com/binarylogic/authlogic_openid/blob/master/lib/authlogic_openid/acts_as_authentic.rb

then to this line:

def save(perform_validation = true, &block)
return false if perform_validation && block_given? && authenticate_with_openid? && !authenticate_with_openid

Once here, the debugger is telling me that block_given? is false.

This is wrong.

block_given? should be true.

So, perhaps I wrote create() incorrectly?

The doc only gives syntax for the saving of UserSession objects.

Please offer clues.

Thanks,
--Audrey

validation option ":if" is ignored

For the validates_length_of_password_field_options, validates_confirmation_of_password_field_options, and validates_length_of_password_confirmation_field_options the :if option is ignored. It appears that a merge is executed against the options hash, overwriting the :if option. Can we change this to a reverse_merge? http://gist.github.com/251422

(I wanted to run this against the test suite but I couldn't get the suite to pass regardless of this patch's presence)

[feature] merge registration and login

Hi. It would be very nice if it was possible to merge the login and registration together. Example:

As a user who never used the website.
When I login with openid.
The user is automatically created.

This way, I don't have a registration phase. The user simple logins, and if it doesn't exist yet on the database, the user is created.

I tried to play with the existing code to implement this behavior with no success. Does it make sense?

Use of config() instead of rw_config() in authlogic_openid 1.0.3 with authlogic >= 2.0.12

Authlogic-Changelog for 2.0.12 released 2009-5-13 reads:
Changed config() convenience method to rw_config() to be more descriptive and less vague.

...but methods 'required_fields' and 'optional_fields' in 'lib/authlogic_openid/acts_as_authentic.rb' still use config().

This results in a NoMethodError for people using authlogic-openid in combination with authlogic >= 2.0.12.

Password-less registration

It seems quite complicated to make login (using auto_register) work without forcing user to enter a password. I won't do any hacks as of not validating/requiring password fields, since those are still needed with old school login/password registration/login.

What would I expect is that authlogic_openid let me register users without password and login/username/email

last update on open_id_authentication plugin breaks authlogic_openid

Hi,
since open_id_authentication is now build on top of Rack::OpenID, the gem is not working anymore.

Here the changes i found for now.

the exception class OpenIdAuthentication::InvalidOpenId was removed as well as the method OpenIdAuthentification.normalize_identifier

These are use in

file lib/authlogic_openid/acts_as_authentic.rb, line 55

  def openid_identifier=(value)
    write_attribute(:openid_identifier, value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value))
    reset_persistence_token if openid_identifier_changed?
  rescue OpenIdAuthentication::InvalidOpenId => e
    @openid_error = e.message
  end

File lib/authlogic_openid/session.rb, line 50

  def openid_identifier=(value)
    @openid_identifier = value.blank? ? nil : OpenIdAuthentication.normalize_identifier(value)
    @openid_error = nil
  rescue OpenIdAuthentication::InvalidOpenId => e
    @openid_identifier = nil
    @openid_error = e.message
  end

Exception uninitialized constant OpenIdAuthentication::InvalidOpenId uninitialized

On a new project, while trying to setup Authlogic and Authlogic OpenId I constantly run into that exception, which in turn unables me to create users / login users.

I'm using Rails 2.3.5 with the following gems:

config.gem 'authlogic', :version => '2.1.3'
config.gem 'rack-openid', :version => '0.2.2', :lib => 'rack/openid'
config.gem 'ruby-openid', :version => '2.1.7', :lib => 'openid'
config.gem 'authlogic-oid', :version => '1.0.4', :lib => 'authlogic_openid'

I'm also using the latest version of rails/open_id_authentication plugin.

Anyone could provide me with some assistance / guide me in the right direction on how to tackle this problem?

Best regards,
DBA

App Timeout

Does OpenID have a session expiration on it? I'm finding in a sample app built on this gem that inactivity for 10 or 20 minutes logs me out.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.