danwrong-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,21 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ spec = eval(File.read(File.join(File.dirname(__FILE__), 'merb_openid.gemspec')))
5
+
6
+ Rake::GemPackageTask.new(spec) do |pkg|
7
+ pkg.gem_spec = spec
8
+ end
9
+
10
+ task :install => [:package] do
11
+ sh %{sudo gem install pkg/#{spec.name}-#{spec.version} --no-update-sources}
12
+ end
13
+
14
+ namespace :jruby do
15
+
16
+ desc "Run :package and install the resulting .gem with jruby"
17
+ task :install => :package do
18
+ sh %{#{SUDO} jruby -S gem install pkg/#{spec.name}-#{Merb::VERSION}.gem --no-rdoc --no-ri}
19
+ end
20
+
21
+ end
@@ -0,0 +1,110 @@
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.blank?) || 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
+ def openid_consumer
32
+ @@openid_consumer ||= OpenID::Consumer.new session, MerbOpenID.store
33
+ end
34
+
35
+ private
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
+ yield :setup_needed, identity_url, response.setup_url
67
+ end
68
+ rescue Timeout::Error
69
+ yield :failed
70
+ end
71
+ end
72
+
73
+ def add_sreg_fields(request, fields)
74
+ if fields.is_a?(Array)
75
+ required, optional = fields, []
76
+ else
77
+ required = fields[:required] || []
78
+ optional = fields[:optional] || []
79
+ end
80
+
81
+ sreg = OpenID::SReg::Request.new
82
+
83
+ sreg.request_fields(required.collect { |f| f.to_s }, true) unless required.empty?
84
+ sreg.request_fields(optional.collect { |f| f.to_s }, false) unless optional.empty?
85
+
86
+ request.add_extension(sreg)
87
+ end
88
+
89
+ def openid_provider_url(request, immediate)
90
+ request.return_to_args['openid_complete'] = '1'
91
+ request.redirect_url(openid_trust_root, openid_return_to, immediate)
92
+ end
93
+
94
+ def openid_trust_root
95
+ @openid_trust_root ||= [request.protocol, request.host, "/"].join
96
+ end
97
+
98
+ def openid_return_to
99
+ @openid_return_to ||= [request.protocol, request.host, request.uri].join
100
+ end
101
+
102
+ end
103
+
104
+ end
105
+
106
+ Merb::Controller.class_eval do
107
+ include MerbOpenID::ControllerExtensions
108
+ end
109
+
110
+ MerbOpenID.store = (Merb::Config[:merb_openid] && Merb::Config[:merb_openid][:store]) || OpenID::Store::Memory.new
@@ -0,0 +1,12 @@
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
+ end
11
+
12
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "merb_openid" do
4
+ it "should do nothing" do
5
+ true.should == true
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: danwrong-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-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: ruby-openid
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 2.0.3
32
+ version:
33
+ description: A Merb plugin for consuming OpenID
34
+ email: dan@danwebb.net
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ - LICENSE
42
+ files:
43
+ - LICENSE
44
+ - README
45
+ - Rakefile
46
+ - lib/merb_openid.rb
47
+ - lib/merb_openid/controller_extensions.rb
48
+ - spec/merb_openid_spec.rb
49
+ - spec/spec_helper.rb
50
+ has_rdoc: true
51
+ homepage:
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.0.1
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: A Merb plugin for consuming OpenID
76
+ test_files: []
77
+