/** * @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; }
/** * @param \SpomkyLabs\Jose\KeyConverter\RSAKey $private * * @return \SpomkyLabs\Jose\KeyConverter\RSAKey */ public static function toPublic(RSAKey $private) { $data = $private->toArray(); $keys = ['p', 'd', 'q', 'dp', 'dq', 'qi']; foreach ($keys as $key) { if (array_key_exists($key, $data)) { unset($data[$key]); } } return new self($data); }