paragonie / paseto

Platform-Agnostic Security Tokens
https://paseto.io
Other
3.23k stars 108 forks source link

I can't get Version 3 working #159

Closed Jacobinoo closed 1 year ago

Jacobinoo commented 1 year ago

I am trying to create a token using version 3 but it doesn't work! I'm getting an error: Uncaught ParagonIE\Paseto\Exception\InvalidVersionException: The given key is not intended for this version of PASETO. I would be grateful if someone would help me.

use ParagonIE\Paseto\Protocol\Version3;
use ParagonIE\Paseto\Keys\SymmetricKey;
use ParagonIE\Paseto\Builder;
use ParagonIE\Paseto\Purpose;
$sharedKey = new SymmetricKey(random_bytes(32));

/**
 * We assume the same key $sharedKey was used from above.
 * @var SymmetricKey $sharedKey
 */

$token = Builder::getLocal($sharedKey, new Version3);

$token = (new Builder())
    ->setKey($sharedKey)
    ->setVersion(new Version3)
    ->setPurpose(Purpose::local())
    // Set it to expire in one day
    ->setIssuedAt()
    ->setNotBefore()
    ->setExpiration(
        (new DateTime())->add(new DateInterval('P01D'))
    )
    // Store arbitrary data
    ->setClaims([
        'example' => 'Hello world',
        'security' => 'Now as easy as PIE'
    ]);
echo $token; // Converts automatically to a string
paragonie-security commented 1 year ago

You need to pass the version as the second argument to SymmetricKey.

- $sharedKey = new SymmetricKey(random_bytes(32));
+ $sharedKey = new SymmetricKey(random_bytes(32), new Version3);

PASETO is very strict about versioning.

Jacobinoo commented 1 year ago

Thank you so much! It works perfectly now.