| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
- *
- * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
- */
- namespace Lcobucci\JWT;
- use InvalidArgumentException;
- use Lcobucci\JWT\Claim\Factory as ClaimFactory;
- use Lcobucci\JWT\Parsing\Decoder;
- /**
- * This class parses the JWT strings and convert them into tokens
- *
- * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
- * @since 0.1.0
- */
- class Parser
- {
- /**
- * The data decoder
- *
- * @var Decoder
- */
- private $decoder;
- /**
- * The claims factory
- *
- * @var ClaimFactory
- */
- private $claimFactory;
- /**
- * Initializes the object
- *
- * @param Decoder $decoder
- * @param ClaimFactory $claimFactory
- */
- public function __construct(
- Decoder $decoder = null,
- ClaimFactory $claimFactory = null
- ) {
- $this->decoder = $decoder ?: new Decoder();
- $this->claimFactory = $claimFactory ?: new ClaimFactory();
- }
- /**
- * Parses the JWT and returns a token
- *
- * @param string $jwt
- *
- * @return Token
- */
- public function parse($jwt)
- {
- $data = $this->splitJwt($jwt);
- $header = $this->parseHeader($data[0]);
- $claims = $this->parseClaims($data[1]);
- $signature = $this->parseSignature($header, $data[2]);
- foreach ($claims as $name => $value) {
- if (isset($header[$name])) {
- $header[$name] = $value;
- }
- }
- if ($signature === null) {
- unset($data[2]);
- }
- return new Token($header, $claims, $signature, $data);
- }
- /**
- * Splits the JWT string into an array
- *
- * @param string $jwt
- *
- * @return array
- *
- * @throws InvalidArgumentException When JWT is not a string or is invalid
- */
- protected function splitJwt($jwt)
- {
- if (!is_string($jwt)) {
- throw new InvalidArgumentException('The JWT string must have two dots');
- }
- $data = explode('.', $jwt);
- if (count($data) != 3) {
- throw new InvalidArgumentException('The JWT string must have two dots');
- }
- return $data;
- }
- /**
- * Parses the header from a string
- *
- * @param string $data
- *
- * @return array
- *
- * @throws InvalidArgumentException When an invalid header is informed
- */
- protected function parseHeader($data)
- {
- $header = (array) $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data));
- if (isset($header['enc'])) {
- throw new InvalidArgumentException('Encryption is not supported yet');
- }
- return $header;
- }
- /**
- * Parses the claim set from a string
- *
- * @param string $data
- *
- * @return array
- */
- protected function parseClaims($data)
- {
- $claims = (array) $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data));
- foreach ($claims as $name => &$value) {
- $value = $this->claimFactory->create($name, $value);
- }
- return $claims;
- }
- /**
- * Returns the signature from given data
- *
- * @param array $header
- * @param string $data
- *
- * @return Signature
- */
- protected function parseSignature(array $header, $data)
- {
- if ($data == '' || !isset($header['alg']) || $header['alg'] == 'none') {
- return null;
- }
- $hash = $this->decoder->base64UrlDecode($data);
- return new Signature($hash);
- }
- }
|