<?php

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2018 Spomky-Labs
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

use Jose\Factory\JWKFactory;
use Jose\Object\JWKInterface;
use Jose\Object\JWKSetInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

/**
 * Class JWKFactoryTest.
 *
 * @group JWKFactory
 * @group Unit
 */
class JWKFactoryTest extends \Jose\Test\BaseTestCase
{
    public function testCreateFromECCertificateFileInDERFormat()
    {
        $result = JWKFactory::createFromCertificateFile(__DIR__.'/../Certificates/EC/DER/prime256v1-cert.der');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"EC","crv":"P-256","x":"xEsr_55aqgFXdrbRNz1_WSNI8UaSUxCka2kGEN1bXsI","y":"SM45Hsr9dnUR6Ox-TpmNv2fbDX4CoVo-3patMUpXANA","x5c":["MIIB0jCCAXegAwIBAgIJAK2o1kQ5JwpUMAoGCCqGSM49BAMCMEUxCzAJBgNVBAYT\nAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRn\naXRzIFB0eSBMdGQwHhcNMTUxMTA4MTUxMTU2WhcNMTYxMTA3MTUxMTU2WjBFMQsw\nCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJu\nZXQgV2lkZ2l0cyBQdHkgTHRkMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExEsr\n\/55aqgFXdrbRNz1\/WSNI8UaSUxCka2kGEN1bXsJIzjkeyv12dRHo7H5OmY2\/Z9sN\nfgKhWj7elq0xSlcA0KNQME4wHQYDVR0OBBYEFKIGgCZoS388STT0qjoX\/swKYBXh\nMB8GA1UdIwQYMBaAFKIGgCZoS388STT0qjoX\/swKYBXhMAwGA1UdEwQFMAMBAf8w\nCgYIKoZIzj0EAwIDSQAwRgIhAK5OqQoBGR\/pj2NOb+PyRKK4k4d3Muj9z\/6LsJK+\nkkgUAiEA+FY4SWKv4mfe0gsOBId0Aah\/HtVZxDBe3bCXOQM8MMM="],"x5t":"ZnnaQDssCKJQZLp6zyHssIZOa7o","x5t#256":"v7VlokKTGL3anRk8Nl0VcqVC9u5j2Fb5tdlQntUgDT4"}', json_encode($result));
    }

    public function testCreateFromKey()
    {
        $jwk = JWKFactory::createFromKey(file_get_contents(__DIR__.'/../Keys/EC/private.es256.encrypted.key'), 'test');
        self::assertEquals('{"kty":"EC","crv":"P-256","d":"q_VkzNnxTG39jHB0qkwA_SeVXud7yCHT7kb7kZv-0xQ","x":"vuYsP-QnrqAbM7Iyhzjt08hFSuzapyojCB_gFsBt65U","y":"oq-E2K-X0kPeqGuKnhlXkxc5fnxomRSC6KLby7Ij8AE"}', json_encode($jwk));
    }

    public function testCreateFromResource()
    {
        $res = openssl_x509_read(file_get_contents(__DIR__.'/../Certificates/RSA/PEM/1024b-rsa-example-cert.pem'));

        $jwk = JWKFactory::createFromX509Resource($res);

        self::assertInstanceOf(JWKInterface::class, $jwk);
        self::assertEquals('{"kty":"RSA","n":"xgEGvHk-U_RY0j9l3MP7o-S2a6uf4XaRBhu1ztdCHz8tMG8Kj4_qJmgsSZQD17sRctHGBTUJWp4CLtBwCf0zAGVzySwUkcHSu1_2mZ_w7Nr0TQHKeWr_j8pvXH534DKEvugr21DAHbi4c654eLUL-JW_wJJYqJh7qHM3W3Fh7ys","e":"AQAB","x5c":["MIICVjCCAb8CAg37MA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG\nA1UECBMFVG9reW8xEDAOBgNVBAcTB0NodW8ta3UxETAPBgNVBAoTCEZyYW5rNERE\nMRgwFgYDVQQLEw9XZWJDZXJ0IFN1cHBvcnQxGDAWBgNVBAMTD0ZyYW5rNEREIFdl\nYiBDQTEjMCEGCSqGSIb3DQEJARYUc3VwcG9ydEBmcmFuazRkZC5jb20wHhcNMTIw\nODIyMDUyNzIzWhcNMTcwODIxMDUyNzIzWjBKMQswCQYDVQQGEwJKUDEOMAwGA1UE\nCAwFVG9reW8xETAPBgNVBAoMCEZyYW5rNEREMRgwFgYDVQQDDA93d3cuZXhhbXBs\nZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYBBrx5PlP0WNI\/ZdzD\n+6Pktmurn+F2kQYbtc7XQh8\/LTBvCo+P6iZoLEmUA9e7EXLRxgU1CVqeAi7QcAn9\nMwBlc8ksFJHB0rtf9pmf8Oza9E0Bynlq\/4\/Kb1x+d+AyhL7oK9tQwB24uHOueHi1\nC\/iVv8CSWKiYe6hzN1txYe8rAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAASPdjigJ\nkXCqKWpnZ\/Oc75EUcMi6HztaW8abUMlYXPIgkV2F7YanHOB7K4f7OOLjiz8DTPFf\njC9UeuErhaA\/zzWi8ewMTFZW\/WshOrm3fNvcMrMLKtH534JKvcdMg6qIdjTFINIr\nevnAhf0cwULaebn+lMs8Pdl7y37+sfluVok="],"x5t":"4bK45ewZ00Wk-a_shpTw2cCqJc8","x5t#256":"5F5GTPOxBGAOsVyuYzqUBjri0R2YDTiDowiQbs6oGgU"}', json_encode($jwk));
    }

    public function testCreateFromECCertificateFileInPEMFormat()
    {
        $result = JWKFactory::createFromCertificateFile(__DIR__.'/../Certificates/EC/PEM/prime256v1-cert.pem');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"EC","crv":"P-256","x":"xEsr_55aqgFXdrbRNz1_WSNI8UaSUxCka2kGEN1bXsI","y":"SM45Hsr9dnUR6Ox-TpmNv2fbDX4CoVo-3patMUpXANA","x5c":["MIIB0jCCAXegAwIBAgIJAK2o1kQ5JwpUMAoGCCqGSM49BAMCMEUxCzAJBgNVBAYT\nAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRn\naXRzIFB0eSBMdGQwHhcNMTUxMTA4MTUxMTU2WhcNMTYxMTA3MTUxMTU2WjBFMQsw\nCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJu\nZXQgV2lkZ2l0cyBQdHkgTHRkMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExEsr\n\/55aqgFXdrbRNz1\/WSNI8UaSUxCka2kGEN1bXsJIzjkeyv12dRHo7H5OmY2\/Z9sN\nfgKhWj7elq0xSlcA0KNQME4wHQYDVR0OBBYEFKIGgCZoS388STT0qjoX\/swKYBXh\nMB8GA1UdIwQYMBaAFKIGgCZoS388STT0qjoX\/swKYBXhMAwGA1UdEwQFMAMBAf8w\nCgYIKoZIzj0EAwIDSQAwRgIhAK5OqQoBGR\/pj2NOb+PyRKK4k4d3Muj9z\/6LsJK+\nkkgUAiEA+FY4SWKv4mfe0gsOBId0Aah\/HtVZxDBe3bCXOQM8MMM="],"x5t":"ZnnaQDssCKJQZLp6zyHssIZOa7o","x5t#256":"v7VlokKTGL3anRk8Nl0VcqVC9u5j2Fb5tdlQntUgDT4"}', json_encode($result));
    }

    public function testCreateFrom32kRSACertificateFileInDERFormat()
    {
        $result = JWKFactory::createFromCertificateFile(__DIR__.'/../Certificates/RSA/DER/32k-rsa-example-cert.der');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"RSA","n":" ","e":"AQAB","x5c":["MIIR2jCCEUMCAg4EMA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG\nA1UECBMFVG9reW8xEDAOBgNVBAcTB0NodW8ta3UxETAPBgNVBAoTCEZyYW5rNERE\nMRgwFgYDVQQLEw9XZWJDZXJ0IFN1cHBvcnQxGDAWBgNVBAMTD0ZyYW5rNEREIFdl\nYiBDQTEjMCEGCSqGSIb3DQEJARYUc3VwcG9ydEBmcmFuazRkZC5jb20wHhcNMTIx\nMDEwMTIxNzQ5WhcNMTcxMDA5MTIxNzQ5WjBKMQswCQYD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0SQ3vYOFqAzVHmiofA\n\/NFyfdqF1i9TMOg\/pQcSlrvoDbeWpp9jsX3yXrqGYCddeUQrMRXC2FijFYA1i1Wz\nGz02S00l00wHc4IycKj0FaR5+aOWvMwlKQE\/\/WZ\/E3ajdbW0w1emTk5A87bT95Y8\nXeJiGhtKjx9zHNe3D1uDUFevRMAOPSGX\/PiPD3fBUYYuWwDepiy1rLQj3ImAcFma\nHFztXUnM1Z3DEgOZGADl4KbUQtUHGki6n1YZmVSzWvPCpfMj7fuLuXd3AV8J\/+La\nDyb6A1CO4zpiqRLP5cpyf6wkSDVdIyGgHXmyBgT1T7Y5778QYsrR05TbPIc7jQXy\nCG+Epam4BwhNf9CcQyt6bCyIKXQ8ScZn3wFfOVG93UwU1zRlTDM37BNXGziM2S14\n1unQyMgoDljoWIIRfqSg1Zj1wtdI2DhcbgixZFCt8ifZZYXP+eHOVpk2b81QzWSY\npFEQ+ay9AgMBAAEwDQYJKoZIhvcNAQEFBQADgYEATPINk\/17H+RLz459iCVQAGes\n8kc5sxYj3CkMlWrGMiCxvsgu2kak6dCa0f3DfiVt54Fry7s0OklHiZmipoiF4RCt\nyJwUSAzRrZFAbkpDg8oIu4Ui\/Bt13kY7xON+u4m0IgkLZSE+8BSjMrfjVvVxe+qH\n5i7X\/ibUTDjgyfdA8XI="],"x5t":"KGApLybHWJmBwZGgBk07AlRD9nU","x5t#256":"YD12k6kc4xuh_5vEHMyyOFpGs6VqTyaKMlxg0Nt2crA"}', json_encode($result));
    }

    public function testCreateFrom32kRSACertificateFileInPEMFormat()
    {
        $result = JWKFactory::createFromCertificateFile(__DIR__.'/../Certificates/RSA/PEM/32k-rsa-example-cert.pem');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"RSA","n":" ","e":"AQAB","x5c":["MIIR2jCCEUMCAg4EMA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG\nA1UECBMFVG9reW8xEDAOBgNVBAcTB0NodW8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xRXHStLoQZ\/MzxVg4bDKIFUECBUOMFeIo\nGxRVYDeWN861iEsoEMeSqG+hBpzNIjZ8pfsvtzCBd4F1ySho2GejqYKlkFqa7IFn\nUjLpOeC5CLn0wIIp2JzMil966ZtIMSi\/LYhtCY+qV38kjpY65+CV2IhO7eFdHko1\nrNqfaZzc2sq7VIRdml3NvjAllnbpT\/XETYYswmu7TnJmhU0SQ3vYOFqAzVHmiofA\n\/NFyfdqF1i9TMOg\/pQcSlrvoDbeWpp9jsX3yXrqGYCddeUQrMRXC2FijFYA1i1Wz\nGz02S00l00wHc4IycKj0FaR5+aOWvMwlKQE\/\/WZ\/E3ajdbW0w1emTk5A87bT95Y8\nXeJiGhtKjx9zHNe3D1uDUFevRMAOPSGX\/PiPD3fBUYYuWwDepiy1rLQj3ImAcFma\nHFztXUnM1Z3DEgOZGADl4KbUQtUHGki6n1YZmVSzWvPCpfMj7fuLuXd3AV8J\/+La\nDyb6A1CO4zpiqRLP5cpyf6wkSDVdIyGgHXmyBgT1T7Y5778QYsrR05TbPIc7jQXy\nCG+Epam4BwhNf9CcQyt6bCyIKXQ8ScZn3wFfOVG93UwU1zRlTDM37BNXGziM2S14\n1unQyMgoDljoWIIRfqSg1Zj1wtdI2DhcbgixZFCt8ifZZYXP+eHOVpk2b81QzWSY\npFEQ+ay9AgMBAAEwDQYJKoZIhvcNAQEFBQADgYEATPINk\/17H+RLz459iCVQAGes\n8kc5sxYj3CkMlWrGMiCxvsgu2kak6dCa0f3DfiVt54Fry7s0OklHiZmipoiF4RCt\nyJwUSAzRrZFAbkpDg8oIu4Ui\/Bt13kY7xON+u4m0IgkLZSE+8BSjMrfjVvVxe+qH\n5i7X\/ibUTDjgyfdA8XI="],"x5t":"KGApLybHWJmBwZGgBk07AlRD9nU","x5t#256":"YD12k6kc4xuh_5vEHMyyOFpGs6VqTyaKMlxg0Nt2crA"}', json_encode($result));
    }

    public function testCreateFromPrivateEC256KeyFileEncrypted()
    {
        $result = JWKFactory::createFromKeyFile(__DIR__.'/../Keys/EC/private.es256.encrypted.key', 'test');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"EC","crv":"P-256","d":"q_VkzNnxTG39jHB0qkwA_SeVXud7yCHT7kb7kZv-0xQ","x":"vuYsP-QnrqAbM7Iyhzjt08hFSuzapyojCB_gFsBt65U","y":"oq-E2K-X0kPeqGuKnhlXkxc5fnxomRSC6KLby7Ij8AE"}', json_encode($result));
    }

    public function testCreateFromPrivateEC384KeyFileEncrypted()
    {
        $result = JWKFactory::createFromKeyFile(__DIR__.'/../Keys/EC/private.es384.encrypted.key', 'test');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"EC","crv":"P-384","d":"pcSSXrbeZEOaBIs7IwqcU9M_OOM81XhZuOHoGgmS_2PdECwcdQcXzv7W8-lYL0cr","x":"6f-XZsg2Tvn0EoEapQ-ylMYNtsm8CPf0cb8HI2EkfY9Bqpt3QMzwlM7mVsFRmaMZ","y":"b8nOnRwmpmEnvA2U8ydS-dbnPv7bwYl-q1qNeh8Wpjor3VO-RTt4ce0Pn25oGGWU"}', json_encode($result));
    }

    public function testCreateFromPrivateEC512KeyFileEncrypted()
    {
        $result = JWKFactory::createFromKeyFile(__DIR__.'/../Keys/EC/private.es512.encrypted.key', 'test');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"EC","crv":"P-521","d":"Fp6KFKRiHIdR_7PP2VKxz6OkS_phyoQqwzv2I89-8zP7QScrx5r8GFLcN5mCCNJt3rN3SIgI4XoIQbNePlAj6vE","x":"AVpvo7TGpQk5P7ZLo0qkBpaT-fFDv6HQrWElBKMxcrJd_mRNapweATsVv83YON4lTIIRXzgGkmWeqbDr6RQO-1cS","y":"AIs-MoRmLaiPyG2xmPwQCHX2CGX_uCZiT3iOxTAJEZuUbeSA828K4WfAA4ODdGiB87YVShhPOkiQswV3LpbpPGhC"}', json_encode($result));
    }

    public function testCreateFromPublicEC256KeyFile()
    {
        $result = JWKFactory::createFromKeyFile(__DIR__.'/../Keys/EC/public.es256.key');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"EC","crv":"P-256","x":"vuYsP-QnrqAbM7Iyhzjt08hFSuzapyojCB_gFsBt65U","y":"oq-E2K-X0kPeqGuKnhlXkxc5fnxomRSC6KLby7Ij8AE"}', json_encode($result));
    }

    public function testCreateFromPublicEC384KeyFile()
    {
        $result = JWKFactory::createFromKeyFile(__DIR__.'/../Keys/EC/public.es384.key');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"EC","crv":"P-384","x":"6f-XZsg2Tvn0EoEapQ-ylMYNtsm8CPf0cb8HI2EkfY9Bqpt3QMzwlM7mVsFRmaMZ","y":"b8nOnRwmpmEnvA2U8ydS-dbnPv7bwYl-q1qNeh8Wpjor3VO-RTt4ce0Pn25oGGWU"}', json_encode($result));
    }

    public function testCreateFromPublicEC512KeyFile()
    {
        $result = JWKFactory::createFromKeyFile(__DIR__.'/../Keys/EC/public.es512.key');

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"EC","crv":"P-521","x":"AVpvo7TGpQk5P7ZLo0qkBpaT-fFDv6HQrWElBKMxcrJd_mRNapweATsVv83YON4lTIIRXzgGkmWeqbDr6RQO-1cS","y":"AIs-MoRmLaiPyG2xmPwQCHX2CGX_uCZiT3iOxTAJEZuUbeSA828K4WfAA4ODdGiB87YVShhPOkiQswV3LpbpPGhC"}', json_encode($result));
    }

    public function testCreateFromJKU()
    {
        $result = JWKFactory::createFromJKU('https://www.googleapis.com/oauth2/v3/certs');

        self::assertInstanceOf(JWKSetInterface::class, $result);
        self::assertTrue(0 !== $result->count());
    }

    public function testCreateFromJKUWithCache()
    {
        $url = 'https://www.googleapis.com/oauth2/v3/certs';
        $cache = new FilesystemAdapter();
        $cache_key = $this->getCacheKey($url);
        $cache->clear();

        $result = JWKFactory::createFromJKU($url, false, $cache);

        self::assertInstanceOf(JWKSetInterface::class, $result);
        self::assertTrue(0 !== $result->count());
        self::assertTrue($cache->hasItem($cache_key));
    }

    public function testCreateFromX5U()
    {
        $result = JWKFactory::createFromX5U('https://www.googleapis.com/oauth2/v1/certs');

        self::assertInstanceOf(JWKSetInterface::class, $result);
        self::assertTrue(0 !== $result->count());
    }

    public function testCreateFromX5UWithCache()
    {
        $url = 'https://www.googleapis.com/oauth2/v1/certs';
        $cache = new FilesystemAdapter();
        $cache_key = $this->getCacheKey($url);
        $cache->clear();

        $result = JWKFactory::createFromX5U($url, false, $cache);

        self::assertInstanceOf(JWKSetInterface::class, $result);
        self::assertTrue(0 !== $result->count());
        self::assertTrue($cache->hasItem($cache_key));
    }

    public function testCreateFromValues()
    {
        $result = JWKFactory::createFromValues([
            'kty' => 'EC',
            'crv' => 'P-521',
            'd' => 'Fp6KFKRiHIdR_7PP2VKxz6OkS_phyoQqwzv2I89-8zP7QScrx5r8GFLcN5mCCNJt3rN3SIgI4XoIQbNePlAj6vE',
            'x' => 'AVpvo7TGpQk5P7ZLo0qkBpaT-fFDv6HQrWElBKMxcrJd_mRNapweATsVv83YON4lTIIRXzgGkmWeqbDr6RQO-1cS',
            'y' => 'AIs-MoRmLaiPyG2xmPwQCHX2CGX_uCZiT3iOxTAJEZuUbeSA828K4WfAA4ODdGiB87YVShhPOkiQswV3LpbpPGhC',
        ]);

        self::assertInstanceOf(JWKInterface::class, $result);
        self::assertEquals('{"kty":"EC","crv":"P-521","d":"Fp6KFKRiHIdR_7PP2VKxz6OkS_phyoQqwzv2I89-8zP7QScrx5r8GFLcN5mCCNJt3rN3SIgI4XoIQbNePlAj6vE","x":"AVpvo7TGpQk5P7ZLo0qkBpaT-fFDv6HQrWElBKMxcrJd_mRNapweATsVv83YON4lTIIRXzgGkmWeqbDr6RQO-1cS","y":"AIs-MoRmLaiPyG2xmPwQCHX2CGX_uCZiT3iOxTAJEZuUbeSA828K4WfAA4ODdGiB87YVShhPOkiQswV3LpbpPGhC"}', json_encode($result));
    }

    private function getCacheKey($url)
    {
        return sprintf('%s-%s', 'JWKFactory-Content', hash('sha512', $url));
    }
}
