authpds 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module Authpds
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ namespace :authpds
2
+ # desc "Explaining what the task does"
3
+ # task :authpds do
4
+ # # Task goes here
5
+ # end
6
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class AuthpdsTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, Authpds
6
+ end
7
+ end
@@ -0,0 +1,49 @@
1
+ scot:
2
+ email: scot.dalton@gmail.com
3
+ password_salt: <%= salt = Authlogic::Random.hex_token %>
4
+ crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("scot" + salt) %>
5
+ persistence_token: <%= Authlogic::Random.hex_token %>
6
+
7
+ ba36:
8
+ id: 1
9
+ password_salt: <%= salt = Authlogic::Random.hex_token %>
10
+ crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("ba36" + salt) %>
11
+ persistence_token: <%= Authlogic::Random.hex_token %>
12
+ username: ba36
13
+ email: barnaby.alter@nyu.edu
14
+ firstname: Barnaby
15
+ lastname: Alter
16
+ user_attributes: <%= {
17
+ :nyuidn => "N18158418",
18
+ :verification => "6EAF453D8B01E5AC27D9",
19
+ :bor_status => "51",
20
+ :aleph_permissions => {} }.to_yaml.inspect %>
21
+ # std5:
22
+ # password_salt: <%#= salt = Authlogic::Random.hex_token %>
23
+ # crypted_password: <%#= Authlogic::CryptoProviders::Sha512.encrypt("std5" + salt) %>
24
+ # persistence_token: <%#= Authlogic::Random.hex_token %>
25
+ # username: std5
26
+ # email: scot.dalton@nyu.edu
27
+ # firstname: Scot
28
+ # lastname: Dalton
29
+ # user_attributes: <%#= {
30
+ # :nyuidn => "N12162279",
31
+ # :verification => "d4465aacaa645f2164908cd4184c09f0",
32
+ # :bor_status => "51",
33
+ # :aleph_permissions => {} }.to_yaml.inspect %>
34
+
35
+ st75:
36
+ id: 3
37
+ password_salt: <%= salt = Authlogic::Random.hex_token %>
38
+ crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("st75" + salt) %>
39
+ persistence_token: <%= Authlogic::Random.hex_token %>
40
+ username: st75
41
+ email: sydney.thompson@nyu.edu
42
+ firstname: Sydney Leigh
43
+ lastname: Thompson
44
+ user_attributes: <%= {
45
+ :nyuidn => "N10450419",
46
+ :verification => "A64C45148D130E78D8AA",
47
+ :bor_status => "50",
48
+ :aleph_permissions => {} }.to_yaml.inspect %>
49
+
@@ -0,0 +1,63 @@
1
+ NYU:
2
+ login_attributes:
3
+ link_code: NYSID
4
+ logout_url: 'http://bobcat.library.nyu.edu/nysid'
5
+ default_institution: true
6
+ display_name: NYU Libraries
7
+ view_attributes:
8
+ test_view_attribute1: test_attribute1_nyu
9
+ test_view_attribute2: test_attribute2_nyu
10
+ ip_addresses:
11
+ - 128.122.0.0-128.122.149.239
12
+ - 172.26.*.*
13
+ - 172.27.*.*
14
+ - 172.22.88.*
15
+ - 216.165.*.*
16
+ - 128.238.*.*
17
+
18
+ NYUAD:
19
+ default_institution: false
20
+ display_name: NYUAD Library
21
+ view_attributes:
22
+ test_view_attribute1: test_attribute1_nyuad
23
+ test_view_attribute2: test_attribute2_nyuad
24
+ ip_addresses:
25
+ - 128.122.0.1
26
+
27
+ NYSID:
28
+ login_attributes:
29
+ link_code: NYSID
30
+ logout_url: 'http://bobcat.library.nyu.edu/nysid'
31
+ default_institution: false
32
+ display_name: New York School of Interior Design Library
33
+ view_attributes:
34
+ test_view_attribute1: test_attribute1_nysid
35
+ test_view_attribute2: test_attribute2_nysid
36
+ ip_addresses:
37
+ - 128.122.0.1
38
+
39
+ CU:
40
+ login_attributes:
41
+ link_code: CU
42
+ logout_url: 'http://bobcat.library.nyu.edu/cooper'
43
+ display_name: Cooper Union Library
44
+ view_attributes:
45
+ test_view_attribute1: test_attribute1_cu
46
+ test_view_attribute2: test_attribute2_cu
47
+ ip_addresses:
48
+ - 199.98.16.0-199.98.31.255
49
+
50
+ NS:
51
+ login_attributes:
52
+ link_code: NS
53
+ logout_url: 'http://bobcat.library.nyu.edu/newschool'
54
+ display_name: New School Libraries
55
+ view_attributes:
56
+ test_view_attribute1: test_attribute1_ns
57
+ test_view_attribute2: test_attribute2_ns
58
+ ip_addresses:
59
+ - 128.122.138.0/24
60
+ - 69.64.210.46
61
+ - 69.64.210.50
62
+ - 69.64.210.42
63
+ - 69.193.198.126
@@ -0,0 +1,9 @@
1
+ class User < ActiveRecord::Base
2
+ serialize :user_attributes
3
+
4
+ acts_as_authentic do |c|
5
+ c.validations_scope = :username
6
+ c.validate_password_field = false
7
+ c.require_password_confirmation = false
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ class UserSession < Authlogic::Session::Base
2
+ pds_url "https://logindev.library.nyu.edu"
3
+ redirect_logout_url "https://logindev.library.nyu.edu/logout"
4
+ calling_system "authpds"
5
+ remember_me true
6
+ remember_me_for 300
7
+ httponly true
8
+ secure true
9
+ login_inaccessible_url "http://library.nyu.edu/errors/bobcat-library-nyu-edu/"
10
+ pds_attributes :id => "id", :uid => "uid",
11
+ :opensso => "opensso", :name => "name", :firstname => "givenname",
12
+ :lastname => "sn", :commonname => "cn", :email => "email",
13
+ :nyuidn => "nyuidn", :verification => "verification", :institute => "institute",
14
+ :bor_status => "bor-status", :bor_type => "bor-type",
15
+ :college_code => "college_code", :college_name => "college_name",
16
+ :dept_name => "dept_name", :dept_code => "dept_code",
17
+ :major_code => "major_code", :major => "major", :ill_permission => "ill-permission",
18
+ :newschool_ldap => "newschool_ldap"
19
+
20
+ def expiration_date
21
+ 1.day.ago
22
+ end
23
+ end
@@ -0,0 +1,78 @@
1
+ require 'authlogic'
2
+ require 'authlogic/test_case'
3
+ require "test/unit"
4
+ require 'rubygems'
5
+ require "active_record"
6
+ require "active_record/fixtures"
7
+ # Configure Rails Environment
8
+ ENV["RAILS_ENV"] = "test"
9
+
10
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
11
+ logger = Logger.new(STDOUT)
12
+ logger.level= Logger::FATAL
13
+ ActiveRecord::Base.logger = logger
14
+ ActiveRecord::Base.configurations = true
15
+ ActiveRecord::Schema.define(:version => 1) do
16
+ drop_table :users if table_exists?(:users)
17
+ create_table :users do |t|
18
+ t.string "username", :default => "", :null => false
19
+ t.string "email"
20
+ t.string "firstname", :limit => 100
21
+ t.string "lastname", :limit => 100
22
+ t.string "mobile_phone"
23
+ t.string "crypted_password"
24
+ t.string "password_salt"
25
+ t.string "session_id"
26
+ t.string "persistence_token", :null => false
27
+ t.integer "login_count", :default => 0, :null => false
28
+ t.datetime "last_request_at"
29
+ t.datetime "current_login_at"
30
+ t.datetime "last_login_at"
31
+ t.string "last_login_ip"
32
+ t.string "current_login_ip"
33
+ t.text "user_attributes"
34
+ t.datetime "refreshed_at"
35
+ t.timestamps
36
+ end unless table_exists?(:users)
37
+ end
38
+
39
+ # Load support files
40
+ require File.dirname(__FILE__) + '/../lib/authpds' unless defined?(Authpds)
41
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
42
+
43
+ class ActiveSupport::TestCase
44
+ VALID_PDS_HANDLE_FOR_NYU = '132012112947113134742310506860'
45
+ VALID_PDS_HANDLE_FOR_NEWSCHOOL = '272201212284614806184193096120278'
46
+ VALID_PDS_HANDLE_FOR_COOPER = '272201212284614806184193096120278'
47
+ INVALID_PDS_HANDLE = "Invalid"
48
+ SESSION_ID = "qwertyuiopasdfghjkllzxcvbnm1234567890"
49
+ include ActiveRecord::TestFixtures
50
+ include Authlogic::TestCase
51
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures"
52
+ self.use_transactional_fixtures = false
53
+ self.use_instantiated_fixtures = false
54
+ self.pre_loaded_fixtures = false
55
+ fixtures :all
56
+ setup :activate_authlogic
57
+ end
58
+
59
+ class Authlogic::TestCase::MockController
60
+ def self.helper_method(*args)
61
+ end
62
+
63
+ include Authpds::Controllers::AuthpdsController
64
+
65
+ def url_for(options={})
66
+ return "http://railsapp.library.nyu.edu/validate?return_url=#{options[:return_url]}"
67
+ end
68
+
69
+ def root_url
70
+ end
71
+
72
+ def performed?
73
+ false
74
+ end
75
+ end
76
+
77
+
78
+
@@ -0,0 +1,72 @@
1
+ require 'test_helper'
2
+
3
+ class PdsTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @pds_url = "https://login.library.nyu.edu"
7
+ @calling_system = "authpds"
8
+ @valid_pds_handle_for_nyu = VALID_PDS_HANDLE_FOR_NYU
9
+ @valid_pds_handle_for_newschool = VALID_PDS_HANDLE_FOR_NEWSCHOOL
10
+ @invalid_pds_handle = INVALID_PDS_HANDLE
11
+ @attribute = "bor_info"
12
+ # Ordered in Ruby 1.9 so :uid will overwrite id
13
+ @bor_info_attributes = { :id => "id", :uid => "uid",
14
+ :opensso => "opensso", :name => "name", :firstname => "givenname",
15
+ :lastname => "sn", :commonname => "cn", :email => "email",
16
+ :nyuidn => "nyuidn", :verification => "verification", :institute => "institute",
17
+ :bor_status => "bor-status", :bor_type => "bor-type",
18
+ :college_code => "college_code", :college_name => "college_name",
19
+ :dept_name => "dept_name", :dept_code => "dept_code",
20
+ :major_code => "major_code", :major => "major", :ill_permission => "ill-permission",
21
+ :newschool_ldap => "newschool_ldap" }
22
+ end
23
+
24
+ test "get_attribute_valid" do
25
+ get_attribute = Authpds::Exlibris::Pds::GetAttribute.new(@pds_url, @calling_system, @valid_pds_handle_for_nyu, "bor_info")
26
+ assert_equal("N12162279", get_attribute.response.at("//id").inner_text)
27
+ get_attribute = Authpds::Exlibris::Pds::GetAttribute.new(@pds_url, @calling_system, @valid_pds_handle_for_nyu, "bor_verification")
28
+ assert_equal("N12162279", get_attribute.response.at("//id").inner_text)
29
+ get_attribute = Authpds::Exlibris::Pds::GetAttribute.new(@pds_url, @calling_system, @valid_pds_handle_for_nyu, "authenticate")
30
+ assert_equal("N12162279", get_attribute.response.at("//id").inner_text)
31
+ get_attribute = Authpds::Exlibris::Pds::GetAttribute.new(@pds_url, @calling_system, @valid_pds_handle_for_nyu, "bor_id")
32
+ assert_equal("N12162279", get_attribute.response.at("//id").inner_text)
33
+ end
34
+
35
+ test "get_attribute_invalid" do
36
+ get_attribute = Authpds::Exlibris::Pds::GetAttribute.new(@pds_url, @calling_system, @invalid_pds_handle, "bor_info")
37
+ assert_equal("Error User does not exist", get_attribute.error)
38
+ end
39
+
40
+ test "bor_info_valid" do
41
+ nyu = Authpds::Exlibris::Pds::BorInfo.new(@pds_url, @calling_system, @valid_pds_handle_for_nyu, @bor_info_attributes)
42
+ assert_equal("N12162279", nyu.id)
43
+ assert_equal("std5", nyu.uid)
44
+ assert_equal("N12162279", nyu.nyuidn)
45
+ assert_equal("51", nyu.bor_status)
46
+ assert_equal("CB", nyu.bor_type)
47
+ assert_equal("true", nyu.opensso)
48
+ assert_equal("Scot Thomas", nyu.name)
49
+ assert_equal("Scot Thomas", nyu.firstname)
50
+ assert_equal("Dalton", nyu.lastname)
51
+ assert_equal("Y", nyu.ill_permission)
52
+ assert_equal("GA", nyu.college_code)
53
+ assert_equal("CSCI", nyu.dept_code)
54
+ assert_equal("Information Systems", nyu.major)
55
+ newschool = Authpds::Exlibris::Pds::BorInfo.new(@pds_url, @calling_system, @valid_pds_handle_for_newschool, @bor_info_attributes)
56
+ assert_equal("N00206454", newschool.id)
57
+ assert_equal("314519567249252", newschool.uid)
58
+ assert_equal("N00206454", newschool.nyuidn)
59
+ assert_equal("31", newschool.bor_status)
60
+ assert_equal("0", newschool.bor_type)
61
+ assert_equal("true", newschool.newschool_ldap)
62
+ assert_equal("Allen", newschool.name)
63
+ assert_equal("Allen", newschool.firstname)
64
+ assert_equal("Jones", newschool.lastname)
65
+ assert_equal("Y", newschool.ill_permission)
66
+ end
67
+
68
+ test "bor_info_invalid" do
69
+ get_attribute = Authpds::Exlibris::Pds::BorInfo.new(@pds_url, @calling_system, @invalid_pds_handle, @bor_info_attributes)
70
+ assert_equal("Error User does not exist", get_attribute.error)
71
+ end
72
+ end
@@ -0,0 +1,123 @@
1
+ require 'test_helper'
2
+ class UserSessionTest < 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 "login_url" do
11
+ user_session = UserSession.new
12
+ assert_equal(
13
+ "https://logindev.library.nyu.edu/pds?func=load-login&institute=&calling_system=authpds&url=http%3A%2F%2Frailsapp.library.nyu.edu%2Fvalidate%3Freturn_url%3D",
14
+ user_session.login_url)
15
+ end
16
+
17
+ test "logout_url" do
18
+ user_session = UserSession.new
19
+ assert_equal(
20
+ "https://logindev.library.nyu.edu/pds?func=logout&url=https%253A%252F%252Flogindev.library.nyu.edu%252Flogout",
21
+ user_session.logout_url)
22
+ end
23
+
24
+ test "validate_url" do
25
+ user_session = UserSession.new
26
+ assert_equal(
27
+ "http://railsapp.library.nyu.edu/validate?return_url=http://railsapp.library.nyu.edu",
28
+ user_session.send(:validate_url, :return_url => "http://railsapp.library.nyu.edu"))
29
+ assert_equal(
30
+ "http://railsapp.library.nyu.edu/validate?return_url=http://railsapp.library.nyu.edu&authpds_custom_param1=custom_param1",
31
+ user_session.send(:validate_url, :controller => "test_controller",
32
+ :action => "test_action", :return_url => "http://railsapp.library.nyu.edu",
33
+ :custom_param1 => "custom_param1"))
34
+ end
35
+
36
+ test "pds_handle" do
37
+ user_session = UserSession.new
38
+ assert_equal(VALID_PDS_HANDLE_FOR_NYU, user_session.send(:pds_handle))
39
+ end
40
+
41
+ test "pds_user" do
42
+ user_session = UserSession.new
43
+ pds_user = user_session.pds_user
44
+ assert_instance_of(Authpds::Exlibris::Pds::BorInfo, pds_user)
45
+ assert_equal("N12162279", pds_user.id)
46
+ assert_equal("std5", pds_user.uid)
47
+ assert_equal("N12162279", pds_user.nyuidn)
48
+ assert_equal("51", pds_user.bor_status)
49
+ assert_equal("CB", pds_user.bor_type)
50
+ assert_equal("true", pds_user.opensso)
51
+ assert_equal("Scot Thomas", pds_user.name)
52
+ assert_equal("Scot Thomas", pds_user.firstname)
53
+ assert_equal("Dalton", pds_user.lastname)
54
+ assert_equal("Y", pds_user.ill_permission)
55
+ assert_equal("GA", pds_user.college_code)
56
+ assert_equal("CSCI", pds_user.dept_code)
57
+ assert_equal("Information Systems", pds_user.major)
58
+ assert_equal("NYU", pds_user.institute)
59
+ end
60
+
61
+ test "persist_session" do
62
+ user_session = UserSession.new
63
+ assert_nil(controller.session["authpds_credentials"])
64
+ assert_nil(user_session.send(:attempted_record))
65
+ assert_nil(user_session.record)
66
+ assert_no_difference('User.count') do
67
+ user_session.send(:persist_session)
68
+ end
69
+ assert_not_nil(user_session.send(:attempted_record))
70
+ assert_nil(user_session.record)
71
+ assert_equal("N12162279", user_session.send(:attempted_record).username)
72
+ end
73
+
74
+ test "after_login" do
75
+ user_session = UserSession.new
76
+ assert_nil(controller.session["auth_test_credentials"])
77
+ assert_nil(user_session.send(:attempted_record))
78
+ assert_nil(user_session.record)
79
+ assert_no_difference('User.count') {
80
+ user_session.send(:after_login)
81
+ }
82
+ assert_nil(controller.session["auth_test_credentials"])
83
+ assert_not_nil(user_session.send(:attempted_record))
84
+ assert_nil(user_session.record)
85
+ assert_equal("N12162279", user_session.send(:attempted_record).username)
86
+ end
87
+
88
+ test "find" do
89
+ user_session = UserSession.new
90
+ assert_nil(controller.session["authpds_credentials"])
91
+ assert_nil(user_session.send(:attempted_record))
92
+ assert_nil(user_session.record)
93
+ assert_difference('User.count') {
94
+ user_session = UserSession.find
95
+ }
96
+ assert_not_nil(controller.session["authpds_credentials"])
97
+ assert_not_nil(user_session.send(:attempted_record))
98
+ assert_not_nil(user_session.record)
99
+ assert_equal(controller.session["authpds_credentials"], user_session.record.persistence_token)
100
+ assert_equal("N12162279", user_session.record.username)
101
+ end
102
+
103
+ test "expiration_date" do
104
+ user_session = UserSession.new
105
+ assert_in_delta(-0.00001, 1.day.ago.to_f, user_session.expiration_date.to_f)
106
+ end
107
+
108
+ test "get_record" do
109
+ user_session = UserSession.new
110
+ record = user_session.send(:get_record, "std5")
111
+ assert_instance_of(User, record)
112
+ assert_equal("std5", record.username)
113
+ assert_nil(record.id)
114
+ assert_nil(record.firstname)
115
+ record = user_session.send(:get_record, "st75")
116
+ assert_instance_of(User, record)
117
+ assert_equal("st75", record.username)
118
+ assert_not_nil(record.id)
119
+ assert_equal(3, record.id)
120
+ assert_not_nil(record.firstname)
121
+ assert_equal("Sydney Leigh", record.firstname)
122
+ end
123
+ end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+ class UserSessionTest < 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
+ InstitutionList.class_variable_set(:@@institutions_yaml_path, nil)
9
+ InstitutionList.instance.instance_variable_set(:@institutions, nil)
10
+ end
11
+
12
+ test "username=" do
13
+ ba36 = User.find(1)
14
+ token1 = ba36.persistence_token
15
+ ba36.username=("ba36")
16
+ token2 = ba36.persistence_token
17
+ assert_equal(token1, token2)
18
+ ba36.username=("ba37")
19
+ token2 = ba36.persistence_token
20
+ assert_not_equal(token1, token2)
21
+ end
22
+
23
+
24
+
25
+ test "user_attributes" do
26
+ user = User.new
27
+ user.user_attributes= {:test_attribute1 => "value1", :test_attribute2 => "value2"}
28
+ assert_equal("value1", user.user_attributes[:test_attribute1])
29
+ assert_equal("value2", user.user_attributes[:test_attribute2])
30
+ user.user_attributes= {:test_attribute3 => "value3", :test_attribute4 => "value4"}
31
+ assert_equal("value1", user.user_attributes[:test_attribute1])
32
+ assert_equal("value2", user.user_attributes[:test_attribute2])
33
+ assert_equal("value3", user.user_attributes[:test_attribute3])
34
+ assert_equal("value4", user.user_attributes[:test_attribute4])
35
+ user.user_attributes= {:test_attribute3 => "value3.1", :test_attribute4 => "value4.1"}
36
+ assert_equal("value1", user.user_attributes[:test_attribute1])
37
+ assert_equal("value2", user.user_attributes[:test_attribute2])
38
+ assert_equal("value3.1", user.user_attributes[:test_attribute3])
39
+ assert_equal("value4.1", user.user_attributes[:test_attribute4])
40
+ end
41
+
42
+ test "primary_institution" do
43
+ user = User.new
44
+ assert_raise ArgumentError do
45
+ user.primary_institution= "NYU"
46
+ end
47
+ InstitutionList.yaml_path= "#{File.dirname(__FILE__)}/../support/config/institutions.yml"
48
+ assert_nothing_raised ArgumentError do
49
+ user.primary_institution= "NYU"
50
+ end
51
+ end
52
+
53
+ test "institutions" do
54
+ user = User.new
55
+ assert_raise ArgumentError do
56
+ user.institutions= "NYU"
57
+ end
58
+ assert_raise ArgumentError do
59
+ user.institutions= ["NYU"]
60
+ end
61
+ assert_nil(user.institutions)
62
+ InstitutionList.yaml_path= "#{File.dirname(__FILE__)}/../support/config/institutions.yml"
63
+ user.institutions= ["NYU"]
64
+ assert_not_nil(user.institutions)
65
+ assert_equal([InstitutionList.instance.get("NYU")], user.institutions)
66
+ end
67
+
68
+ test "expired?" do
69
+ user = User.new
70
+ user.expiration_date = 1.week.ago
71
+ user.refreshed_at = 1.week.ago - 1.second
72
+ assert(user.expired?)
73
+ user.expiration_date = 1.week.ago
74
+ user.refreshed_at = 1.week.ago + 1.second
75
+ assert(!user.expired?)
76
+ end
77
+ end