1. Go to this page and download the library: Download algsupport/jwt library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
algsupport / jwt example snippets
[
'components' => [
'jwt' => [
'class' => \algsupport\jwt\Jwt::class,
'signer' => ... // Signer ID, or signer object, or signer configuration, see "Available signers" below
'signingKey' => ... // Secret key string or path to the signing key file, see "Keys" below
// ... any additional configuration here
],
],
],
[
'components' => [
'jwt' => [
'class' => \algsupport\jwt\JwtTools::class,
// ... any additional configuration here
],
],
],
$now = new \DateTimeImmutable();
/** @var \Lcobucci\JWT\Token\UnencryptedToken $token */
$token = Yii::$app->jwt->getBuilder()
// Configures the issuer (iss claim)
->issuedBy('http://example.com')
// Configures the audience (aud claim)
->permittedFor('http://example.org')
// Configures the id (jti claim)
->identifiedBy('4f1g23a12aa')
// Configures the time that the token was issued (iat claim)
->issuedAt($now)
// Configures the time that the token can be used (nbf claim)
->canOnlyBeUsedAfter($now->modify('+1 minute'))
// Configures the expiration time of the token (exp claim)
->expiresAt($now->modify('+1 hour'))
// Configures a new claim, called "uid"
->withClaim('uid', 1)
// Configures a new header, called "foo"
->withHeader('foo', 'bar')
// Builds a new token
->getToken(
Yii::$app->jwt->getConfiguration()->signer(),
Yii::$app->jwt->getConfiguration()->signingKey()
);
$tokenString = $token->toString();
Yii::$app->jwt->getConfiguration()->setValidationConstraints(/* constaints here */);
[
'validationConstraints' => /*
array of instances of Lcobucci\JWT\Validation\Constraint
or
array of configuration arrays that can be resolved as Constraint instances
or
anonymous function that can be resolved as array of Constraint instances with signature
`function(\algsupport\jwt\Jwt|\algsupport\jwt\JwtTools $jwt)` where $jwt will be an instance of used component
*/,
]
class ExampleController extends Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => \algsupport\jwt\JwtHttpBearerAuth::class,
];
return $behaviors;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.