Created
June 13, 2010 22:30
-
-
Save augustl/437053 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'openid' | |
require 'openid/consumer' | |
require "openid/store/filesystem" | |
require 'openid/extensions/sreg' | |
class LoginController < ApplicationController | |
def create | |
response = openid_consumer.begin(params[:openid_url]) | |
sregreq = ::OpenID::SReg::Request.new | |
sregreq.request_fields(["email"], true) | |
sregreq.request_fields(["fullname", "nickname"], false) | |
response.add_extension(sregreq) | |
redirect_to response.redirect_url(root_url, login_complete_url) | |
rescue Timeout::Error | |
flash.now[:error] = "OpenID request timed out." | |
render :action => "new" | |
rescue OpenID::DiscoveryFailure | |
flash.now[:error] = "Couldn't find an OpenID for that URL (#{params[:openid_url]})." | |
render :action => "new" | |
rescue OpenID::OpenIDError | |
flash.now[:error] = "An unknown OpenID related error occurred." | |
render :action => "new" | |
end | |
def complete | |
response = openid_consumer.complete(request.query_parameters, request.url) | |
if response.status == :success | |
sreg = ::OpenID::SReg::Response.from_success_response(response).inspect | |
p sreg["nickname"] | |
p sreg["fullname"] | |
p sreg["email"] | |
session[:open_id] = response.identity_url | |
redirect_to root_path | |
else | |
flash[:error] = "An unknown error occurred when handling the OpenID response." | |
redirect_to login_path | |
end | |
end | |
def destroy | |
session.delete(:open_id) | |
redirect_to root_path | |
end | |
private | |
def openid_consumer | |
# OpenID::Store::Filesystem.new(Rails.root.join('tmp/openids')) | |
# OpenID::Store::Memory.new | |
@openid_consumer ||= OpenID::Consumer.new(openid_session, OpenID::Store::Filesystem.new(Rails.root.join('tmp/openids'))) | |
end | |
def openid_session | |
session[:openid_consumer] ||= {} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
YourApp::Application.routes.draw do |map| | |
scope :controller => "login"do | |
match "login" => :new, :via => :get | |
match "login" => :create, :via => :post | |
match "login/complete" => :complete | |
match "logout" => :destroy | |
end | |
root :to => "projects#index" | |
end |
I tested it against google and facebook and myopenid. I had no idea ax even existed though :)
It's working very well.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mmm, sreg instead of Ax though. From what I hear, most servers use Ax?
Man, doing it "by hand" like this just make so much more sense to me than using plugins...
Has this been working well for you?