I wanto to decode paseto token for getting payload to set on laravel session. The code that I impement show result Base64::decode() only expects characters in the correct base64 alphabet. Can anyone solve my problem.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use ParagonIE\Paseto\Builder;
use ParagonIE\Paseto\Parser;
use ParagonIE\Paseto\Keys\SymmetricKey;
class DashboardController extends Controller
{
public function index()
{
// Your PASETO token
$pasetoToken = 'v2.local.2P2TzwkaMdoz2Y8NFTsB_kgHWG9lPNNCRzdqGtuZlUbYuz0iMuDrby8utgqfs0g1pGxmSe_8cTCgiJ_xFkPAY7tllw.bnVsbA';
// Your secret key
$secretKey = 'YELLOW SUBMARINE, BLACK WIZARDRY';
try {
// Create a SymmetricKey instance
$key = SymmetricKey::fromEncodedString($secretKey);
// Parse and verify the PASETO token
$decodedToken = (new Parser())
->setKey($key)
->parse($pasetoToken);
// Token is valid, access the payload
$payload = $decodedToken->getClaims();
// Output the payload
var_dump($payload);
} catch (\ParagonIE\Paseto\Exception\PasetoException $e) {
// Handle the exception (e.g., token verification failure)
echo 'PASETO Exception: ' . $e->getMessage();
} catch (\Exception $e) {
// Handle other exceptions
echo 'Exception: ' . $e->getMessage();
}
return view('dashboard.index');
}
}
the error response is
Exception: Base64::decode() only expects characters in the correct base64 alphabet
I want the result
user_id: 1
role: "customer"
SymmetricKey::fromEncodedStringexpects a base64-encoded payload, the correct way is to just usenew SymmetricKey($secretKey).The latest version of
paragonie/pasetodoes not support version 2 of the protocol, so I got a "Disallowed or unsupported version" error. Downgrading to version 1.x made the following code work:Output:
PASETO Version 2 is deprecated: https://github.com/paseto-standard/paseto-spec/blob/master/docs/01-Protocol-Versions/Version2.md