I am using JWT Token Authentication with lexik/jwt-authentication-bundle version 2.18.1.
I have an API and my complete user information is stored in the JWT token I get from an external system.
The information is an multidimensional array with the user identifier "uid" being stored in the user.
Example:
{
"application": {
"appcode": "my_app"
},
"user": {
"uid": "p320666",
"firstname": "John",
"mail": "John.Doe.com",
"lastname": "Doe"
},
"grants": [
"create"
],
"resources": [
{
"code": "foo",
"values": [
"5"
]
}
],
"updateDate": {}
}
My user identifier is uid and it is in the 2nd level of my information array.
If I set in my configuration user_id_claim: user
I get understandably the warning:
Array to string conversion
because in JWTAuthenticator.php
$passport = new SelfValidatingPassport(
new UserBadge(
(string)$payload[$idClaim],
function ($userIdentifier) use ($payload) {
return $this->loadUser($payload, $userIdentifier);
}
)
);
it tries to read my user array as a string.
If I set uid
in my configuration it isn’t found. user.uid
also doesn’t work.
Is there an option to access my uid
information without changing the structure of the payload?
2
Answers
I solved it now by writing a custom authenticator:
security.yaml
services.yaml
lexik_jwt_authentication.yaml
and finally my custom authenticator where I changed the line
(string) $payload[$idClaim]['uid'],
and also call an extra class TokenDataProcessor to extract my complete user info from the payload:Try this,
To extract the uid property from the JSON string in PHP, you can decode the JSON string into an associative array using
json_decode()
, and then access the uid property as you would with any associative array. Here’s how you can do it:In this code:
json_decode($jsonString, true)
decodes the JSON string into an associative array. The second parameter true is used to specify that we want an associative array instead of an object.We then access the uid property using array notation
($data['user']['uid'])
.This will give you the value of the uid property from your JSON string.