How To Decode Paseto Token Laravel

78 views Asked by At

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"
1

There are 1 answers

0
soulseekah On BEST ANSWER

SymmetricKey::fromEncodedString expects a base64-encoded payload, the correct way is to just use new SymmetricKey($secretKey).

The latest version of paragonie/paseto does 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:

require 'vendor/autoload.php';

use ParagonIE\Paseto\Builder;
use ParagonIE\Paseto\Parser;
use ParagonIE\Paseto\Keys\SymmetricKey;
use ParagonIE\Paseto\ProtocolCollection;

// Your PASETO token
$pasetoToken = 'v2.local.2P2TzwkaMdoz2Y8NFTsB_kgHWG9lPNNCRzdqGtuZlUbYuz0iMuDrby8utgqfs0g1pGxmSe_8cTCgiJ_xFkPAY7tllw.bnVsbA';

// Your secret key
$secretKey = 'YELLOW SUBMARINE, BLACK WIZARDRY';

// Create a SymmetricKey instance
$key = new SymmetricKey($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);

Output:

array(2) {
  'uid' =>
  int(1)
  'role' =>
  string(8) "customer"
}

PASETO Version 2 is deprecated: https://github.com/paseto-standard/paseto-spec/blob/master/docs/01-Protocol-Versions/Version2.md