{
return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
}
+
+ /**
+ * Explode a LDAP dn string into an array of components.
+ * @param string $dn
+ * @param int $withAttrib
+ * @return array
+ */
+ public function explodeDn(string $dn, int $withAttrib)
+ {
+ return ldap_explode_dn($dn, $withAttrib);
+ }
+
+ /**
+ * Escape a string for use in an LDAP filter.
+ * @param string $value
+ * @param string $ignore
+ * @param int $flags
+ * @return string
+ */
+ public function escape(string $value, string $ignore = "", int $flags = 0)
+ {
+ return ldap_escape($value, $ignore, $flags);
+ }
}
if ($ldapUser === null) {
return false;
}
+
if ($ldapUser['uid'] !== $user->external_auth_id) {
return false;
}
$newAttrs = [];
foreach ($attrs as $key => $attrText) {
$newKey = '${' . $key . '}';
- $newAttrs[$newKey] = $attrText;
+ $newAttrs[$newKey] = $this->ldap->escape($attrText);
}
return strtr($filterString, $newAttrs);
}
$baseDn = $this->config['base_dn'];
$groupsAttr = strtolower($this->config['group_attribute']);
- $groups = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, 'CN='.$groupName, [$groupsAttr]);
+ $groupFilter = 'CN=' . $this->ldap->escape($groupName);
+ $groups = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $groupFilter, [$groupsAttr]);
if ($groups['count'] === 0) {
return [];
}
/**
* Filter out LDAP CN and DN language in a ldap search return
* Gets the base CN (common name) of the string
- * @param string $ldapSearchReturn
+ * @param array $userGroupSearchResponse
* @return array
*/
- protected function groupFilter($ldapSearchReturn)
+ protected function groupFilter(array $userGroupSearchResponse)
{
$groupsAttr = strtolower($this->config['group_attribute']);
$ldapGroups = [];
$count = 0;
- if (isset($ldapSearchReturn[$groupsAttr]['count'])) {
- $count = (int) $ldapSearchReturn[$groupsAttr]['count'];
+
+ if (isset($userGroupSearchResponse[$groupsAttr]['count'])) {
+ $count = (int) $userGroupSearchResponse[$groupsAttr]['count'];
}
+
for ($i=0; $i<$count; $i++) {
- $dnComponents = ldap_explode_dn($ldapSearchReturn[$groupsAttr][$i], 1);
+ $dnComponents = $this->ldap->explodeDn($userGroupSearchResponse[$groupsAttr][$i], 1);
if (!in_array($dnComponents[0], $ldapGroups)) {
$ldapGroups[] = $dnComponents[0];
}
}
+
return $ldapGroups;
}
$this->mockUser = factory(User::class)->make();
}
+ protected function mockEscapes($times = 1)
+ {
+ $this->mockLdap->shouldReceive('escape')->times($times)->andReturnUsing(function($val) {
+ return ldap_escape($val);
+ });
+ }
+
+ protected function mockExplodes($times = 1)
+ {
+ $this->mockLdap->shouldReceive('explodeDn')->times($times)->andReturnUsing(function($dn, $withAttrib) {
+ return ldap_explode_dn($dn, $withAttrib);
+ });
+ }
+
public function test_login()
{
$this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId);
'dn' => ['dc=test' . config('services.ldap.base_dn')]
]]);
$this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true);
+ $this->mockEscapes(4);
$this->visit('/login')
->see('Username')
'mail' => [$this->mockUser->email]
]]);
$this->mockLdap->shouldReceive('bind')->times(3)->andReturn(true);
+ $this->mockEscapes(2);
$this->visit('/login')
->see('Username')
'dn' => ['dc=test' . config('services.ldap.base_dn')]
]]);
$this->mockLdap->shouldReceive('bind')->times(3)->andReturn(true, true, false);
+ $this->mockEscapes(2);
$this->visit('/login')
->see('Username')
->dontSee('External Authentication');
}
- public function test_login_maps_roles_and_retains_existsing_roles()
+ public function test_login_maps_roles_and_retains_existing_roles()
{
$roleToReceive = factory(Role::class)->create(['name' => 'ldaptester', 'display_name' => 'LdapTester']);
$roleToReceive2 = factory(Role::class)->create(['name' => 'ldaptester-second', 'display_name' => 'LdapTester Second']);
]
]]);
$this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true);
+ $this->mockEscapes(5);
+ $this->mockExplodes(6);
$this->visit('/login')
->see('Username')
]
]]);
$this->mockLdap->shouldReceive('bind')->times(5)->andReturn(true);
+ $this->mockEscapes(4);
+ $this->mockExplodes(2);
$this->visit('/login')
->see('Username')
]
]]);
$this->mockLdap->shouldReceive('bind')->times(5)->andReturn(true);
+ $this->mockEscapes(4);
+ $this->mockExplodes(2);
$this->visit('/login')
->see('Username')
]
]]);
$this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true);
+ $this->mockEscapes(5);
+ $this->mockExplodes(6);
$this->visit('/login')
->see('Username')