|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Pdsinterop\Solid\Auth\Utils; |
| 4 | + |
| 5 | +use DateInterval; |
| 6 | +use Exception; |
| 7 | +use Jose\Component\Core\JWK; |
| 8 | +use Jose\Component\Core\Util\ECKey; |
| 9 | +use Jose\Component\Core\Util\RSAKey; |
| 10 | +use Lcobucci\Clock\SystemClock; |
| 11 | +use Lcobucci\JWT\Configuration; |
| 12 | +use Lcobucci\JWT\Signer\Ecdsa\Sha256; |
| 13 | +use Lcobucci\JWT\Signer\Key\InMemory; |
| 14 | +use Lcobucci\JWT\Token\InvalidTokenStructure; |
| 15 | +use Lcobucci\JWT\Validation\Constraint\LooseValidAt; |
| 16 | +use Lcobucci\JWT\Validation\Constraint\SignedWith; |
| 17 | +use Lcobucci\JWT\Validation\RequiredConstraintsViolated; |
| 18 | +use Pdsinterop\Solid\Auth\Exception\AuthorizationHeaderException; |
| 19 | +use Pdsinterop\Solid\Auth\Exception\InvalidTokenException; |
| 20 | +use Psr\Http\Message\ServerRequestInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * This class contains code to fetch the WebId from a request |
| 24 | + * that is make in legacy mode (bearer token with pop) |
| 25 | + * |
| 26 | + * @ TODO: Make sure this code complies with the spec and validate the tokens properly; |
| 27 | + * https://datatracker.ietf.org/doc/html/rfc7800 |
| 28 | + */ |
| 29 | +class Bearer { |
| 30 | + |
| 31 | + private JtiValidator $jtiValidator; |
| 32 | + |
| 33 | + public function __construct(JtiValidator $jtiValidator) |
| 34 | + { |
| 35 | + $this->jtiValidator = $jtiValidator; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * This method fetches the WebId from a request and verifies |
| 40 | + * that the request has a valid pop token that matches |
| 41 | + * the access token. |
| 42 | + * |
| 43 | + * @param ServerRequestInterface $request Server Request |
| 44 | + * |
| 45 | + * @return string the WebId, or "public" if no WebId is found |
| 46 | + * |
| 47 | + * @throws Exception "Invalid token" when the pop token is invalid |
| 48 | + */ |
| 49 | + public function getWebId($request) { |
| 50 | + $serverParams = $request->getServerParams(); |
| 51 | + |
| 52 | + if (empty($serverParams['HTTP_AUTHORIZATION'])) { |
| 53 | + $webId = "public"; |
| 54 | + } else { |
| 55 | + $this->validateRequestHeaders($serverParams); |
| 56 | + |
| 57 | + [, $jwt] = explode(" ", $serverParams['HTTP_AUTHORIZATION'], 2); |
| 58 | + |
| 59 | + try { |
| 60 | + $this->validateJwt($jwt, $request); |
| 61 | + } catch (RequiredConstraintsViolated $e) { |
| 62 | + throw new InvalidTokenException($e->getMessage(), 0, $e); |
| 63 | + } |
| 64 | + $idToken = $this->getIdTokenFromJwt($jwt); |
| 65 | + |
| 66 | + try { |
| 67 | + $this->validateIdToken($idToken, $request); |
| 68 | + } catch (RequiredConstraintsViolated $e) { |
| 69 | + throw new InvalidTokenException($e->getMessage(), 0, $e); |
| 70 | + } |
| 71 | + $webId = $this->getSubjectFromIdToken($idToken); |
| 72 | + } |
| 73 | + |
| 74 | + return $webId; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param string $jwt JWT access token, raw |
| 79 | + * @param ServerRequestInterface $request Server Request |
| 80 | + * @return bool |
| 81 | + * |
| 82 | + * FIXME: Add more validations to the token; |
| 83 | + */ |
| 84 | + public function validateJwt($jwt, $request) { |
| 85 | + $jwtConfig = Configuration::forUnsecuredSigner(); |
| 86 | + $jwtConfig->parser()->parse($jwt); |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * validates that the provided OIDC ID Token |
| 92 | + * @param string $token The OIDS ID Token (raw) |
| 93 | + * @param ServerRequestInterface $request Server Request |
| 94 | + * @return bool True if the id token is valid |
| 95 | + * @throws InvalidTokenException when the tokens is not valid |
| 96 | + * |
| 97 | + * FIXME: Add more validations to the token; |
| 98 | + */ |
| 99 | + public function validateIdToken($token, $request) { |
| 100 | + $jwtConfig = Configuration::forUnsecuredSigner(); |
| 101 | + $jwtConfig->parser()->parse($token); |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + private function getIdTokenFromJwt($jwt) { |
| 106 | + $jwtConfig = Configuration::forUnsecuredSigner(); |
| 107 | + try { |
| 108 | + $jwt = $jwtConfig->parser()->parse($jwt); |
| 109 | + } catch(Exception $e) { |
| 110 | + throw new InvalidTokenException("Invalid JWT token", 409, $e); |
| 111 | + } |
| 112 | + |
| 113 | + $idToken = $jwt->claims()->get("id_token"); |
| 114 | + if ($idToken === null) { |
| 115 | + throw new InvalidTokenException('Missing "id_token"'); |
| 116 | + } |
| 117 | + return $idToken; |
| 118 | + } |
| 119 | + |
| 120 | + private function getSubjectFromIdToken($idToken) { |
| 121 | + $jwtConfig = Configuration::forUnsecuredSigner(); |
| 122 | + try { |
| 123 | + $jwt = $jwtConfig->parser()->parse($idToken); |
| 124 | + } catch(Exception $e) { |
| 125 | + throw new InvalidTokenException("Invalid ID token", 409, $e); |
| 126 | + } |
| 127 | + |
| 128 | + $sub = $jwt->claims()->get("sub"); |
| 129 | + if ($sub === null) { |
| 130 | + throw new InvalidTokenException('Missing "sub"'); |
| 131 | + } |
| 132 | + return $sub; |
| 133 | + } |
| 134 | + |
| 135 | + private function validateRequestHeaders($serverParams) { |
| 136 | + if (str_contains($serverParams['HTTP_AUTHORIZATION'], ' ') === false) { |
| 137 | + throw new AuthorizationHeaderException("Authorization Header does not contain parameters"); |
| 138 | + } |
| 139 | + |
| 140 | + if (str_starts_with(strtolower($serverParams['HTTP_AUTHORIZATION']), 'bearer') === false) { |
| 141 | + throw new AuthorizationHeaderException('Only "bearer" authorization scheme is supported'); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments