Skip to content

Commit 9c1083e

Browse files
authored
Fix Psalm issues (#1)
* Fix psalm issues
1 parent 8d13ec5 commit 9c1083e

File tree

6 files changed

+86
-9
lines changed

6 files changed

+86
-9
lines changed

lib/IdP/ADFS.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
class ADFS
1111
{
12+
/**
13+
* @param \SimpleSAML\IdP @idp
14+
* @return void
15+
* @throws \SimpleSAML\Error\Error
16+
*/
1217
public static function receiveAuthnRequest(\SimpleSAML\IdP $idp)
1318
{
1419
try {
@@ -41,6 +46,15 @@ public static function receiveAuthnRequest(\SimpleSAML\IdP $idp)
4146
$idp->handleAuthenticationRequest($state);
4247
}
4348

49+
50+
/**
51+
* @param string $issuer
52+
* @param string $target
53+
* @param string $nameid
54+
* @param array $attributes
55+
* @param int $assertionLifetime
56+
* @return string
57+
*/
4458
private static function generateResponse($issuer, $target, $nameid, $attributes, $assertionLifetime)
4559
{
4660
$issueInstant = \SimpleSAML\Utils\Time::generateTimestamp();
@@ -108,13 +122,26 @@ private static function generateResponse($issuer, $target, $nameid, $attributes,
108122
return $result;
109123
}
110124

125+
126+
/**
127+
* @param string $response
128+
* @param string $key
129+
* @param string $cert
130+
* @param string $algo
131+
* @return string
132+
*/
111133
private static function signResponse($response, $key, $cert, $algo)
112134
{
113135
$objXMLSecDSig = new XMLSecurityDSig();
114136
$objXMLSecDSig->idKeys = ['AssertionID'];
115137
$objXMLSecDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
116138
$responsedom = \SAML2\DOMDocumentFactory::fromString(str_replace("\r", "", $response));
117139
$firstassertionroot = $responsedom->getElementsByTagName('Assertion')->item(0);
140+
141+
if (is_null($firstassertionroot)) {
142+
throw new \Exception("No assertion found in response.");
143+
}
144+
118145
$objXMLSecDSig->addReferenceList(
119146
[$firstassertionroot],
120147
XMLSecurityDSig::SHA256,
@@ -129,11 +156,20 @@ private static function signResponse($response, $key, $cert, $algo)
129156
$public_cert = file_get_contents($cert);
130157
$objXMLSecDSig->add509Cert($public_cert, true);
131158
}
159+
160+
/** @var \DOMElement $objXMLSecDSig->sigNode */
132161
$newSig = $responsedom->importNode($objXMLSecDSig->sigNode, true);
133162
$firstassertionroot->appendChild($newSig);
134163
return $responsedom->saveXML();
135164
}
136165

166+
167+
/**
168+
* @param string $url
169+
* @param string $wresult
170+
* @param string $wctx
171+
* @return void
172+
*/
137173
private static function postResponse($url, $wresult, $wctx)
138174
{
139175
$config = \SimpleSAML\Configuration::getInstance();
@@ -193,6 +229,7 @@ public static function getHostedMetadata($entityid)
193229
$hasNewCert = true;
194230
}
195231

232+
/** @var array $certInfo */
196233
$certInfo = Crypto::loadPublicKey($config, true);
197234
$keys[] = [
198235
'type' => 'X509Certificate',
@@ -203,6 +240,7 @@ public static function getHostedMetadata($entityid)
203240
];
204241

205242
if ($config->hasValue('https.certificate')) {
243+
/** @var array $httpsCert */
206244
$httpsCert = Crypto::loadPublicKey($config, true, 'https.');
207245
$keys[] = [
208246
'type' => 'X509Certificate',
@@ -223,7 +261,7 @@ public static function getHostedMetadata($entityid)
223261
);
224262

225263
if (!$config->hasValue('OrganizationURL')) {
226-
throw new \SimpleSAMl\Error\Exception('If OrganizationName is set, OrganizationURL must also be set.');
264+
throw new \SimpleSAML\Error\Exception('If OrganizationName is set, OrganizationURL must also be set.');
227265
}
228266
$metadata['OrganizationURL'] = $config->getLocalizedString('OrganizationURL');
229267
}
@@ -271,6 +309,11 @@ public static function getHostedMetadata($entityid)
271309
}
272310

273311

312+
/**
313+
* @param array $state
314+
* @throws \Exception
315+
* @return void
316+
*/
274317
public static function sendResponse(array $state)
275318
{
276319
$spMetadata = $state["SPMetadata"];
@@ -323,6 +366,12 @@ public static function sendResponse(array $state)
323366
ADFS::postResponse($wreply, $wresult, $wctx);
324367
}
325368

369+
370+
/**
371+
* @param \SimpleSAML\IdP $idp
372+
* @param array $state
373+
* @return void
374+
*/
326375
public static function sendLogoutResponse(\SimpleSAML\IdP $idp, array $state)
327376
{
328377
// NB:: we don't know from which SP the logout request came from
@@ -332,6 +381,12 @@ public static function sendLogoutResponse(\SimpleSAML\IdP $idp, array $state)
332381
);
333382
}
334383

384+
385+
/**
386+
* @param \SimpleSAML\IdP $idp
387+
* @throws \Exception
388+
* @return void
389+
*/
335390
public static function receiveLogoutMessage(\SimpleSAML\IdP $idp)
336391
{
337392
// if a redirect is to occur based on wreply, we will redirect to url as
@@ -351,7 +406,15 @@ public static function receiveLogoutMessage(\SimpleSAML\IdP $idp)
351406
$idp->handleLogoutRequest($state, $assocId);
352407
}
353408

354-
// accepts an association array, and returns a URL that can be accessed to terminate the association
409+
410+
/**
411+
* accepts an association array, and returns a URL that can be accessed to terminate the association
412+
*
413+
* @param \SimpleSAML\IdP $idp
414+
* @param array $association
415+
* @param string $relayState
416+
* @return string
417+
*/
355418
public static function getLogoutURL(\SimpleSAML\IdP $idp, array $association, $relayState)
356419
{
357420
$metadata = \SimpleSAML\Metadata\MetaDataStorageHandler::getMetadataHandler();

lib/SAML2/XML/fed/Endpoint.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace SimpleSAML\Module\adfs\SAML2\XML\fed;
44

5-
use Webmozard\Assert\Assert;
5+
use Webmozart\Assert\Assert;
66

77
/**
88
* Class representing fed Endpoint.
@@ -17,6 +17,8 @@ class Endpoint
1717
*
1818
* @param \DOMElement $parent The element we should append this endpoint to.
1919
* @param string $name The name of the element we should create.
20+
* @param string $address
21+
* @return \DOMElement
2022
*/
2123
public static function appendXML(\DOMElement $parent, $name, $address)
2224
{

lib/SAML2/XML/fed/SecurityTokenServiceType.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ class SecurityTokenServiceType extends \SAML2\XML\md\RoleDescriptor
1515
/**
1616
* List of supported protocols.
1717
*
18-
* @var array
18+
* @var array $protocolSupportEnumeration
1919
*/
2020
public $protocolSupportEnumeration = [Constants::NS_FED];
2121

2222
/**
2323
* The Location of Services.
2424
*
25-
* @var string
25+
* @var string|null $Location
2626
*/
27-
public $Location;
27+
public $Location = null;
28+
2829

2930
/**
3031
* Initialize a SecurityTokenServiceType element.
@@ -49,6 +50,10 @@ public function toXML(\DOMElement $parent)
4950
{
5051
Assert::string($this->Location);
5152

53+
if (is_null($this->Location)) {
54+
throw new \Exception('Location not set');
55+
}
56+
5257
$e = parent::toXML($parent);
5358
$e->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:fed', Constants::NS_FED);
5459
$e->setAttributeNS(\SAML2\Constants::NS_XSI, 'xsi:type', 'fed:SecurityTokenServiceType');
@@ -63,7 +68,7 @@ public function toXML(\DOMElement $parent)
6368
/**
6469
* Get the location of this service.
6570
*
66-
* @return string The full URL where this service can be reached.
71+
* @return string|null The full URL where this service can be reached.
6772
*/
6873
public function getLocation()
6974
{
@@ -75,6 +80,7 @@ public function getLocation()
7580
* Set the location of this service.
7681
*
7782
* @param string $location The full URL where this service can be reached.
83+
* @return void
7884
*/
7985
public function setLocation($location)
8086
{

lib/SAML2/XML/fed/TokenTypesOffered.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TokenTypesOffered
1414
* Add tokentypesoffered to an XML element.
1515
*
1616
* @param \DOMElement $parent The element we should append this endpoint to.
17+
* @return \DOMElement
1718
*/
1819
public static function appendXML(\DOMElement $parent)
1920
{

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
$projectRoot = dirname(__DIR__);
4+
/** @psalm-suppress UnresolvableInclude */
45
require_once($projectRoot.'/vendor/autoload.php');
56

67
// Symlink module into ssp vendor lib so that templates and urls can resolve correctly

www/idp/metadata.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
$hasNewCert = false;
3838
}
3939

40+
/** @var array $certInfo */
4041
$certInfo = \SimpleSAML\Utils\Crypto::loadPublicKey($idpmeta, true);
4142
$availableCerts['idp.crt'] = $certInfo;
4243
$keys[] = [
@@ -47,6 +48,7 @@
4748
];
4849

4950
if ($idpmeta->hasValue('https.certificate')) {
51+
/** @var array $httpsCert */
5052
$httpsCert = \SimpleSAML\Utils\Crypto::loadPublicKey($idpmeta, true, 'https.');
5153
Assert::keyExists($httpsCert, 'certData');
5254
$availableCerts['https.crt'] = $httpsCert;
@@ -173,9 +175,11 @@
173175
header('Content-Type: application/xml');
174176

175177
// make sure to export only the md:EntityDescriptor
176-
$metaxml = substr($metaxml, strpos($metaxml, '<md:EntityDescriptor'));
178+
$i = strpos($metaxml, '<md:EntityDescriptor');
179+
$metaxml = substr($metaxml, $i ? $i : 0);
177180
// 22 = strlen('</md:EntityDescriptor>')
178-
$metaxml = substr($metaxml, 0, strrpos($metaxml, '</md:EntityDescriptor>') + 22);
181+
$i = strrpos($metaxml, '</md:EntityDescriptor>');
182+
$metaxml = substr($metaxml, 0, $i ? $i + 22 : 0);
179183
echo $metaxml;
180184

181185
exit(0);

0 commit comments

Comments
 (0)