You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: admin/authentication-support.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,23 @@
1
1
# Authentication Support
2
2
3
3
Authentication can easily be handled when using the API Platform's admin library.
4
-
In the following section, we will assume [the API is secured using JWT](https://api-platform.com/docs/core/jwt), but the
5
-
process is similar for other authentication mechanisms. The `login_uri` is the full URI to the route specified by the `firewalls.login.json_login.check_path` config in the [JWT documentation](https://api-platform.com/docs/core/jwt).
4
+
In the following section, we will assume [the API is secured using JWT](../core/jwt.md), but the
5
+
process is similar for other authentication mechanisms. The `authenticationTokenUri` is the full URI to the path / route specified by the `firewalls.{name}.json_login.check_path` config in the [JWT documentation](../core/jwt.md).
6
6
7
7
The first step is to create a client to handle the authentication process:
Copy file name to clipboardExpand all lines: core/filters.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -975,7 +975,7 @@ A constant score query filter is basically a class implementing the `ApiPlatform
975
975
and the `ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Filter\FilterInterface`. API Platform includes a convenient
976
976
abstract class implementing this last interface and providing utility methods: `ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Filter\AbstractFilter`.
977
977
978
-
Suppose you want to use the [match filter](https://api-platform.com/docs/core/filters/#match-filter) on a property named `$fullName` and you want to add the [and operator](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-boolean) to your query:
978
+
Suppose you want to use the [match filter](#match-filter) on a property named `$fullName` and you want to add the [and operator](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-boolean) to your query:
> [JSON Web Token (JWT)](https://jwt.io/) is a JSON-based open standard ([RFC 7519](https://tools.ietf.org/html/rfc7519)) for creating access tokens that assert some number of claims. For example, a server could generate a token that has the claim "logged in as admin" and provide that to a client. The client could then use that token to prove that he/she is logged in as admin. The tokens are signed by the server's key, so the server is able to verify that the token is legitimate. The tokens are designed to be compact, URL-safe and usable especially in web browser single sign-on (SSO) context.
API Platform allows to easily add a JWT-based authentication to your API using [LexikJWTAuthenticationBundle](https://github.com/lexik/LexikJWTAuthenticationBundle).
7
-
To install this bundle, [just follow its documentation](https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md).
8
8
9
9
## Installing LexikJWTAuthenticationBundle
10
10
11
-
`LexikJWTAuthenticationBundle` requires your application to have a properly configured user provider.
12
-
You can either use the [Doctrine user provider](https://symfony.com/doc/current/security/user_provider.html#entity-user-provider) provided
13
-
by Symfony (recommended), [create a custom user provider](https://symfony.com/doc/current/security/user_provider.html#creating-a-custom-user-provider)
14
-
or use [API Platform's FOSUserBundle integration](fosuser-bundle.md).
11
+
We begin by installing the bundle:
15
12
16
-
Here's a sample configuration using the data provider provided by FOSUserBundle:
Then we need to generate the public and private keys used for signing JWT tokens. If you're using the [API Platform distribution](../distribution/index.md), you may run this from the project's root directory:
This takes care of using the correct passphrase to encrypt the private key, and setting the correct permissions on the
29
+
keys allowing the web server to read them.
30
+
31
+
If you want the keys to be auto generated in `dev` environment, see an example in the [docker-entrypoint script of api-platform/demo](https://github.com/api-platform/demo/blob/master/api/docker/php/docker-entrypoint.sh).
32
+
33
+
The keys should not be checked in to the repository (i.e. it's in `api/.gitignore`). However, note that a JWT token could
34
+
only pass signature validation against the same pair of keys it was signed with. This is especially relevant in a production
35
+
environment, where you don't want to accidentally invalidate all your clients' tokens at every deployment.
36
+
37
+
For more information, refer to [the bundle's documentation](https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md)
38
+
or read a [general introduction to JWT here](https://jwt.io/introduction/).
39
+
40
+
We're not done yet! Let's move on to configuring the Symfony SecurityBundle for JWT authentication.
41
+
42
+
## Configuring the Symfony SecurityBundle
43
+
44
+
It is necessary to configure a user provider. You can either use the [Doctrine entity user provider](https://symfony.com/doc/current/security/user_provider.html#entity-user-provider)
45
+
provided by Symfony (recommended), [create a custom user provider](https://symfony.com/doc/current/security/user_provider.html#creating-a-custom-user-provider)
46
+
or use [API Platform's FOSUserBundle integration](fosuser-bundle.md) (not recommended).
47
+
48
+
If you choose to use the Doctrine entity user provider, start by [creating your `User` class](https://symfony.com/doc/current/security.html#a-create-your-user-class).
You must also declare the route used for `/authentication_token`:
87
+
88
+
```yaml
89
+
# api/config/routes.yaml
90
+
authentication_token:
91
+
path: /authentication_token
92
+
methods: ['POST']
93
+
```
94
+
95
+
If you want to avoid loading the `User` entity from database each time a JWT token needs to be authenticated, you may consider using
96
+
the [database-less user provider](https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/8-jwt-user-provider.md) provided by LexikJWTAuthenticationBundle. However, it means you will have to fetch the `User` entity from the database yourself as needed (probably through the Doctrine EntityManager).
53
97
98
+
Refer to the section on [Security](security.md) to learn how to control access to API resources and operations. You may
99
+
also want to [configure Swagger UI for JWT authentication](#documenting-the-authentication-mechanism-with-swaggeropen-api).
100
+
101
+
### Adding Authentication to an API Which Uses a Path Prefix
102
+
103
+
If your API uses a [path prefix](https://symfony.com/doc/current/routing/external_resources.html#prefixing-the-urls-of-imported-routes), the security configuration would look something like this instead:
## Documenting the Authentication Mechanism with Swagger/Open API
@@ -84,7 +162,7 @@ The "Authorize" button will automatically appear in Swagger UI.
84
162
### Adding a New API Key
85
163
86
164
All you have to do is configure the API key in the `value` field.
87
-
By default, [only the authorization header mode is enabled](https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#2-use-the-token) in [LexikJWTAuthenticationBundle](https://github.com/lexik/LexikJWTAuthenticationBundle).
165
+
By default, [only the authorization header mode is enabled](https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#2-use-the-token) in LexikJWTAuthenticationBundle.
88
166
You must set the [JWT token](https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#1-obtain-the-token) as below and click on the "Authorize" button.
89
167
90
168
```
@@ -93,7 +171,6 @@ Bearer MY_NEW_TOKEN
93
171
94
172

95
173
96
-
97
174
## Testing with Behat
98
175
99
176
Let's configure Behat to automatically send an `Authorization` HTTP header containing a valid JWT token when a scenario is marked with a `@login` annotation. Edit `features/bootstrap/FeatureContext.php` and add the following methods:
0 commit comments