Skip to content

Commit 7ecd271

Browse files
committed
Add documentation chapter for Oauth2TokenHandler
1 parent 19e4c7c commit 7ecd271

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

security/access_token.rst

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,127 @@ create your own User from the claims, you must
875875
}
876876
}
877877

878+
3) Configure the Oauth2TokenHandler
879+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
880+
881+
Symfony provides a generic access token handler that calls the configured token introspection endpoint to validate the token and retrieve the user information from it.
882+
It requires the ``symfony/http-client`` package to make the needed HTTP requests. If you haven't installed it yet, run this command:
883+
884+
.. code-block:: terminal
885+
886+
$ composer require symfony/http-client
887+
888+
First, configure a dedicated scoped HTTP client for the token handler:
889+
890+
.. configuration-block::
891+
892+
.. code-block:: yaml
893+
# config/packages/framework.yaml
894+
framework:
895+
http_client:
896+
scoped_clients:
897+
oauth2.client:
898+
base_uri: 'https://authorization-server.example.com/introspection'
899+
scope: 'https://authorization-server\.example\.com'
900+
headers:
901+
Authorization: 'Basic Y2xpZW50OnBhc3N3b3Jk'
902+
903+
.. code-block:: xml
904+
905+
<!-- config/packages/framework.xml -->
906+
<?xml version="1.0" encoding="UTF-8" ?>
907+
<container xmlns="http://symfony.com/schema/dic/services"
908+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
909+
xmlns:framework="http://symfony.com/schema/dic/symfony"
910+
xsi:schemaLocation="http://symfony.com/schema/dic/services
911+
https://symfony.com/schema/dic/services/services-1.0.xsd
912+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
913+
914+
<framework:config>
915+
<framework:http-client>
916+
<framework:scoped-client name="oauth2.client"
917+
base-uri="https://authorization-server.example.com/introspection"
918+
scope="https://authorization-server\.example\.com"
919+
>
920+
<!-- Introspection Endpoint usually requires client authentication -->
921+
<framework:header name="Authorization">Basic Y2xpZW50OnBhc3N3b3Jk</framework:header>
922+
</framework:scoped-client>
923+
</framework:http-client>
924+
</framework:config>
925+
</container>
926+
927+
.. code-block:: php
928+
929+
// config/packages/framework.php
930+
use Symfony\Config\FrameworkConfig;
931+
932+
return static function (FrameworkConfig $framework): void {
933+
$framework->httpClient()->scopedClient('oauth2.client')
934+
->baseUri('https://authorization-server.example.com/introspection')
935+
->scope('https://authorization-server\.example\.com')
936+
->header('Authorization', 'Basic Y2xpZW50OnBhc3N3b3Jk') // Introspection Endpoint usually requires client authentication
937+
;
938+
};
939+
940+
Then, configure the ``oauth2`` token handler to use this scoped HTTP client:
941+
942+
.. configuration-block::
943+
944+
.. code-block:: yaml
945+
946+
# config/packages/security.yaml
947+
security:
948+
firewalls:
949+
main:
950+
pattern: ^/
951+
access_token:
952+
token_handler:
953+
oauth2: ~
954+
token_extractors: 'header'
955+
realm: 'My API'
956+
957+
.. code-block:: xml
958+
959+
<!-- config/packages/security.xml -->
960+
<?xml version="1.0" encoding="UTF-8"?>
961+
<srv:container xmlns="http://symfony.com/schema/dic/security"
962+
xmlns:srv="http://symfony.com/schema/dic/services"
963+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
964+
xsi:schemaLocation="http://symfony.com/schema/dic/services
965+
https://symfony.com/schema/dic/services/services-1.0.xsd
966+
http://symfony.com/schema/dic/security
967+
https://symfony.com/schema/dic/security/security-1.0.xsd">
968+
969+
<config>
970+
<firewall name="main">
971+
<access-token>
972+
<token-handler>
973+
<oauth2/>
974+
</token-handler>
975+
</access-token>
976+
</firewall>
977+
</config>
978+
</srv:container>
979+
980+
.. code-block:: php
981+
982+
// config/packages/security.php
983+
use Symfony\Config\SecurityConfig;
984+
985+
return static function (SecurityConfig $security) {
986+
$security->firewall('main')
987+
->accessToken()
988+
->tokenHandler()
989+
->oauth2()
990+
->tokenExtractors('header')
991+
->realm('My API')
992+
;
993+
};
994+
995+
.. versionadded:: 7.3
996+
997+
The support for OAuth2 Token Introspection handler was introduced in Symfony 7.3.
998+
878999
Using CAS 2.0
8791000
-------------
8801001

0 commit comments

Comments
 (0)