casful_authentication_generator 0.1.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 Brian Hogan
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,67 @@
1
+ = casful_authentication_generator
2
+
3
+ Helper methods to make authenticating your site with CAS a little easier. Based on Restful Authentication, and uses the rubycas-client plugin.
4
+
5
+ == Installation
6
+
7
+ sudo gem install casful_authentication_generator
8
+
9
+ == Usage
10
+
11
+ In your Rails application's root folder, run this:
12
+
13
+ ruby script/generate casful_authentication user https://cas.example.com/
14
+
15
+ This will install the rubycas-client plugin and will create the helper file and the initializer to set up CAS.
16
+
17
+ Then, add
18
+
19
+ include CasAuthentication
20
+
21
+ to either ApplicationController or specific controllers that need protecting.
22
+
23
+ Finally, use
24
+
25
+ before_filter :login_required
26
+
27
+ to protect your controllers.
28
+
29
+ == Customization
30
+
31
+ There are a few ways to customize this code:
32
+
33
+ == Locating users
34
+
35
+ The plugin assumes you have a User table and each user has a <tt>login</tt> attribute. When CAS sends the username back to your app, the helpers will look up the user in your table by the login name.
36
+
37
+ Want to change this? Redefine the <tt>locate_user</tt> method in your controller.
38
+
39
+ def locate_user
40
+ User.find_by_username(session[:cas_user]) if session[:cas_user]
41
+ end
42
+
43
+ The name of this method depends on how you called the generator. aIt would be <tt>locate_account</tt> if you generated the code using "account" instead of "user".
44
+
45
+ == Access Denied
46
+
47
+ By default, the access_denied method will be called if the user supplied by CAS is not found in your system. If you want to change the behavior, implement the method <tt>access_denied</tt> in your controller.
48
+
49
+ def access_denied
50
+ render :text => "You don't have access to this"
51
+ end
52
+
53
+ == Skipping plugin installation
54
+
55
+ Run the generator with the --skip-plugin switch to keep it from installing the rubycas-client plugin.
56
+
57
+
58
+ == History
59
+
60
+ === 0.1.0 = May 26th, 2009
61
+ Initial release
62
+
63
+
64
+
65
+ == Copyright
66
+
67
+ Copyright (c) 2009 Brian Hogan. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "casful_authentication_generator"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "brianhogan@napcs.com"
10
+ gem.homepage = "http://github.com/napcs/casful_authentication_generator"
11
+ gem.authors = ["Brian Hogan"]
12
+ gem.require_paths = ['.']
13
+ gem.files = FileList["[A-Z]*", "templates/*", "*.rb" ]
14
+ gem.rubyforge_project = "casfulauth"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/*_test.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/*_test.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ if File.exist?('VERSION.yml')
48
+ config = YAML.load(File.read('VERSION.yml'))
49
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
50
+ else
51
+ version = ""
52
+ end
53
+
54
+ rdoc.rdoc_dir = 'rdoc'
55
+ rdoc.title = "casful_authentication_generator #{version}"
56
+ rdoc.rdoc_files.include('README*')
57
+ rdoc.rdoc_files.include('lib/**/*.rb')
58
+ end
59
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,60 @@
1
+ class CasfulAuthenticationGenerator < Rails::Generator::NamedBase
2
+
3
+ attr_reader :cas_server
4
+ def add_options!(opt)
5
+ opt.separator ''
6
+ opt.separator 'Options:'
7
+ opt.on('--skip-plugin', "does not install the plugin") { |value| options[:skip_plugin] = value }
8
+ end
9
+
10
+ def banner
11
+ "Usage: #{$0} casful_authentication ModelName https://cas.example.com/cas/"
12
+ end
13
+
14
+ def initialize(runtime_args, runtime_options = {})
15
+
16
+ super
17
+ runtime_args.shift.inspect
18
+ @cas_server = runtime_args.shift
19
+ end
20
+
21
+ def manifest
22
+
23
+
24
+ record do |m|
25
+
26
+
27
+ unless options[:skip_plugin]
28
+ puts "Installing the Cas plugin from"
29
+ puts "http://rubycas-client.googlecode.com/svn/trunk/rubycas-client"
30
+ `ruby script/plugin install http://rubycas-client.googlecode.com/svn/trunk/rubycas-client --force`
31
+ end
32
+
33
+ m.template "cas_authentication.rb", "lib/cas_authentication.rb"
34
+ m.template "cas.rb", "config/initializers/cas.rb"
35
+
36
+ output = %Q{
37
+ Done! Be sure to add
38
+
39
+ include CasAuthentication
40
+
41
+ to your ApplicationController.
42
+
43
+ Then just add
44
+
45
+ before_filter :login_required
46
+
47
+ to any controllers that need CAS authentication.
48
+
49
+ }
50
+
51
+ puts output
52
+ end
53
+
54
+
55
+
56
+
57
+
58
+ end
59
+
60
+ end
data/templates/cas.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'casclient'
2
+ require 'casclient/frameworks/rails/filter'
3
+
4
+ CASClient::Frameworks::Rails::Filter.configure(
5
+ :cas_base_url => "<%=cas_server %>"
6
+ )
@@ -0,0 +1,78 @@
1
+ module CasAuthentication
2
+ protected
3
+
4
+ # Inclusion hook to make #current_user and #logged_in?
5
+ # available as ActionView helper methods.
6
+ def self.included(base)
7
+ base.send :helper_method, :current_<%=singular_name %>, :logged_in?
8
+ end
9
+
10
+ def logged_in?
11
+ !!current_<%=singular_name %>
12
+ end
13
+
14
+ def login_required
15
+ logged_in?
16
+ end
17
+
18
+ def login_from_cas
19
+ unless session[:cas_user]
20
+ ## HACK HACK HACK.
21
+ # This is gonna get called twice. The first time it gets called is the redirection
22
+ # to CAS. This needs to redirect. We will explicitly return
23
+ # if we don't have a cas_user in the session.
24
+ # The second time through the CAS filter will not redirect. The return
25
+ # is not necessary, and in fact we'll never get this far if CAS didn't
26
+ # let them through, so session[:cas_user] WILL have data in it, so we will
27
+ # not return.
28
+ CASClient::Frameworks::Rails::Filter.filter(self)
29
+ return unless session[:cas_user]
30
+ end
31
+ # look them up in our system - based on restful_auth's login_from_session
32
+ self.current_user = locate_<%=singular_name %>
33
+ access_denied if self.current_<%=singular_name %>.nil?
34
+ end
35
+
36
+ # override this in your own controller
37
+ # if you want different rules, for example
38
+ # if you would like to find only active users,
39
+ # or users of a certain role.
40
+ def locate_<%=singular_name %>
41
+ <%=class_name %>.find_by_username(session[:cas_user]) if session[:cas_user]
42
+ end
43
+
44
+
45
+ # feel free to override this method in your own controller
46
+ # to handle situations where they get past CAS but
47
+ # don't get past your own internal database.
48
+ def access_denied
49
+ render :text => "You don't have access to this"
50
+ end
51
+
52
+
53
+ # Accesses the current user from the session.
54
+ # Future calls avoid the database because nil is not equal to false.
55
+ def current_<%=singular_name %>
56
+ @current_user ||= (login_from_cas) unless @current_<%=singular_name %> == false
57
+ end
58
+
59
+ # Store the given user id in the session.
60
+ def current_<%=singular_name %>=(new_<%=singular_name %>)
61
+ @current_<%=singular_name %> = new_<%=singular_name %> || false
62
+ end
63
+
64
+ # Store the URI of the current request in the session.
65
+ #
66
+ # We can return to this location by calling #redirect_back_or_default.
67
+ def store_location
68
+ session[:return_to] = request.request_uri
69
+ end
70
+
71
+ # Redirect to the URI stored by the most recent store_location call or
72
+ # to the passed default.
73
+ def redirect_back_or_default(default)
74
+ redirect_to(session[:return_to] || default)
75
+ session[:return_to] = nil
76
+ end
77
+
78
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class CasfulAuthenticationGeneratorTest < Test::Unit::TestCase
4
+
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'casful_authentication_generator'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: casful_authentication_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Hogan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-26 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: brianhogan@napcs.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
30
+ - casful_authentication_generator.rb
31
+ - templates/cas.rb
32
+ - templates/cas_authentication.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/napcs/casful_authentication_generator
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - .
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project: casfulauth
55
+ rubygems_version: 1.3.1
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: TODO
59
+ test_files:
60
+ - test/casful_authentication_generator_test.rb
61
+ - test/test_helper.rb