-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Document how to add/remove token extractors
- Loading branch information
Showing
5 changed files
with
146 additions
and
13 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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
Extending JWTTokenAuthenticator | ||
=============================== | ||
|
||
The `JWTTokenAuthenticator` class is responsible of authenticating JWT tokens. It is used through the `lexik_jwt_authentication.guard.jwt_token_authenticator` which can be customized. | ||
|
||
Nowadays it's common to manage various security contexts in the same application, that's what we tried to help for we tried to give the most flexible but still structured way to do it: _creating your own authenticators by extending the service_. | ||
|
||
Creating your own Token Authenticator | ||
------------------------------------- | ||
|
||
The following code can be used for creating your own authenticators. | ||
|
||
```yaml | ||
# services.yml | ||
services: | ||
app.jwt_token_authenticator: | ||
class: AppBundle\Security\Guard\JWTTokenAuthenticator | ||
parent: lexik_jwt_authentication.guard.jwt_token_authenticator | ||
``` | ||
```php | ||
namespace AppBundle\Security\Guard; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator as BaseAuthenticator; | ||
|
||
class JWTTokenAuthenticator extends BaseAuthenticator | ||
{ | ||
// Your own logic | ||
} | ||
``` | ||
|
||
__Note:__ The code examples of this section require to have this step done, it may not be repeated. | ||
|
||
Using different Token Extractors per Authenticator | ||
-------------------------------------------------- | ||
|
||
Token extractors are set up in the main configuration of this bundle, usually found in your `app/config/config.yml`. | ||
If your application contains multiple firewalls with different security contexts, you may want to configure the different token extractors which should be used on each firewall respectively. This can be done by having as much authenticators as firewalls (for creating authenticators, see [the first section of this topic](#creating-your-own-token-authenticator)). | ||
|
||
|
||
```php | ||
namespace AppBundle\Security\Guard; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator as BaseAuthenticator; | ||
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor; | ||
|
||
class JWTTokenAuthenticator extends BaseAuthenticator | ||
{ | ||
/** | ||
* @return TokenExtractor\TokenExtractorInterface | ||
*/ | ||
protected function getTokenExtractor() | ||
{ | ||
// Return a custom extractor, no matter of what are configured | ||
return new TokenExtractor\AuthorizationHeaderTokenExtractor('Token', 'Authorization'); | ||
|
||
// Or retrieve the chain token extractor for mapping/unmapping extractors for this authenticator | ||
$chainExtractor = parent::getTokenExtractor(); | ||
|
||
// Clear the token extractor map from all configured extractors | ||
$chainExtractor->clearMap(); | ||
|
||
// Or only remove a specific extractor | ||
$chainTokenExtractor->removeExtractor(function (TokenExtractor\TokenExtractorInterface $extractor) { | ||
return $extractor instanceof TokenExtractor\CookieTokenExtractor; | ||
}); | ||
|
||
// Add a new query parameter extractor to the configured ones | ||
$chainExtractor->addExtractor(new TokenExtractor\QueryParameterTokenExtractor('jwt')); | ||
|
||
// Return the chain token extractor with the new map | ||
return $chainTokenExtractor; | ||
} | ||
} | ||
``` |
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
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 |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
* ChainTokenExtractor is the class responsible of extracting a JWT token | ||
* from a {@link Request} object using all mapped token extractors. | ||
* | ||
* Note: The extractor map is reinitialized to the configured extractors for | ||
* each different instance. | ||
* | ||
* @author Robin Chalas <[email protected]> | ||
*/ | ||
class ChainTokenExtractor implements \IteratorAggregate, TokenExtractorInterface | ||
|
@@ -35,6 +38,36 @@ public function addExtractor(TokenExtractorInterface $extractor) | |
$this->map[] = $extractor; | ||
} | ||
|
||
/** | ||
* Removes a token extractor from the map. | ||
* | ||
* @param Closure $filter A function taking an extractor as argument, | ||
* used to find the extractor to remove, | ||
* | ||
* @return bool True in case of success, false otherwise | ||
*/ | ||
public function removeExtractor(\Closure $filter) | ||
{ | ||
$filtered = array_filter($this->map, $filter); | ||
|
||
if (!$extractorToUnmap = current($filtered)) { | ||
return false; | ||
} | ||
|
||
$key = array_search($extractorToUnmap, $this->map); | ||
unset($this->map[$key]); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Clears the token extractor map. | ||
*/ | ||
public function clearMap() | ||
{ | ||
$this->map = []; | ||
} | ||
|
||
/** | ||
* Iterates over the token extractors map calling {@see extract()} | ||
* until a token is found. | ||
|
@@ -58,7 +91,7 @@ public function extract(Request $request) | |
* An extractor is initialized only if we really need it (at | ||
* the corresponding iteration). | ||
* | ||
* @return \Generator The generated TokenExtractorInterface implementations | ||
* @return \Generator The generated {@link TokenExtractorInterface} implementations | ||
*/ | ||
public function getIterator() | ||
{ | ||
|
@@ -68,12 +101,4 @@ public function getIterator() | |
} | ||
} | ||
} | ||
|
||
/** | ||
* Clears the token extractor map. | ||
*/ | ||
public function clearMap() | ||
{ | ||
$this->map = []; | ||
} | ||
} |