/** * Constructor * * @param JWKParameter ...$params * @throws \UnexpectedValueException If missing required parameter */ public function __construct(JWKParameter ...$params) { parent::__construct(...$params); foreach (self::MANAGED_PARAMS as $name) { if (!$this->has($name)) { throw new \UnexpectedValueException("Missing '{$name}' parameter."); } } if ($this->keyTypeParameter()->value() != KeyTypeParameter::TYPE_OCT) { throw new \UnexpectedValueException("Invalid key type."); } }
/** * Initialize from an array representing a JSON object. * * @param array $members * @throws \UnexpectedValueException * @return self */ public static function fromArray(array $members) { if (!isset($members["keys"]) || !is_array($members["keys"])) { throw new \UnexpectedValueException("JWK Set must have a 'keys' member."); } $jwks = array_map(function ($jwkdata) { return JWK::fromArray($jwkdata); }, $members["keys"]); unset($members["keys"]); $obj = new self(...$jwks); $obj->_additional = $members; return $obj; }
/** * Derive algorithm name from the header and optionally from the given JWK. * * @param Header $header Header * @param JWK $jwk Optional JWK * @throws \UnexpectedValueException If algorithm parameter is not present * or header and JWK algorithms differ. * @return string Algorithm name */ public static function deriveAlgorithmName(Header $header, JWK $jwk = null) { if ($header->hasAlgorithm()) { $alg = $header->algorithm()->value(); } // if JWK is set, and has an algorithm parameter if (isset($jwk) && $jwk->hasAlgorithmParameter()) { $jwk_alg = $jwk->algorithmParameter()->value(); // check that algorithms match if (isset($alg) && $alg != $jwk_alg) { throw new \UnexpectedValueException("JWK algorithm '{$jwk_alg}' doesn't match" . " the header's algorithm '{$alg}'."); } $alg = $jwk_alg; } if (!isset($alg)) { throw new \UnexpectedValueException("No algorithm parameter."); } return $alg; }
/** * Get value as a JWK. * * @return JWK */ public function jwk() { return JWK::fromArray($this->_value); }