-
-
Notifications
You must be signed in to change notification settings - Fork 602
Description
Hi folks, hi Luís,
Two possibilities i could think of:
1)
extend documentation
add section token creation: "adding payload consisting of associative array with multiple key value pairs"
please add a best practice example to the documentation on how to add payload data in the form of an associative array (consisting of multiple key => value pairs) on token creation.
Right now i have came up with a solution such as:
Example:
# example data
$data = array(
"success_url" => $scheme.$salesChannelUrl.'/addProduct2Cart',
"cancel_url" => $scheme.$salesChannelUrl.$cancelUrlPath,
"template_id" => $id,
"customer" => $token,
"locale" => "de_DE",
"timezone" => "Europe/Vienna",
"theme" => "default",
"additional_data" => base64_encode(json_encode([
"sw-access-key" => $key,
"sw-context-token" => $token,
"product-id" => $product,
"sales-channel-url" => $salesChannelUrl,
"quantity" => $quantity,
"jwtSecretKey" => $jwtSecret,
"isLogin" => 1,
"checkCustomerUrl" => $salesChannelUrl.$checkCustomerUrl
]))
);
# actual token creation
$builder = null;
$counter = 0;
foreach ($data as $key => $value) {
if($counter < 1){
$builder = $config->builder()
->withClaim($key, $value);
}
else{
$builder->withClaim($key, $value);
}
$counter++;
}
# get the token
$createdJwtTokenObject = $builder->getToken($signer, $signingKey);
$createdJwtTokenStringRepresentation = $builder->getToken($signer, $signingKey)->toString();
# now one could request data from a JWT talking API server with this created token
$response = $client->request(
'POST',
'https://server-jwt-api.example.com/external/public/api/jwttoken/store', [
'query' => ['jwt' => $createdJwtTokenStringRepresentation]
]);
$stream = $response->getBody();
$array = json_decode($stream->getContents(), true);
# and by example return as response to javascript script or whatever...
$response = new Response(
$array['data'],
Response::HTTP_OK,
['content-type' => 'text/html']
);
return $response;
- but the foreach loop to please the withClaim function seems not elegant and is bad to read.
OR Alternative for future jwt releases
2)
maybe it makes sense to provide a new function in future lcobucci/jwt releases to $config->builder()->withClaims($array) to deal with associative array payload data? Maybe not the user but lcobucci/jwt should take care about to proper handle the adding of multiple payload data on token creation?
i hope this helps others with similar issues as i had.
Kind regards,
Raphael