-
Notifications
You must be signed in to change notification settings - Fork 1
/
Auth42.php
291 lines (265 loc) · 7.83 KB
/
Auth42.php
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/*
* Copyright 2013 Muffin.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* A simple (and light) exception class, for catching specific errors.
*/
class Auth42Exception extends Exception
{
public function __construct ($message, $code = 0, $previous = null)
{
$body = "An error occured with 42 ldap authentification: ";
parent::__construct ($body . $message, $code, $previous);
}
}
/**
* A simple abstract layer to use ldap in order to manage 42 authentications.
* @see ldap_bind
* @see ldap_connect
*
* @author lambda2 <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link https://gist.github.com/sanecz/9025826 A gist to understand simple bind
*/
class Auth42
{
/* The 42 ldap url */
const serverUrl = "ldaps://ldap.42.fr";
protected $dn;
protected $bind;
protected $password;
protected $handle;
/**
* Will construct a new Auth42 object, wich is a ldap based search and
* authentication tool.
*
* @param array $dn the dn to use. If no dn is provided, will use
* ou=2013,ou=people,dc=42,dc=fr as the default dn.
* @param string $password an optionnal password to connect with.
*/
public function __construct ($dn = array (), $password = "")
{
if ( count ($dn) )
{
$this->dn = $dn;
}
else
{
$this->dn = array (
"uid" => "",
"ou" => array ("2013", "people"),
"dc" => array ("42", "fr")
);
}
$this->password = $password;
}
/**
* Will try to bind the ldap with the previously given dn and password.
* At the end, will close the ldap_handle.
* @return int the binding result (0 on failure).
* @throws Auth42Exception
*/
public function bind ()
{
$r = $this->bindOnHandle();
ldap_close ($this->handle);
return ($r);
}
/**
* Will try to bind the ldap with the registered informations.
* This function will not automatically close the ldap handle.
* @return int the binding result (0 on failure).
* @throws Auth42Exception
*/
protected function bindOnHandle()
{
$ok = false;
try
{
$this->handle = ldap_connect (self::serverUrl);
ldap_set_option ($this->handle, LDAP_OPT_PROTOCOL_VERSION, 3);
if ($this->handle)
{
$bindDn = $this->computeDn();
$ok = $this->bind = ldap_bind ($this->handle, $bindDn, $this->password);
return ($ok);
}
}
catch (Exception $e)
{
//ldap_close ($this->handle);
throw new Auth42Exception("Bind error (" . $e->getMessage () . ")");
}
return ($ok);
}
/**
* Will try to search the given query on the ldap.
* if a login and a password are supplied, will try to bind the ldap with theses
* id before the request. On failure, will throw an Auth42Exception.
* If no filter is supplied, the default filter ("dc=42,dc=fr") is applied.
*
* @param string $query the query to execute, like 'uid=a*'.
* @param string $login an optionnal login to bind the server.
* @param string $password an optionnal password to bind the server.
* @param string $filter a filter to apply on the search request. Default: "dc=42,dc=fr".
* @return array the search result(s), or false on failure.
* @throws Auth42Exception when the auth fail.
*/
public function search($query, $login = NULL, $password = NULL, $filter = NULL)
{
if ($filter == NULL)
{
$filter = "dc=42,dc=fr";
}
if ($login and $password)
{
$this->setPassword($password);
$this->setDn(array (
"uid" => $login,
"ou" => array ("2013", "people"),
"dc" => array ("42", "fr")
));
}
$res = $this->bindOnHandle();
$sr = ldap_search($this->handle, $filter, $query);
if ($sr)
{
$info = ldap_get_entries($this->handle, $sr);
}
else
{
$info = false;
}
ldap_close ($this->handle);
return ($info);
}
/**
* Will try to authenticate the user on the 42 ldap with the given
* password and username.
*
* @param string $login the login (uid) of the student
* @param string $password the password of the student
* @return boolean true on success, false otherwise.
* @link http://www.php.net/manual/fr/language.exceptions.php finally >= 5.5
*/
static function authenticate ($login, $password)
{
$tmp = new Auth42 (array (
"uid" => $login,
"ou" => array ("2013", "people"),
"dc" => array ("42", "fr")
), $password);
try
{
$res = $tmp->bind();
}
catch (Exception $exc)
{
$res = NULL;
}
finally
{
return ($res != false);
}
}
/**
* Will convert the arrays of dn into an undersandable string.
* @return string a string with all the dn(s).
*/
protected function computeDn ()
{
$computed = array ();
foreach ($this->dn as $key => $value)
{
if ( is_array ($value) )
{
foreach ($value as $field)
{
$computed[] = $key . '=' . $field;
}
}
else
{
$computed[] = $key . '=' . $value;
}
}
return (implode (',', $computed));
}
/*
* =====================================================================
* DN Management
* =====================================================================
*/
/**
* Will add (or replace) the value of the given key with
* the given (new) value(s). If there is more than one value to add, use
* an array instead of a string.
*
* @param string $key the key to add / replace.
* @param string|array $value the value(s) to add.
*/
public function addOrReplaceDn ($key, $value)
{
$this->dn[$key] = $value;
}
/**
* Remove the given dn key of the current instance.
* @param string $key the dn to remove.
*/
public function removeDn ($key)
{
unset ($this->dn[$key]);
}
/*
* =====================================================================
* Getters and Setters
* =====================================================================
*/
public function getDn ()
{
return $this->dn;
}
public function getBind ()
{
return $this->bind;
}
public function getPassword ()
{
return $this->password;
}
public function getHandle ()
{
return $this->handle;
}
public function setDn ($dn)
{
$this->dn = $dn;
}
public function setBind ($bind)
{
$this->bind = $bind;
}
public function setPassword ($password)
{
$this->password = $password;
}
public function setHandle ($handle)
{
$this->handle = $handle;
}
}
?>