Skip to content

Commit 176fe0f

Browse files
authored
Merge pull request #30 from pdsinterop/fix/public-request
Fix bug caused by "public" scenario not being correctly handled.
2 parents e705275 + 44232bb commit 176fe0f

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

src/Utils/DPop.php

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class DPop {
2828

2929
private JtiValidator $jtiValidator;
3030

31-
public function __construct(JtiValidator $jtiValidator)
32-
{
33-
$this->jtiValidator = $jtiValidator;
34-
}
31+
public function __construct(JtiValidator $jtiValidator)
32+
{
33+
$this->jtiValidator = $jtiValidator;
34+
}
3535

3636
/**
3737
* This method fetches the WebId from a request and verifies
@@ -48,29 +48,28 @@ public function __construct(JtiValidator $jtiValidator)
4848
public function getWebId($request) {
4949
$serverParams = $request->getServerParams();
5050

51-
$this->validateRequestHeaders($serverParams);
52-
53-
[, $jwt] = explode(" ", $serverParams['HTTP_AUTHORIZATION'], 2);
51+
if (isset($serverParams['HTTP_AUTHORIZATION']) === false) {
52+
$webId = "public";
53+
} else {
54+
$this->validateRequestHeaders($serverParams);
5455

55-
$dpop = $serverParams['HTTP_DPOP'];
56+
[, $jwt] = explode(" ", $serverParams['HTTP_AUTHORIZATION'], 2);
5657

57-
//@FIXME: check that there is just one DPoP token in the request
58-
try {
59-
$dpopKey = $this->getDpopKey($dpop, $request);
60-
} catch (InvalidTokenStructure $e) {
61-
throw new InvalidTokenException("Invalid JWT token: {$e->getMessage()}", 0, $e);
62-
}
58+
$dpop = $serverParams['HTTP_DPOP'];
6359

64-
try {
65-
$this->validateJwtDpop($jwt, $dpopKey);
66-
} catch (RequiredConstraintsViolated $e) {
67-
throw new InvalidTokenException($e->getMessage(), 0, $e);
68-
}
60+
//@FIXME: check that there is just one DPoP token in the request
61+
try {
62+
$dpopKey = $this->getDpopKey($dpop, $request);
63+
} catch (InvalidTokenStructure $e) {
64+
throw new InvalidTokenException("Invalid JWT token: {$e->getMessage()}", 0, $e);
65+
}
6966

70-
if ($jwt) {
67+
try {
68+
$this->validateJwtDpop($jwt, $dpopKey);
69+
} catch (RequiredConstraintsViolated $e) {
70+
throw new InvalidTokenException($e->getMessage(), 0, $e);
71+
}
7172
$webId = $this->getSubjectFromJwt($jwt);
72-
} else {
73-
$webId = "public";
7473
}
7574

7675
return $webId;
@@ -274,10 +273,6 @@ private function getSubjectFromJwt($jwt) {
274273
}
275274

276275
private function validateRequestHeaders($serverParams) {
277-
if (isset($serverParams['HTTP_AUTHORIZATION']) === false) {
278-
throw new AuthorizationHeaderException("Authorization Header missing");
279-
}
280-
281276
if (str_contains($serverParams['HTTP_AUTHORIZATION'], ' ') === false) {
282277
throw new AuthorizationHeaderException("Authorization Header does not contain parameters");
283278
}

tests/unit/Utils/DPOPTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ final public function testGetWebIdWithoutRequest(): void
234234
}
235235

236236
/**
237-
* @testdox Dpop SHOULD complain WHEN asked to get WebId from Request without Authorization Header
237+
* @testdox Dpop SHOULD return 'public' WHEN asked to get WebId from Request without Authorization Header
238238
*
239239
* @covers ::getWebId
240240
*/
@@ -245,10 +245,10 @@ final public function testGetWebIdWithoutHttpAuthorizationHeader(): void
245245

246246
$request = new ServerRequest(array(),array(), $this->url);
247247

248-
$this->expectException(AuthorizationHeaderException::class);
249-
$this->expectExceptionMessage('Authorization Header missing');
248+
$actual = $dpop->getWebId($request);
249+
$expected = 'public';
250250

251-
$dpop->getWebId($request);
251+
$this->assertEquals($expected, $actual);
252252
}
253253

254254
/**

0 commit comments

Comments
 (0)