public addClaim ( Emarref\Jwt\Claim\ClaimInterface $claim ) | ||
$claim | Emarref\Jwt\Claim\ClaimInterface |
/** * @param array $spec * @return Token */ private function getTokenWithout(array $spec) { $mappings = [self::VALID_USER_ID => new PublicClaim('userId', self::USER_ID), self::VALID_GROUP_ID => new PublicClaim('groupId', self::GROUP_ID), self::VALID_APP_ID => new PublicClaim('appId', self::APP_ID), self::VALID_EXPIRY_DATE => new PublicClaim('exp', $this->getNonExpiredDate()), self::VALID_IS_ADMIN => new PublicClaim('isAdmin', self::IS_ADMIN), self::VALID_SEGMENTS => new PublicClaim('segments', $this->testSegments)]; $spec = array_diff(array_keys($mappings), $spec); $token = new Token(); foreach ($spec as $desiredClaim) { $token->addClaim($mappings[$desiredClaim]); } return $token; }
protected static function authorization() { $token = new Emarref\Jwt\Token(); $parameter = new Emarref\Jwt\HeaderParameter\Custom('typ', 'JWT'); $token->addHeader($parameter, true); $token->addClaim(new Emarref\Jwt\Claim\Expiration(new \DateTime(self::$duration))); $jwt = new Emarref\Jwt\Jwt(); $algorithm = new Emarref\Jwt\Algorithm\Hs256(self::$appSecret); $encryption = Emarref\Jwt\Encryption\Factory::create($algorithm); $serializedToken = $jwt->serialize($token, $encryption); return $serializedToken; }
/** * @param string $jwt * @return Token */ public function deserialize($jwt) { $token = new Token(); list($encodedHeader, $encodedPayload, $encodedSignature) = explode('.', $jwt); $decodedHeader = $this->encoding->decode($encodedHeader); $decodedPayload = $this->encoding->decode($encodedPayload); $decodedSignature = $this->encoding->decode($encodedSignature); foreach ($this->parseHeaders($decodedHeader) as $header) { $token->addHeader($header); } foreach ($this->parsePayload($decodedPayload) as $claim) { $token->addClaim($claim); } $token->setSignature($decodedSignature); return $token; }
/** * @param string $jwt * * @return Token * @throws \InvalidArgumentException */ public function deserialize($jwt) { $token = new Token(); if (empty($jwt)) { throw new \InvalidArgumentException('Not a valid JWT string passed for deserialization'); } list($encodedHeader, $encodedPayload, $encodedSignature) = array_pad(explode('.', $jwt, 3), 3, null); $decodedHeader = $this->encoding->decode($encodedHeader); $decodedPayload = $this->encoding->decode($encodedPayload); $decodedSignature = $this->encoding->decode($encodedSignature); foreach ($this->parseHeaders($decodedHeader) as $header) { $token->addHeader($header); } foreach ($this->parsePayload($decodedPayload) as $claim) { $token->addClaim($claim); } $token->setSignature($decodedSignature); return $token; }