cassy 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,11 +27,21 @@ Create this configuration file at `config/cassy.yml`. Fill it with these values:
27
27
 
28
28
  The first two keys are the time-to-expiry for the login and service tickets respectively. The class for the authentication can be any constant which responds to a `validates` method. By default, only Devise authentication is supported at the moment.
29
29
 
30
+ Next, you will need to tell Cassy to load its routes in your application which you can do by calling `cassy` in `config/routes.rb`:
31
+
32
+ Rails.application.routes.draw do
33
+ cassy
34
+
35
+ # your routes go here
36
+ end
37
+
30
38
  Boom, done. Now this application will act as a CAS server.
31
39
 
40
+ For customization options please see the "Customization" section below.
41
+
32
42
  ## Configuration
33
43
 
34
- The configuration options for this gem go into a file, probably `config/cassy.yml` at the root of the project if you've set it up as advised, and allow the engine to be customised.
44
+ The configuration options for this gem goes into a file called `config/cassy.yml` at the root of the project if you've set it up as advised, and this allows the engine to be configured.
35
45
 
36
46
  These configuration options are detailed here for your convenience. For specific term definitions, please consult the CAS spec.
37
47
 
@@ -40,4 +50,21 @@ These configuration options are detailed here for your convenience. For specific
40
50
  `maximum_unused_service_ticket_lifetime`: The time before a service ticket would expire.
41
51
  `username_field`: Defines the field on the users table which is used for the lookup for the username. Defaults to "username".
42
52
  `username_label`: Allows for the "Username" label on the sign in page to be given a different value. Helpful if you want to call it "Email" or "User Name" instead.
53
+
54
+ ## Customization
55
+
56
+ ### Sessions Controller
57
+
58
+ In Cassy, it is possible to override the controller which is used for authentication. To do this, the controller can be configured in `config/routes.rb`:
59
+
60
+ cassy :controllers => "sessions"
61
+
62
+ By doing this, it will point at the `SessionsController` rather than the default of `Cassy::SessionsController`. This controller then should inherit from `Cassy::SessionsController` to inherit the original behaviour and will need to point to the views of Cassy:
63
+
64
+ class SessionsController < Cassy::SessionsController
65
+ def new
66
+ # custom behaviour goes here
67
+ super
68
+ end
69
+
43
70
 
@@ -1,22 +1,12 @@
1
+ require 'cassy/generators/views_generator'
2
+ require 'cassy/routes'
3
+
1
4
  module Cassy
2
5
  extend ActiveSupport::Autoload
3
6
 
4
7
  autoload :CAS
5
8
  autoload :Utils
6
9
  autoload :Engine
7
-
8
- def self.draw_routes
9
- Rails.application.routes.draw do
10
- scope(:path => "cas") do
11
- get 'login', :to => "cassy/sessions#new"
12
- post 'login', :to => "cassy/sessions#create"
13
-
14
- get 'logout', :to => "cassy/sessions#destroy"
15
-
16
- get 'serviceValidate', :to => "cassy/sessions#service_validate"
17
- end
18
- end
19
- end
20
10
 
21
11
  def self.root
22
12
  Pathname.new(File.dirname(__FILE__) + "../..")
@@ -13,6 +13,10 @@ module Cassy
13
13
  # You can leave this empty if you don't need to set up anything.
14
14
  def self.setup(options)
15
15
  end
16
+
17
+ def self.find_user(credentials)
18
+ raise NotImplementedError
19
+ end
16
20
 
17
21
  # This is called prior to #validate (i.e. each time the user tries to log in).
18
22
  # Any per-instance initialization for the authenticator should be done here.
@@ -25,6 +29,22 @@ module Cassy
25
29
  @extra_attributes = {}
26
30
  end
27
31
 
32
+ def self.extra_attributes
33
+ @extra_attributes
34
+ end
35
+
36
+ def self.extra_attributes_to_extract
37
+ if @options[:extra_attributes].kind_of? Array
38
+ attrs = @options[:extra_attributes]
39
+ elsif @options[:extra_attributes].kind_of? String
40
+ attrs = @options[:extra_attributes].split(',').collect{|col| col.strip}
41
+ else
42
+ attrs = []
43
+ end
44
+
45
+ attrs
46
+ end
47
+
28
48
  # Override this to implement your authentication credential validation.
29
49
  # This is called each time the user tries to log in. The credentials hash
30
50
  # holds the credentials as entered by the user (generally under :username
@@ -36,10 +56,6 @@ module Cassy
36
56
  raise NotImplementedError, "This method must be implemented by a class extending #{self.class}"
37
57
  end
38
58
 
39
- def extra_attributes
40
- @extra_attributes
41
- end
42
-
43
59
  protected
44
60
  def self.read_standard_credentials(credentials)
45
61
  @username = credentials[:username]
@@ -1,10 +1,15 @@
1
1
  module Cassy
2
2
  module Authenticators
3
3
  class Devise < Base
4
- def self.validate(credentials)
4
+
5
+ def self.find_user(credentials)
5
6
  # Find the user with the given email
6
7
  method = "find_by_#{Cassy.config[:username_field] || 'email'}"
7
- user = User.send(method, credentials[:username])
8
+ User.send(method, credentials[:username])
9
+ end
10
+
11
+ def self.validate(credentials)
12
+ user = find_user(credentials)
8
13
  # Did we find a user, and is their password valid?
9
14
  user && user.valid_password?(credentials[:password])
10
15
  end
@@ -11,12 +11,17 @@ class Cassy::Authenticators::Test < Cassy::Authenticators::Base
11
11
 
12
12
  raise Cassy::AuthenticatorError, "Username is 'do_error'!" if @username == 'do_error'
13
13
 
14
- # @extra_attributes[:test_utf_string] = "Ютф"
15
- # @extra_attributes[:test_numeric] = 123.45
16
- # @extra_attributes[:test_serialized] = {:foo => 'bar', :alpha => [1,2,3]}
17
-
18
14
  valid_password = options[:password] || "testpassword"
19
15
 
20
16
  return @password == valid_password
21
17
  end
18
+
19
+ def self.find_user(*args)
20
+ # To stop NotImplementedError raising
21
+ @user = Object.new
22
+ def @user.full_name
23
+ "Example User"
24
+ end
25
+ @user
26
+ end
22
27
  end
@@ -43,11 +43,12 @@ module Cassy
43
43
  # The optional 'extra_attributes' parameter takes a hash of additional attributes
44
44
  # that will be sent along with the username in the CAS response to subsequent
45
45
  # validation requests from clients.
46
- def generate_ticket_granting_ticket(username)
46
+ def generate_ticket_granting_ticket(username, extra_attributes={})
47
47
  # 3.6 (ticket granting cookie/ticket)
48
48
  tgt = Cassy::TicketGrantingTicket.new
49
49
  tgt.ticket = "TGC-" + Cassy::Utils.random_string
50
50
  tgt.username = username
51
+ tgt.extra_attributes = extra_attributes
51
52
  tgt.client_hostname = env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_HOST'] || env['REMOTE_ADDR']
52
53
  tgt.save!
53
54
  tgt
@@ -0,0 +1,16 @@
1
+ require 'rails/generators'
2
+ require 'tmpdir'
3
+
4
+ # "Borrowed" from Devise
5
+ module Cassy
6
+ module Generators
7
+ class ViewsGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("../../../../app/views", __FILE__)
9
+ desc "Copies all Cassy views to your application."
10
+
11
+ def copy_views
12
+ directory "cassy", "app/views/cassy"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+ def cassy(options={})
4
+ options[:controllers] ||= HashWithIndifferentAccess.new
5
+ options[:controllers][:sessions] ||= "cassy/sessions"
6
+ scope(:path => "cas") do
7
+ root :to => "#{options[:controllers][:sessions]}#new"
8
+ get 'login', :to => "#{options[:controllers][:sessions]}#new"
9
+ post 'login', :to => "#{options[:controllers][:sessions]}#create"
10
+
11
+ get 'logout', :to => "#{options[:controllers][:sessions]}#destroy"
12
+
13
+ get 'serviceValidate', :to => "#{options[:controllers][:sessions]}#service_validate"
14
+ get 'proxyValidate', :to => "#{options[:controllers][:sessions]}#proxy_validate"
15
+ end
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cassy
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.2
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - ryan@rubyx.com
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-28 00:00:00 +10:00
13
+ date: 2011-08-01 00:00:00 +10:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -116,7 +116,9 @@ files:
116
116
  - lib/cassy/authenticators.rb
117
117
  - lib/cassy/cas.rb
118
118
  - lib/cassy/engine.rb
119
+ - lib/cassy/generators/views_generator.rb
119
120
  - lib/cassy/models.rb
121
+ - lib/cassy/routes.rb
120
122
  - lib/cassy/utils.rb
121
123
  - lib/cassy.rb
122
124
  - lib/tasks/cassy_tasks.rake
@@ -137,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
139
  requirements:
138
140
  - - ">="
139
141
  - !ruby/object:Gem::Version
140
- hash: -1080296284138574585
142
+ hash: 2685991807394344055
141
143
  segments:
142
144
  - 0
143
145
  version: "0"
@@ -146,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
148
  requirements:
147
149
  - - ">="
148
150
  - !ruby/object:Gem::Version
149
- hash: -1080296284138574585
151
+ hash: 2685991807394344055
150
152
  segments:
151
153
  - 0
152
154
  version: "0"