PHP Classes

File: src/Rules/ForAudience.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   PHP PASeTo   src/Rules/ForAudience.php   Download  
File: src/Rules/ForAudience.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP PASeTo
Encrypt and decrypt data with PaSeTO protocol
Author: By
Last change:
Date: 4 years ago
Size: 1,375 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
ParagonIE\Paseto\Rules;

use
ParagonIE\Paseto\{
   
JsonToken,
   
ValidationRuleInterface
};
use
ParagonIE\Paseto\Exception\PasetoException;

/**
 * Class ForAudience
 * @package ParagonIE\Paseto\Rules
 */
class ForAudience implements ValidationRuleInterface
{
   
/** @var string $failure */
   
protected $failure = 'OK';

   
/** @var string $issuer */
   
protected $audience;

   
/**
     * ForAudience constructor.
     * @param string $audience
     */
   
public function __construct(string $audience)
    {
       
$this->audience = $audience;
    }

   
/**
     * @return string
     */
   
public function getFailureMessage(): string
   
{
        return
$this->failure;
    }

   
/**
     * @param JsonToken $token
     * @return bool
     */
   
public function isValid(JsonToken $token): bool
   
{
        try {
           
$audience = $token->getAudience();
            if (!\
hash_equals($this->audience, $audience)) {
               
$this->failure = 'This token is not intended for ' .
                   
$this->audience . ' (expected); instead, it is intended for ' .
                   
$audience . ' instead.';
                return
false;
            }
        } catch (
PasetoException $ex) {
           
$this->failure = $ex->getMessage();
            return
false;
        }
        return
true;
    }
}