Skip to content

Commit

Permalink
Update Validator.php
Browse files Browse the repository at this point in the history
  • Loading branch information
evansims committed Jul 18, 2023
1 parent a2258a1 commit 9d9c74b
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/Token/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Auth0\SDK\Token;

use Auth0\SDK\Contract\Token\ValidatorInterface;
use Auth0\SDK\Exception\InvalidTokenException;

use function in_array;
use function is_array;
Expand All @@ -27,15 +28,15 @@ public function __construct(
*
* @param array<string> $expects An array of allowed values for the 'aud' claim. Successful if ANY match.
*
* @throws \Auth0\SDK\Exception\InvalidTokenException when claim validation fails
* @throws InvalidTokenException when claim validation fails
*/
public function audience(
array $expects,
): self {
$audience = $this->getClaim('aud');

if (null === $audience) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingAudienceClaim();
throw InvalidTokenException::missingAudienceClaim();
}

if (! is_array($audience)) {
Expand All @@ -46,34 +47,34 @@ public function audience(
return $this;
}

throw \Auth0\SDK\Exception\InvalidTokenException::mismatchedAudClaim(implode(', ', $expects), implode(', ', $audience));
throw InvalidTokenException::mismatchedAudClaim(implode(', ', $expects), implode(', ', $audience));
}

/**
* Validate the 'azp' claim.
*
* @param array<string> $expects An array of allowed values for the 'azp' claim. Successful if ANY match.
*
* @throws \Auth0\SDK\Exception\InvalidTokenException when claim validation fails
* @throws InvalidTokenException when claim validation fails
*/
public function authorizedParty(
array $expects,
): self {
$audience = $this->getClaim('aud');

if (null === $audience) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingAudienceClaim();
throw InvalidTokenException::missingAudienceClaim();
}

if (is_array($audience)) {
$azp = $this->getClaim('azp');

if (null === $azp || ! is_string($azp)) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingAzpClaim();
throw InvalidTokenException::missingAzpClaim();
}

if (! in_array($azp, $expects, true)) {
throw \Auth0\SDK\Exception\InvalidTokenException::mismatchedAzpClaim(implode(', ', $expects), $azp);
throw InvalidTokenException::mismatchedAzpClaim(implode(', ', $expects), $azp);
}
}

Expand All @@ -87,7 +88,7 @@ public function authorizedParty(
* @param int $leeway leeway in seconds to allow during time calculations
* @param null|int $now Optional. Unix timestamp representing the current point in time to use for time calculations.
*
* @throws \Auth0\SDK\Exception\InvalidTokenException when claim validation fails
* @throws InvalidTokenException when claim validation fails
*/
public function authTime(
int $maxAge,
Expand All @@ -98,13 +99,13 @@ public function authTime(
$now ??= time();

if (null === $authTime || ! is_numeric($authTime)) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingAuthTimeClaim();
throw InvalidTokenException::missingAuthTimeClaim();
}

$validUntil = (int) $authTime + $maxAge + $leeway;

if ($now > $validUntil) {
throw \Auth0\SDK\Exception\InvalidTokenException::mismatchedAuthTimeClaim($now, $validUntil);
throw InvalidTokenException::mismatchedAuthTimeClaim($now, $validUntil);
}

return $this;
Expand All @@ -116,7 +117,7 @@ public function authTime(
* @param int $leeway leeway in seconds to allow during time calculations
* @param null|int $now Optional. Unix timestamp representing the current point in time to use for time calculations.
*
* @throws \Auth0\SDK\Exception\InvalidTokenException when claim validation fails
* @throws InvalidTokenException when claim validation fails
*/
public function expiration(
int $leeway = 60,
Expand All @@ -126,13 +127,13 @@ public function expiration(
$now ??= time();

if (null === $expires || ! is_numeric($expires)) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingExpClaim();
throw InvalidTokenException::missingExpClaim();
}

$expires = (int) $expires + $leeway;

if ($now > $expires) {
throw \Auth0\SDK\Exception\InvalidTokenException::mismatchedExpClaim($now, $expires);
throw InvalidTokenException::mismatchedExpClaim($now, $expires);
}

return $this;
Expand All @@ -141,14 +142,14 @@ public function expiration(
/**
* Validate the 'iat' claim is present.
*
* @throws \Auth0\SDK\Exception\InvalidTokenException when claim validation fails
* @throws InvalidTokenException when claim validation fails
*/
public function issued(): self
{
$issued = $this->getClaim('iat');

if (null === $issued) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingIatClaim();
throw InvalidTokenException::missingIatClaim();
}

return $this;
Expand All @@ -159,19 +160,19 @@ public function issued(): self
*
* @param string $expects the value to compare with the claim
*
* @throws \Auth0\SDK\Exception\InvalidTokenException when claim validation fails
* @throws InvalidTokenException when claim validation fails
*/
public function issuer(
string $expects,
): self {
$claim = $this->getClaim('iss');

if (null === $claim || ! is_string($claim)) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingIssClaim();
throw InvalidTokenException::missingIssClaim();
}

if ($claim !== $expects) {
throw \Auth0\SDK\Exception\InvalidTokenException::mismatchedIssClaim($expects, $claim);
throw InvalidTokenException::mismatchedIssClaim($expects, $claim);
}

return $this;
Expand All @@ -182,19 +183,19 @@ public function issuer(
*
* @param string $expects the value to compare with the claim
*
* @throws \Auth0\SDK\Exception\InvalidTokenException when claim validation fails
* @throws InvalidTokenException when claim validation fails
*/
public function nonce(
string $expects,
): self {
$claim = $this->getClaim('nonce');

if (null === $claim || ! is_string($claim)) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingNonceClaim();
throw InvalidTokenException::missingNonceClaim();
}

if ($claim !== $expects) {
throw \Auth0\SDK\Exception\InvalidTokenException::mismatchedNonceClaim($expects, $claim);
throw InvalidTokenException::mismatchedNonceClaim($expects, $claim);
}

return $this;
Expand All @@ -205,19 +206,19 @@ public function nonce(
*
* @param array<string> $expects An array of allowed values for the 'org_id' claim. Successful if ANY match.
*
* @throws \Auth0\SDK\Exception\InvalidTokenException when claim validation fails
* @throws InvalidTokenException when claim validation fails
*/
public function organization(
array $expects,
): self {
$claim = $this->getClaim('org_id');

if (null === $claim || ! is_string($claim)) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingOrgIdClaim();
throw InvalidTokenException::missingOrgIdClaim();
}

if (! in_array($claim, $expects, true)) {
throw \Auth0\SDK\Exception\InvalidTokenException::mismatchedOrgIdClaim(implode(', ', $expects), $claim);
throw InvalidTokenException::mismatchedOrgIdClaim(implode(', ', $expects), $claim);
}

return $this;
Expand All @@ -226,14 +227,14 @@ public function organization(
/**
* Validate the 'sub' claim is present.
*
* @throws \Auth0\SDK\Exception\InvalidTokenException when claim validation fails
* @throws InvalidTokenException when claim validation fails
*/
public function subject(): self
{
$claim = $this->getClaim('sub');

if (null === $claim) {
throw \Auth0\SDK\Exception\InvalidTokenException::missingSubClaim();
throw InvalidTokenException::missingSubClaim();
}

return $this;
Expand Down

0 comments on commit 9d9c74b

Please sign in to comment.