Fingertips-authentication-needed-san 1.0.0

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) 2009 Fingertips, Eloy Duran <eloy@fngtps.com>
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.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ == AuthenticationNeededSan
2
+
3
+ A thin wrapper around the Rails ‘flash’ object, to assist in redirecting a user
4
+ ‘back’ to the page she originally requested.
5
+
6
+ For more info see the AuthenticationNeededSan class documentation.
7
+
8
+ === Example
9
+
10
+ Consider an application which uses the authorization-san plugin. The
11
+ ApplicationController would look something like the following:
12
+
13
+ class ApplicationController < ActionController::Base
14
+ # If nobody was logged in and this resource is not accessable by all,
15
+ # request authentication. Otherwise reply that the resource is forbidden.
16
+ def access_forbidden
17
+ # If the user is logged in and still can't view the page, we have to tell
18
+ # them access is forbidden.
19
+ if !@authenticated.nil?
20
+ send_response_document :forbidden
21
+ else
22
+ authentication_needed!
23
+ end
24
+ end
25
+ end
26
+
27
+ The authorization-san plugin is available at:
28
+ http://github.com/Fingertips/authorization-san
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the authentication_needed_san plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the authentication_needed_san plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'AuthenticationNeeded-San'
20
+ rdoc.options << '--line-numbers' << '--inline-source' << '--charset=utf-8'
21
+ rdoc.rdoc_files.include('README.rdoc', 'lib/authentication_needed_san.rb', 'LICENSE')
22
+ end
23
+
24
+ begin
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |s|
27
+ s.name = "authentication-needed-san"
28
+ s.summary = s.description = "A thin wrapper around the Rails `flash' object to assist in redirecting the user `back' after authentication."
29
+ s.email = "eloy@fngtps.com"
30
+ s.homepage = "http://github.com/Fingertips/authentication-needed-san"
31
+ s.authors = ["Eloy Duran"]
32
+ end
33
+ rescue LoadError
34
+ end
35
+
36
+ begin
37
+ require 'jewelry_portfolio/tasks'
38
+ JewelryPortfolio::Tasks.new do |p|
39
+ p.account = 'Fingertips'
40
+ end
41
+ rescue LoadError
42
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 0
4
+ :patch: 0
@@ -0,0 +1,66 @@
1
+ # == AuthenticationNeededSan
2
+ #
3
+ # AuthenticationNeededSan is a module which assists your controllers in dealing
4
+ # with cases where authentication is needed, but you’d like to redirect the
5
+ # user ‘back’ to the page she originally requested once the authentication flow
6
+ # has been finished.
7
+ #
8
+ # Since it uses the +flash+ internally, the data _won't_ be around after
9
+ # the user makes another request. This is becasue you do not want the user
10
+ # to be redirected ‘back’ to a page ‘out of the blue’. Which is what would
11
+ # happen if we’d use the +session+.
12
+ #
13
+ # However, sometimes you might want to keep the data around for another
14
+ # request. Use still_authentication_needed! in this case.
15
+ module AuthenticationNeededSan
16
+ class ProtocolNotImplementedError < StandardError; end
17
+
18
+ # Returns a hash of options that need to be kept around until
19
+ # finish_authentication_needed! is called.
20
+ def after_authentication
21
+ flash[:after_authentication] ||= {}
22
+ end
23
+
24
+ # Call this method when authentication is needed and you want the user to
25
+ # be redirected back to the URL she requested.
26
+ #
27
+ # Any extra +options+ given will be available as well, through the
28
+ # after_authentication accessor.
29
+ #
30
+ # Your class should implement the +when_authentication_needed+ instance
31
+ # method, which you use to define what should happen when
32
+ # authentication_needed! is called. Normally you’d probably redirect the
33
+ # user to a ‘login’ page.
34
+ def authentication_needed!(options = {})
35
+ after_authentication.merge! options
36
+ after_authentication[:redirect_to] ||= request.url
37
+
38
+ if respond_to?(:when_authentication_needed, true)
39
+ when_authentication_needed
40
+ else
41
+ raise ProtocolNotImplementedError,
42
+ "[!] The class `#{self.class.name}' should implement #when_authentication_needed to define what should be done after #authentication_needed! is called."
43
+ end
44
+ end
45
+
46
+ # Returns whether or not there currently is any after_authentication data.
47
+ def authentication_needed?
48
+ !after_authentication.blank?
49
+ end
50
+
51
+ # Force the after_authentication to be available after the next request.
52
+ #
53
+ # You’d use this if, for instance, authentication failed and the user needs
54
+ # to try it again.
55
+ def still_authentication_needed!
56
+ flash.keep :after_authentication
57
+ end
58
+
59
+ # Finish the after_authentication flow, which means the user will be
60
+ # redirected ‘back’ to the page she originally requested _before_
61
+ # authentication_needed! was called.
62
+ def finish_authentication_needed!
63
+ flash.discard :after_authentication
64
+ redirect_to after_authentication[:redirect_to]
65
+ end
66
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'authentication_needed_san'
2
+ ApplicationController.send(:include, AuthenticationNeededSan)
@@ -0,0 +1,100 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class TestController < ApplicationController
4
+ def does_not_need_authentication
5
+ render :nothing => true
6
+ end
7
+
8
+ def needs_authentication
9
+ authentication_needed! :extra_option => "I was merged!"
10
+ end
11
+
12
+ def needs_more_authentication
13
+ still_authentication_needed!
14
+ render :nothing => true
15
+ end
16
+
17
+ def authenticate
18
+ finish_authentication_needed!
19
+ end
20
+
21
+ private
22
+
23
+ def when_authentication_needed
24
+ redirect_to new_session_url
25
+ end
26
+
27
+ def new_session_url
28
+ "http://test/sessions/new"
29
+ end
30
+ end
31
+
32
+ class AuthenticationNeededTest < ActionController::TestCase
33
+ tests TestController
34
+
35
+ test "should set a redirect_to value, which is the requested url, if authentication is needed" do
36
+ get :needs_authentication
37
+ assert_equal url_for(:needs_authentication), flash[:after_authentication][:redirect_to]
38
+ end
39
+
40
+ test "should merge extra options into the after_authentication hash" do
41
+ get :needs_authentication
42
+ assert_equal "I was merged!", flash[:after_authentication][:extra_option]
43
+ end
44
+
45
+ test "should invoke the when_authentication_needed instance method after #authentication_needed! is done" do
46
+ get :needs_authentication
47
+ assert_redirected_to new_session_url
48
+ end
49
+
50
+ test "should raise a AuthenticationNeededSan::ProtocolNotImplementedError if the class does not implement the when_authentication_needed instance method" do
51
+ class << @controller
52
+ undef :when_authentication_needed
53
+ end
54
+
55
+ assert_raises(AuthenticationNeededSan::ProtocolNotImplementedError) { get :needs_authentication }
56
+ end
57
+
58
+ test "should return `false' if authentication is not needed" do
59
+ get :does_not_need_authentication
60
+ assert !@controller.send(:authentication_needed?)
61
+ end
62
+
63
+ test "should return `true' if authentication is needed" do
64
+ get :needs_authentication
65
+ assert @controller.send(:authentication_needed?)
66
+ end
67
+
68
+ test "should allow the authentication_needed data to survive an extra request if authentication is still needed" do
69
+ flash = stubbed_flash
70
+ flash.expects(:keep).with(:after_authentication)
71
+ get :needs_more_authentication, {}, {}, flash
72
+ end
73
+
74
+ test "should redirect back to original user’s requested URL after authentication" do
75
+ get :authenticate, {}, {}, { :after_authentication => { :redirect_to => new_session_url } }
76
+ assert_redirected_to new_session_url
77
+ end
78
+
79
+ test "should discard the :after_authentication data when #finish_authentication_needed! is called" do
80
+ flash = stubbed_flash
81
+ flash.expects(:discard).with(:after_authentication)
82
+ get :authenticate, {}, {}, flash
83
+ end
84
+
85
+ private
86
+
87
+ def url_for(action)
88
+ @controller.url_for(:action => action)
89
+ end
90
+
91
+ def new_session_url
92
+ @controller.send :new_session_url
93
+ end
94
+
95
+ def stubbed_flash
96
+ flash = { :after_authentication => { :redirect_to => new_session_url } }
97
+ @controller.stubs(:flash).returns(flash)
98
+ flash
99
+ end
100
+ end
@@ -0,0 +1,19 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ begin
4
+ require 'rubygems'
5
+ rescue LoadError
6
+ end
7
+ require 'active_support'
8
+ require 'action_controller'
9
+ require 'action_controller/test_case'
10
+
11
+ require 'test/unit'
12
+
13
+ ActionController::Base.logger = nil
14
+ ActionController::Base.ignore_missing_templates = false if ActionController::Base.respond_to?(:ignore_missing_templates)
15
+ ActionController::Routing::Routes.reload rescue nil
16
+
17
+ class ApplicationController < ActionController::Base; end
18
+
19
+ require File.expand_path('../../rails/init', __FILE__)
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Fingertips-authentication-needed-san
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eloy Duran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A thin wrapper around the Rails `flash' object to assist in redirecting the user `back' after authentication.
17
+ email: eloy@fngtps.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/authentication_needed_san.rb
31
+ - rails/init.rb
32
+ - test/authentication_needed_san_test.rb
33
+ - test/test_helper.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/Fingertips/authentication-needed-san
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: A thin wrapper around the Rails `flash' object to assist in redirecting the user `back' after authentication.
60
+ test_files:
61
+ - test/authentication_needed_san_test.rb
62
+ - test/test_helper.rb