<?php

namespace AuthStack\OIDC\tests\units;

use atoum;
use AuthStack\OIDC\Exception\TokenException;
use function AuthStack\OIDC\id_token;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use AuthStack\OIDC\IDToken as Token;
use Lcobucci\JWT\Signer\Rsa\Sha384;

class IDToken extends atoum\test
{
    public function testValidIDToken()
    {
        $this->given($jwt = $this->getSignedJWT())
            ->then
            ->object($token = new Token($jwt))->isInstanceOf(Token::class)
            ->boolean($token->verify(new Sha256(), $this->getPublicKey()))->isTrue()
            ->object($decoded = $token->getToken())->isInstanceOf(\Lcobucci\JWT\Token::class)
            ->array($decoded->getClaims())->isNotEmpty()
            ->string($decoded->getClaim('sub'))->isEqualTo('dyson.frost@yahoo.com')
            ;

        $token->getToken();
    }

    public function testTokenFailsAtSignature()
    {
        // Signature fails because we signed using RSA SHA256 but checked with RSA SHA384
        // Failure occurs if key is invalid or any other combination different to original signature
        $this->exception(function()
        {
            $token = id_token(($this->getSignedJWT()), new Sha384(), $this->getPublicKey());

        })->isInstanceOf(TokenException::class)
            ;
    }

    protected function getSignedJWT()
    {
        $builder = new Builder();

        $token = $builder
            ->setIssuer('https://authstack.co.uk')
            ->setSubject('dyson.frost@yahoo.com')
            ->setAudience('your-relying-party-unique-id')
            ->setExpiration(time() + 3600)
            ->setIssuedAt(time())
            ->set('auth_time', time())
            ->set('name', 'Dyson Frost')
            ->set('given_name', 'Dyson')
            ->set('last_name', 'Frost')
            ->set('nickname', 'dyce')
            ->set('preferred_username', 'dyson.frost@yahoo.com')
            ->set('profile', 'N/A')
            ->set('picture', 'N/A')
            ->set('website', 'https://www.authstack.co.uk')
            ->set('gender', 'male')
            ->set('birthdate', '1970-01-01')
            ->set('zoneinfo', 'Europe/London')
            ->set('locale', 'en-GB')
            ->sign(new Sha256(), new Key($this->getPrivateKey(), null))
            ->getToken();
        ;

        return (string)$token;
    }

    protected function getPrivateKey()
    {
        return <<<EOF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htRpNYPyejc1ijXO/jO7N1M9LVbdxUuWAcyCO6G6P5JtmqTjvH56ChRjl5ClSen
UTJui34GhkECgYEAjkWmRckuvTmY+/5/LoVH7qbliQysy7weBhNgam4Lrq2uP7TW
g9M9S0wtRTrbldlbwL8NtBgcc+SXR+bdHcPmUNnUTEW6uCQsoGXow7O2eNFpyFS7
7jaia0AoWGTc5lot/MxZ4eamfslm5h5AmzvFfaPDj0oYNwlCunXqH/hBz50CgYEA
rqzIuYCPfaOMAQEDhTiVXXHUAgmaAFhdhnJobFNSjooJ1FxN+7p5BITYTYfYV430
lvrd1YHZyBywGIIr1FqQGLgI5BeR0FoPOgpRFwJCQe/UuELGLNSlq6zx0ZBzVaW6
V6oSr7AKjQDJIAtRXpu9EklriviA9NuY7Ubko+kuGsECgYEAtww5y3RaO0eQGPJm
Xmwa05IJf9G82jnSrnla5fIhPgzvLvzhJctz65JiP9eKIMXirJUaa+xL/FwnKil6
BqWRZhnQ+Xz238NNnZfjJSmABTOX0bngQNsoBlDPovS/9rxWz2mwY5N1j5g2sAw2
4VkxBy7lVA96u9D1nzbCMu/2wms=
-----END PRIVATE KEY-----
EOF;

    }

    protected function getPublicKey()
    {
        return <<<EOF
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn+P4VrNUBzZalm0kT3ud
zAcHWmN4QBFr3xhcvFUd4iZZ4LFtt2QkI7iiQjnKNUTwPcuD0iCNab+R4UxpoFgH
SmJawzapAObt+TTajBv6KkCh2sbNE+yjMn1x2Ewh6/hVXmqbqDR/9DIImkMBj7GT
i3J+/A6nt1LmCYqlaxhYkHIMKFzAH5txGOWwlLulexRMjf5uneI1sjmuGBUb9K61
ftrHPxjJ7eVf8tKeSSL9wnxgw26sQbfUNETjMrc+6dNwO62KoZzQ/jTVmd387Rhr
jYwwOwtv9iseq0i4PqC7YAfq0RTtS6GLmGCvFDUlRryfGvX9UjRoshVcecjpX8WK
ZQIDAQAB
-----END PUBLIC KEY-----
EOF;

    }
}