@@ -21,15 +21,9 @@ this is not yet the case.
21
21
1) Configure the Access Token Authenticator
22
22
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23
23
24
- The access token authenticator can be configured using three different options:
25
-
26
- * ``header_token ``: the token is sent through the request header. Usually ``Authorization `` with the ``Bearer `` scheme.
27
- * ``query_token ``: the token is part of the query string. Usually ``access_token ``.
28
- * ``body_token ``: the token is part of the request body during a POST request. Usually ``access_token ``.
29
-
30
- You must configure a ``token_handler `` when enabling this authenticator.
31
- The token handler is a service that is able to load and verify the token (e.g. expiration, digital signature...)
32
- and return the associated user identifier.
24
+ The access token authenticator requires a ``token_handler ``.
25
+ The token handler is a service that is able to load and verify the token
26
+ (e.g. expiration, digital signature...) and return the associated user identifier.
33
27
34
28
.. configuration-block ::
35
29
@@ -39,7 +33,7 @@ and return the associated user identifier.
39
33
security :
40
34
firewalls :
41
35
main :
42
- header_token :
36
+ access_token :
43
37
token_handler : App\Security\AccessTokenHandler
44
38
45
39
2) Create your Access Token Handler
@@ -58,7 +52,7 @@ using a fictive Doctrine repository.
58
52
.. code-block :: php
59
53
60
54
// src/Security/AccessTokenHandler.php
61
- namespace App\Controller ;
55
+ namespace App\Security ;
62
56
63
57
use App\Repository\AccessTokenRepository;
64
58
use Symfony\Component\Security\Http\Authenticator\AccessTokenHandlerInterface;
@@ -71,46 +65,106 @@ using a fictive Doctrine repository.
71
65
72
66
public function getUserIdentifierFrom(string $token): string
73
67
{
68
+ // The handler tries to find the access token.
74
69
$accessToken = $this->repository->findOneByValue($token);
70
+
71
+ // If no access token is found or if it expired, the handler throws and exception.
75
72
if ($accessToken === null || !$accessToken->isValid()) {
76
73
throw new BadCredentialsException('Invalid credentials.');
77
74
}
78
75
76
+ // The access token is valid, the handler returned the associated user ID.
79
77
return $accessToken->getUserId();
80
78
}
81
79
}
82
80
83
81
.. caution ::
84
82
85
83
It is important to check the token is valid.
86
- For instance, in the example we verify the token has not expired.
84
+ For instance, in the example we call the method ``isValid() `` that should verify
85
+ the token has not expired.
87
86
With self-contained access tokens such as JWT, the handler is required to
88
87
verify the digital signature and understand all claims,
89
88
especially `iat `, `nbf ` and `exp `.
90
89
90
+ 3) Access Token Extractors
91
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
92
+
93
+ By default, this authenticator will try to extract the access token from the
94
+ request header parameter ``Authorization ``.
95
+ You can change the token extractors depending on your needs.
96
+
97
+ Three extractors are provided. They fulfil with the RFC6750 sections 2.1 to 2.3.
98
+
99
+ * ``security.access_token_extractor.header ``: extracted from the ``Authorization `` request header parameter
100
+ * ``security.access_token_extractor.query_string ``: extracted from the ``access_token `` query string
101
+ * ``security.access_token_extractor.request_body ``: extracted from the ``access_token `` request body
102
+
103
+ In the following example, we enable all extractors.
104
+
105
+ .. caution ::
106
+ Please note that the order is important: extractors are called recursively.
107
+
108
+ .. configuration-block ::
109
+
110
+ .. code-block :: yaml
111
+
112
+ # config/packages/security.yaml
113
+ security :
114
+ firewalls :
115
+ main :
116
+ pattern : ^/
117
+ access_token :
118
+ token_handler : App\Security\AccessTokenHandler
119
+ token_extractors :
120
+ - security.access_token_extractor.header
121
+ - security.access_token_extractor.query_string
122
+ - security.access_token_extractor.request_body
123
+
124
+
91
125
Important Security Considerations
92
126
---------------------------------
93
127
94
128
Because of the security weaknesses associated with the URI method,
95
129
including the high likelihood that the URL containing the access token will be logged,
96
- it **SHOULD NOT ** be used unless it is impossible to transport the access token in the
130
+ ``security.access_token_extractor.query_string `` and ``security.access_token_extractor.request_body ``
131
+ **SHOULD NOT ** be used unless it is impossible to transport the access token in the
97
132
request header field or the HTTP request entity-body.
98
133
99
134
The request body method **SHOULD NOT ** be used except in application contexts
100
135
where participating browsers do not have access to the "Authorization" request header field.
101
136
102
- In other words: ``query_token `` and ``body_token` authenticators are not recommended.
103
-
104
137
Customizing the Authenticators
105
138
------------------------------
106
139
140
+ You can define a specific extractor by creating a service that implements the interface
141
+ :class: ``Symfony\C omponent\S ecurity\H ttp\A uthenticator\A ccessToken\A ccessTokenExtractorInterface` `::.
142
+ This interface has a unique method ``extractToken(Request $request): ?string `` and shall return the access token
143
+ if any or ``null `` otherwise.
144
+
145
+ .. configuration-block ::
146
+
147
+ .. code-block :: php
148
+
149
+ // src/Security/CustomExtractor.php
150
+ namespace App\Security;
151
+
152
+ use Symfony\Component\Security\Http\Authenticator\AccessToken\AccessTokenExtractorInterface;
153
+
154
+ class CustomExtractor implements AccessTokenExtractorInterface
155
+ {
156
+ public function extractToken(Request $request): ?string
157
+ {
158
+ ... // Extract the token from the request object or return null
159
+ }
160
+ }
107
161
108
162
109
163
Customizing the Success Handler
110
164
-------------------------------
111
165
112
- Sometimes, the default success handling does not fit your use-case (e.g.
113
- when you need to generate and return additional response header parameters).
166
+ Sometimes, the default success handling does not fit your use-case
167
+ (e.g. when you need to generate and return additional response header parameters).
114
168
To customize how the success handler behaves, create your own handler as a class that implements
115
169
:class: `Symfony\\ Component\\ Security\\ Http\\ Authentication\\ AuthenticationSuccessHandlerInterface `::
116
170
@@ -143,7 +197,7 @@ Then, configure this service ID as the ``success_handler``:
143
197
security :
144
198
firewalls :
145
199
main :
146
- header_token :
200
+ access_token :
147
201
token_handler : App\Security\AccessTokenHandler
148
202
success_handler : App\Security\Authentication\AuthenticationSuccessHandler
149
203
0 commit comments