Skip to content

Commit ff7188e

Browse files
committed
Rework exceptions as assertions for PHP8
1 parent 1edaed2 commit ff7188e

2 files changed

Lines changed: 60 additions & 65 deletions

File tree

src/Auth/Process/GenerateUniqueId.php

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,27 @@ public function __construct(array $config, $reserved)
8888
*
8989
* @param string $value base64 encoded value from LDAP
9090
* @return string decoded guid
91-
* @throws \SimpleSAML\Error\Exception
91+
* @throws \SimpleSAML\Assert\AssertionFailedException
9292
*/
9393
private function decodeActiveDirectory(string $value): string
9494
{
95-
try {
96-
$decoded = base64_decode($value);
97-
$unpacked = unpack('Va/v2b/n2c/Nd', $decoded);
98-
$guid = strtolower(
99-
sprintf(
100-
'%08X%04X%04X%04X%04X%08X',
101-
$unpacked['a'],
102-
$unpacked['b1'],
103-
$unpacked['b2'],
104-
$unpacked['c1'],
105-
$unpacked['c2'],
106-
$unpacked['d']
107-
)
108-
);
109-
} catch (\Exception $e) {
110-
throw new Error\Exception(
111-
"GenerateUniqueId: unable to unpack " . $this->sourceAttribute . ": " . $e->getMessage()
112-
);
113-
}
95+
$decoded = base64_decode($value);
96+
Assert::notFalse($decoded, 'unable to unpack ' . $this->sourceAttribute . ': base64_decode failed');
97+
Assert::minLength($decoded, 12, 'unable to unpack ' . $this->sourceAttribute . ': decoded string too short');
98+
$unpacked = unpack('Va/v2b/n2c/Nd', $decoded);
99+
Assert::notFalse($unpacked, 'unable to unpack ' . $this->sourceAttribute . ': unpack failed');
100+
$guid = strtolower(
101+
sprintf(
102+
'%08X%04X%04X%04X%04X%08X',
103+
$unpacked['a'],
104+
$unpacked['b1'],
105+
$unpacked['b2'],
106+
$unpacked['c1'],
107+
$unpacked['c2'],
108+
$unpacked['d']
109+
)
110+
);
111+
Assert::length($guid, 32, 'unable to unpack ' . $this->sourceAttribute . ': repack failed');
114112
return $guid;
115113
}
116114

@@ -119,29 +117,27 @@ private function decodeActiveDirectory(string $value): string
119117
*
120118
* @param string $value base64 encoded value from LDAP
121119
* @return string decoded guid
122-
* @throws \SimpleSAML\Error\Exception
120+
* @throws \SimpleSAML\Assert\AssertionFailedException
123121
*/
124122
private function decodeBinaryBigEndian(string $value): string
125123
{
126-
try {
127-
$decoded = base64_decode($value);
128-
$unpacked = unpack('Na/n2b/n2c/Nd', $decoded);
129-
$guid = strtolower(
130-
sprintf(
131-
'%08X%04X%04X%04X%04X%08X',
132-
$unpacked['a'],
133-
$unpacked['b1'],
134-
$unpacked['b2'],
135-
$unpacked['c1'],
136-
$unpacked['c2'],
137-
$unpacked['d']
138-
)
139-
);
140-
} catch (\Exception $e) {
141-
throw new Error\Exception(
142-
"GenerateUniqueId: unable to unpack " . $this->sourceAttribute . ": " . $e->getMessage()
143-
);
144-
}
124+
$decoded = base64_decode($value, true);
125+
Assert::notFalse($decoded, 'unable to unpack ' . $this->sourceAttribute . ': base64_decode failed');
126+
Assert::minLength($decoded, 12, 'unable to unpack ' . $this->sourceAttribute . ': decoded string too short');
127+
$unpacked = unpack('Na/n2b/n2c/Nd', $decoded);
128+
Assert::notFalse($unpacked, 'unable to unpack ' . $this->sourceAttribute . ': unpack failed');
129+
$guid = strtolower(
130+
sprintf(
131+
'%08X%04X%04X%04X%04X%08X',
132+
$unpacked['a'],
133+
$unpacked['b1'],
134+
$unpacked['b2'],
135+
$unpacked['c1'],
136+
$unpacked['c2'],
137+
$unpacked['d']
138+
)
139+
);
140+
Assert::length($guid, 32, 'unable to unpack ' . $this->sourceAttribute . ': repack failed');
145141
return $guid;
146142
}
147143

@@ -150,23 +146,20 @@ private function decodeBinaryBigEndian(string $value): string
150146
*
151147
* @param string $value value from LDAP
152148
* @return string decoded uuid
153-
* @throws \SimpleSAML\Error\Exception
149+
* @throws \SimpleSAML\Assert\AssertionFailedException
154150
*/
155151
private function decodeUuidString(string $value): string
156152
{
157-
if (
158-
preg_match(
159-
'/^([0-9a-f]{8})\-?([0-9a-f]{4})\-?([0-9a-f]{4})\-?([0-9a-f]{4})\-?([0-9a-f]{12})$/',
160-
strtolower($value),
161-
$m
162-
)
163-
) {
164-
return implode('', array_slice($m, 1, 5));
165-
} else {
166-
throw new Error\Exception(
167-
"GenerateUniqueId: unable to unpack " . $this->sourceAttribute
168-
);
169-
}
153+
preg_match(
154+
'/^([0-9a-f]{8})\-?([0-9a-f]{4})\-?([0-9a-f]{4})\-?([0-9a-f]{4})\-?([0-9a-f]{12})$/',
155+
strtolower($value),
156+
$m
157+
);
158+
Assert::count($m, 6, 'unable to unpack ' . $this->sourceAttribute . ': wrong number of parts in uuid');
159+
160+
$guid = implode('', array_slice($m, 1, 5));
161+
Assert::length($guid, 32, 'unable to unpack ' . $this->sourceAttribute . ': repack failed');
162+
return $guid;
170163
}
171164

172165
/**
@@ -175,7 +168,6 @@ private function decodeUuidString(string $value): string
175168
* @param string $value uuid
176169
* @param string $source authentication source
177170
* @return string hashed version
178-
* @throws \SimpleSAML\Error\Exception
179171
*/
180172
private function privacyHash(string $value, string $source = ''): string
181173
{
@@ -187,7 +179,7 @@ private function privacyHash(string $value, string $source = ''): string
187179
* Process this filter
188180
*
189181
* @param mixed &$state
190-
* @throws \SimpleSAML\Error\Exception
182+
* @throws \SimpleSAML\Assert\AssertionFailedException
191183
* @return void
192184
*/
193185
public function process(array &$state): void
@@ -206,8 +198,9 @@ public function process(array &$state): void
206198

207199
foreach ($state['Attributes'][$this->scopeAttribute] as $scope) {
208200
if (strpos($scope, '@') !== false) {
209-
$scope = explode('@', $scope, 2);
210-
$scope = $scope[1];
201+
$scopeParts = explode('@', $scope, 2);
202+
/** @psalm-suppress PossiblyUndefinedArrayOffset */
203+
$scope = $scopeParts[1];
211204
}
212205

213206
foreach ($state['Attributes'][$this->sourceAttribute] as $value) {
@@ -223,7 +216,7 @@ public function process(array &$state): void
223216
$uuid = $this->decodeUuidString($value);
224217
break;
225218
default:
226-
$uuid = preg_replace('/[^a-z0-9]/', '', $value);
219+
$uuid = preg_replace('/[^a-z0-9]/', '', (string) $value);
227220
}
228221

229222
if ($uuid === null or $uuid === '') {

tests/src/Auth/Process/GenerateUniqueIdTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SimpleSAML\Test\Module\genuniqueid\Auth\Process;
66

77
use PHPUnit\Framework\TestCase;
8+
use SimpleSAML\Assert;
89
use SimpleSAML\Configuration;
910
use SimpleSAML\Error;
1011
use SimpleSAML\Module\genuniqueid\Auth\Process\GenerateUniqueId;
@@ -225,14 +226,15 @@ public function testBogusMicrosoft(): void
225226
'eduPersonPrincipalName' => ['nobody@example.org'],
226227
],
227228
];
228-
$this->expectException(Error\Exception::class);
229-
$this->expectExceptionMessage("GenerateUniqueId: unable to unpack objectGUID");
229+
$this->expectException(Assert\AssertionFailedException::class);
230+
$this->expectExceptionMessage("unable to unpack objectGUID:");
230231
$result = self::processFilter(
231232
[
232233
'encoding' => 'microsoft',
233234
],
234235
$request
235236
);
237+
$this->assertEquals($result['eduPersonUniqueId'], '');
236238
}
237239

238240
public function testEdirectory(): void
@@ -269,8 +271,8 @@ public function testBogusEdirectory(): void
269271
'eduPersonPrincipalName' => ['nobody@example.org'],
270272
],
271273
];
272-
$this->expectException(Error\Exception::class);
273-
$this->expectExceptionMessage("GenerateUniqueId: unable to unpack guid");
274+
$this->expectException(Assert\AssertionFailedException::class);
275+
$this->expectExceptionMessage("unable to unpack guid:");
274276
$result = self::processFilter(
275277
[
276278
'encoding' => 'edirectory',
@@ -313,8 +315,8 @@ public function testBogusOpenLdap(): void
313315
'eduPersonPrincipalName' => ['nobody@example.org'],
314316
],
315317
];
316-
$this->expectException(Error\Exception::class);
317-
$this->expectExceptionMessage("GenerateUniqueId: unable to unpack entryUUID");
318+
$this->expectException(Assert\AssertionFailedException::class);
319+
$this->expectExceptionMessage("unable to unpack entryUUID");
318320
$result = self::processFilter(
319321
[
320322
'encoding' => 'openldap',

0 commit comments

Comments
 (0)