Example #1
0
 public function testUrlDecode()
 {
     $dec_str = "1";
     $str = "MQ";
     $dec = Akita_JOSE_Base64::urlDecode($str);
     $this->assertEquals($dec_str, $dec);
     $dec_str = "1234";
     $str = "MTIzNA";
     $dec = Akita_JOSE_base64::urlDecode($str);
     $this->assertequals($dec_str, $dec);
     $dec_str = "ABCDEFG";
     $str = "QUJDREVGRw";
     $dec = Akita_JOSE_base64::urlDecode($str);
     $this->assertequals($dec_str, $dec);
 }
 /**
  * return JWT payload
  *
  * @param string $jwt JWT string
  * @param bool $return_is_array return format is array or not
  * @return array JWT Payload
  */
 public static function getPayload($jwt, $return_is_array = false)
 {
     // split 3 parts
     $part = explode('.', $jwt);
     if (!is_array($part) || empty($part) || count($part) !== 3) {
         return false;
     }
     if ($return_is_array) {
         $payload = json_decode(Akita_JOSE_Base64::urlDecode($part[1]), true);
     } else {
         $payload = Akita_JOSE_Base64::urlDecode($part[1]);
     }
     return $payload;
 }
 /**
  * verify signature
  *
  * @param string $signatureBaseString Hash algorithm
  * @param mixed $key private key or shared key
  */
 public function verify($key)
 {
     // split 3 parts
     $part = explode('.', $this->_tokenstring);
     if (!is_array($part) || empty($part) || count($part) !== 3) {
         return false;
     }
     $decoded_signature = Akita_JOSE_Base64::urlDecode($part[2]);
     $signinginput = self::getSigningInput($this->_tokenstring);
     switch (substr($this->_header['alg'], 0, 2)) {
         case "HS":
             $hashAlg = "sha" . substr($this->_header['alg'], 2, 3);
             $generated_signature = hash_hmac($hashAlg, $signinginput, $key, true);
             return $generated_signature === $decoded_signature;
             break;
         case "RS":
             $hashAlg = "sha" . substr($this->_header['alg'], 2, 3);
             return $this->RSAVerify($hashAlg, $signinginput, $decoded_signature, $key);
             break;
         default:
             return empty($part[2]);
             break;
     }
 }