-
Notifications
You must be signed in to change notification settings - Fork 1
/
ldap.rb
272 lines (216 loc) · 6.05 KB
/
ldap.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require 'active_support'
require 'active_support/core_ext'
require 'active_support/json'
require 'net/ldap'
require 'yaml'
module LDAP
def self.connection_spec
Hash[
host: config['hostname'],
port: config['port'],
encryption: config['encryption'],
auth: {
method: :simple,
username: config['username'],
password: config['password']
}
]
end
def self.environment
ENV['RACK_ENV'] || 'development'
end
def self.config_file
File.realpath(File.dirname(__FILE__) + '/config/ldap.yml')
end
def self.config
@@config ||= YAML.load_file(config_file).fetch(environment)
end
def self.connection
@connection ||= Net::LDAP.new(connection_spec)
end
# Difference in seconds between the UNIX Epoch (1970-01-01)
# and the AD Epoch (1601-01-01)
AD_EPOCH_OFFSET = 11644477200
def self.now
to_ad_ts(Time.now)
end
def self.to_ad_ts(timestamp)
(timestamp.to_i + AD_EPOCH_OFFSET) * 10_000_000
end
def self.at(timestamp)
# number of nanosec / 100, i.e. 10 times the number of microsec,
# divide by 10_000_000 so it becomes number of seconds
Time.at(timestamp.to_i / 10_000_000 - AD_EPOCH_OFFSET)
end
class Result < Array
def initialize(*args)
if args.first.respond_to?(:key?)
options = args.shift
@filter, @scope, @base =
options.values_at(:filter, :scope, :base)
end
super
end
attr_reader :filter, :base
def scope
case @scope
when Net::LDAP::SearchScope_BaseObject then :base
when Net::LDAP::SearchScope_SingleLevel then :single
when Net::LDAP::SearchScope_WholeSubtree then :subtree
end
end
end
class Person
UTF8_ATTRIBUTES = %w(
givenName
sn
displayName
sAMAccountName
accountExpires
mail
otherMailbox
telephoneNumber
roomNumber
mobile
otherMobile
division
employeeType
employeeId
employeeNumber
lockoutTime
whenCreated
whenChanged
).freeze
ATTRIBUTES = UTF8_ATTRIBUTES + %w(
objectGUID
thumbnailPhoto
memberOf
).freeze
def self.base_dn
@_base_dn ||= LDAP.config['base']
end
def self.branches
@_branches ||= LDAP.config['branches']
end
def self.attributes
ATTRIBUTES
end
def self.utf8_convert_attributes
UTF8_ATTRIBUTES
end
def self.export_attributes
@export_attributes ||= attributes \
- %w( objectGUID ) \
+ %w(active? created_at updated_at locked_out_at locked_out? extension expiration guid)
end
def self.phone_prefix
'+39065459'
end
def self.filter_for(options)
filter = case options.delete(:active)
when false
Net::LDAP::Filter.le('accountExpires', LDAP.now.to_s)
when :any, 'any'
Net::LDAP::Filter.eq('accountExpires', '*')
else
Net::LDAP::Filter.ge('accountExpires', LDAP.now.to_s)
end
filter &= Net::LDAP::Filter.eq('objectClass', 'person')
options.each do |key, val|
filter &= val.present? ?
Net::LDAP::Filter.eq(key.to_s, val.to_s) :
Net::LDAP::Filter.ne(key.to_s, '*')
end
return filter
end
def self.search(options)
options = options.symbolize_keys
scope = options.delete(:scope) == :single ?
Net::LDAP::SearchScope_SingleLevel :
Net::LDAP::SearchScope_WholeSubtree
filter = filter_for(options)
result = Result.new(filter: filter, base: branches, scope: scope)
branches.each do |dn|
result.concat LDAP.connection.search(
base: dn,
filter: filter,
attributes: attributes,
scope: scope
).map! {|entry| new(entry)}
end
result.compact!
return result.freeze
end
def initialize(entry)
@dn = entry.dn.dup.force_encoding('utf-8').freeze
@attributes = self.class.attributes.inject({}) do |h, attr|
h.update(attr => entry[attr].reject(&:blank?))
end.freeze
end
attr_reader :attributes, :dn
def memberOf
attributes.fetch('memberOf').map(&:upcase)
end
def extension
# Extract the extension from the given full number
self['telephoneNumber'].gsub(self.class.phone_prefix, '')
end
def created_at
Time.parse(self['whenCreated'])
end
def updated_at
Time.parse(self['whenChanged'])
end
def active?
expiration.future?
end
def expiration
LDAP.at(self['accountExpires'])
end
def account_name
self['sAMAccountName']
end
def guid
b = self['objectGUID'].unpack('C*')
return unless b.size == 16
guid_bytes = [b[3], b[2], b[1], b[0], b[5], b[4], b[7], b[6], b[8], b[9], *b[10..15]]
guid_fmt = ['%02x'*4, '%02x'*2, '%02x'*2, '%02x'*2, '%02x'*6].join('-')
(guid_fmt % guid_bytes).upcase
end
def locked_out_at
return if self['lockoutTime'].nil? || self['lockoutTime'] == '0' # Not Locked Out
LDAP.at(self['lockoutTime'])
end
def locked_out?
!locked_out_at.nil?
end
def [](name)
value = attributes.fetch(name)
(value.size < 2 ? value.first.to_s : value).tap do |v|
if self.class.utf8_convert_attributes.include?(name)
v.force_encoding('utf-8')
end
end
end
def to_hash(attrs = nil)
(attrs || self.class.export_attributes).inject({'dn' => dn}) do |h, attr|
value = self.respond_to?(attr) ? self.public_send(attr) : self[attr]
h.update(attr => value)
end
end
def as_json(options)
return to_hash if options.blank?
options.symbolize_keys!
attrs = self.class.export_attributes
attrs &= Array.wrap(options[:only]).map(&:to_s) if options.key?(:only)
attrs -= Array.wrap(options[:except]).map(&:to_s) if options.key?(:except)
to_hash(attrs)
end
protected
def method_missing(name, *args, &block)
self[name.to_s]
rescue KeyError
super
end
end
end