Skip to content

Commit 4efa847

Browse files
committed
Prepare for SSP 2.0
1 parent 76d376f commit 4efa847

10 files changed

Lines changed: 65 additions & 112 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
}
3232
},
3333
"require": {
34-
"php": ">=5.6",
34+
"php": ">=7.2",
3535
"ext-ldap": "*",
3636
"simplesamlphp/composer-module-installer": "~1.1",
37+
"simplesamlphp/simplesamlphp": "dev-master",
3738
"webmozart/assert": "~1.4"
3839
},
3940
"require-dev": {
40-
"simplesamlphp/simplesamlphp": "^1.17",
4141
"simplesamlphp/simplesamlphp-test-framework": "^0.1.0"
4242
},
4343
"support": {

default-enable

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/Auth/Ldap.php

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ class Ldap
6565
* @psalm-suppress NullArgument
6666
*/
6767
public function __construct(
68-
$hostname,
69-
$enable_tls = true,
70-
$debug = false,
71-
$timeout = 0,
72-
$port = 389,
73-
$referrals = true
68+
string $hostname,
69+
bool $enable_tls = true,
70+
bool $debug = false,
71+
int $timeout = 0,
72+
int $port = 389,
73+
bool $referrals = true
7474
) {
7575
// Debug
7676
Logger::debug('Library - LDAP __construct(): Setup LDAP with ' .
@@ -152,7 +152,7 @@ public function __construct(
152152
* @param int|null $type The exception's type
153153
* @return \Exception
154154
*/
155-
private function makeException($description, $type = null)
155+
private function makeException(string $description, int $type = null): \Exception
156156
{
157157
$errNo = @ldap_errno($this->ldap);
158158

@@ -234,8 +234,13 @@ private function makeException($description, $type = null)
234234
* - Zero entries were found
235235
* @psalm-suppress TypeDoesNotContainType
236236
*/
237-
private function search($base, $attribute, $value, $searchFilter = null, $scope = "subtree")
238-
{
237+
private function search(
238+
string $base,
239+
$attribute,
240+
string $value,
241+
?string $searchFilter = null,
242+
string $scope = "subtree"
243+
): string {
239244
// Create the search filter
240245
/** @var array $attribute */
241246
$attribute = self::escapeFilterValue($attribute, false);
@@ -342,11 +347,11 @@ private function search($base, $attribute, $value, $searchFilter = null, $scope
342347
public function searchfordn(
343348
$base,
344349
$attribute,
345-
$value,
346-
$allowZeroHits = false,
347-
$searchFilter = null,
348-
$scope = 'subtree'
349-
) {
350+
string $value,
351+
bool $allowZeroHits = false,
352+
?string $searchFilter = null,
353+
string $scope = 'subtree'
354+
): ?string {
350355
// Traverse all search bases, returning DN if found
351356
$bases = Utils\Arrays::arrayize($base);
352357
foreach ($bases as $current) {
@@ -395,10 +400,10 @@ public function searchformultiple(
395400
$bases,
396401
$filters,
397402
$attributes = [],
398-
$and = true,
399-
$escape = true,
400-
$scope = 'subtree'
401-
) {
403+
bool $and = true,
404+
bool $escape = true,
405+
string $scope = 'subtree'
406+
): array {
402407
// Escape the filter values, if requested
403408
if ($escape) {
404409
$filters = $this->escapeFilterValue($filters, false);
@@ -414,6 +419,7 @@ public function searchformultiple(
414419
$filter = ($and ? '(&' : '(|') . $filter . ')';
415420
}
416421
} else {
422+
/** @psalm-suppress RedundantConditionGivenDocblockType */
417423
Assert::string($filters);
418424
$filter = $filters;
419425
}
@@ -516,7 +522,7 @@ public function searchformultiple(
516522
* LDAP_INAPPROPRIATE_AUTH, LDAP_INSUFFICIENT_ACCESS
517523
* @throws Error\Exception on other errors
518524
*/
519-
public function bind($dn, $password, array $sasl_args = null)
525+
public function bind(string $dn, string $password, array $sasl_args = null): ?bool
520526
{
521527
if ($sasl_args != null) {
522528
if (!function_exists('ldap_sasl_bind')) {
@@ -580,7 +586,7 @@ public function bind($dn, $password, array $sasl_args = null)
580586
* @param mixed $value
581587
* @return void
582588
*/
583-
public function setOption($option, $value)
589+
public function setOption($option, $value): void
584590
{
585591
// Attempt to set the LDAP option
586592
if (!@ldap_set_option($this->ldap, $option, $value)) {
@@ -615,7 +621,7 @@ public function setOption($option, $value)
615621
* The array of attributes and their values.
616622
* @see http://no.php.net/manual/en/function.ldap-read.php
617623
*/
618-
public function getAttributes($dn, $attributes = null, $maxsize = null)
624+
public function getAttributes(string $dn, $attributes = null, int $maxsize = null): array
619625
{
620626
// Preparations, including a pretty debug message...
621627
$description = 'all attributes';
@@ -698,10 +704,10 @@ public function getAttributes($dn, $attributes = null, $maxsize = null)
698704
*
699705
* @param array $config
700706
* @param string $username
701-
* @param string $password
707+
* @param string|null $password
702708
* @return array|false
703709
*/
704-
public function validate($config, $username, $password = null)
710+
public function validate(array $config, string $username, string $password = null)
705711
{
706712
/**
707713
* Escape any characters with a special meaning in LDAP. The following
@@ -755,7 +761,7 @@ public function validate($config, $username, $password = null)
755761
* @param bool $singleValue
756762
* @return string|array Array $values, but escaped
757763
*/
758-
public static function escapeFilterValue($values = [], $singleValue = true)
764+
public static function escapeFilterValue($values = [], bool $singleValue = true)
759765
{
760766
// Parameter validation
761767
$values = Utils\Arrays::arrayize($values);
@@ -793,7 +799,7 @@ public static function escapeFilterValue($values = [], $singleValue = true)
793799
* @static
794800
* @return string
795801
*/
796-
public static function asc2hex32($string)
802+
public static function asc2hex32(string $string): string
797803
{
798804
for ($i = 0; $i < strlen($string); $i++) {
799805
$char = substr($string, $i, 1);
@@ -808,6 +814,7 @@ public static function asc2hex32($string)
808814
return $string;
809815
}
810816

817+
811818
/**
812819
* Convert SASL authz_id into a DN
813820
*
@@ -816,7 +823,7 @@ public static function asc2hex32($string)
816823
* @param string $authz_id
817824
* @return string|null
818825
*/
819-
private function authzidToDn($searchBase, $searchAttributes, $authz_id)
826+
private function authzidToDn(string $searchBase, array $searchAttributes, string $authz_id): ?string
820827
{
821828
if (preg_match("/^dn:/", $authz_id)) {
822829
return preg_replace("/^dn:/", "", $authz_id);
@@ -832,46 +839,24 @@ private function authzidToDn($searchBase, $searchAttributes, $authz_id)
832839
return $authz_id;
833840
}
834841

842+
835843
/**
836844
* ldap_exop_whoami accessor, if available. Use requested authz_id
837845
* otherwise.
838846
*
839-
* ldap_exop_whoami() has been provided as a third party patch that
840-
* waited several years to get its way upstream:
841-
* http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/databases/php-ldap/files
842-
*
843-
* When it was integrated into PHP repository, the function prototype
844-
* was changed, The new prototype was used in third party patch for
845-
* PHP 7.0 and 7.1, hence the version test below.
846-
*
847847
* @param string $searchBase
848848
* @param array $searchAttributes
849849
* @throws \Exception
850850
* @return string
851851
*/
852-
public function whoami($searchBase, $searchAttributes)
852+
public function whoami(string $searchBase, array $searchAttributes): string
853853
{
854-
$authz_id = '';
855-
if (function_exists('ldap_exop_whoami')) {
856-
if (version_compare(phpversion(), '7', '<')) {
857-
/** @psalm-suppress TooManyArguments */
858-
if (ldap_exop_whoami($this->ldap, $authz_id) === false) {
859-
throw $this->makeException('LDAP whoami exop failure');
860-
}
861-
} else {
862-
$authz_id = ldap_exop_whoami($this->ldap);
863-
if ($authz_id === false) {
864-
throw $this->makeException('LDAP whoami exop failure');
865-
}
866-
}
867-
} else {
868-
Assert::string($authz_id);
869-
/** @var string $authz_id */
870-
$authz_id = $this->authz_id;
854+
$authz_id = ldap_exop_whoami($this->ldap);
855+
if ($authz_id === false) {
856+
throw $this->makeException('LDAP whoami exop failure');
871857
}
872858

873859
$dn = $this->authzidToDn($searchBase, $searchAttributes, $authz_id);
874-
875860
if (empty($dn)) {
876861
throw $this->makeException('Cannot figure userID');
877862
}

lib/Auth/Process/AttributeAddFromLDAP.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ class AttributeAddFromLDAP extends BaseFilter
4848
*/
4949
protected $search_attributes;
5050

51-
5251
/**
5352
* LDAP search filter to use in the LDAP query
5453
*
5554
* @var string
5655
*/
5756
protected $search_filter;
5857

59-
6058
/**
6159
* What to do with attributes when the target already exists. Either replace, merge or add.
6260
*
@@ -71,7 +69,7 @@ class AttributeAddFromLDAP extends BaseFilter
7169
* @param array $config Configuration information about this filter.
7270
* @param mixed $reserved For future use.
7371
*/
74-
public function __construct($config, $reserved)
72+
public function __construct(array $config, $reserved)
7573
{
7674
parent::__construct($config, $reserved);
7775

@@ -94,9 +92,8 @@ public function __construct($config, $reserved)
9492
* @param array &$request The current request
9593
* @return void
9694
*/
97-
public function process(&$request)
95+
public function process(array &$request): void
9896
{
99-
Assert::isArray($request);
10097
Assert::keyExists($request, 'Attributes');
10198

10299
$attributes = &$request['Attributes'];

lib/Auth/Process/AttributeAddUsersGroups.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ class AttributeAddUsersGroups extends BaseFilter
2525
* are then added to the request attributes.
2626
*
2727
* @throws \SimpleSAML\Error\Exception
28-
* @param $request
28+
* @param array $request
2929
* @return void
3030
*/
31-
public function process(&$request)
31+
public function process(array &$request): void
3232
{
33-
Assert::isArray($request);
3433
Assert::keyExists($request, 'Attributes');
3534

3635
// Log the process
@@ -82,7 +81,7 @@ public function process(&$request)
8281
* @param array $attributes
8382
* @return array
8483
*/
85-
protected function getGroups($attributes)
84+
protected function getGroups(array $attributes): array
8685
{
8786
// Log the request
8887
Logger::debug(
@@ -144,7 +143,7 @@ protected function getGroups($attributes)
144143
* @param array $attributes
145144
* @return array
146145
*/
147-
protected function getGroupsOpenLdap($attributes)
146+
protected function getGroupsOpenLdap(array $attributes): array
148147
{
149148
// Log the OpenLDAP specific search
150149
Logger::debug(
@@ -193,7 +192,7 @@ protected function getGroupsOpenLdap($attributes)
193192
* @param array $attributes
194193
* @return array
195194
*/
196-
protected function getGroupsActiveDirectory($attributes)
195+
protected function getGroupsActiveDirectory(array $attributes): array
197196
{
198197
// Log the AD specific search
199198
Logger::debug(
@@ -223,6 +222,7 @@ protected function getGroupsActiveDirectory($attributes)
223222
return $this->searchActiveDirectory($attributes[$map['dn']][0]);
224223
}
225224

225+
226226
/**
227227
* Looks for groups from the list of DN's passed. Also
228228
* recursively searches groups for further membership.
@@ -232,7 +232,7 @@ protected function getGroupsActiveDirectory($attributes)
232232
* @param array $memberof
233233
* @return array
234234
*/
235-
protected function search(array $memberof)
235+
protected function search(array $memberof): array
236236
{
237237
// Used to determine what DN's have already been searched
238238
static $searched = [];
@@ -308,10 +308,8 @@ protected function search(array $memberof)
308308
* @param string $dn
309309
* @return array
310310
*/
311-
protected function searchActiveDirectory($dn)
311+
protected function searchActiveDirectory(string $dn): array
312312
{
313-
Assert::stringNotEmpty($dn);
314-
315313
// Shorten the variable name
316314
$map = &$this->attribute_map;
317315

0 commit comments

Comments
 (0)