/** * Decode the token * * @param string $token * @return true */ protected function _decode($token) { $segments64 = Steelcode_String_Helper::explode('.', $token); if (count($segments64) !== 3) { $this->_setMessage('Wrong number of segments. Token is not a valid JSON web token'); return false; } $segments = $segments64; foreach ($segments as $index => $value) { $segments[$index] = $this->urlSafeB64Decode($value); if ($index < 2) { $segments[$index] = Steelcode_Json_Helper::decode($segments[$index]); } } if (empty($segments[0])) { $this->_setMessage('Invalid header encoding'); return false; } if (empty($segments[1])) { $this->_setMessage('Invalid claims encoding'); return false; } if (empty($segments[0]->alg) || !isset($this->_methods[$segments[0]->alg])) { $this->_setMessage('Invalid or unsupported algorithm'); return false; } $this->_algorithm = $segments[0]->alg; if (!$this->_verify("{$segments64[0]}.{$segments64[1]}", $segments[2], $this->_key, $this->_algorithm)) { $this->_setMessage('Signature verification failed'); return false; } if (isset($segments[1]->nbf) && $segments[1]->nbf > Steelcode_Date_Helper::time() + $this->_leeway) { $this->_setMessage('Cannot handle this token before ' . Steelcode_Date_Helper::date(DateTime::ISO8601, $segments[1]->nbf)); return false; } if (isset($segments[1]->iat) && $segments[1]->iat > Steelcode_Date_Helper::time() + $this->_leeway) { $this->_setMessage('Cannot handle token prior to ' . Steelcode_Date_Helper::date(DateTime::ISO8601, $segments[1]->iat)); return false; } if (isset($segments[1]->exp) && Steelcode_Date_Helper::time() - $this->_leeway >= $segments[1]->exp) { $this->_setMessage('Token expired'); return false; } $this->_header = (array) $segments[0]; $this->_payload = (array) $segments[1]; $this->_signature = $segments[2]; return true; }
/** * Flush the output */ public function flushOutput() { foreach ($this->_levelFlags as $level => $state) { if ($state === false || empty($this->_topLevels[$level])) { unset($this->_topLevels[$level]); } } echo Steelcode_Json_Helper::encode($this->_topLevels); }