rails_openid 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +57 -21
- data/Rakefile +4 -0
- data/VERSION +1 -1
- data/generators/open_id_scaffold/USAGE +0 -0
- data/generators/open_id_scaffold/open_id_scaffold_generator.rb +48 -0
- data/generators/open_id_scaffold/templates/INSTALL +6 -0
- data/generators/open_id_scaffold/templates/create_open_ids.rb +16 -0
- data/generators/open_id_scaffold/templates/new.html.erb +19 -0
- data/generators/open_id_scaffold/templates/open_id.rb +22 -0
- data/generators/open_id_scaffold/templates/open_id_test.rb +21 -0
- data/generators/open_id_scaffold/templates/open_ids.yml +34 -0
- data/generators/open_id_scaffold/templates/sessions_controller.rb +43 -0
- data/generators/open_id_scaffold/templates/sessions_controller_test.rb +220 -0
- data/lib/rails_openid.rb +18 -10
- metadata +22 -9
- data/.document +0 -5
- data/.gitignore +0 -22
data/README.rdoc
CHANGED
@@ -6,34 +6,23 @@ infant library, use at your own peril.
|
|
6
6
|
|
7
7
|
== Installation
|
8
8
|
|
9
|
-
This gem is provided through
|
10
|
-
pull gems from
|
9
|
+
This gem is provided through RubyGems.org so you need to have gem configured to
|
10
|
+
pull gems from RubyGems.org.
|
11
11
|
|
12
|
-
=== Enabling
|
12
|
+
=== Enabling RubyGems.org (formerly known as GemCutter)
|
13
13
|
|
14
|
-
|
14
|
+
You can skip this if you have RubyGems.org enabled (which is going to be the
|
15
|
+
default in the future anyway). A properly configured environment should list
|
16
|
+
rubygems.org or gemcutter.org in the gem sources, like:
|
15
17
|
|
16
18
|
$ gem sources
|
17
19
|
*** CURRENT SOURCES ***
|
18
|
-
|
19
|
-
http://gemcutter.org
|
20
|
-
http://gems.rubyforge.org/
|
21
|
-
http://gems.github.com
|
22
20
|
|
23
|
-
|
24
|
-
know two ways to do. One is installing Gemcutter and running gem tumble:
|
21
|
+
http://rubygems.org/
|
25
22
|
|
26
|
-
|
27
|
-
$ gem tumble
|
23
|
+
If you don't have it, you can probably add it this way:
|
28
24
|
|
29
|
-
|
30
|
-
already there.
|
31
|
-
|
32
|
-
The other way is by hand like this:
|
33
|
-
|
34
|
-
$ gem source -a http://gemcutter.org
|
35
|
-
|
36
|
-
I'm not sure if there's any difference. I think there isn't one.
|
25
|
+
$ gem source -a http://rubygems.org/
|
37
26
|
|
38
27
|
=== Installing rails_openid manually
|
39
28
|
|
@@ -43,7 +32,21 @@ It's simple a matter of running:
|
|
43
32
|
|
44
33
|
and that's it. Let me know if something breaks.
|
45
34
|
|
46
|
-
=== Installing through your Ruby on Rails project
|
35
|
+
=== Installing through your Ruby on Rails 3 project
|
36
|
+
|
37
|
+
In the +Gemfile+ file of your Ruby on Rails project you'll need to add:
|
38
|
+
|
39
|
+
gem "rails_openid"
|
40
|
+
|
41
|
+
after that run
|
42
|
+
|
43
|
+
bundle install
|
44
|
+
|
45
|
+
and watch the magic happen, all required gems will be installed. Configuring
|
46
|
+
your Rails project like that is something you'll need anyway, so this is my
|
47
|
+
recommended way.
|
48
|
+
|
49
|
+
=== Installing through your Ruby on Rails 2.3 project
|
47
50
|
|
48
51
|
In the +environment.rb+ file of your Ruby on Rails project you'll have some
|
49
52
|
commented out lines like this:
|
@@ -65,6 +68,39 @@ and you'll get this and all the gems your Rails project need installed.
|
|
65
68
|
Configuring your Rails project like that is something you'll need anyway, so
|
66
69
|
this is my recommended way.
|
67
70
|
|
71
|
+
== Getting started
|
72
|
+
|
73
|
+
A good way to get started is to run the open_id_scaffold generator:
|
74
|
+
|
75
|
+
./script/generate open_id_scaffold
|
76
|
+
|
77
|
+
Be sure to have the following line in your environment.rb (or the equivalent
|
78
|
+
in Rails 3)
|
79
|
+
|
80
|
+
config.gem "rails_openid"
|
81
|
+
config.gem "ruby-openid", :lib => "openid"
|
82
|
+
|
83
|
+
To be able to run the provided tests, you'll need mocha, so add:
|
84
|
+
|
85
|
+
config.gem "mocha"
|
86
|
+
|
87
|
+
to your config/environments/test.rb
|
88
|
+
|
89
|
+
If you don't have a user model, you'll want to create one. You can do it like
|
90
|
+
this:
|
91
|
+
|
92
|
+
./script/generate model User name:string email:string nickname:string
|
93
|
+
|
94
|
+
To link your user model to their open ids add the following line to your user
|
95
|
+
model:
|
96
|
+
|
97
|
+
has_many :open_ids
|
98
|
+
|
99
|
+
rails_openid come with tests you already have in your project. Their failures
|
100
|
+
will show things you need to complete on your own. Also, if you go through the
|
101
|
+
generated code, search for TODOs and you'll find the places where there's code
|
102
|
+
you are likely to want to change.
|
103
|
+
|
68
104
|
== API Documentation
|
69
105
|
|
70
106
|
Up to date api documentation should be automatically generated on
|
data/Rakefile
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# Copyright 2010 J. Pablo Fernández
|
3
|
+
|
1
4
|
require 'rubygems'
|
2
5
|
require 'rake'
|
3
6
|
|
@@ -10,6 +13,7 @@ begin
|
|
10
13
|
gem.email = "pupeno@pupeno.com"
|
11
14
|
gem.homepage = "http://github.com/pupeno/rails_openid"
|
12
15
|
gem.authors = ["J. Pablo Fernández"]
|
16
|
+
gem.files = %w(LICENSE README.rdoc Rakefile VERSION) + Dir.glob("{lib,generators}/**/*")
|
13
17
|
#gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
18
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
19
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
File without changes
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# Copyright 2010 J. Pablo Fernández
|
3
|
+
|
4
|
+
class OpenIdScaffoldGenerator < Rails::Generator::Base
|
5
|
+
def manifest
|
6
|
+
record do |m|
|
7
|
+
# Migration.
|
8
|
+
m.directory "db/migrate"
|
9
|
+
m.migration_template "create_open_ids.rb", "db/migrate", :migration_file_name => "create_open_ids"
|
10
|
+
|
11
|
+
# Model
|
12
|
+
m.directory "app/models"
|
13
|
+
m.directory "test/fixtures"
|
14
|
+
m.directory "test/unit"
|
15
|
+
m.file "open_id.rb", "app/models/open_id.rb"
|
16
|
+
m.file "open_ids.yml", "test/fixtures/open_ids.yml"
|
17
|
+
m.file "open_id_test.rb", "test/unit/open_id_test.rb"
|
18
|
+
|
19
|
+
# Controller
|
20
|
+
m.directory "app/controllers"
|
21
|
+
m.directory "app/views/sessions"
|
22
|
+
m.directory "test/functional"
|
23
|
+
m.file "sessions_controller.rb", "app/controllers/sessions_controller.rb"
|
24
|
+
m.file "new.html.erb", "app/views/sessions/new.html.erb"
|
25
|
+
m.file "sessions_controller_test.rb", "test/functional/sessions_controller_test.rb"
|
26
|
+
|
27
|
+
m.route_resource ":session, :only => [:new, :create, :destroy], :member => { :finish_creating => :get }"
|
28
|
+
|
29
|
+
# Read me
|
30
|
+
m.readme 'INSTALL'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Rails::Generator::Commands::Create
|
36
|
+
# Generate singleton resources. Copied, pasted and modified from
|
37
|
+
# http://api.rubyonrails.org/classes/Rails/Generator/Commands/Create.html
|
38
|
+
def route_resource(resource)
|
39
|
+
sentinel = 'ActionController::Routing::Routes.draw do |map|'
|
40
|
+
|
41
|
+
logger.route "map.resource #{resource}"
|
42
|
+
unless options[:pretend]
|
43
|
+
gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
|
44
|
+
"#{match}\n map.resource #{resource}\n"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreateOpenIds < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :open_ids do |t|
|
4
|
+
# TODO: Change if your users table is not users.
|
5
|
+
t.integer :user_id
|
6
|
+
t.string :identifier
|
7
|
+
t.string :display_identifier
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :open_ids
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
<h1>Log in or Register</h1>
|
2
|
+
|
3
|
+
<%=h flash[:notice] %>
|
4
|
+
<%=h flash[:message] %>
|
5
|
+
|
6
|
+
<p>You don't need to register on this web site. That happens automatically the
|
7
|
+
first time you log in. To log in, just pick one of the services you'd like to
|
8
|
+
use for your identity and you'll be in.</p>
|
9
|
+
<p>For example, if you have a Gmail account, you could use Google, if you have a
|
10
|
+
Yahoo! Mail account, then use Yahoo. If you don't have any, you can create an
|
11
|
+
account in any of those or in myOpenID. If you know what you are doing, you
|
12
|
+
can use any OpenID server.</p>
|
13
|
+
|
14
|
+
|
15
|
+
<% form_tag session_url, :method => :post, :class => :openid do -%>
|
16
|
+
<%= label :openid_identifier, "OpenID" %>
|
17
|
+
<%= text_field_tag :openid_identifier %>
|
18
|
+
<%= submit_tag "Log in" %>
|
19
|
+
<% end -%>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class OpenId < ActiveRecord::Base
|
2
|
+
# TODO: If your user model is not user, change it here.
|
3
|
+
# TODO: Add has_many :open_ids to your user model.
|
4
|
+
belongs_to :user
|
5
|
+
|
6
|
+
# TODO: If your user model is not user, change it here.
|
7
|
+
validates_presence_of :user
|
8
|
+
validates_presence_of :identifier
|
9
|
+
attr_accessible :identifier, :display_identifier
|
10
|
+
end
|
11
|
+
|
12
|
+
# == Schema Information
|
13
|
+
#
|
14
|
+
# Table name: open_ids
|
15
|
+
#
|
16
|
+
# id :integer not null, primary key
|
17
|
+
# user_id :integer
|
18
|
+
# identifier :string(255)
|
19
|
+
# display_identifier :string(255)
|
20
|
+
# created_at :datetime
|
21
|
+
# updated_at :datetime
|
22
|
+
#
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class OpenIdTest < ActiveSupport::TestCase
|
4
|
+
# Replace this with your real tests.
|
5
|
+
test "the truth" do
|
6
|
+
assert true
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# == Schema Information
|
11
|
+
#
|
12
|
+
# Table name: open_ids
|
13
|
+
#
|
14
|
+
# id :integer(4) not null, primary key
|
15
|
+
# user_id :integer(4) not null
|
16
|
+
# identifier :string(255) not null
|
17
|
+
# display_identifier :string(255)
|
18
|
+
# created_at :datetime
|
19
|
+
# updated_at :datetime
|
20
|
+
#
|
21
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
2
|
+
|
3
|
+
# TODO: change the user: part if your user model is called firently or rewrite
|
4
|
+
# this file to fit your fixtures.
|
5
|
+
|
6
|
+
one:
|
7
|
+
# TODO: change to point to an actual user.
|
8
|
+
user: one
|
9
|
+
identifier: http://example.com/one
|
10
|
+
display_identifier: http://example.com/one
|
11
|
+
|
12
|
+
the_one:
|
13
|
+
# TODO: change to point to the same user as before.
|
14
|
+
user: one
|
15
|
+
identifier: http://one.example.org
|
16
|
+
display_identifier: http://one.example.org
|
17
|
+
|
18
|
+
two:
|
19
|
+
# TODO: change to point to an actual user.
|
20
|
+
user: two
|
21
|
+
identifier: http://example.com/two
|
22
|
+
display_identifier: http://example.com/two
|
23
|
+
|
24
|
+
# == Schema Information
|
25
|
+
#
|
26
|
+
# Table name: open_ids
|
27
|
+
#
|
28
|
+
# id :integer not null, primary key
|
29
|
+
# user_id :integer
|
30
|
+
# identifier :string(255)
|
31
|
+
# display_identifier :string(255)
|
32
|
+
# created_at :datetime
|
33
|
+
# updated_at :datetime
|
34
|
+
#
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class SessionsController < ApplicationController
|
2
|
+
include RailsOpenId
|
3
|
+
|
4
|
+
def new
|
5
|
+
# render an openid form
|
6
|
+
end
|
7
|
+
|
8
|
+
def destroy
|
9
|
+
# TODO: whatever you need to remove the user from the session.
|
10
|
+
session["user_id"] = nil
|
11
|
+
flash[:notice] = "You are now logged out."
|
12
|
+
redirect_to root_url
|
13
|
+
end
|
14
|
+
|
15
|
+
def create
|
16
|
+
# TODO: pick what you want to ask for, email, nickname, fullname, etc.
|
17
|
+
send_open_id_request(params, new_session_url, finish_creating_session_url, ['email', 'nickname', 'fullname'])
|
18
|
+
end
|
19
|
+
|
20
|
+
def finish_creating
|
21
|
+
oid_data = process_open_id_response(params, finish_creating_session_url, new_session_url)
|
22
|
+
|
23
|
+
if oid_data
|
24
|
+
oid = OpenId.find(:first, :conditions => ['identifier = ?', oid_data[:identity_url]], :include => :user)
|
25
|
+
|
26
|
+
if not oid
|
27
|
+
# TODO: whatever you need to do to create a new user.
|
28
|
+
user = User.create!(
|
29
|
+
:name => oid_data['name'],
|
30
|
+
:nickname => oid_data['nickname'],
|
31
|
+
:email => oid_data['email'])
|
32
|
+
oid = user.open_ids.create(
|
33
|
+
:identifier => oid_data[:identity_url],
|
34
|
+
:display_identifier => oid_data[:display_identifier])
|
35
|
+
end
|
36
|
+
|
37
|
+
# TODO: do whatever you need to do to mark the user as logged in, merge it (if you are using ubiquitous_user), etc.
|
38
|
+
session["user_id"] = oid.user.id
|
39
|
+
flash[:notice] = "Welcome #{oid.user.name}, you are now logged in."
|
40
|
+
redirect_to root_url
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class SessionsControllerTest < ActionController::TestCase
|
5
|
+
test 'get log in form' do
|
6
|
+
assert_no_difference('User.count') do
|
7
|
+
get :new
|
8
|
+
end
|
9
|
+
assert_response :success
|
10
|
+
end
|
11
|
+
|
12
|
+
test 'start openid authentication' do
|
13
|
+
# Mock out OpenID Request
|
14
|
+
oidreq = mock('OpenID Request')
|
15
|
+
oidreq.expects(:add_extension)
|
16
|
+
oidreq.expects(:return_to_args).returns({})
|
17
|
+
oidreq.expects(:send_redirect?).returns(true)
|
18
|
+
oidreq.expects(:redirect_url).returns('http://cyberspace.com/login')
|
19
|
+
OpenID::Consumer.any_instance.stubs(:begin).returns(oidreq)
|
20
|
+
|
21
|
+
assert_no_difference('User.count') do
|
22
|
+
post :create, :openid_identifier => 'william.cyberspace.com'
|
23
|
+
end
|
24
|
+
|
25
|
+
assert_redirected_to 'http://cyberspace.com/login'
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'start openid authentication with no redirect' do
|
29
|
+
# Mock out OpenID Request
|
30
|
+
oidreq = mock('OpenID Request')
|
31
|
+
oidreq.expects(:add_extension)
|
32
|
+
oidreq.expects(:return_to_args).returns({})
|
33
|
+
oidreq.expects(:send_redirect?).returns(false)
|
34
|
+
body = "blah blah blah"
|
35
|
+
oidreq.expects(:html_markup).returns(body)
|
36
|
+
OpenID::Consumer.any_instance.stubs(:begin).returns(oidreq)
|
37
|
+
|
38
|
+
assert_no_difference('User.count') do
|
39
|
+
post :create, :openid_identifier => 'william.cyberspace.com'
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_response :success
|
43
|
+
assert_equal body, @response.body
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'fail to start openid authentication due to invalid identifier' do
|
47
|
+
OpenID::Consumer.any_instance.stubs(:begin).raises(OpenID::OpenIDError, "Mock OpenID error")
|
48
|
+
|
49
|
+
assert_no_difference('User.count') do
|
50
|
+
post :create, :openid_identifier => 'william.cyberspace.com'
|
51
|
+
end
|
52
|
+
|
53
|
+
assert_redirected_to new_session_url
|
54
|
+
end
|
55
|
+
|
56
|
+
test 'fail to start openid authentication due to empty identifier' do
|
57
|
+
assert_no_difference('User.count') do
|
58
|
+
post :create, :openid_identifier => ''
|
59
|
+
end
|
60
|
+
|
61
|
+
assert_match /enter an OpenID/, flash[:error]
|
62
|
+
end
|
63
|
+
|
64
|
+
test 'fail to start openid authentication due to nil identifier' do
|
65
|
+
assert_no_difference('User.count') do
|
66
|
+
post :create, :openid_identifier => nil
|
67
|
+
end
|
68
|
+
|
69
|
+
assert_match /enter an OpenID/, flash[:error]
|
70
|
+
end
|
71
|
+
|
72
|
+
test 'fail to start openid authentication due to missing identifier' do
|
73
|
+
assert_no_difference('User.count') do
|
74
|
+
post :create
|
75
|
+
end
|
76
|
+
|
77
|
+
assert_match /enter an OpenID/, flash[:error]
|
78
|
+
end
|
79
|
+
|
80
|
+
test 'log in with existing user' do
|
81
|
+
mock_openid_response(:open_id => open_ids(:one))
|
82
|
+
|
83
|
+
assert_no_difference('User.count') do
|
84
|
+
get :finish_creating, :did_sreg => 'y'
|
85
|
+
end
|
86
|
+
|
87
|
+
assert_response :redirect
|
88
|
+
assert_logged_in(open_ids(:one).identifier)
|
89
|
+
end
|
90
|
+
|
91
|
+
test 'log in creating user' do
|
92
|
+
name = 'Ray Bradbury'
|
93
|
+
email = 'ray@mars.com'
|
94
|
+
identifier = 'http://ray.mars.com'
|
95
|
+
mock_openid_response(:name => name, :email => email, :identifier => identifier)
|
96
|
+
|
97
|
+
assert_difference('User.count', +1) do
|
98
|
+
get :finish_creating, :did_sreg => 'y'
|
99
|
+
end
|
100
|
+
|
101
|
+
assert_response :redirect
|
102
|
+
assert_logged_in(identifier)
|
103
|
+
# Verify the user is properly create.
|
104
|
+
oid = OpenId.find(:first, :conditions => ['identifier = ?', identifier])
|
105
|
+
assert oid
|
106
|
+
assert_equal name, oid.user.name
|
107
|
+
assert_equal email, oid.user.email
|
108
|
+
end
|
109
|
+
|
110
|
+
test 'log in creating a user without metadata' do
|
111
|
+
identifier = 'http://ray.mars.com'
|
112
|
+
mock_openid_response(:identifier => identifier)
|
113
|
+
|
114
|
+
assert_difference('User.count', +1) do
|
115
|
+
get :finish_creating, :did_sreg => 'y'
|
116
|
+
end
|
117
|
+
|
118
|
+
assert_response :redirect
|
119
|
+
assert_logged_in(identifier)
|
120
|
+
# Verify the user is properly create.
|
121
|
+
oid = OpenId.find(:first, :conditions => ['identifier = ?', identifier])
|
122
|
+
assert oid
|
123
|
+
assert_equal identifier, oid.identifier
|
124
|
+
assert oid.user.name.blank?
|
125
|
+
assert oid.user.nickname.blank?
|
126
|
+
assert oid.user.email.blank?
|
127
|
+
end
|
128
|
+
|
129
|
+
test 'log in creating a user without metadata and an ugly identifier' do
|
130
|
+
identifier = 'http://ray.mars.com/very/long/and/ugly/identifier/than/nobody/wants/to/ever/see'
|
131
|
+
mock_openid_response(:identifier => identifier)
|
132
|
+
|
133
|
+
assert_difference('User.count', +1) do
|
134
|
+
get :finish_creating, :did_sreg => 'y'
|
135
|
+
end
|
136
|
+
|
137
|
+
assert_response :redirect
|
138
|
+
assert_logged_in(identifier)
|
139
|
+
# Verify the user is properly create.
|
140
|
+
oid = OpenId.find(:first, :conditions => ['identifier = ?', identifier])
|
141
|
+
assert oid
|
142
|
+
#assert_equal User::ANON_NAME, oid.user.name_or_else
|
143
|
+
assert oid.user.name.blank?
|
144
|
+
assert oid.user.email.blank?
|
145
|
+
end
|
146
|
+
|
147
|
+
test 'log in and log out' do
|
148
|
+
mock_openid_response(:open_id => open_ids(:one))
|
149
|
+
|
150
|
+
assert_no_difference('User.count') do
|
151
|
+
get :finish_creating, :did_sreg => 'y'
|
152
|
+
end
|
153
|
+
|
154
|
+
assert_logged_in(open_ids(:one).identifier)
|
155
|
+
|
156
|
+
assert_no_difference('User.count') do
|
157
|
+
get :destroy
|
158
|
+
end
|
159
|
+
assert_nil session[:user_id]
|
160
|
+
assert_nil session[:user_name]
|
161
|
+
assert_match /You are now logged out/, flash[:notice]
|
162
|
+
end
|
163
|
+
|
164
|
+
test 'fail to log in due to cancelled OpenID request' do
|
165
|
+
mock_openid_response(:outcome => :cancel, :identifier => 'example.com')
|
166
|
+
|
167
|
+
assert_no_difference('User.count') do
|
168
|
+
get :finish_creating
|
169
|
+
end
|
170
|
+
|
171
|
+
assert_match /We couldn\'t verify your OpenID/, flash[:error]
|
172
|
+
end
|
173
|
+
|
174
|
+
test 'fail to log in due to cancelled OpenID request with no display identifier' do
|
175
|
+
mock_openid_response(:outcome => :cancel, :identifier => 'example.com', :display_identifier => '')
|
176
|
+
|
177
|
+
assert_no_difference('User.count') do
|
178
|
+
get :finish_creating
|
179
|
+
end
|
180
|
+
|
181
|
+
assert_match /We couldn\'t verify your OpenID/, flash[:error]
|
182
|
+
end
|
183
|
+
|
184
|
+
private
|
185
|
+
|
186
|
+
def assert_logged_in(identifier)
|
187
|
+
oid = OpenId.find(:first, :conditions => ['identifier = ?', identifier])
|
188
|
+
assert oid
|
189
|
+
assert_equal oid.user.id, session[:user_id]
|
190
|
+
#assert_equal oid.user.name_or_else, session[:user_name]
|
191
|
+
#assert_match /#{oid.user.name}.*you are now logged in/, flash[:notice]
|
192
|
+
assert_match /you are now logged in/, flash[:notice]
|
193
|
+
end
|
194
|
+
|
195
|
+
def mock_openid_response(options = {})
|
196
|
+
identifier = options[:identifier] || options[:open_id].identifier
|
197
|
+
display_identifier = options[:display_identifier] || (options[:open_id] && options[:open_id].display_identifier) || identifier
|
198
|
+
outcome = options[:outcome] || :success
|
199
|
+
|
200
|
+
oidresp = mock("OpenID Response: #{outcome}")
|
201
|
+
oidresp.expects(:status).returns(outcome == :success ? OpenID::Consumer::SUCCESS : OpenID::Consumer::CANCEL).at_least(0)
|
202
|
+
oidresp.expects(:identity_url).returns(identifier).at_least(0)
|
203
|
+
oidresp.expects(:display_identifier).returns(display_identifier).at_least(0)
|
204
|
+
OpenID::Consumer.any_instance.stubs(:complete).returns(oidresp)
|
205
|
+
|
206
|
+
if outcome == :success
|
207
|
+
email = options[:email] or (options[:open_id] and options[:open_id].user and options[:open_id].user.email)
|
208
|
+
name = options[:name] or (options[:open_id] and options[:open_id].user and options[:open_id].user.name)
|
209
|
+
nickname = options[:nickname] or (name and name.split[0]) # users never have a nickname
|
210
|
+
|
211
|
+
sreg_resp = mock('OpenID SReg Response')
|
212
|
+
sreg_resp.expects(:data).at_least(0).returns({
|
213
|
+
'email' => email,
|
214
|
+
'name' => name,
|
215
|
+
'nickname' => nickname
|
216
|
+
})
|
217
|
+
OpenID::SReg::Response.stubs(:from_success_response).returns(sreg_resp)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
data/lib/rails_openid.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# Copyright 2010 J. Pablo Fernández
|
3
|
+
|
1
4
|
require 'openid/extensions/sreg'
|
2
5
|
require 'openid/store/filesystem'
|
3
6
|
|
@@ -6,13 +9,14 @@ module RailsOpenId
|
|
6
9
|
# Create the OpenID request, and in the process, verify the URI is valid.
|
7
10
|
identifier = params[:openid_identifier]
|
8
11
|
if identifier.blank?
|
12
|
+
# TODO: throw an exception, don't redirect.
|
9
13
|
flash[:error] = "Please, enter an OpenID identifier (that is, your OpenID address)."
|
10
14
|
redirect_to fallback
|
11
15
|
return
|
12
16
|
end
|
13
|
-
|
17
|
+
|
14
18
|
oidreq = consumer.begin(identifier)
|
15
|
-
|
19
|
+
|
16
20
|
if not meta.empty?
|
17
21
|
# Request email, nickname and fullname.
|
18
22
|
sregreq = OpenID::SReg::Request.new
|
@@ -20,7 +24,7 @@ module RailsOpenId
|
|
20
24
|
oidreq.add_extension(sregreq)
|
21
25
|
oidreq.return_to_args['did_sreg'] = 'y'
|
22
26
|
end
|
23
|
-
|
27
|
+
|
24
28
|
if oidreq.send_redirect?(root_url, return_to)
|
25
29
|
redirect_to oidreq.redirect_url(root_url, return_to)
|
26
30
|
else
|
@@ -29,23 +33,27 @@ module RailsOpenId
|
|
29
33
|
render :text => oidreq.html_markup(root_url, root_url, :form_tag_attrs => {'id' => 'openid_form'})
|
30
34
|
end
|
31
35
|
rescue OpenID::OpenIDError => e
|
36
|
+
# TODO: throw an exception, don't redirect.
|
32
37
|
flash[:error] = "#{identifier} doesn't seem to be a valid, working OpenID. Maybe it has a typo?"
|
33
38
|
redirect_to fallback
|
34
39
|
return
|
35
40
|
end
|
36
|
-
|
41
|
+
|
37
42
|
def process_open_id_response(params, current_url, fallback)
|
38
43
|
parameters = params.reject {|k,v| request.path_parameters[k] }
|
39
44
|
oidresp = consumer.complete(parameters, current_url)
|
40
|
-
|
45
|
+
|
41
46
|
if oidresp.status == OpenID::Consumer::SUCCESS
|
42
|
-
data =
|
47
|
+
data = {}
|
43
48
|
if params[:did_sreg]
|
44
49
|
sreg_resp = OpenID::SReg::Response.from_success_response(oidresp)
|
45
|
-
data
|
50
|
+
data.merge! sreg_resp.data
|
46
51
|
end
|
47
|
-
|
52
|
+
data[:identity_url] = oidresp.identity_url
|
53
|
+
data[:display_identifier] = oidresp.display_identifier
|
54
|
+
return data
|
48
55
|
else
|
56
|
+
# TODO: throw an exception, don't redirect.
|
49
57
|
# Possible non-succes statuses: OpenID::Consumer::FAILURE, OpenID::Consumer::SETUP_NEEDED, OpenID::Consumer::CANCEL
|
50
58
|
if not oidresp.display_identifier.blank?
|
51
59
|
flash[:error] = "We couldn't verify your OpenID #{oidresp.display_identifier}."
|
@@ -56,9 +64,9 @@ module RailsOpenId
|
|
56
64
|
return nil
|
57
65
|
end
|
58
66
|
end
|
59
|
-
|
67
|
+
|
60
68
|
private
|
61
|
-
|
69
|
+
|
62
70
|
def consumer
|
63
71
|
if @consumer.nil?
|
64
72
|
dir = Pathname.new(RAILS_ROOT).join('db').join('cstore')
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_openid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- "J. Pablo Fern\xC3\xA1ndez"
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-03-13 00:00:00 +01:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -23,15 +28,21 @@ extra_rdoc_files:
|
|
23
28
|
- LICENSE
|
24
29
|
- README.rdoc
|
25
30
|
files:
|
26
|
-
- .document
|
27
|
-
- .gitignore
|
28
31
|
- LICENSE
|
29
32
|
- README.rdoc
|
30
33
|
- Rakefile
|
31
34
|
- VERSION
|
35
|
+
- generators/open_id_scaffold/USAGE
|
36
|
+
- generators/open_id_scaffold/open_id_scaffold_generator.rb
|
37
|
+
- generators/open_id_scaffold/templates/INSTALL
|
38
|
+
- generators/open_id_scaffold/templates/create_open_ids.rb
|
39
|
+
- generators/open_id_scaffold/templates/new.html.erb
|
40
|
+
- generators/open_id_scaffold/templates/open_id.rb
|
41
|
+
- generators/open_id_scaffold/templates/open_id_test.rb
|
42
|
+
- generators/open_id_scaffold/templates/open_ids.yml
|
43
|
+
- generators/open_id_scaffold/templates/sessions_controller.rb
|
44
|
+
- generators/open_id_scaffold/templates/sessions_controller_test.rb
|
32
45
|
- lib/rails_openid.rb
|
33
|
-
- test/helper.rb
|
34
|
-
- test/test_rails-openid.rb
|
35
46
|
has_rdoc: true
|
36
47
|
homepage: http://github.com/pupeno/rails_openid
|
37
48
|
licenses: []
|
@@ -45,18 +56,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
56
|
requirements:
|
46
57
|
- - ">="
|
47
58
|
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
48
61
|
version: "0"
|
49
|
-
version:
|
50
62
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
63
|
requirements:
|
52
64
|
- - ">="
|
53
65
|
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 0
|
54
68
|
version: "0"
|
55
|
-
version:
|
56
69
|
requirements: []
|
57
70
|
|
58
71
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.3.
|
72
|
+
rubygems_version: 1.3.6
|
60
73
|
signing_key:
|
61
74
|
specification_version: 3
|
62
75
|
summary: ruby-openid wrappers to make it simpler for a rails app
|
data/.document
DELETED