php-jwt icon indicating copy to clipboard operation
php-jwt copied to clipboard

Question about Payload

Open rafaelpstein opened this issue 6 years ago • 3 comments

If possible insert into payload sub array?

Example: "user": { "avatar": "https:/gravatar.com/avatar/abc123", "name": "xxx", "email": "xxx@xxx }, "aud": "zzzz", "iss": "zzzzz"

rafaelpstein avatar May 07 '20 20:05 rafaelpstein

That would be very useful to add additional data to the token

LukeSavefrogs avatar Aug 11 '20 02:08 LukeSavefrogs

As far as I see in the RFC-7519, its a valid request.

ajupazhamayil avatar Jun 20 '23 12:06 ajupazhamayil

Sorry about the very late reply.

I see this usage is already available. An example code would be the following:

<?php
// composer require firebase/php-jwt

require __DIR__ . '/vendor/autoload.php';

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$key = 'example_key';
$payload = [
    'iss' => 'http://example.org',
    'user' => [
        'name' => 'http://example.org'
    ], 
    'aud' => 'http://example.com',
    'iat' => 1356999524,
    'nbf' => 1357000000
];

/**
 * IMPORTANT:
 * You must specify supported algorithms for your application. See
 * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
 * for a list of spec-compliant algorithms.
 */
$jwt = JWT::encode($payload, $key, 'HS256');
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
print_r($decoded);

// Pass a stdClass in as the third parameter to get the decoded header values
$headers = new stdClass();
$decoded = JWT::decode($jwt, new Key($key, 'HS256'), $headers);
print_r($headers);

Thank you for asking. Closing this thread.

ajupazhamayil avatar Jun 20 '23 12:06 ajupazhamayil