Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 86 additions & 12 deletions src/Utils/Bearer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
*/
class Bearer {

private JtiValidator $jtiValidator;
private JtiValidator $jtiValidator;
private $leeway;
private $allowedAlgs;

public function __construct(JtiValidator $jtiValidator)
{
$this->jtiValidator = $jtiValidator;
$this->leeway = 'PT60S';
$this->allowedAlgs = ['RS256', 'ES256'];
}

/**
Expand Down Expand Up @@ -62,6 +66,7 @@ public function getWebId($request) {

try {
$this->validateIdToken($idToken, $request);
// $this->validateJwtIdToken($jwt, $idToken, $request); FIXME: removed this check - uncertain how other implementations work
} catch (RequiredConstraintsViolated $e) {
throw new InvalidTokenException($e->getMessage(), 0, $e);
}
Expand All @@ -72,33 +77,102 @@ public function getWebId($request) {
}

/**
* @param string $jwt JWT access token, raw
* @param string $jwt JWT access token, raw
* @param ServerRequestInterface $request Server Request
* @return bool
*
* FIXME: Add more validations to the token;
*/
public function validateJwt($jwt, $request) {
$jwtConfig = Configuration::forUnsecuredSigner();
$jwtConfig->parser()->parse($jwt);
$accessToken = $jwtConfig->parser()->parse($jwt);

$exp = $accessToken->claims()->get("exp");
if ($exp === null) {
throw new InvalidTokenException('Access token has no expiry (exp)');
}
$validationConstraints = [];
$leeway = new DateInterval($this->leeway);
$clock = SystemClock::fromUTC();
$validationConstraints[] = new LooseValidAt($clock, $leeway); // It will use the current time to validate (iat, nbf and exp)
if (!$jwtConfig->validator()->validate($accessToken, ...$validationConstraints)) {
$jwtConfig->validator()->assert($accessToken, ...$validationConstraints); // throws an explanatory exception
}

$alg = $accessToken->headers()->get("alg");
if ($alg === "none") {
throw new InvalidTokenException('Access token without signature is not allowed');
}

if (!in_array($alg, $this->allowedAlgs)) {
throw new InvalidTokenException('Access token algorithm is not allowed');
}

return true;
}

/**
* validates that the provided OIDC ID Token
* @param string $token The OIDS ID Token (raw)
* validates the provided OIDC ID Token
* @param string $token The OIDC ID Token (raw)
* @param ServerRequestInterface $request Server Request
* @return bool True if the id token is valid
* @return bool True if the id token is valid
* @throws InvalidTokenException when the tokens is not valid
*
* FIXME: Add more validations to the token;
*/
public function validateIdToken($token, $request) {
public function validateIdToken($idTokenJwt, $request) {
$jwtConfig = Configuration::forUnsecuredSigner();
$jwtConfig->parser()->parse($token);
$idToken = $jwtConfig->parser()->parse($idTokenJwt);

$exp = $idToken->claims()->get("exp");
if ($exp === null) {
throw new InvalidTokenException('ID token has no expiry (exp)');
}
$validationConstraints = [];
$leeway = new DateInterval($this->leeway);
$clock = SystemClock::fromUTC();
$validationConstraints[] = new LooseValidAt($clock, $leeway); // It will use the current time to validate (iat, nbf and exp)
if (!$jwtConfig->validator()->validate($idToken, ...$validationConstraints)) {
$jwtConfig->validator()->assert($idToken, ...$validationConstraints); // throws an explanatory exception
}

$alg = $idToken->headers()->get("alg");
if ($alg === "none") {
throw new InvalidTokenException('ID token without signature is not allowed');
}

if (!in_array($alg, $this->allowedAlgs)) {
throw new InvalidTokenException('ID token algorithm is not allowed');
}

return true;
}

/**
* validates that the provided OIDC ID Token at_hash matches the access token
* @param string $token The OIDC ID Token (raw)
* @param ServerRequestInterface $request Server Request
* @return bool True if the id token is valid
* @throws InvalidTokenException when the tokens is not valid
*/
public function validateJwtIdToken($jwt, $idTokenJwt, $request) {
$jwtConfig = Configuration::forUnsecuredSigner();
$idToken = $jwtConfig->parser()->parse($idTokenJwt);
$accessToken = $jwtConfig->parser()->parse($jwt);

$idTokenAccessTokenHash = $idToken->claims()->get("at_hash");
$accessTokenHash = $this->generateTokenHash($jwt);

if ($idTokenAccessTokenHash !== $accessTokenHash) {
throw new InvalidTokenException('ID token access token hash does not match');
}
return true;
}

private function generateTokenHash($accessToken) {
$atHash = hash('sha256', $accessToken);
$atHash = substr($atHash, 0, 32);
$atHash = hex2bin($atHash);
$atHash = Base64Url::encode($atHash);
return $atHash;
}

private function getIdTokenFromJwt($jwt) {
$jwtConfig = Configuration::forUnsecuredSigner();
try {
Expand Down
141 changes: 120 additions & 21 deletions src/Utils/DPop.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@
*/
class DPop {

private JtiValidator $jtiValidator;

private JtiValidator $jtiValidator;
private $leeway;
private $allowedAlgs;

public function __construct(JtiValidator $jtiValidator)
{
$this->jtiValidator = $jtiValidator;
$this->leeway = 'PT60S'; // allow 60 seconds clock skew
$this->allowedAlgs = ["RS256", "ES256"];
}

/**
Expand Down Expand Up @@ -150,12 +154,12 @@ public function makeJwkThumbprint($jwk) {
* When the DPoP proof is used in conjunction with the presentation of
* an access token in protected resource access, see Section 7, the DPoP
* proof MUST also contain the following claim:
* ath: hash of the access token. The value MUST be the result of a
* base64url encoding (as defined in Section 2 of [RFC7515]) the
* SHA-256 [SHS] hash of the ASCII encoding of the associated access
* token's value.
* See also: https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop#section-7
*
* ath: hash of the access token. The value MUST be the result of a
* base64url encoding (as defined in Section 2 of [RFC7515]) the
* SHA-256 [SHS] hash of the ASCII encoding of the associated access
* token's value.
* See also: https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop#section-7
*
* Validates the above part of the oauth dpop specification
*
* @param string $jwt JWT access token, raw
Expand All @@ -165,17 +169,48 @@ public function makeJwkThumbprint($jwk) {
*/
public function validateJwtDpop($jwt, $dpop, $request) {
$this->validateDpop($dpop, $request);
$this->validateJwt($jwt, $request);

$jwtConfig = Configuration::forUnsecuredSigner();
$jwtConfig->parser()->parse($dpop);

/**
* @FIXME: ATH claim is not yet supported/required by the Solid OIDC specification.
* Once the Solid spec catches up to the DPOP spec, not having an ATH is incorrect.
* At that point, instead of returning "true", throw an exception:
*
* @see https://github.com/pdsinterop/php-solid-auth/issues/34
*/
// throw new InvalidTokenException('DPoP "ath" claim is missing');
$accessToken = $jwtConfig->parser()->parse($jwt);

$cnf = $accessToken->claims()->get("cnf");
if ($cnf === null) {
throw new InvalidTokenException('Access token Confirmation claim (cnf) is missing');
}

if (!isset($cnf['jkt'])) {
throw new InvalidTokenException('Access token Confirmation claim (cnf) is missing Thumbprint (jkt)');
}

$jkt = $cnf['jkt'];

$dpopJwt = $jwtConfig->parser()->parse($dpop);
$jwk = $dpopJwt->headers()->get('jwk');

$jwkThumbprint = $this->makeJwkThumbprint($jwk);
if ($jwkThumbprint !== $jkt) {
throw new InvalidTokenException('Access token JWK thumbprint (jkt) does not match JWK from DPoP header');
}

$ath = $dpopJwt->claims()->get('ath');

if ($ath !== null) {
$expectedAth = Base64Url::encode(hash('sha256', (string) $jwt, true));
if (!is_string($ath) || !hash_equals($expectedAth, $ath)) {
throw new InvalidTokenException('DPoP "ath" claim does not match the access token');
}
} else {
/**
* @FIXME: ATH claim is not yet supported/required by the Solid OIDC specification.
* Once the Solid spec catches up to the DPOP spec, not having an ATH is incorrect.
* At that point throw new InvalidTokenException('DPoP "ath" claim is missing')
* when it is absent. When it is present, it must match.
*
* @see https://github.com/pdsinterop/php-solid-auth/issues/34
*/
// throw new InvalidTokenException('DPoP "ath" claim is missing');
}
return true;
}

Expand All @@ -190,16 +225,17 @@ public function validateJwtDpop($jwt, $dpop, $request) {
*/
public function validateIdTokenDpop($token, $dpop, $request) {
$this->validateDpop($dpop, $request);
$this->validateIdToken($token, $request);
$jwtConfig = Configuration::forUnsecuredSigner();
$jwt = $jwtConfig->parser()->parse($token);
$cnf = $jwt->claims()->get("cnf");

if ($cnf === null) {
throw new InvalidTokenException('JWT Confirmation claim (cnf) is missing');
throw new InvalidTokenException('ID token Confirmation claim (cnf) is missing');
}

if (!isset($cnf['jkt'])) {
throw new InvalidTokenException('JWT Confirmation claim (cnf) is missing Thumbprint (jkt)');
throw new InvalidTokenException('ID token Confirmation claim (cnf) is missing Thumbprint (jkt)');
}

$jkt = $cnf['jkt'];
Expand All @@ -215,6 +251,69 @@ public function validateIdTokenDpop($token, $dpop, $request) {
return true;
}

public function validateJwt($jwt, $request) {
$jwtConfig = Configuration::forUnsecuredSigner();
$accessToken = $jwtConfig->parser()->parse($jwt);
/*
$cnf = $accessToken->claims()->get("cnf");
if ($cnf === null) {
throw new InvalidTokenException('Access token Confirmation claim (cnf) is missing');
}

if (!isset($cnf['jkt'])) {
throw new InvalidTokenException('Access token Confirmation claim (cnf) is missing Thumbprint (jkt)');
}
*/
$exp = $accessToken->claims()->get("exp");
if ($exp === null) {
throw new InvalidTokenException('Access token has no expiry (exp)');
}
$validationConstraints = [];
$leeway = new DateInterval($this->leeway);
$clock = SystemClock::fromUTC();
$validationConstraints[] = new LooseValidAt($clock, $leeway); // It will use the current time to validate (iat, nbf and exp)
if (!$jwtConfig->validator()->validate($accessToken, ...$validationConstraints)) {
$jwtConfig->validator()->assert($accessToken, ...$validationConstraints); // throws an explanatory exception
}

$alg = $accessToken->headers()->get("alg");
if ($alg === "none") {
throw new InvalidTokenException('Access token without signature is not allowed');
}
if (!in_array($alg, $this->allowedAlgs)) {
throw new InvalidTokenException('Access token algorithm is not allowed');
}

return true;
}

public function validateIdToken($idTokenJwt, $request) {
$jwtConfig = Configuration::forUnsecuredSigner();
$idToken = $jwtConfig->parser()->parse($idTokenJwt);

$exp = $idToken->claims()->get("exp");
if ($exp === null) {
throw new InvalidTokenException('ID token has no expiry (exp)');
}
$validationConstraints = [];
$leeway = new DateInterval($this->leeway);
$clock = SystemClock::fromUTC();
$validationConstraints[] = new LooseValidAt($clock, $leeway); // It will use the current time to validate (iat, nbf and exp)
if (!$jwtConfig->validator()->validate($idToken, ...$validationConstraints)) {
$jwtConfig->validator()->assert($idToken, ...$validationConstraints); // throws an explanatory exception
}

$alg = $idToken->headers()->get("alg");
if ($alg === "none") {
throw new InvalidTokenException('ID token without signature is not allowed');
}
if (!in_array($alg, $this->allowedAlgs)) {
throw new InvalidTokenException('ID token algorithm is not allowed');
}

return true;
}

/**
* Validates that the DPOP token matches all requirements from
* https://datatracker.ietf.org/doc/html/draft-ietf-oauth-dpop#section-4.2
Expand Down Expand Up @@ -334,7 +433,7 @@ public function validateDpop($dpop, $request) {

// 8. the token was issued within an acceptable timeframe (see Section 9.1), and

$leeway = new DateInterval("PT60S"); // allow 60 seconds clock skew
$leeway = new DateInterval($this->leeway);
$clock = SystemClock::fromUTC();
$validationConstraints[] = new LooseValidAt($clock, $leeway); // It will use the current time to validate (iat, nbf and exp)
if (!$jwtConfig->validator()->validate($dpop, ...$validationConstraints)) {
Expand Down
Loading
Loading