JWT::urlsafeB64Decode PHP Method

urlsafeB64Decode() public static method

Decode a string with URL-safe Base64.
public static urlsafeB64Decode ( string $input ) : string
$input string A Base64 encoded string
return string A decoded string
    public static function urlsafeB64Decode($input)
    {
        $remainder = strlen($input) % 4;
        if ($remainder) {
            $padlen = 4 - $remainder;
            $input .= str_repeat('=', $padlen);
        }
        return base64_decode(strtr($input, '-_', '+/'));
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Decodes a JWT string into a PHP object.
  *
  * @param string      $jwt       The JWT
  * @param string|Array|null $key The secret key, or map of keys
  * @param bool        $verify    Don't skip verification process
  *
  * @return object      The JWT's payload as a PHP object
  * @throws UnexpectedValueException Provided JWT was invalid
  * @throws DomainException          Algorithm was not provided
  * 
  * @uses jsonDecode
  * @uses urlsafeB64Decode
  */
 public static function decode($jwt, $key = null, $verify = true)
 {
     $tks = explode('.', $jwt);
     if (count($tks) != 3) {
         throw new UnexpectedValueException('Wrong number of segments');
     }
     list($headb64, $bodyb64, $cryptob64) = $tks;
     if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
         throw new UnexpectedValueException('Invalid segment encoding');
     }
     if (null === ($payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)))) {
         throw new UnexpectedValueException('Invalid segment encoding');
     }
     $sig = JWT::urlsafeB64Decode($cryptob64);
     if ($verify) {
         if (empty($header->alg)) {
             throw new DomainException('Empty algorithm');
         }
         if (is_array($key)) {
             if (isset($header->kid)) {
                 $key = $key[$header->kid];
             } else {
                 throw new DomainException('"kid" empty, unable to lookup correct key');
             }
         }
         if (!JWT::verify("{$headb64}.{$bodyb64}", $sig, $key, $header->alg)) {
             throw new UnexpectedValueException('Signature verification failed');
         }
         // Check token expiry time if defined.
         if (isset($payload->exp) && time() >= $payload->exp) {
             throw new UnexpectedValueException('Expired Token');
         }
     }
     return $payload;
 }
All Usage Examples Of JWT::urlsafeB64Decode