/** * @param $json * @return IJWKSet * @throws InvalidJWKAlgorithm * @throws JWKInvalidIdentifierException */ public static function fromJson($json) { $json = str_replace(array("\n", "\r", "\t"), '', trim($json)); $res = json_decode($json, true); if (!isset($res[JWKSetParameters::Keys])) { throw new JWKInvalidIdentifierException(); } $keys = $res[JWKSetParameters::Keys]; $jwk_set = new JWKSet(); foreach ($keys as $key) { $kty = @$key[JSONWebKeyParameters::KeyType]; $kid = @$key[JSONWebKeyParameters::KeyId]; $use = @$key[JSONWebKeyParameters::PublicKeyUse]; $alg = @$key[JSONWebKeyParameters::Algorithm]; if (empty($alg)) { $alg = JSONWebSignatureAndEncryptionAlgorithms::RS256; } if (empty($kty) || empty($kid) || empty($use)) { continue; } if (!in_array($kty, JSONWebKeyTypes::$supported_keys)) { continue; } $n = @$key[RSAKeysParameters::Modulus]; $e = @$key[RSAKeysParameters::Exponent]; $x5c = @$key[PublicJSONWebKeyParameters::X_509CertificateChain]; if (is_null($x5c)) { $x5c = array(); } $x5u = @$key[PublicJSONWebKeyParameters::X_509Url]; $x5t = @$key[PublicJSONWebKeyParameters::X_509CertificateSHA_1_Thumbprint]; $x5t_S256 = @$key[PublicJSONWebKeyParameters::X_509CertificateSHA_256_Thumbprint]; $jwk = RSAJWKFactory::build(new RSAJWKParamsPublicKeySpecification($n, $e, $alg, $use, $x5c, $x5u, $x5t, $x5t_S256, $kid)); $jwk_set->addKey($jwk); } return $jwk_set; }
public function testLoadFromJsonJWKSet() { $json_jwk_set = <<<JWK_SET { "keys":[ { "kty":"RSA", "n":"w9x1sXTkzuxJRHfLYdCv1DN2SsD90ufkSt_HOSjM7PSFsh-yGrqP85Hia2y_2bogz03L4GUrrGBXk8OlKxEK_U1QxhhRYyFKuyo2Y6jx2t8RXCE1duskyRikcEFMQtfacZiNeLlr_0SqlxQJBNgBi_e3g3UIFzyEXpRQS7X0AJ6xuRLT7-Nl1BT3QSB-cBsENgHb10zQNaOG3VnyNehrtofHzPyF4PO4q1dVK7qaqyjp50sX7ya7TXqG3e0dNV-vyIN5AVG-UKOGiON8XB9UQj0x4zWiIa7PYG298m6Jx_26ZLNU0RyF3kXbUzwDBdpOyhXjoyOwQ1V42BxDyqhaow", "e":"AQAB", "kid":"PHPOP-00S", "use":"sig" }, {"kty":"EC", "crv":"P-256", "x":"MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4", "y":"4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM", "use":"enc", "kid":"1"}, {"kty":"RSA", "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw", "e":"AQAB", "alg":"RS256", "use":"enc", "kid":"2011-04-29"}, {"kty":"RSA", "use":"sig", "kid":"1b94c", "use":"enc", "n":"vrjOfz9Ccdgx5nQudyhdoR17V-IubWMeOZCwX_jj0hgAsz2J_pqYW08PLbK_PdiVGKPrqzmDIsLI7sA25VEnHU1uCLNwBuUiCO11_-7dYbsr4iJmG0Qu2j8DsVyT1azpJC_NG84Ty5KKthuCaPod7iI7w0LK9orSMhBEwwZDCxTWq4aYWAchc8t-emd9qOvWtVMDC2BXksRngh6X5bUYLy6AyHKvj-nUy1wgzjYQDwHMTplCoLtU-o-8SNnZ1tmRoGE9uJkBLdh5gFENabWnU5m1ZqZPdwS-qo-meMvVfJb6jJVWRpl2SUtCnYG2C32qvbWbjZ_jBPD5eunqsIo1vQ", "e":"AQAB", "x5c": [ "MIIDQjCCA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UyMTY5MPvACWpkA6SdS4xSvdXK3IVfOWA==" ] } ] } JWK_SET; $jwk_set = JWKSet::fromJson($json_jwk_set); $this->assertTrue(!is_null($jwk_set)); $count = count($jwk_set->getKeys()); $this->assertTrue($count === 3); $jwk = $jwk_set->getKeyById("2011-04-29"); $this->assertTrue(!is_null($jwk)); }