sheffield_ldap_lookup 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create @sheffield_ldap_lookup
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Shuo Chen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # SheffieldLdapLookup
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sheffield_ldap_lookup'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sheffield_ldap_lookup
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ module SheffieldLdapLookup
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("../templates", __FILE__)
4
+
5
+ def create_initializer
6
+ copy_file "sheffield_ldap_lookup.rb", "config/initializers/sheffield_ldap_lookup.rb"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ SheffieldLdapLookup::LdapFinder.ldap_config = YAML.load_file("#{Rails.root}/config/ldap.yml")[Rails.env]
@@ -0,0 +1,32 @@
1
+ require 'net/ldap'
2
+ module SheffieldLdapLookup
3
+ class LdapFinder < Struct.new(:keyword)
4
+ class << self
5
+ attr_accessor :ldap_config
6
+ end
7
+
8
+ def lookup
9
+ begin
10
+ @lookup ||= connection.search(filter: ldap_filter)[0]
11
+ rescue
12
+ {}
13
+ end
14
+ end
15
+
16
+ def ldap_filter(filter_class = Net::LDAP::Filter)
17
+ filter_class.eq(search_attribute, keyword)
18
+ end
19
+
20
+ def search_attribute
21
+ keyword =~ /\A[^@]+@[^@]+\z/ ? 'mail' : 'uid'
22
+ end
23
+
24
+ def ldap_config
25
+ self.class.ldap_config
26
+ end
27
+
28
+ def connection(ldap_class = Net::LDAP)
29
+ @connection ||= ldap_class.new(host: ldap_config['host'], port: ldap_config['port'], base: ldap_config['base'])
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module SheffieldLdapLookup
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "sheffield_ldap_lookup/version"
2
+ require "sheffield_ldap_lookup/ldap_finder"
3
+ module SheffieldLdapLookup
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sheffield_ldap_lookup/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sheffield_ldap_lookup"
8
+ gem.version = SheffieldLdapLookup::VERSION
9
+ gem.authors = ["Shuo Chen"]
10
+ gem.email = ["s.chen@epigenesys.co.uk"]
11
+ gem.description = "A gem to fetch information from University of Sheffield LDAP server based on username or email address."
12
+ gem.summary = "LDAP lookup"
13
+ gem.homepage = "http://www.epigenesys.co.uk"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency('net-ldap')
21
+
22
+ gem.add_development_dependency('rake')
23
+ gem.add_development_dependency('rails', '~> 3.2')
24
+ gem.add_development_dependency('rspec')
25
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'sheffield_ldap_lookup/ldap_finder.rb'
3
+
4
+ describe SheffieldLdapLookup::LdapFinder do
5
+ LDAP_CONFIG = { 'host' => 'localhost', 'port' => '389', 'base' => 'ou=Users' }
6
+ describe "#connection" do
7
+ it "should create a new LDAP connection" do
8
+ subject.stub(ldap_config: LDAP_CONFIG)
9
+ ldap_class = mock
10
+ ldap_class.should_receive(:new).with(host: LDAP_CONFIG['host'], port: LDAP_CONFIG['port'], base: LDAP_CONFIG['base'])
11
+ subject.connection(ldap_class)
12
+ end
13
+ end
14
+
15
+ describe "#ldap_config" do
16
+ it "should load the LDAP configuration" do
17
+ SheffieldLdapLookup::LdapFinder.ldap_config = LDAP_CONFIG
18
+ subject.ldap_config.should == LDAP_CONFIG
19
+ end
20
+ end
21
+
22
+ describe "#search_attribute" do
23
+ describe "determine to search against uid or email based on the format of the keyword" do
24
+ it "should use 'uid' attribute for username" do
25
+ finder = SheffieldLdapLookup::LdapFinder.new('username')
26
+ finder.search_attribute.should == 'uid'
27
+ end
28
+
29
+ it "should use 'mail' attribute for email" do
30
+ finder = SheffieldLdapLookup::LdapFinder.new('test@test.com')
31
+ finder.search_attribute.should == 'mail'
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "#ldap_filter" do
37
+ it "should create a LDAP filter for the attribute and keyword" do
38
+ finder = SheffieldLdapLookup::LdapFinder.new('username')
39
+ finder.stub(search_attribute: 'uid')
40
+ filter_class = mock
41
+ filter_class.should_receive(:eq).with('uid', 'username')
42
+ finder.ldap_filter filter_class
43
+ end
44
+ end
45
+
46
+ describe "#lookup" do
47
+ describe "use the LDAP filter to search for the entity and return the first result" do
48
+ let(:ldap_filter) { mock }
49
+ let(:connection) { mock }
50
+ before { subject.stub(ldap_filter: ldap_filter, connection: connection) }
51
+
52
+ it "should search the LDAP connection using the filter" do
53
+ connection.should_receive(:search).with(filter: ldap_filter).and_return([])
54
+ subject.lookup
55
+ end
56
+
57
+ it "should return the first result" do
58
+ result = mock
59
+ connection.stub(search: [result])
60
+ subject.lookup.should == result
61
+ end
62
+
63
+ it "should return an empty hash if cannot connect to LDAP" do
64
+ connection.stub(search: ->{raise})
65
+ subject.lookup.should == {}
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1 @@
1
+ $: << File.expand_path('../../lib', __FILE__)
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sheffield_ldap_lookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shuo Chen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: net-ldap
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.2'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A gem to fetch information from University of Sheffield LDAP server based
79
+ on username or email address.
80
+ email:
81
+ - s.chen@epigenesys.co.uk
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rvmrc
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - lib/generators/sheffield_ldap_lookup/install_generator.rb
93
+ - lib/generators/sheffield_ldap_lookup/templates/sheffield_ldap_lookup.rb
94
+ - lib/sheffield_ldap_lookup.rb
95
+ - lib/sheffield_ldap_lookup/ldap_finder.rb
96
+ - lib/sheffield_ldap_lookup/version.rb
97
+ - sheffield_ldap_lookup.gemspec
98
+ - spec/lib/ldap_finder_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: http://www.epigenesys.co.uk
101
+ licenses: []
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ segments:
113
+ - 0
114
+ hash: -4032560973816351922
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ segments:
122
+ - 0
123
+ hash: -4032560973816351922
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.24
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: LDAP lookup
130
+ test_files:
131
+ - spec/lib/ldap_finder_spec.rb
132
+ - spec/spec_helper.rb