<?php

/* The MIT License (MIT)
*
* Copyright (c) 2022 Petr Muzikant <pmuzikant@email.cz>
*
* > Permission is hereby granted, free of charge, to any person obtaining a copy
* > of this software and associated documentation files (the "Software"), to deal
* > in the Software without restriction, including without limitation the rights
* > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* > copies of the Software, and to permit persons to whom the Software is
* > furnished to do so, subject to the following conditions:
* >
* > The above copyright notice and this permission notice shall be included in
* > all copies or substantial portions of the Software.
* >
* > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* > THE SOFTWARE.
*/

declare(strict_types=1);

namespace muzosh\web_eid_authtoken_validation_php\validator;

use muzosh\web_eid_authtoken_validation_php\certificate\CertificateData;
use muzosh\web_eid_authtoken_validation_php\exceptions\AuthTokenSignatureValidationException;
use muzosh\web_eid_authtoken_validation_php\testutil\AbstractTestWithValidator;
use muzosh\web_eid_authtoken_validation_php\testutil\AuthTokenValidators;

/**
 * @internal
 * @coversNothing
 */
class AuthTokenSignatureTest extends AbstractTestWithValidator
{
    public const AUTH_TOKEN_WRONG_CERT = '{"algorithm":"ES384",'.
        '"unverifiedCertificate":"MIIEBDCCA2WgAwIBAgIQH9NeN14jo0ReaircrN2YvDAKBggqhkjOPQQDBDBgMQswCQYDVQQGEwJFRTEbMBkGA1UECgwSU0sgSUQgU29sdXRpb25zIEFTMRcwFQYDVQRhDA5OVFJFRS0xMDc0NzAxMzEbMBkGA1UEAwwSV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N2zEXZSJdP0q1iH1RR2AzZ8Ddz6SKRn/bZSzjcd4b7h3AyOEQr2hcidYkxT7sAJCAMPtOUryqp2WbTEUoOpbWrKqp8GjaAiVpBGDn/Xdu5M2Z6dvwZHnFGgRrZXtyUbcAgRW7MQJ0s/9GCVro3iqUzNN",'.
        '"appVersion":"https://web-eid.eu/web-eid-app/releases/2.0.0+0",'.
        '"signature":"arx164xRiwhIQDINe0J+ZxJWZFOQTx0PBtOaWaxAe7gofEIHRIbV1w0sOCYBJnvmvMem9hU4nc2+iJx2x8poYck4Z6eI3GwtiksIec3XQ9ZIk1n/XchXnmPn3GYV+HzJ",'.
        '"format":"web-eid:1.0"}';

    public function testWhenValidTokenAndNonceThenValidationSucceeds(): void
    {
        $result = $this->validator->validate($this->validAuthToken, self::VALID_CHALLENGE_NONCE);
        $this->assertEquals('JÕEORG,JAAK-KRISTJAN,38001085718', CertificateData::getSubjectCN($result));
        $this->assertEquals('JAAK-KRISTJAN', CertificateData::getSubjectGivenName($result));
        $this->assertEquals('JÕEORG', CertificateData::getSubjectSurname($result));
        $this->assertEquals('PNOEE-38001085718', CertificateData::getSubjectSerialNumber($result));
        $this->assertEquals('EE', CertificateData::getSubjectCountryCode($result));
    }

    public function testWhenValidTokenAndWrongChallengeNonceThenValidationFails()
    {
        $invalidChallengeNonce = '12345678123456781234567812345678912356789124';

        $this->expectException(AuthTokenSignatureValidationException::class);
        $this->validator->validate($this->validAuthToken, $invalidChallengeNonce);
    }

    public function testWhenValidTokenAndWrongOriginThenValidationFails(): void
    {
        $authTokenValidator = AuthTokenValidators::getAuthTokenValidator('https://invalid.org');

        $this->expectException(AuthTokenSignatureValidationException::class);
        $authTokenValidator->validate($this->validAuthToken, self::VALID_CHALLENGE_NONCE);
    }

    public function testWhenTokenWithWrongCertThenValidationFails(): void
    {
        $authTokenValidator = AuthTokenValidators::getAuthTokenValidator();
        $authTokenWithWrongCert = $authTokenValidator->parse(self::AUTH_TOKEN_WRONG_CERT);

        $this->expectException(AuthTokenSignatureValidationException::class);
        $authTokenValidator->validate($authTokenWithWrongCert, self::VALID_CHALLENGE_NONCE);
    }
}
