merb_openid 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Dan Webb
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,68 @@
1
+ merb_openid
2
+ ===========
3
+
4
+ A plugin for Merb that wraps the ruby-openid gem (2.x) and provides an easy interface for consuming OpenIDs
5
+ in a very similar way to the open_id_authentication plugin for Rails.
6
+
7
+ Installation
8
+ ============
9
+
10
+ Easy:
11
+
12
+ gem install danwrong-merb_openid --source http://gems.github.com
13
+
14
+ Then in your application's init.rb:
15
+
16
+ dependency 'merb_openid'
17
+
18
+ The plugin uses the pretty limited memory store by default but you can add a new store using Merb::Config:
19
+
20
+ Merb::Config[:merb_openid][:store] = OpenID::Store::Memory.new
21
+
22
+ You'll probably want to use a database store on production apps. I'll start adding adapters based on the
23
+ various ORMs soon.
24
+
25
+ Usage
26
+ =====
27
+
28
+ In your routes you need to make sure that the url you consume OpenIDs from can accept get requests, so:
29
+
30
+ r.match('openid').(:controller => 'session', :action => 'openid')
31
+
32
+ Then in your controller:
33
+
34
+ class Session < Merb::Controller
35
+
36
+ def openid
37
+ if openid_request? # has the user provided a url (openid_url)
38
+ openid_authenticate do |result, identity_url|
39
+ if result == :success
40
+ user = User.find_by_openid_url(identity_url)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ SReg
49
+ ====
50
+
51
+ Getting SReg data is easy too:
52
+
53
+ class Session < Merb::Controller
54
+
55
+ def openid
56
+ if openid_request? # has the user provided a url (openid_url)
57
+ openid_authenticate(:fields => [:fullname, :email]) do |result, identity_url, sreg|
58
+ if result == :success
59
+ user = User.find_by_openid_url(identity_url)
60
+ user.name = sreg[:fullname]
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ More to come!
data/Rakefile ADDED
@@ -0,0 +1,75 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/contrib/rubyforgepublisher'
4
+
5
+ spec = eval(File.read(File.join(File.dirname(__FILE__), 'merb_openid.gemspec')))
6
+
7
+ Rake::GemPackageTask.new(spec) do |pkg|
8
+ pkg.gem_spec = spec
9
+ pkg.need_zip = true
10
+ pkg.need_tar = true
11
+ end
12
+
13
+ task :install => [:package] do
14
+ sh %{sudo gem install pkg/#{spec.name}-#{spec.version} --no-update-sources}
15
+ end
16
+
17
+ task :verify_user do
18
+ raise "RUBYFORGE_USER environment variable not set!" unless ENV['RUBYFORGE_USER']
19
+ end
20
+
21
+ desc "Publish gem+tgz+zip on RubyForge. You must make sure lib/version.rb is aligned with the CHANGELOG file"
22
+ task :publish_packages => [:verify_user, :package] do
23
+ package_name = [spec.name, spec.version].join '-'
24
+
25
+ release_files = FileList[
26
+ "pkg/#{package_name}.gem",
27
+ "pkg/#{package_name}.tgz",
28
+ "pkg/#{package_name}.zip"
29
+ ]
30
+ unless spec.version =~ /RC[0-9]$/
31
+ require 'meta_project'
32
+ require 'rake/contrib/xforge'
33
+
34
+ Rake::XForge::Release.new(MetaProject::Project::XForge::RubyForge.new('merbopenid')) do |xf|
35
+ # Never hardcode user name and password in the Rakefile!
36
+ xf.user_name = ENV['RUBYFORGE_USER']
37
+ xf.files = release_files.to_a
38
+ xf.release_name = "Merb OpenID #{spec.version}"
39
+ xf.release_notes = ''
40
+ xf.release_changes = ''
41
+ end
42
+ else
43
+ puts "SINCE THIS IS A PRERELEASE, FILES ARE UPLOADED WITH SSH, NOT TO THE RUBYFORGE FILE SECTION"
44
+ puts "YOU MUST TYPE THE PASSWORD #{release_files.length} TIMES..."
45
+
46
+ host = "merbopenid-website@rubyforge.org"
47
+ remote_dir = "/var/www/gforge-projects/merbopenid"
48
+
49
+ publisher = Rake::SshFilePublisher.new(
50
+ host,
51
+ remote_dir,
52
+ File.dirname(__FILE__),
53
+ *release_files
54
+ )
55
+ publisher.upload
56
+
57
+ puts "UPLOADED THE FOLLOWING FILES:"
58
+ release_files.each do |file|
59
+ name = file.match(/pkg\/(.*)/)[1]
60
+ puts "* http://merbopenid.rubyforge.org/#{name}"
61
+ end
62
+
63
+ puts "They are not linked to anywhere, so don't forget to tell people!"
64
+ end
65
+ end
66
+
67
+
68
+ namespace :jruby do
69
+
70
+ desc "Run :package and install the resulting .gem with jruby"
71
+ task :install => :package do
72
+ sh %{#{SUDO} jruby -S gem install pkg/#{spec.name}-#{Merb::VERSION}.gem --no-rdoc --no-ri}
73
+ end
74
+
75
+ end
@@ -0,0 +1,105 @@
1
+ require 'openid'
2
+ require 'openid/extensions/sreg'
3
+ require 'openid/store/memory'
4
+
5
+ module MerbOpenID
6
+
7
+ def self.store
8
+ @@store
9
+ end
10
+
11
+ def self.store=(val)
12
+ @@store = val
13
+ end
14
+
15
+ module ControllerExtensions
16
+
17
+ def openid_request?(open_id_param=params[:openid_url])
18
+ !!((open_id_param && !open_id_param.strip.empty?) || params[:openid_complete])
19
+ end
20
+
21
+ def openid_authenticate(options={}, &block)
22
+ open_id_param = options.delete(:open_id_param) || params[:openid_url]
23
+
24
+ unless params[:openid_complete]
25
+ begin_openid_authentication(open_id_param, options, &block)
26
+ else
27
+ complete_openid_authentication(&block)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def openid_consumer
34
+ @@openid_consumer ||= OpenID::Consumer.new session, MerbOpenID.store
35
+ end
36
+
37
+ def begin_openid_authentication(openid_url, options={})
38
+ fields = options[:sreg] || {}
39
+ immediate = (options[:immediate] === true)
40
+
41
+ request = openid_consumer.begin(openid_url)
42
+
43
+ add_sreg_fields(request, fields)
44
+
45
+ redirect openid_provider_url(request, immediate)
46
+
47
+ rescue OpenID::OpenIDError, Timeout::Error
48
+ yield :missing
49
+ end
50
+
51
+ def complete_openid_authentication
52
+ query_string_params = Merb::Request.query_parse(request.query_string)
53
+
54
+ begin
55
+ response = openid_consumer.complete(query_string_params, openid_return_to)
56
+ identity_url = response.endpoint.claimed_id
57
+
58
+ case response.status
59
+ when OpenID::Consumer::SUCCESS
60
+ yield :successful, identity_url, OpenID::SReg::Response.from_success_response(response)
61
+ when OpenID::Consumer::CANCEL
62
+ yield :cancelled, identity_url, nil
63
+ when OpenID::Consumer::FAILURE
64
+ yield :failed, identity_url, nil
65
+ when OpenID::Consumer::SETUP_NEEDED
66
+ # get around ruby openid 2.x bug
67
+ yield :setup_needed, identity_url, response.instance_variable_get(:@setup_url)
68
+ end
69
+ rescue Timeout::Error
70
+ yield :failed
71
+ end
72
+ end
73
+
74
+ def add_sreg_fields(request, fields)
75
+ if fields.is_a?(Array)
76
+ required, optional = fields, []
77
+ else
78
+ required = fields[:required] || []
79
+ optional = fields[:optional] || []
80
+ end
81
+
82
+ sreg = OpenID::SReg::Request.new
83
+
84
+ sreg.request_fields(required.collect { |f| f.to_s }, true) unless required.empty?
85
+ sreg.request_fields(optional.collect { |f| f.to_s }, false) unless optional.empty?
86
+
87
+ request.add_extension(sreg)
88
+ end
89
+
90
+ def openid_provider_url(request, immediate)
91
+ request.return_to_args['openid_complete'] = '1'
92
+ request.redirect_url(openid_trust_root, openid_return_to, immediate)
93
+ end
94
+
95
+ def openid_trust_root
96
+ @openid_trust_root ||= [request.protocol, request.host, "/"].join
97
+ end
98
+
99
+ def openid_return_to
100
+ @openid_return_to ||= [request.protocol, request.host, request.uri].join
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -0,0 +1,15 @@
1
+ if defined?(Merb::Plugins)
2
+
3
+ Merb::BootLoader.before_app_loads do
4
+ dependency 'openid', '>= 2.0.0'
5
+ Merb::Config[:merb_openid] ||= {}
6
+ end
7
+
8
+ Merb::BootLoader.after_app_loads do
9
+ require 'merb_openid/controller_extensions'
10
+
11
+ Merb::Controller.class_eval { include MerbOpenID::ControllerExtensions }
12
+ MerbOpenID.store = (Merb::Config[:merb_openid] && Merb::Config[:merb_openid][:store]) || OpenID::Store::Memory.new
13
+ end
14
+
15
+ end
@@ -0,0 +1,90 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'merb_openid/controller_extensions'
3
+
4
+ describe "Controller with merb_openid mixed in" do
5
+ before(:each) do
6
+ mock_consumer = mock('openid_consumer')
7
+
8
+ mock_openid_request = mock('openid_request')
9
+ mock_openid_request.stub!(:return_to_args).and_return({})
10
+ mock_openid_request.stub!(:add_extension)
11
+ mock_openid_request.stub!(:redirect_url).and_return('http://openidprovider.com')
12
+
13
+
14
+ @controller = Class.new {
15
+ include MerbOpenID::ControllerExtensions
16
+
17
+ attr_accessor :params, :session, :mock_consumer, :mock_request
18
+
19
+ def initialize
20
+ @params, @session = {}, {}
21
+ end
22
+
23
+ def openid_consumer
24
+ @mock_openid_consumer ||= begin
25
+ c = mock_consumer
26
+ c.stub!(:begin).and_return(mock_request)
27
+ c
28
+ end
29
+ end
30
+
31
+ def request
32
+ Struct.new(:protocol, :host, :uri).new('http://', 'danwebb.net', '/foreskin')
33
+ end
34
+
35
+ def redirect(url)
36
+ end
37
+
38
+ }.new
39
+
40
+ @controller.mock_consumer = mock_consumer
41
+ @controller.mock_request = mock_openid_request
42
+ end
43
+
44
+ it 'should be an OpenID request if openid_url param provided' do
45
+ @controller.params[:openid_url] = 'aurl.com'
46
+
47
+ @controller.should be_openid_request
48
+ end
49
+
50
+ it 'should be an OpenID request if open_id_complete param is provided' do
51
+ @controller.params[:openid_complete] = '1'
52
+
53
+ @controller.should be_openid_request
54
+ end
55
+
56
+ it 'should not be an openid request if openid_url is provided but its blank' do
57
+ @controller.params[:openid_url] = ' '
58
+
59
+ @controller.should_not be_openid_request
60
+ end
61
+
62
+ it 'openid_authenticate should begin a OpenID request if openid_complete is not in the params hash' do
63
+ @controller.params[:openid_url] = 'danwebb.net'
64
+ @controller.should_receive(:begin_openid_authentication)
65
+ @controller.openid_authenticate
66
+ end
67
+
68
+ it 'openid_authenticate should complete a OpenID request if openid_complete is in the params hash' do
69
+ @controller.params[:openid_complete] = '1'
70
+ @controller.should_receive(:complete_openid_authentication)
71
+ @controller.openid_authenticate
72
+ end
73
+
74
+ it 'should add sreg fields if fields option given' do
75
+ @controller.params[:openid_url] = 'danwebb.net'
76
+
77
+ @controller.mock_request.should_receive(:add_extension)
78
+
79
+ @controller.openid_authenticate(:sreg => [:email])
80
+ end
81
+
82
+ it 'should redirect to OpenID provider' do
83
+ @controller.params[:openid_url] = 'danwebb.net'
84
+
85
+ @controller.should_receive(:redirect).with('http://openidprovider.com')
86
+
87
+ @controller.openid_authenticate
88
+ end
89
+
90
+ end
@@ -0,0 +1,2 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merb_openid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Dan Webb
8
+ autorequire: merb_openid
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-25 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ruby-openid
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.3
34
+ version:
35
+ description: A Merb plugin for consuming OpenID
36
+ email: dan@danwebb.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ - LICENSE
44
+ files:
45
+ - LICENSE
46
+ - README
47
+ - Rakefile
48
+ - lib/merb_openid.rb
49
+ - lib/merb_openid/controller_extensions.rb
50
+ - spec/merb_openid_spec.rb
51
+ - spec/spec_helper.rb
52
+ has_rdoc: true
53
+ homepage:
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.2.0
75
+ signing_key:
76
+ specification_version: 2
77
+ summary: A Merb plugin for consuming OpenID
78
+ test_files: []
79
+