-
-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove mentions to non-existing container
This has confused enough people, let's just make things simpler and link to the configuration object for those who want to use. Signed-off-by: Luís Cobucci <[email protected]>
- Loading branch information
Showing
4 changed files
with
135 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,82 @@ | ||
# Issuing tokens | ||
|
||
!!! Note | ||
The examples here fetch the configuration object from a hypothetical dependency injection container. | ||
You can create it in the same script or require it from a different file. It basically depends on how your system is bootstrapped. | ||
|
||
To issue new tokens you must create a new token a builder (easier when using the [configuration object](configuration.md)), customise it, and ask it to build the token: | ||
To issue new tokens you must create a new token a builder, customise it, and ask it to build the token: | ||
|
||
```php | ||
use Lcobucci\JWT\Configuration; | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use Lcobucci\JWT\Encoding\ChainedFormatter; | ||
use Lcobucci\JWT\Encoding\JoseEncoder; | ||
use Lcobucci\JWT\Signer\Key\InMemory; | ||
use Lcobucci\JWT\Signer\Hmac\Sha256; | ||
use Lcobucci\JWT\Token\Builder; | ||
|
||
$config = $container->get(Configuration::class); | ||
assert($config instanceof Configuration); | ||
require 'vendor/autoload.php'; | ||
|
||
$tokenBuilder = (new Builder(new JoseEncoder(), ChainedFormatter::default())); | ||
$algorithm = new Sha256(); | ||
$signingKey = InMemory::plainText(random_bytes(32)); | ||
|
||
$now = new DateTimeImmutable(); | ||
$token = $config->builder() | ||
// 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 issue (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($config->signer(), $config->signingKey()); | ||
$token = $tokenBuilder | ||
// 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 issue (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($algorithm, $signingKey); | ||
|
||
echo $token->toString(); | ||
``` | ||
|
||
Once you've created a token, you're able to retrieve its data and convert it to its string representation: | ||
|
||
```php | ||
use Lcobucci\JWT\Configuration; | ||
<?php | ||
declare(strict_types=1); | ||
|
||
$config = $container->get(Configuration::class); | ||
assert($config instanceof Configuration); | ||
use Lcobucci\JWT\Encoding\ChainedFormatter; | ||
use Lcobucci\JWT\Encoding\JoseEncoder; | ||
use Lcobucci\JWT\Signer\Key\InMemory; | ||
use Lcobucci\JWT\Signer\Hmac\Sha256; | ||
use Lcobucci\JWT\Token\Builder; | ||
|
||
$token = $config->builder() | ||
->issuedBy('http://example.com') | ||
->withClaim('uid', 1) | ||
->withHeader('foo', 'bar') | ||
->getToken($config->signer(), $config->signingKey()); | ||
require 'vendor/autoload.php'; | ||
|
||
$tokenBuilder = (new Builder(new JoseEncoder(), ChainedFormatter::default())); | ||
$algorithm = new Sha256(); | ||
$signingKey = InMemory::plainText(random_bytes(32)); | ||
|
||
$token = $tokenBuilder | ||
->issuedBy('http://example.com') | ||
->withClaim('uid', 1) | ||
->withHeader('foo', 'bar') | ||
->getToken($algorithm, $signingKey); | ||
|
||
$token->headers(); // Retrieves the token headers | ||
$token->claims(); // Retrieves the token claims | ||
|
||
echo $token->headers()->get('foo'); // will print "bar" | ||
echo $token->claims()->get('iss'); // will print "http://example.com" | ||
echo $token->claims()->get('uid'); // will print "1" | ||
echo $token->headers()->get('foo'), PHP_EOL; // will print "bar" | ||
echo $token->claims()->get('iss'), PHP_EOL; // will print "http://example.com" | ||
echo $token->claims()->get('uid'), PHP_EOL; // will print "1" | ||
|
||
echo $token->toString(), PHP_EOL; // The string representation of the object is a JWT string | ||
|
||
echo $token->toString(); // The string representation of the object is a JWT string | ||
``` | ||
|
||
!!! Note | ||
Some systems make use of components to handle dependency injection. | ||
If your application follows that practice, using a [configuration object](configuration.md) might simplify the wiring of this library. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
# Parsing tokens | ||
|
||
!!! Note | ||
The examples here fetch the configuration object from a hypothetical dependency injection container. | ||
You can create it in the same script or require it from a different file. It basically depends on how your system is bootstrapped. | ||
|
||
To parse a token you must create a new parser (easier when using the [configuration object](configuration.md)) and ask it to parse a string: | ||
To parse a token you must create a new parser and ask it to parse a string: | ||
|
||
```php | ||
use Lcobucci\JWT\Configuration; | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use Lcobucci\JWT\Encoding\CannotDecodeContent; | ||
use Lcobucci\JWT\Encoding\JoseEncoder; | ||
use Lcobucci\JWT\Token\InvalidTokenStructure; | ||
use Lcobucci\JWT\Token\Parser; | ||
use Lcobucci\JWT\Token\UnsupportedHeaderFound; | ||
use Lcobucci\JWT\UnencryptedToken; | ||
|
||
$config = $container->get(Configuration::class); | ||
assert($config instanceof Configuration); | ||
require 'vendor/autoload.php'; | ||
|
||
$token = $config->parser()->parse( | ||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.' | ||
. 'eyJzdWIiOiIxMjM0NTY3ODkwIn0.' | ||
. '2gSBz9EOsQRN9I-3iSxJoFt7NtgV6Rm0IL6a8CAwl3Q' | ||
); | ||
$parser = new Parser(new JoseEncoder()); | ||
|
||
try { | ||
$token = $parser->parse( | ||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.' | ||
. 'eyJzdWIiOiIxMjM0NTY3ODkwIn0.' | ||
. '2gSBz9EOsQRN9I-3iSxJoFt7NtgV6Rm0IL6a8CAwl3Q' | ||
); | ||
} catch (CannotDecodeContent | InvalidTokenStructure | UnsupportedHeaderFound $e) { | ||
echo 'Oh no, an error: ' . $e->getMessage(); | ||
} | ||
assert($token instanceof UnencryptedToken); | ||
|
||
$token->headers(); // Retrieves the token headers | ||
$token->claims(); // Retrieves the token claims | ||
echo $token->claims()->get('sub'), PHP_EOL; // will print "1234567890" | ||
|
||
``` | ||
|
||
!!! Important | ||
In case of parsing errors the Parser will throw an exception of type `InvalidArgumentException`. | ||
!!! Note | ||
Some systems make use of components to handle dependency injection. | ||
If your application follows that practice, using a [configuration object](configuration.md) might simplify the wiring of this library. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters