Skip to content

Commit f0ea8c5

Browse files
author
Jonathan Creasy
committed
adding LDAP authentication support
1 parent bab6185 commit f0ea8c5

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

application/config/rest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
|
4545
| Is login required and if so, which type of login?
4646
|
47-
| '' = no login required, 'basic' = unsecure login, 'digest' = more secure login
47+
| '' = no login required, 'basic' = unsecure login, 'digest' = more secure login, 'ldap' = digest with ldap
4848
|
4949
*/
5050
$config['rest_auth'] = false;
@@ -65,19 +65,20 @@
6565
| $config['auth_override_class_method']['accounts']['user'] = 'basic';
6666
|
6767
| Here 'deals' and 'accounts' are controller names, 'view', 'insert' and 'user' are methods within. (NOTE: leave off the '_get' or '_post' from the end of the method name)
68-
| Acceptable values are; 'none', 'digest' and 'basic'.
68+
| Acceptable values are; 'none', 'digest', 'ldap', and 'basic'
6969
|
7070
*/
7171
// $config['auth_override_class_method']['deals']['view'] = 'none';
7272
// $config['auth_override_class_method']['deals']['insert'] = 'digest';
7373
// $config['auth_override_class_method']['accounts']['user'] = 'basic';
74+
// $config['auth_override_class_method']['accounts']['create'] = 'ldap';
7475

7576
/*
7677
|--------------------------------------------------------------------------
7778
| REST Login usernames
7879
|--------------------------------------------------------------------------
7980
|
80-
| Array of usernames and passwords for login
81+
| Array of usernames and passwords for login, if ldap is configured this is ignored
8182
|
8283
| array('admin' => '1234')
8384
|
@@ -290,4 +291,4 @@
290291
$config['rest_ajax_only'] = FALSE;
291292

292293
/* End of file config.php */
293-
/* Location: ./system/application/config/rest.php */
294+
/* Location: ./system/application/config/rest.php */

application/libraries/REST_Controller.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ abstract class REST_Controller extends CI_Controller
109109
*/
110110
protected $_zlib_oc = FALSE;
111111

112+
/**
113+
* The LDAP Distinguished Name of the User post authentication
114+
*
115+
* @var string
116+
*/
117+
protected $_user_ldap_dn = ''
118+
112119
/**
113120
* List all supported methods, the first will be the default format
114121
*
@@ -909,6 +916,94 @@ public function validation_errors()
909916

910917
// SECURITY FUNCTIONS ---------------------------------------------------------
911918

919+
/**
920+
* Perform LDAP Authentication
921+
*
922+
* @param string $username The username to validate
923+
* @param string $password The password to validate
924+
* @return boolean
925+
*/
926+
protected function _perform_ldap_auth($username = '', $password = NULL)
927+
{
928+
if (empty($username))
929+
{
930+
log_message('debug', 'LDAP Auth: failure, empty username');
931+
return false;
932+
}
933+
934+
log_message('debug', 'LDAP Auth: Loading Config');
935+
936+
$this->config->load('ldap.php', true);
937+
938+
$ldaptimeout = $this->config->item('timeout', 'ldap');
939+
$ldaphost = $this->config->item('server', 'ldap');
940+
$ldapport = $this->config->item('port', 'ldap');
941+
$ldaprdn = $this->config->item('binduser', 'ldap');
942+
$ldappass = $this->config->item('bindpw', 'ldap');
943+
$ldapbasedn = $this->config->item('basedn', 'ldap');
944+
945+
log_message('debug', 'LDAP Auth: Connect to ' . $ldaphost);
946+
947+
$ldapconfig['authrealm'] = $this->config->item('domain', 'ldap');
948+
949+
// connect to ldap server
950+
$ldapconn = ldap_connect($ldaphost, $ldapport);
951+
952+
if ($ldapconn) {
953+
954+
log_message('debug', 'Setting timeout to ' . $ldaptimeout . ' seconds');
955+
956+
ldap_set_option($ldapconn, LDAP_OPT_NETWORK_TIMEOUT, $ldaptimeout);
957+
958+
log_message('debug', 'LDAP Auth: Binding to ' . $ldaphost . ' with dn ' . $ldaprdn);
959+
960+
// binding to ldap server
961+
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
962+
963+
// verify binding
964+
if ($ldapbind) {
965+
log_message('debug', 'LDAP Auth: bind successful');
966+
} else {
967+
log_message('error', 'LDAP Auth: bind unsuccessful');
968+
return false;
969+
}
970+
971+
}
972+
973+
// search for user
974+
if (($res_id = ldap_search( $ldapconn, $ldapbasedn, "uid=$username")) == false) {
975+
log_message('error', 'LDAP Auth: User ' . $username . ' not found in search');
976+
return false;
977+
}
978+
979+
if (ldap_count_entries($ldapconn, $res_id) != 1) {
980+
log_message('error', 'LDAP Auth: failure, username ' . $username . 'found more than once');
981+
return false;
982+
}
983+
984+
if (( $entry_id = ldap_first_entry($ldapconn, $res_id))== false) {
985+
log_message('error', 'LDAP Auth: failure, entry of searchresult could not be fetched');
986+
return false;
987+
}
988+
989+
if (( $user_dn = ldap_get_dn($ldapconn, $entry_id)) == false) {
990+
log_message('error', 'LDAP Auth: failure, user-dn could not be fetched');
991+
return false;
992+
}
993+
994+
// User found, could not authenticate as user
995+
if (($link_id = ldap_bind($ldapconn, $user_dn, $password)) == false) {
996+
log_message('error', 'LDAP Auth: failure, username/password did not match: ' . $user_dn);
997+
return false;
998+
}
999+
1000+
log_message('debug', 'LDAP Auth: Success ' . $user_dn . ' authenticated successfully');
1001+
1002+
$this->_user_ldap_dn = $user_dn;
1003+
ldap_close($ldapconn);
1004+
return true;
1005+
}
1006+
9121007
/**
9131008
* Check if the user is logged in.
9141009
*
@@ -923,6 +1018,14 @@ protected function _check_login($username = '', $password = NULL)
9231018
return FALSE;
9241019
}
9251020

1021+
$auth_source = strtolower($this-config->item('auth_source'));
1022+
1023+
if ($auth_source == 'ldap')
1024+
{
1025+
log_message('debug', 'performing LDAP authentication for $username');
1026+
return $this->_perform_ldap_auth($username, $password);
1027+
}
1028+
9261029
$valid_logins = & $this->config->item('rest_valid_logins');
9271030

9281031
if ( ! array_key_exists($username, $valid_logins))

0 commit comments

Comments
 (0)