Skip to content

Commit 55f686b

Browse files
authored
Fix Random utility import to use SimpleSAML\Utils\Random (#72)
* Fix Random utility import to use SimpleSAML\Utils\Random * Use IDValue::generateID() for ticket IDs (previous ID generation approaches are deprecated) * Refactor ticket ID generation to pass prefixes to generateID instead of str_replace
1 parent 40911d3 commit 55f686b

3 files changed

Lines changed: 12 additions & 18 deletions

File tree

‎src/Cas/Factories/TicketFactory.php‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace SimpleSAML\Module\casserver\Cas\Factories;
2727

2828
use SimpleSAML\Configuration;
29-
use SimpleSAML\XML\Utils\Random;
29+
use SimpleSAML\XMLSchema\Type\IDValue;
3030

3131
class TicketFactory
3232
{
@@ -58,11 +58,10 @@ public function __construct(Configuration $config)
5858
*/
5959
public function createSessionTicket(string $sessionId, int $expiresAt): array
6060
{
61-
$randomUtils = new Random();
6261
return [
6362
'id' => $sessionId,
6463
'validBefore' => $expiresAt,
65-
'renewId' => $randomUtils->generateID(),
64+
'renewId' => IDValue::generateID()->getValue(),
6665
];
6766
}
6867

@@ -73,8 +72,7 @@ public function createSessionTicket(string $sessionId, int $expiresAt): array
7372
*/
7473
public function createServiceTicket(array $content): array
7574
{
76-
$randomUtils = new Random();
77-
$id = str_replace('_', 'ST-', $randomUtils->generateID());
75+
$id = IDValue::generateID('ST-')->getValue();
7876
$expiresAt = time() + $this->serviceTicketExpireTime;
7977

8078
return array_merge(['id' => $id, 'validBefore' => $expiresAt], $content);
@@ -87,9 +85,8 @@ public function createServiceTicket(array $content): array
8785
*/
8886
public function createProxyGrantingTicket(array $content): array
8987
{
90-
$randomUtils = new Random();
91-
$id = str_replace('_', 'PGT-', $randomUtils->generateID());
92-
$iou = str_replace('_', 'PGTIOU-', $randomUtils->generateID());
88+
$id = IDValue::generateID('PGT-')->getValue();
89+
$iou = IDValue::generateID('PGTIOU-')->getValue();
9390

9491
$expireAt = time() + $this->proxyGrantingTicketExpireTime;
9592

@@ -103,8 +100,7 @@ public function createProxyGrantingTicket(array $content): array
103100
*/
104101
public function createProxyTicket(array $content): array
105102
{
106-
$randomUtils = new Random();
107-
$id = str_replace('_', 'PT-', $randomUtils->generateID());
103+
$id = IDValue::generateID('PT-')->getValue();
108104
$expiresAt = time() + $this->proxyTicketExpireTime;
109105

110106
return array_merge(['id' => $id, 'validBefore' => $expiresAt], $content);

‎src/Shib13/AuthnResponse.php‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use SimpleSAML\Utils;
1818
use SimpleSAML\XML\DOMDocumentFactory;
1919
use SimpleSAML\XML\Utils as XMLUtils;
20-
use SimpleSAML\XML\Utils\Random;
2120
use SimpleSAML\XML\Validator;
21+
use SimpleSAML\XMLSchema\Type\IDValue;
2222
use SimpleXMLElement;
2323

2424
/**
@@ -362,25 +362,24 @@ public function generate(Configuration $idp, Configuration $sp, string $shire, ?
362362
$scopedAttributes = [];
363363
}
364364

365-
$randomUtils = new Random();
366365
$timeUtils = new Utils\Time();
367366

368-
$id = $randomUtils->generateID();
367+
$id = IDValue::generateID()->getValue();
369368
$issueInstant = $timeUtils->generateTimestamp();
370369

371370
// 30 seconds timeskew back in time to allow differing clocks
372371
$notBefore = $timeUtils->generateTimestamp(time() - 30);
373372

374373
$assertionExpire = $timeUtils->generateTimestamp(time() + 300); // 5 minutes
375-
$assertionid = $randomUtils->generateID();
374+
$assertionid = IDValue::generateID()->getValue();
376375

377376
$spEntityId = $sp->getString('entityid');
378377

379378
$audience = $sp->getOptionalString('audience', $spEntityId);
380379
$base64 = $sp->getOptionalBoolean('base64attributes', false);
381380

382381
$namequalifier = $sp->getOptionalString('NameQualifier', $spEntityId);
383-
$nameid = $randomUtils->generateID();
382+
$nameid = (string)IDValue::generateID();
384383
$subjectNode =
385384
'<Subject>' .
386385
'<NameIdentifier' .

‎tests/src/TicketValidatorTest.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use SimpleSAML\Module\casserver\Cas\Ticket\FileSystemTicketStore;
1313
use SimpleSAML\Module\casserver\Cas\Ticket\TicketStore;
1414
use SimpleSAML\Module\casserver\Cas\TicketValidator;
15-
use SimpleSAML\XML\Utils\Random;
15+
use SimpleSAML\XMLSchema\Type\IDValue;
1616

1717
class TicketValidatorTest extends TestCase
1818
{
@@ -170,8 +170,7 @@ public static function urlSanitizationProvider(): array
170170
*/
171171
private function createTicket(string $serviceUrl, int $expiration = 0): array
172172
{
173-
$randomUtils = new Random();
174-
$id = $randomUtils->generateID();
173+
$id = IDValue::generateID()->getValue();
175174
$serviceTicket = [
176175
'id' => $id,
177176
'validBefore' => time() + $expiration,

0 commit comments

Comments
 (0)