cul-ldap 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/VERSION +1 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/cul-ldap.gemspec +28 -0
- data/lib/cul/ldap.rb +92 -0
- data/lib/cul/ldap/entry.rb +33 -0
- data/lib/cul/ldap/version.rb +9 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f3aec72c508a6c195d0ee5912c1bad2c4623d528
|
4
|
+
data.tar.gz: dbd019c07e1582a28879d0e97fb1e2e4dc5bef86
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 077ae779ae3cc4fe2495bd20df9aee09812fee27e856902a218e975bc4c45517c48ad43da8a51b6bf7d4aed3d4e33374777dbed9f71b7275f5ad900699170bb6
|
7
|
+
data.tar.gz: 41ee7ddaecbcb9b90007a5161fd2baa5852fc06b8eb3d9760c71c5f93525bb072d2939faa7ee0c34527cd1be654a1ac90b8f6d8d4e1e406f7232ec2e249633f2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Carla Galarza
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Cul::LDAP
|
2
|
+
|
3
|
+
Gem for CUL's common queries to CU's LDAP server. This is just a starting point with one query that is commonly needed. Needs some work to build out new functionality. Uses net-ldap to make queries. Cul::LDAP could be delegated to Net::LDAP if more functionality like the one already implemented there is required.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'cul-ldap'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install cul-ldap
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
require 'cul/ldap'
|
25
|
+
ldap = Cul::LDAP.new
|
26
|
+
entry = ldap.find_by_uni("abc123")
|
27
|
+
entry = ldap.find_by_name("Doe, Jane")
|
28
|
+
```
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
33
|
+
|
34
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cul/cul-ldap.
|
39
|
+
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cul/ldap"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/cul-ldap.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "cul-ldap"
|
7
|
+
spec.version = IO.read("VERSION").strip
|
8
|
+
spec.authors = ["Carla Galarza"]
|
9
|
+
spec.email = ["cmg2228@columbia.edu"]
|
10
|
+
|
11
|
+
spec.summary = "Common queries of CU's LDAP server"
|
12
|
+
spec.homepage = "https://github.com/cul/cul-ldap"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "net-ldap", '~> 0.16'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "rspec-its", "~> 1.2"
|
28
|
+
end
|
data/lib/cul/ldap.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require "net/ldap"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
require "cul/ldap/version"
|
5
|
+
require "cul/ldap/entry"
|
6
|
+
|
7
|
+
module Cul
|
8
|
+
class LDAP < Net::LDAP
|
9
|
+
CONFIG_FILENAME = 'cul_ldap.yml'
|
10
|
+
CONFIG_DEFAULTS = {
|
11
|
+
host: 'ldap.columbia.edu',
|
12
|
+
port: '636',
|
13
|
+
encryption: {
|
14
|
+
method: :simple_tls,
|
15
|
+
tls_options: OpenSSL::SSL::SSLContext::DEFAULT_PARAMS
|
16
|
+
}
|
17
|
+
}.freeze
|
18
|
+
|
19
|
+
def initialize(options = {})
|
20
|
+
super(build_config(options)) # All keys have to be symbols.
|
21
|
+
end
|
22
|
+
|
23
|
+
# LDAP lookup based on UNI. If record could not be found returns nil.
|
24
|
+
#
|
25
|
+
# @param [String] uni
|
26
|
+
# @return [Cul::LDAP::Entry] containing all the ldap information available for the uni given
|
27
|
+
# @return [nil] if record for uni could not be found, or more than one record was found
|
28
|
+
def find_by_uni(uni)
|
29
|
+
entries = search(base: "ou=People,o=Columbia University, c=US", filter: Net::LDAP::Filter.eq("uid", uni))
|
30
|
+
(entries.count == 1) ? entries.first : nil
|
31
|
+
end
|
32
|
+
|
33
|
+
# LDAP lookup based on name.
|
34
|
+
#
|
35
|
+
# @param [String] name
|
36
|
+
# @return [Cul::LDAP::Entry] containing the entry matching this name, if it is unique
|
37
|
+
# @return [nil] if record could not be found or if there is more than one match
|
38
|
+
def find_by_name(name)
|
39
|
+
if name.include?(',')
|
40
|
+
name = name.split(',').map(&:strip).reverse.join(" ")
|
41
|
+
end
|
42
|
+
entries = search(base: "ou=People,o=Columbia University, c=US", filter: Net::LDAP::Filter.eq("cn", name))
|
43
|
+
(entries.count == 1) ? entries.first : nil
|
44
|
+
end
|
45
|
+
|
46
|
+
# Wrapper around Net::LDAP#search, converts Net::LDAP::Entry objects to
|
47
|
+
# Cul::LDAP::Entry objects.
|
48
|
+
def search(args = {})
|
49
|
+
super(args).tap do |result|
|
50
|
+
if result.is_a?(Array)
|
51
|
+
result.map!{ |e| Cul::LDAP::Entry.new(e) }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def build_config(options)
|
59
|
+
config = CONFIG_DEFAULTS.merge(options)
|
60
|
+
credentials = config.fetch(:auth, nil)
|
61
|
+
credentials = nil if !credentials.nil? && credentials.empty?
|
62
|
+
|
63
|
+
# If rails app fetch credentials using rails code, otherwise read from
|
64
|
+
# cul_ldap.yml if credentials are nil.
|
65
|
+
if credentials.nil?
|
66
|
+
credentials = rails_credentials || credentials_from_file
|
67
|
+
credentials = nil if !credentials.nil? && credentials.empty?
|
68
|
+
end
|
69
|
+
|
70
|
+
unless credentials.nil?
|
71
|
+
credentials = credentials.map { |k, v| [k.to_sym, v] }.to_h
|
72
|
+
credentials[:method] = :simple unless credentials.key?(:method)
|
73
|
+
end
|
74
|
+
|
75
|
+
config[:auth] = credentials
|
76
|
+
config
|
77
|
+
end
|
78
|
+
|
79
|
+
def credentials_from_file
|
80
|
+
(File.exist?(CONFIG_FILENAME)) ? YAML.load_file(CONFIG_FILENAME) : nil
|
81
|
+
end
|
82
|
+
|
83
|
+
def rails_credentials
|
84
|
+
if defined?(Rails.application.config_for) && File.exist?(File.join(Rails.root, 'config', CONFIG_FILENAME))
|
85
|
+
raise "Missing cul-ldap credentials in config/#{CONFIG_FILENAME}" if Rails.application.config_for(:cul_ldap).empty?
|
86
|
+
Rails.application.config_for(:cul_ldap)
|
87
|
+
else
|
88
|
+
nil
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module Cul
|
4
|
+
class LDAP::Entry < SimpleDelegator
|
5
|
+
def name
|
6
|
+
self[:cn].first
|
7
|
+
end
|
8
|
+
|
9
|
+
def email
|
10
|
+
(self[:mail].empty?) ? "#{uni}@columbia.edu" : mail.first
|
11
|
+
end
|
12
|
+
|
13
|
+
def first_name
|
14
|
+
self[:givenname].first
|
15
|
+
end
|
16
|
+
|
17
|
+
def last_name
|
18
|
+
self[:sn].first
|
19
|
+
end
|
20
|
+
|
21
|
+
def organizational_unit
|
22
|
+
self[:ou].first
|
23
|
+
end
|
24
|
+
|
25
|
+
def uni
|
26
|
+
self[:uni].first
|
27
|
+
end
|
28
|
+
|
29
|
+
def title
|
30
|
+
self[:title].first
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cul-ldap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Carla Galarza
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-ldap
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.16'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-its
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.2'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- cmg2228@columbia.edu
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- VERSION
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- cul-ldap.gemspec
|
101
|
+
- lib/cul/ldap.rb
|
102
|
+
- lib/cul/ldap/entry.rb
|
103
|
+
- lib/cul/ldap/version.rb
|
104
|
+
homepage: https://github.com/cul/cul-ldap
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.5.2
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Common queries of CU's LDAP server
|
128
|
+
test_files: []
|