a4sex / jwt-manager
jwt-manager for FreedomSex project
Requires
- php: ^8.2
- firebase/php-jwt: ^6.0
- symfony/deprecation-contracts: *
Requires (Dev)
- phpunit/phpunit: ^9.5
Suggests
This package is auto-updated.
Last update: 2024-12-15 15:01:30 UTC
README
for FreedomSex projects
for you without a guarantee of version compatibility, and everything else
Firebase\JWT is used internally
You need to create keys for signing the token.
mkdir -P /config/keys/
cd config/keys
Keys Example
openssl genrsa -out private.key 1024
openssl rsa -in private.key -pubout -outform PEM -out public.key
Specify the paths when creating an instance of the manager. And TimeToLive generated token as the last argument
$manager = new JWTManager(
'../keys/private.key',
'../keys/public.key',
$this->ttl
);
Generate JWT token
Simple JWT token. Only Expires in payload
$token = $manager->create();
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjE2Mzg1NDkwMTN9.GfefYgNnxpUCU_n8t0d37tQP-pjP7euGhrHTDx4T3ta0Eaa5Bedved5KbzZF-yXMUstnXr3TVRu3dkbKCaf0h2OJp13LT1WgvsyrkMIeO2KRG-vwsFrGrzAHRu2O5OKa7WI3sIFDE-oc_khyPFvO01UdiLtpEISOh8ys3Dh32-8
HEADER
{
"typ": "JWT",
"alg": "RS256"
}
PAYLOAD
{
"exp": 1638549013
}
Payload based on User instance
$token = $manager->create($user);
// getId - uid
// getRoles - roles
// getIdentityId - uuid
// getAccess - access
// getSubject -sub
Deprecations
uuid
- deprecated since 0.4: use uid
and id
$token = $manager->create($user);
// getId - id
// getUid - uid
// or
// getIdentityId - uid
// getId - id
// and
// getUid - uid (deprecated)
// and
// getIdentityId - uuid
Expires "alternative" way
$token = $manager->create($user, 1638549013);
Load payload
$payload = $manager->load($token); // object return
$payload = (array) $manager->load($token); // array return
[
"uid" => 1
"exp" => 1638549013
]
Inheritance and Overriding
Override method populatePayload
to Define your own structure
public function populatePayload(array $payload, $user = null): array