Probar la autenticación por token
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login Subscribe¿Qué tal una prueba como ésta... pero en la que iniciamos sesión con una clave API? Creemos un nuevo método: función pública testPostToCreateTreasureWithApiKey():
| // ... lines 1 - 10 | |
| class DragonTreasureResourceTest extends ApiTestCase | |
| { | |
| // ... lines 13 - 61 | |
| public function testPostToCreateTreasureWithApiKey(): void | |
| { | |
| // ... lines 64 - 70 | |
| } | |
| } |
Esto empezará más o menos igual que antes. Copiaré la parte superior de la prueba anterior, quitaré el actingAs()... y añadiré un dump() cerca de la parte inferior:
| // ... lines 1 - 10 | |
| class DragonTreasureResourceTest extends ApiTestCase | |
| { | |
| // ... lines 13 - 61 | |
| public function testPostToCreateTreasureWithApiKey(): void | |
| { | |
| $this->browser() | |
| ->post('/api/treasures', [ | |
| 'json' => [], | |
| ]) | |
| ->dump() | |
| ->assertStatus(422) | |
| ; | |
| } | |
| } |
Así, como antes, estamos enviando datos no válidos y esperamos un código de estado 422.
Copia ese nombre de método, luego gira y ejecuta sólo esta prueba:
symfony php bin/phpunit --filter=testPostToCreateTreasureWithApiKey
Y... ninguna sorpresa: obtenemos un código de estado 401 porque no estamos autenticados.
Enviemos una cabecera Authorization, pero una no válida para empezar. Pasa una claveheaders configurada en una matriz con Authorization y luego la palabra Bearer y luego... foo.
Esto debería seguir fallando:
symfony php bin/phpunit --filter=testPostToCreateTreasureWithApiKey
Y... ¡lo hace! Pero con un mensaje de error diferente: invalid_token. ¡Qué bien!
Utilizar un código real
Para pasar un token real, tenemos que introducir un token real en la base de datos. Hazlo con $token = ApiTokenFactory::createOne():
| // ... lines 1 - 12 | |
| class DragonTreasureResourceTest extends ApiTestCase | |
| { | |
| // ... lines 15 - 63 | |
| public function testPostToCreateTreasureWithApiKey(): void | |
| { | |
| $token = ApiTokenFactory::createOne([ | |
| // ... line 67 | |
| ]); | |
| // ... lines 69 - 79 | |
| } | |
| } |
¿Necesitamos controlar algún campo de esto? En realidad sí. Abre DragonTreasure. Si nos desplazamos hacia arriba, la operación Post requiere ROLE_TREASURE_CREATE:
| // ... lines 1 - 27 | |
| ( | |
| // ... lines 29 - 30 | |
| operations: [ | |
| // ... lines 32 - 37 | |
| new Post( | |
| security: 'is_granted("ROLE_TREASURE_CREATE")', | |
| ), | |
| // ... lines 41 - 49 | |
| ], | |
| // ... lines 51 - 64 | |
| ) | |
| // ... lines 66 - 83 | |
| class DragonTreasure | |
| { | |
| // ... lines 86 - 243 | |
| } |
Cuando nos autenticamos a través del formulario de acceso, gracias a role_hierarchy, siempre tenemos eso. Pero cuando utilizamos una clave API, para obtener ese rol, el token necesita el ámbito correspondiente.
Para asegurarnos de que lo tenemos, en la prueba, establece la propiedad scopes enApiToken::SCOPE_TREASURE_CREATE:
| // ... lines 1 - 4 | |
| use App\Entity\ApiToken; | |
| // ... lines 6 - 12 | |
| class DragonTreasureResourceTest extends ApiTestCase | |
| { | |
| // ... lines 15 - 63 | |
| public function testPostToCreateTreasureWithApiKey(): void | |
| { | |
| $token = ApiTokenFactory::createOne([ | |
| 'scopes' => [ApiToken::SCOPE_TREASURE_CREATE] | |
| ]); | |
| // ... lines 69 - 79 | |
| } | |
| } |
Ahora pasa esto a la cabecera: $token->getToken(). Ah... y déjame arreglarscopes: que debería ser una matriz:
| // ... lines 1 - 12 | |
| class DragonTreasureResourceTest extends ApiTestCase | |
| { | |
| // ... lines 15 - 63 | |
| public function testPostToCreateTreasureWithApiKey(): void | |
| { | |
| $token = ApiTokenFactory::createOne([ | |
| 'scopes' => [ApiToken::SCOPE_TREASURE_CREATE] | |
| ]); | |
| // ... line 69 | |
| $this->browser() | |
| ->post('/api/treasures', [ | |
| // ... line 72 | |
| 'headers' => [ | |
| 'Authorization' => 'Bearer '.$token->getToken() | |
| ] | |
| ]) | |
| // ... lines 77 - 78 | |
| ; | |
| } | |
| } |
¡Creo que ya estamos listos! Ejecuta la prueba:
symfony php bin/phpunit --filter=testPostToCreateTreasureWithApiKey
Y... ¡ya está! ¡Vemos los bonitos 422 errores de validación!
Probar un token con un alcance incorrecto
Hagamos una prueba para asegurarnos de que no tenemos acceso si a nuestro token le falta este ámbito. Copia todo el método de prueba... y pégalo a continuación. LlámalotestPostToCreateTreasureDeniedWithoutScope().
Esta vez, cambia scopes por otra cosa, como SCOPE_TREASURE_EDIT. A continuación, ahora esperamos un código de estado 403:
| // ... lines 1 - 12 | |
| class DragonTreasureResourceTest extends ApiTestCase | |
| { | |
| // ... lines 15 - 80 | |
| public function testPostToCreateTreasureDeniedWithoutScope(): void | |
| { | |
| $token = ApiTokenFactory::createOne([ | |
| 'scopes' => [ApiToken::SCOPE_TREASURE_EDIT] | |
| ]); | |
| $this->browser() | |
| ->post('/api/treasures', [ | |
| 'json' => [], | |
| 'headers' => [ | |
| 'Authorization' => 'Bearer '.$token->getToken() | |
| ] | |
| ]) | |
| ->assertStatus(403) | |
| ; | |
| } | |
| } |
Esta vez, vamos a ejecutar todas las pruebas:
symfony php bin/phpunit
Y... ¡todo verde! Un 422 y luego un 403. Ve a eliminar los volcados de ambos puntos.
Por cierto, si utilizas mucho los tokens de la API en tus pruebas, pasar la cabecera Authorizationpuede resultar molesto. Browser tiene una forma en la que podemos crear un objeto Browser personalizado con métodos personalizados. Por ejemplo, podrías añadir un método authWithToken(), pasar un array de ámbitos, y entonces crearía ese token y lo pondría en la cabecera
$this->browser()
->authWithToken([ApiToken::SCOPE_TREASURE_CREATE])
// ...
;
Esto no funciona en absoluto ahora mismo, pero consulta la documentación de Browser para aprender cómo hacerlo.
Siguiente: en la API Platform 3.1, el comportamiento de la operación PUT está cambiando. Hablemos de cómo, y de lo que tenemos que hacer en nuestro código para prepararnos para ello.
14 Comments
For those who need it, I always got a 415 response and in the error document that I created, it appeared
{ "@context": "/api/contexts/Error", "@type": "hydra:Error", "hydra:title": "An error occurred", "hydra:description": "The content-type \"application/json\" is not supported. Supported MIME types are \"application/ld+json\".", .....more ....I had to add the Content-Type in the headers to fix it and continue with the flow of the course.
Hey @Rodrypaladin ,
I see in this chapter we allow
application/jsoncontent type as well along withapplication/ld+json. Probably your config is different? Anyway, thanks for this tip!Cheers!
I don't know if my configuration is different. I only know that from the very first moment following the course, in the Patch applications I have had to incorporate it if necessary.
The important thing is that I was able to solve it by my own means.
Hey @Rodrypaladin ,
Good catch on solving this yourself 👍
Cheers!
I'm following this awesome tutorial using Symfony 7.2 ;)
I'm getting the following error on the second test (testPostToCreateTreasure):
Naturally, the rest of the tests then fail because Entity Manager is closed after the afore mentioned failure.
Been Googling for a while now, and I can't seem to find anything that relates to the issue I'm having.
I'm guessing I need to clear the Entity Manager... I'm just not exactly sure how to do that correctly from inside a test case.
Everything I've tried so far doesn't work... so I'm hoping someone maybe has a solution to this minor headache ^_^
Appreciate the help... and keep up the good work :D
Hey @NinjaScareCrow ,
Hm, if your database is truncated between tests but somehow the entity manager isn't cleared, Doctrine might still hold references to old entities in memory. You can try to clear the entity manager manually in the beginning of your test, or in the
setUp()method called before each test, something like this:I wonder if it helps with that test failure.
Cheers!
Hey Victor
Unfortunately that didn't work :(
I had to adapt it slightly as
self::$containerdoesn't exist.I'm still getting the same error on the second test when I rull all the tests using symfony php bin/phpunit.
I even tried getting the entity manager and clearing it at the beginning of the test cases, but that also didn't work.
Any other ideas? ;)
Hey @NinjaScareCrow
How are you restoring your database after each test? Are you using the dama bundle?
Cheers!
So, my app has two firewalls configured. One for the API called "api" (using LexikJWTAuthenticationBundle) and one for the Web-Admin-Backend ("main") using "knpuniversity/oauth2-client-bundle" in combination with "stevenmaguire/oauth2-keycloak".
That means, that different from Chtioui's config, the API-Tokens are created externally, meaning that in my TestClass inside the setUp() function I do the following:
And in the test function that test the secured API-Endpoint, I can do the following:
But now I also wanted to test a web-route, but that somehow is not possible.
If I try:
I get a 500 response, an in the dump from the template it says:
The profileUrl is set in the KeycloakAuthenticator, in which a PostAuthenticationToken is set, which is why I also create one of these in the setup cuntion with the Admin-User I created in the fixtures.
Then I try to do the following:
But that does not seem to work, because my app still thinks, that I am logged out.
Any ideas on what I am doing wrong?
Thanks in advance.
Hey @TristanoMilano!
Sorry for my super slow reply! Hmm, this is tricky. Here's what I can tell you:
->actingAs()method from browser is a wrapper around the->loginUser()method of Symfony - https://github.com/symfony/symfony/blob/9e810dea50cb47deb8500ad7ffb56d5a5622b46e/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php#L103TestBrowserTokenobject - https://github.com/symfony/symfony/blob/9e810dea50cb47deb8500ad7ffb56d5a5622b46e/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php#L113So, it may be that simple. Your user is getting logged in, but instead of a
PostAuthenticationTokenwith thisprofileUrlset, it's aTestBrowserTokenwith no attribute set.If that's true, you may need to write your own fake authentication logic in your test that mimics the
loginUser()method from Symfony... but with your token.Let me know if that helps!
Cheers!
Hello symfonycasts Team !
i'm using LexikJWTAuthenticationBundle to handle token authentication everything works fine until I tried to update a user's login credentials (password & email) with the PATCH operation the update request succedeed but i've got token this error : "Invalid credentials" after finishing updating the user I want to understand something here do we need to login again the user after updating his login credentials or therre is any other solution to generate a new token after the user updates his login credentials?
Cheers!
Hey @Chtioui!
Hmm very interesting! So, you're using LexikJWTAuthenticationBundle with "stateless" authentication is that correct? What I mean is: you send the JWT with every API request, right? You do not rely on just "logging in once" and then using the session to authenticate you on future request. Let me know if that assumption is incorrect :).
Anyways, if you ARE using "stateless" authentication, then I have no idea what's happening yet :P.
But if you ARE relying on session authentication, then I still don't totally know what's happening, but I can offer some info. On each request, Symfony attempts to load the
Userobject from the session. It then checks to see if some of the important fields on thatUserobject (the one that was stored in the session) have since changed in the database - for example, if the password has been changed. If any of these fields have changed, it does NOT load yourUser(i.e. it effectively logs you out). This is a security mechanism so that if you change your password on one computer because someone hacked your account, it will log ALL devices out of your account immediately. The logic for this is here: https://github.com/symfony/symfony/blob/0eb03203c800b11bac4496a3e84c75e2966d5507/src/Symfony/Component/Security/Http/Firewall/ContextListener.php#L281-L321However, normally, if you change your password on a request, then at the end of that request, when Symfony serializes the
Userinto the session, it will serialize theUserobject that contains the NEW hashed password. And so, in the next request, everything will work fine.So, something feels weird to me. Can you tell me a bit more about your situation - are you using stateless auth or session-based auth? When exactly do you get the "Invalid credentials" error - is that on the NEXT request? Are you sending a JWT on that? What does your JWT contain?
Cheers!
Hello @weaverryan,
-Yes i'm using stateless authentication with the LexikJWTAuthenticationBundle and i'm using the user's username and password for generating the JWT token.
-I get the "Invalid credentials" error on the next request and the user is no longer authenticated in my web app.
-I think whats happening is normal because the user is updating his login credentials (username & password in my case) that's why i'm getting the error "Invalid credentials" with the status 401 also I get this error only when update the user's login credentials but if I update other properties of the user's class everything works fine and I got no errors from the api response.
-I want to know if there is any mechanism to integrate to solve the problem or do I need to authenticate again the user after he updates one of his login credentials.
*firewalls in security.yaml:
security firewall
Lexik bundle file configuration:
Lexic bundle configuration file
Cheers!
Hey @Chtioui!
Hmm. If the user changes their username, it DOES make sense that you would lose authentication since the
usernameis what's added to the JWT... and then thatusernameis read from the JWT to find the user. So if you change the username, that user won't be found. To fix that, you could change your JWT to use theidinstead, which is probably safer anyways. I'm not sure exactly how you're supposed to do this - but the https://symfony.com/bundles/LexikJWTAuthenticationBundle/current/2-data-customization.html seems to be close.I don't understand why changing the
passwordis also causing problems - but you did mention that:So perhaps you're adding and using the password in the token in some manual way already.
Anyways, to fix the issue, you'll need to either:
A) Re-authenticate after this (as you know is already possible)
or
B) Somehow send back a fresh JWT from the user endpoint where you update the email/password. I'm not sure how standard this is... but in theory, you could register an event listener that could add a custom response header - e.g.
X-JWTto this endpoint. It's a non-trivial problem. You might, insideUser::setPlainPassword()andsetUsername()set some flag on yourUserlike$this->needsNewJWT = true. Then register aResponseListenerand look for that (you can get theUserobject via$request->attributes->get('data').I hope this gives you some hints :)
Cheers!
"Houston: no signs of life"
Start the conversation!