authpds 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,4 +2,28 @@
2
2
 
3
3
  This project provides a mechanism for authenticating via Ex Libris' Patron Directory Services (PDS) and provides hooks for making authorization decisions based on the user information provided by PDS. It leverages the authlogic gem and depends on a User-like model.
4
4
 
5
- For con
5
+
6
+ Generate User-like model:
7
+ rails generate model User username:string email:string firstname:string \
8
+ lastname:string mobile_phone:string crypted_password:string password_salt:string \
9
+ session_id:string persistence_token:string login_count:string last_request_at:string \
10
+ current_login_at:string last_login_at:string last_login_ip:string current_login_ip:string \
11
+ user_attributes:text refreshed_at:datetime
12
+
13
+ Generate UserSession model
14
+ rails generate authlogic:session user_session
15
+
16
+ Create UserSessions controller
17
+ rails generate controller UserSessions --no-assets --no-helper
18
+
19
+ Mixin authpds methods into UserSessionsController
20
+ class UserSessionsController < ApplicationController
21
+ include Authpds::Controllers::AuthpdsUserSessionsController
22
+ end
23
+
24
+ Mixin authpds methods into ApplicationController
25
+ class ApplicationController < ActionController::Base
26
+ protect_from_forgery
27
+ require 'authpds'
28
+ include Authpds::Controllers::AuthpdsController
29
+ end
@@ -7,7 +7,8 @@ AUTHPDS_PATH = File.dirname(__FILE__) + "/authpds/"
7
7
  'institution',
8
8
  'institution_list',
9
9
  'exlibris/pds',
10
- 'controllers/authpds_controller'
10
+ 'controllers/authpds_controller',
11
+ 'controllers/authpds_user_sessions_controller'
11
12
  ].each do |library|
12
13
  require AUTHPDS_PATH + library
13
14
  end
@@ -28,9 +28,6 @@ module Authpds
28
28
 
29
29
  def primary_institution=(primary_institution)
30
30
  primary_institution = primary_institution.name if primary_institution.is_a?(Institution)
31
- raise ArgumentError.new(
32
- "Institution #{primary_institution} does not exist.\n" +
33
- "Please maker sure the institutions yaml file is configured correctly.") if InstitutionList.instance.get(primary_institution).nil?
34
31
  self.user_attributes=({:primary_institution => primary_institution})
35
32
  end
36
33
 
@@ -1,11 +1,12 @@
1
1
  class Institution < Struct.new(:display_name, :name, :default_institution,
2
2
  :application_layout, :ip_addresses, :parent_institution, :view_attributes, :login_attributes)
3
+ require 'ipaddr'
3
4
 
4
5
  # Better initializer than Struct gives us, take a hash instead
5
6
  # of an ordered array. :services=>[] is an array of service ids,
6
7
  # not actual Services!
7
- def initialize(h={}, controller)
8
- members.each {|m| self.send( ("#{m}=").to_sym , (h.delete(m.to_sym) || h.delete(m))) }
8
+ def initialize(h={})
9
+ members.each {|m| self.send( ("#{m}=").to_sym , (h.delete("#{m}".to_sym) || h.delete("#{m}"))) }
9
10
  default_institution = false unless default_institution
10
11
  # Log the fact that there are left overs in the hash
11
12
  # Rails.logger.warn("The following institution settings were ignored: #{h.inspect}.") unless h.empty?
@@ -20,14 +21,13 @@ class Institution < Struct.new(:display_name, :name, :default_institution,
20
21
  # Check the list of IP addresses for the given IP
21
22
  def includes_ip?(prospective_ip_address)
22
23
  return false if ip_addresses.nil?
23
- require 'ipaddr'
24
24
  ip_prospect = IPAddr.new(prospective_ip_address)
25
25
  ip_addresses.each do |ip_address|
26
26
  ip_range = (ip_address.match(/[\-\*]/)) ?
27
27
  (ip_address.match(/\-/)) ?
28
28
  (IPAddr.new(ip_address.split("-")[0])..IPAddr.new(ip_address.split("-")[1])) :
29
- (ip_address.gsub(/\*/, "0")..ip_address.gsub(/\*/, "255")) :
30
- IPAddr.new(ip_address).to_range
29
+ (IPAddr.new(ip_address.gsub(/\*/, "0"))..IPAddr.new(ip_address.gsub(/\*/, "255"))) :
30
+ IPAddr.new(ip_address).to_range
31
31
  return true if ip_range === ip_prospect unless ip_range.nil?
32
32
  end
33
33
  return false;
@@ -53,8 +53,8 @@ class InstitutionList
53
53
  institution_hash["name"] = institution_name
54
54
  # Merge with parent institution
55
55
  institution_hash =
56
- institution_list[institution_hash["parent_institution"]].
57
- merge(institution_hash) unless institution_hash["parent_institution"].nil?
56
+ institution_list[institution_hash["parent_institution"]].
57
+ merge(institution_hash) unless institution_hash["parent_institution"].nil?
58
58
  @institutions[institution_name] = Institution.new(institution_hash)
59
59
  end
60
60
  end
@@ -131,6 +131,12 @@ module Authpds
131
131
  rw_config(:pds_record_identifier, value, :id)
132
132
  end
133
133
  alias_method :pds_record_identifier=, :pds_record_identifier
134
+
135
+ # PDS user method to call to get users primary institution
136
+ def pds_record_primary_institution(value = nil)
137
+ rw_config(:pds_record_primary_institution, value, :institute)
138
+ end
139
+ alias_method :pds_record_primary_institution=, :pds_record_primary_institution
134
140
  end
135
141
 
136
142
  module AuthpdsCallbackMethods
@@ -138,7 +144,12 @@ module Authpds
138
144
  def pds_record_identifier
139
145
  self.class.pds_record_identifier
140
146
  end
141
-
147
+
148
+ # Hook for more complicated logic to determine PDS user primary institution
149
+ def pds_record_primary_institution
150
+ self.class.pds_record_primary_institution
151
+ end
152
+
142
153
  # Hook to determine if we should set up an SSO session
143
154
  def valid_sso_session?
144
155
  return false
@@ -242,6 +253,7 @@ module Authpds
242
253
  self.attempted_record.expiration_date = expiration_date
243
254
  # Do this part only if user data has expired.
244
255
  if self.attempted_record.expired?
256
+ self.attempted_record.primary_institution= pds_user.send(pds_record_primary_institution)
245
257
  pds_attributes.each { |user_attr, pds_attr|
246
258
  self.attempted_record.send("#{user_attr}=".to_sym, pds_user.send(pds_attr.to_sym)) if user.respond_to?("#{user_attr}=".to_sym) }
247
259
  # Set default pds user attributes
@@ -1,3 +1,3 @@
1
1
  module Authpds
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,14 +1,14 @@
1
1
  NYU:
2
2
  login_attributes:
3
- link_code: NYSID
4
- logout_url: 'http://bobcat.library.nyu.edu/nysid'
5
- default_institution: true
3
+ link_code: NYU
4
+ logout_url: 'http://bobcat.library.nyu.edu'
5
+ default_institution: false
6
6
  display_name: NYU Libraries
7
7
  view_attributes:
8
8
  test_view_attribute1: test_attribute1_nyu
9
9
  test_view_attribute2: test_attribute2_nyu
10
10
  ip_addresses:
11
- - 128.122.0.0-128.122.149.239
11
+ - 128.122.0.0-128.122.149.238
12
12
  - 172.26.*.*
13
13
  - 172.27.*.*
14
14
  - 172.22.88.*
@@ -16,7 +16,7 @@ NYU:
16
16
  - 128.238.*.*
17
17
 
18
18
  NYUAD:
19
- default_institution: false
19
+ default_institution: true
20
20
  display_name: NYUAD Library
21
21
  view_attributes:
22
22
  test_view_attribute1: test_attribute1_nyuad
@@ -41,7 +41,7 @@ require File.dirname(__FILE__) + '/../lib/authpds' unless defined?(Authpds)
41
41
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
42
42
 
43
43
  class ActiveSupport::TestCase
44
- VALID_PDS_HANDLE_FOR_NYU = '132012112947113134742310506860'
44
+ VALID_PDS_HANDLE_FOR_NYU = '232012135435113217012644778009'
45
45
  VALID_PDS_HANDLE_FOR_NEWSCHOOL = '272201212284614806184193096120278'
46
46
  VALID_PDS_HANDLE_FOR_COOPER = '272201212284614806184193096120278'
47
47
  INVALID_PDS_HANDLE = "Invalid"
@@ -72,7 +72,16 @@ class Authlogic::TestCase::MockController
72
72
  def performed?
73
73
  false
74
74
  end
75
- end
76
-
77
75
 
76
+ def request
77
+ @request ||= Authlogic::TestCase::MockRequest.new(self)
78
+ end
79
+
80
+ def env
81
+ @env ||= {'REMOTE_ADDR' => "128.122.149.239"}
82
+ end
83
+ end
78
84
 
85
+ class UserSessionsController < Authlogic::TestCase::MockController
86
+ include Authpds::Controllers::AuthpdsUserSessionsController
87
+ end
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+ class ApplicationControllerTest < ActiveSupport::TestCase
3
+
4
+ def setup
5
+ activate_authlogic
6
+ controller.session[:session_id] = "FakeSessionID"
7
+ InstitutionList.class_variable_set(:@@institutions_yaml_path, nil)
8
+ InstitutionList.instance.instance_variable_set(:@institutions, nil)
9
+ controller.instance_variable_set(:@current_primary_institution, nil)
10
+ end
11
+
12
+ test "current_user_session_nil" do
13
+ assert_nil(controller.current_user_session)
14
+ end
15
+
16
+ test "current_user_session" do
17
+ assert_nil(controller.current_user_session)
18
+ controller.cookies[:PDS_HANDLE] = { :value => VALID_PDS_HANDLE_FOR_NYU }
19
+ user_session = controller.current_user_session
20
+ assert_not_nil(user_session)
21
+ end
22
+
23
+ test "current_user_nil" do
24
+ assert_nil(controller.current_user)
25
+ end
26
+
27
+ test "current_user" do
28
+ assert_nil(controller.current_user)
29
+ controller.cookies[:PDS_HANDLE] = { :value => VALID_PDS_HANDLE_FOR_NYU }
30
+ user = controller.current_user
31
+ assert_not_nil(user)
32
+ assert_equal("N12162279", user.username)
33
+ end
34
+
35
+ test "current_primary_institution_nil" do
36
+ assert_nil(controller.current_primary_institution)
37
+ end
38
+
39
+ test "current_primary_institution_default" do
40
+ assert_nil(controller.current_primary_institution)
41
+ controller.request[:session_id] = "FakeSessionID"
42
+ InstitutionList.yaml_path= "#{File.dirname(__FILE__)}/../support/config/institutions.yml"
43
+ assert_equal(InstitutionList.instance.get("NYUAD"), controller.current_primary_institution)
44
+ end
45
+
46
+
47
+ test "current_primary_institution_user" do
48
+ assert_nil(controller.current_primary_institution)
49
+ InstitutionList.yaml_path= "#{File.dirname(__FILE__)}/../support/config/institutions.yml"
50
+ controller.cookies[:PDS_HANDLE] = { :value => VALID_PDS_HANDLE_FOR_NYU }
51
+ assert_equal("N12162279", controller.current_user.username)
52
+ assert_equal(InstitutionList.instance.get("NYU"), controller.current_user.primary_institution)
53
+ assert_equal(InstitutionList.instance.get("NYU"), controller.current_primary_institution)
54
+ end
55
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+ class UserSessionsControllerTest < ActiveSupport::TestCase
3
+
4
+ def setup
5
+ activate_authlogic
6
+ controller.session[:session_id] = "FakeSessionID"
7
+ controller.cookies[:PDS_HANDLE] = { :value => VALID_PDS_HANDLE_FOR_NYU }
8
+ end
9
+
10
+ test "current_user_session" do
11
+ user_session = controller.current_user_session
12
+ end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authpds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-01 00:00:00.000000000 Z
12
+ date: 2012-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &2151877740 !ruby/object:Gem::Requirement
16
+ requirement: &2151877500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2151877740
24
+ version_requirements: *2151877500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: authlogic
27
- requirement: &2151876500 !ruby/object:Gem::Requirement
27
+ requirement: &2151876340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2151876500
35
+ version_requirements: *2151876340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &2151871260 !ruby/object:Gem::Requirement
38
+ requirement: &2151871060 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2151871260
46
+ version_requirements: *2151871060
47
47
  description: Libraries that use Ex Libris products, can integrate Rails application
48
48
  with PDS to provide single sign-on across systems.
49
49
  email:
@@ -71,6 +71,8 @@ files:
71
71
  - test/support/user.rb
72
72
  - test/support/user_session.rb
73
73
  - test/test_helper.rb
74
+ - test/unit/authpds_controller_test.rb
75
+ - test/unit/authpds_user_sessions_controller_test.rb
74
76
  - test/unit/pds_test.rb
75
77
  - test/unit/user_session_test.rb
76
78
  - test/unit/user_test.rb
@@ -106,6 +108,8 @@ test_files:
106
108
  - test/support/user.rb
107
109
  - test/support/user_session.rb
108
110
  - test/test_helper.rb
111
+ - test/unit/authpds_controller_test.rb
112
+ - test/unit/authpds_user_sessions_controller_test.rb
109
113
  - test/unit/pds_test.rb
110
114
  - test/unit/user_session_test.rb
111
115
  - test/unit/user_test.rb