anu-ldap 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (8) hide show
  1. data/Gemfile +5 -0
  2. data/Gemfile.lock +31 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +31 -0
  5. data/Rakefile +18 -0
  6. data/VERSION +1 -0
  7. data/lib/anu-ldap.rb +80 -0
  8. metadata +97 -0
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "jeweler"
4
+ gem "net-ldap"
5
+ gem "rspec"
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.3)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.6.5)
12
+ net-ldap (0.3.1)
13
+ rake (0.9.2.2)
14
+ rdoc (3.12)
15
+ json (~> 1.4)
16
+ rspec (2.9.0)
17
+ rspec-core (~> 2.9.0)
18
+ rspec-expectations (~> 2.9.0)
19
+ rspec-mocks (~> 2.9.0)
20
+ rspec-core (2.9.0)
21
+ rspec-expectations (2.9.0)
22
+ diff-lcs (~> 1.1.3)
23
+ rspec-mocks (2.9.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ jeweler
30
+ net-ldap
31
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 David Porter
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.
@@ -0,0 +1,31 @@
1
+ = AnuLdap
2
+
3
+ == Description
4
+
5
+ AnuLdap is a Ruby module for easily querying the ANU LDAP server.
6
+
7
+ == Install
8
+
9
+ $ gem install anu-ldap
10
+
11
+ == Usage
12
+
13
+ require "anu-ldap"
14
+
15
+ # Find users by email address:
16
+
17
+ entries = AnuLdap.find_by_email("david.porter@anu.edu.au")
18
+ entries.each do |e|
19
+ puts "Uni ID: #{e[:uni_id]}"
20
+ puts "Email: #{e[:email]}"
21
+ puts "Given name: #{e[:given_name]}"
22
+ puts "Surname: #{e[:surname]}"
23
+ end
24
+
25
+ # Authenticate a user:
26
+
27
+ if AnuLdap.authenticate("u3990518", "secret")
28
+ puts "Successfully authenticated"
29
+ else
30
+ puts "Authentication failure"
31
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "anu-ldap"
8
+ gem.summary = "A Ruby library for querying the ANU LDAP server."
9
+ gem.email = "david.porter@anu.edu.au"
10
+ gem.homepage = "https://doiweb-repo2.anu.edu.au/repositories/es-projects/anu-auth"
11
+ gem.authors = ["David Porter"]
12
+ gem.add_dependency "net-ldap"
13
+ gem.files = FileList["lib/**/*.rb", "bin/*", '[A-Z]*', 'spec/**/*']
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.2
@@ -0,0 +1,80 @@
1
+ require "net/ldap"
2
+
3
+ # This module encapsulates queries of the ANU LDAP server.
4
+ module AnuLdap
5
+
6
+ # Find all ANU LDAP entries with the given _uni_id_.
7
+ #
8
+ # Return entries as a hash with keys +:uni_id+, +:email+, +:given_name+,
9
+ # +:surname+, +:full_name+.
10
+ def self.find_all_by_uni_id(uni_id)
11
+ find_by("uid", uni_id)
12
+ end
13
+
14
+ # Find first ANU LDAP entry with the given _uni_id_.
15
+ #
16
+ # Return the entry as a hash with keys +:uni_id+, +:email+, +:given_name+,
17
+ # +:surname+, +:full_name+.
18
+ def self.find_by_uni_id(uni_id)
19
+ find_all_by_uni_id(uni_id).first
20
+ end
21
+
22
+ # Find all ANU LDAP entries with the given _email_.
23
+ #
24
+ # Return entries as a hash with keys +:uni_id+, +:email+, +:given_name+,
25
+ # +:surname+, +:full_name+.
26
+ def self.find_all_by_email(email)
27
+ find_by("mail", email)
28
+ end
29
+
30
+ # Find first ANU LDAP entry with the given _email_.
31
+ #
32
+ # Return the entry as a hash with keys +:uni_id+, +:email+, +:given_name+,
33
+ # +:surname+, +:full_name+.
34
+ def self.find_by_email(email)
35
+ find_all_by_email(email).first
36
+ end
37
+
38
+ # Authenticate credentials against ANU LDAP.
39
+ #
40
+ # Returns _true_ if authentication is successful, and _false_ otherwise.
41
+ def self.authenticate(uni_id, password)
42
+ ldap = get_new_ldap()
43
+ ldap.auth "uid=#{uni_id}, ou=people, o=anu.edu.au", password
44
+
45
+ ldap.bind
46
+ end
47
+
48
+ private
49
+
50
+ # Find all ANU LDAP entries with a given key-value pair.
51
+ def self.find_by(key_name, key_value)
52
+ filter = Net::LDAP::Filter.eq(key_name, key_value)
53
+
54
+ entries = get_new_ldap().search(:base => "ou=people,o=anu.edu.au",
55
+ :filter => filter,
56
+ :attributes => ["uid", "mail",
57
+ "givenName", "sn", "cn"])
58
+
59
+ entries.map { |e| make_user_hash(e) }
60
+ end
61
+
62
+ # Get a new connection to the ANU LDAP server.
63
+ def self.get_new_ldap()
64
+ Net::LDAP.new(:host => "ldap.anu.edu.au",
65
+ :port => 636,
66
+ :encryption => :simple_tls)
67
+ end
68
+
69
+ # Create a hash of user details from an ldap entry.
70
+ def self.make_user_hash(ldap_entry)
71
+ {
72
+ :uni_id => ldap_entry.uid.first,
73
+ :email => ldap_entry.mail.first,
74
+ :given_name => ldap_entry.givenName.first,
75
+ :surname => ldap_entry.sn.first,
76
+ :full_name => ldap_entry.cn.first
77
+ }
78
+ end
79
+ end
80
+
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: anu-ldap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Porter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jeweler
16
+ requirement: &70317091417720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70317091417720
25
+ - !ruby/object:Gem::Dependency
26
+ name: net-ldap
27
+ requirement: &70317091431160 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70317091431160
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70317091428520 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70317091428520
47
+ - !ruby/object:Gem::Dependency
48
+ name: net-ldap
49
+ requirement: &70317091437380 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70317091437380
58
+ description:
59
+ email: david.porter@anu.edu.au
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files:
63
+ - LICENSE
64
+ - README.rdoc
65
+ files:
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE
69
+ - README.rdoc
70
+ - Rakefile
71
+ - VERSION
72
+ - lib/anu-ldap.rb
73
+ homepage: https://doiweb-repo2.anu.edu.au/repositories/es-projects/anu-auth
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.17
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: A Ruby library for querying the ANU LDAP server.
97
+ test_files: []