Ejemplo n.º 1
0
 /**
  * @param string $kid               The key ID
  * @param array  $values            Array of values that represent a key
  * @param array  $additional_values Add additional parameters to your key ('alg'=>'RS256'...)
  *
  * @return $this
  */
 public function loadKeyFromValues($kid, array $values, array $additional_values = [])
 {
     if (!array_key_exists('kty', $values)) {
         throw new \InvalidArgumentException('Unable to determine the key type');
     }
     /*
      * @var \Jose\JWKInterface[]
      */
     $keys = [];
     switch ($values['kty']) {
         case 'RSA':
             $rsa = new RSAKey($values);
             if ($rsa->isPrivate()) {
                 $keys['private'] = $this->getJWKManager()->createJWK(array_merge($rsa->toArray(), $additional_values));
             }
             $keys['public'] = $this->getJWKManager()->createJWK(array_merge(RSAKey::toPublic($rsa)->toArray(), $additional_values));
             break;
         case 'EC':
             $rsa = new ECKey($values);
             if ($rsa->isPrivate()) {
                 $keys['private'] = $this->getJWKManager()->createJWK(array_merge($rsa->toArray(), $additional_values));
             }
             $keys['public'] = $this->getJWKManager()->createJWK(array_merge(ECKey::toPublic($rsa)->toArray(), $additional_values));
             break;
         case 'oct':
             $keys['asymmetric'] = $this->getJWKManager()->createJWK(array_merge($values, $additional_values));
             break;
         case 'dir':
             $keys['direct'] = $this->getJWKManager()->createJWK(array_merge($values, $additional_values));
             break;
         case 'none':
             break;
         default:
             throw new \InvalidArgumentException('Unsupported key type');
     }
     foreach ($keys as $name => $jwk) {
         if (is_null($jwk->getKeyID())) {
             $jwk->setValue('kid', $kid);
         }
         $this->addKeyInKeySet($name, $jwk);
     }
     return $this;
 }