1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
# Extending the library
!!! 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.
We've designed a few extension points in this library.
These should enable people to easily customise our core components if they want to.
## Builder
The token builder defines a fluent interface for plain token creation.
To create your own builder of it you must implement the `Lcobucci\JWT\Builder` interface:
```php
use Lcobucci\JWT\Builder;
final class MyCustomTokenBuilder implements Builder
{
// implement all methods
}
```
Then, register a custom factory in the [configuration object]:
```php
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\ClaimsFormatter;
use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$configuration = $configuration->withBuilderFactory(
static function (ClaimsFormatter $formatter): Builder {
return new MyCustomTokenBuilder($formatter);
}
);
```
## Claims formatter
By default, we provide formatters that:
- unify the audience claim, making sure we use strings when there's only one item in that claim
- format date based claims using microseconds (float)
You may customise and even create your own formatters:
```php
use Lcobucci\JWT\ClaimsFormatter;
use Lcobucci\JWT\Configuration;
use Serializable;
final class ClaimSerializer implements ClaimsFormatter
{
/** @inheritdoc */
public function formatClaims(array $claims): array
{
foreach ($claims as $claim => $claimValue) {
if ($claimValue instanceof Serializable) {
$claims[$claim] = $claimValue->serialize();
}
}
return $claims;
}
}
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$builder = $config->builder(new ClaimSerializer());
```
The class `Lcobucci\JWT\Encoding\ChainedFormatter` allows for users to combine multiple formatters.
## Parser
The token parser defines how a JWT string should be converted into token objects.
To create your own parser of it you must implement the `Lcobucci\JWT\Parser` interface:
```php
use Lcobucci\JWT\Parser;
final class MyCustomTokenParser implements Parser
{
// implement all methods
}
```
Then register an instance in the [configuration object]:
```php
use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$configuration = $configuration->withParser(new MyCustomTokenParser());
```
## Signer
The signer defines how to create and verify signatures.
To create your own signer of it you must implement the `Lcobucci\JWT\Signer` interface:
```php
use Lcobucci\JWT\Signer;
final class SignerForAVeryCustomizedAlgorithm implements Signer
{
// implement all methods
}
```
Then pass an instance of it while creating an instance of the [configuration object], [issuing a token](issuing-tokens.md), or [validating a token].
## Key
The key object is passed down to signers and provide the necessary information to create and verify signatures.
To create your own signer of it you must implement the `Lcobucci\JWT\Signer\Key` interface:
```php
use Lcobucci\JWT\Signer\Key;
final class KeyWithSomeMagicalProperties implements Key
{
// implement all methods
}
```
## Validator
The token validator defines how to apply validation constraint to either validate or assert tokens.
To create your own validator of it you must implement the `Lcobucci\JWT\Validator` interface:
```php
use Lcobucci\JWT\Validator;
final class MyCustomTokenValidator implements Validator
{
// implement all methods
}
```
Then register an instance in the [configuration object]:
```php
use Lcobucci\JWT\Configuration;
$config = $container->get(Configuration::class);
assert($config instanceof Configuration);
$configuration = $configuration->withValidator(new MyCustomTokenValidator());
```
## Validation constraints
A validation constraint define how one or more claims/headers should be validated.
Custom validation constraints are handy to provide advanced rules for the registered claims or to validate private claims.
To create your own implementation of constraint you must implement the `Lcobucci\JWT\Validation\Constraint` interface:
```php
use Lcobucci\JWT\Token;
use Lcobucci\JWT\UnencryptedToken;
use Lcobucci\JWT\Validation\Constraint;
use Lcobucci\JWT\Validation\ConstraintViolation;
final class SubjectMustBeAValidUser implements Constraint
{
public function assert(Token $token): void
{
if (! $token instanceof UnencryptedToken) {
throw new ConstraintViolation('You should pass a plain token');
}
if (! $this->existsInDatabase($token->claims()->get('sub'))) {
throw new ConstraintViolation('Token related to an unknown user');
}
}
private function existsInDatabase(string $userId): bool
{
// ...
}
}
```
Then use it while [validating a token].
[configuration object]: configuration.md
[validating a token]: validating-tokens.md
|