예제 #1
0
 /**
  * @param string $input
  * @return array
  * @throws InvalidJWTException
  */
 public static function deserialize($input)
 {
     $e_parts = explode(IBasicJWT::SegmentSeparator, $input);
     if (count($e_parts) < 2) {
         throw new InvalidJWTException(sprintf('%s has only 2 or less encoded parts!'));
     }
     $e_header = $e_parts[0];
     $e_payload = $e_parts[1];
     $e_signature = count($e_parts) > 2 ? $e_parts[2] : '';
     $header = JOSEHeaderSerializer::deserialize($e_header);
     $payload = $header->getType()->getString() === 'JWT' ? JWTClaimSetSerializer::deserialize($e_payload) : JWTRawSerializer::deserialize($e_payload);
     $signature = !empty($e_signature) ? JWTRawSerializer::deserialize($e_signature) : '';
     return array($header, $payload, $signature);
 }
예제 #2
0
 /**
  * https://tools.ietf.org/html/rfc7516#section-9
  * @param string $compact_serialization
  * @return IBasicJWT
  * @throws InvalidJWKType
  * @throws InvalidCompactSerializationException
  */
 public static function build($compact_serialization)
 {
     $segments = explode(IBasicJWT::SegmentSeparator, $compact_serialization);
     // JWSs have three segments separated by two period ('.') characters.
     // JWEs have five segments separated by four period ('.') characters.
     switch (count($segments)) {
         case 3:
             // JWS or unsecured one
             $header = JOSEHeaderSerializer::deserialize($segments[0]);
             if ($header->getAlgorithm()->getString() === 'none' && empty($segments[2])) {
                 return UnsecuredJWT::fromCompactSerialization($compact_serialization);
             }
             return JWSFactory::build(new JWS_CompactFormatSpecification($compact_serialization));
             break;
         case 5:
             // JWE
             return JWEFactory::build(new JWE_CompactFormatSpecification($compact_serialization));
             break;
         default:
             throw new InvalidCompactSerializationException();
             break;
     }
     return null;
 }