Token Types & The ApiToken Entity
Okay, so what if you need to allow programmatic access to your API?
The Types of Access Tokens
When you talk to an API via code, you send an API token, commonly known as an access token:
fetch('/api/kittens', {
headers: {
'Authorization': 'Bearer THE-ACCESS-TOKEN',
}
});
Exactly how you get that token will vary. But there are two main cases.
First, as a user on the site, like a dragon, you want to generate an API token so that you can personally use it in a script you're writing. This is like a GitHub personal access token. These are literally created via a web interface. We're going to show this.
The second main use case is when a third party wants to make requests to your API on behalf of a user of your system. Like some new site called DragonTreasureOrganizer.com wants to be able to make an API request to our API on behalf of some of our users - like it will fetch the treasure's for a user and display them artfully on their site. In this situation, instead of our users generating tokens manually and then... like... entering them into that site, you'll offer OAuth. OAuth is basically a mechanism for normal users to securely give access tokens for their account to a third party. And so, your site, or somewhere in your infrastructure you'll have an OAuth server.
That's beyond the scope of this tutorial. But the important thing is that after OAuth is done, the API client wll end up with, you guessed it, an API token! So no matter which journey you're in, if you're doing programmatic access, your API users will end up with an access token. And then your job will be to read and understand that. We'll do exactly that.
JWT vs Database Storage?
So as I mentioned, we're going to show a system where we allow users to generate their own access tokens. So how do we do that? Again, there are two main ways. Death by choices!
The first is to generate something called a JSON Web Token or JWT. The cool thing about JWTs are that no database storage is needed. They're special strings that actually contain info inside of them. For example, you could create a JWT string that includes the user id and some scopes.
One downside of JWTs are that there's no easy way to "log out"... because there's no out-of-the-box way to invalidate JWTs. You give them an expiration when you create them... but then they're valid until then... no matter what, unless you add some extra complexity... which kinda defeats the purpose.
JWT's are trendy, popular and fun! But... you may not need them. They're awesome when you have a single sign-on system because, if that JWT is used to authenticate with multiple systems or APIs, each API can validate the JWT all on their own: without needing to make an API request to a central authentication system.
So you might end up using JWTs and there's a great bundle for them called LexikJWTAuthenticationBundle. JWT's are also the type of access token that OpenID gives you in the end.
Instead of JWTs, the second main option is dead simple: generate a random token string and store it in the database. This also allows you to invalidate access tokens by... just deleting them! This is what we'll do.
Generating the Entity
So let's get to work. To store API tokens, we need a new entity! Find your terminal and run:
php ./bin/console make:entity
And let's call it ApiToken. Say no to making this an API resource. In theory, you could allow users to authenticate via a login form or HTTP basic and then send a POST request to create API tokens if you want to... but we won't.
Add an ownedBy property. This is going to be a ManyToOne to User and not nullable. And I'll say "yes" to the inverse. So the idea is that every User can have many API tokens. When an API token is used, we want to know which User it's related to. We'll use that during authentication. Calling the property apiTokens is fine and say no to orphan removal. Next property: expiresAt, make that a datetime_immutable and I'll say yes to nullable. Maybe we allow tokens to never expire by leaving this field blank. Next up is token, which will be a string. I'm going to set the length to 68 - we'll see why in a minute - not nullable. And finally, add a scopes property as a json type. This is going to be kind of cool: we'll store an array of "permissions" that this API token should have. Say, not nullable on that one as well. Hit enter to finish.
All right, spin over to your editor. No surprises: that created an ApiToken entity... and there's nothing very interesting inside of it:
| // ... lines 1 - 2 | |
| namespace App\Entity; | |
| use App\Repository\ApiTokenRepository; | |
| use Doctrine\ORM\Mapping as ORM; | |
| #[ORM\Entity(repositoryClass: ApiTokenRepository::class)] | |
| class ApiToken | |
| { | |
| #[ORM\Id] | |
| #[ORM\GeneratedValue] | |
| #[ORM\Column] | |
| private ?int $id = null; | |
| #[ORM\ManyToOne(inversedBy: 'apiTokens')] | |
| #[ORM\JoinColumn(nullable: false)] | |
| private ?User $ownedBy = null; | |
| #[ORM\Column(nullable: true)] | |
| private ?\DateTimeImmutable $expiresAt = null; | |
| #[ORM\Column(length: 68)] | |
| private string $token = null; | |
| #[ORM\Column] | |
| private array $scopes = []; | |
| // ... lines 28 - 80 | |
| } |
So let's go over and make the migration for it:
symfony console make:migration
Spin over and peek at that file to make sure it looks good. Yup! It creates the api_token table:
| // ... lines 1 - 12 | |
| final class Version20230209183006 extends AbstractMigration | |
| { | |
| public function getDescription(): string | |
| { | |
| return ''; | |
| } | |
| public function up(Schema $schema): void | |
| { | |
| // this up() migration is auto-generated, please modify it to your needs | |
| $this->addSql('CREATE SEQUENCE api_token_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); | |
| $this->addSql('CREATE TABLE api_token (id INT NOT NULL, owned_by_id INT NOT NULL, expires_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, token VARCHAR(68) NOT NULL, scopes JSON NOT NULL, PRIMARY KEY(id))'); | |
| $this->addSql('CREATE INDEX IDX_7BA2F5EB5E70BCD7 ON api_token (owned_by_id)'); | |
| $this->addSql('COMMENT ON COLUMN api_token.expires_at IS \'(DC2Type:datetime_immutable)\''); | |
| $this->addSql('ALTER TABLE api_token ADD CONSTRAINT FK_7BA2F5EB5E70BCD7 FOREIGN KEY (owned_by_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); | |
| } | |
| public function down(Schema $schema): void | |
| { | |
| // this down() migration is auto-generated, please modify it to your needs | |
| $this->addSql('CREATE SCHEMA public'); | |
| $this->addSql('DROP SEQUENCE api_token_id_seq CASCADE'); | |
| $this->addSql('ALTER TABLE api_token DROP CONSTRAINT FK_7BA2F5EB5E70BCD7'); | |
| $this->addSql('DROP TABLE api_token'); | |
| } | |
| } |
Run that with:
symfony console doctrine:migrations:migrate
And... awesome! Next: let's add a way to generate the random token string. Then, we'll talk about scopes and load up our fixtures with some API tokens.
12 Comments
I understand that one can always argue that things here are meant as examples. But I suppose that people actually implement things as you show here. The problem is that this way of storing API tokens is basically the same as storing passwords in clear text.
While this is an easy solution for demonstration, I strongly suggest to not do it this way on a production system.
It would be better to generate an access token consisting of two parts:
You would then need to store that hashed secret in the token entity as well of course.
For example, connect the parts with a dot so that it is easy to parse the token.
You can then fetch the token from the database using the identifier and do a „password verification“ using the secret.
I think it is not too complicated to add this. But way more secure.
Of course you need to tell the user that they must immediately save the token somewhere because you can not show it to the user again for security (and because you don’t know the secret in clear text anymore). But I saw this already on other services. So it should be fine.
Or maybe use something like a url signer (which is probably faster).
But however.. do not save API tokens in clear text.
Hey @MatthiasN!
I think this is really good advice. My intention was to show people that "you can just store tokens in the database". And while that's true (just like how you can "store passwords in a database"), in both cases it shouldn't mean storing in plaintext. So yes, this is definitely a better idea.
Anyway, thank you for posting this - good advice! I'll also add a note to the tutorial about it.
Oh, and let me add some more technical details here to help people, building on what you said:
So, for example, you'd generate 2 random strings, separated by a
.. This full string would be the API key that you show to the user.But then, you store the first part (the "locator" or "identifier") in the database on a
locatorkey ofApiToken. Then you store the second part (thesecret) on another property ofApiToken- but HASH it first. For example, here is how we generate the random locator and hash the secret in the ResetPasswordBundle - https://github.com/SymfonyCasts/reset-password-bundle/blob/main/src/Generator/ResetPasswordTokenGenerator.php#L52-L65Then, when verify if a token is valid, you split the token on
.to get the first and second part. You then query for theApiTokenvia the first part (thelocator). If found, you has the 2nd part that was just sent you to you and see if it equals the hashedsecreton theApiTokenyou just found usinghash_equals- e.g. https://github.com/SymfonyCasts/reset-password-bundle/blob/3e0bb051a514459a3c63a5064be40208e4f34783/src/ResetPasswordHelper.php#L126If anyone has any questions, let me know :)
Cheers!
Hello!
Is it possible to have 2 types of tokens in the same app, in API Platform 3 :
a jwt token for the users: this is a 'user specific' token. A user gets a jwt token from the server after successful authentication with his email and password.
an 'api token' that would be used by some cron jobs. This api token would be a way to authenticate the cron job. This token would be a 'company' specific token, not a user token, as the cron job is run for a company not for a user. The api token would be either in the URL of the end point (a string), either (better) in the headers of the request run by the cron job. And basically the api token would not change over time: it is a string, specific to a company, and it never expires, so no need for any refresh token.
So can we have simultaneously 2 differents mechanisms of authentication in the same app : classical jwt/refresh token for the users and api tokens for cron jobs (one api token per company) ?
Thanks!
Hey @Marko!
Sure, I think you could definitely do this :). The easy part is creating the 2 tokens. You can make a JWT after the user authenticates successfully. And you could, for example, add an
apiTokenproperty to someCompanyentity and allow an admin for aCompanyto see this in some area on your site (where they could then copy and paste this for their CRON jobs).The trickier part is consuming the tokens, well, maybe not SO tricky I hope :). You could have one authenticator that handles the JWT. This is your normal situation. Then, you could create a 2nd custom authenticator that handles your company token. If these tokens are sent in different ways (e.g.
Authorizationheader for JWT vs some other header for company token), that'll make your job a little easier because each authenticator can figure out "is this a token I should authenticate or is it the other type?". But you could also accomplish this by giving each token some prefix - e.g.company_- and using that.Anyway, I think the truly tricky part is this: when using a JWT, you are authenticating as a
User. So, aUserobject is what you would return from your authenticator... and then anywhere in your code where you say$this->getUser(), it would be aUserobject. But for the company token, your authenticator would return aCompany, and when you use$this->getUser()in your code, that will return aCompanyobject. It's THAT difference that could cause some confusion in your code.I can see two general ideas for solving this:
1) If the company token is only meant to be used for a few specific endpoints, then I might put those few endpoints under their own Symfony firewall. Then, in those endpoints, just be sure that if you're ever referencing the currently authenticated "user", that you know it's the Company.
2) Code carefully :). If you need to be able to support both a
Useror aCompanyacross your entire application, then be sure to create smart security rules that do no depend on the specificUser/Companyobject. I can give more tips about this if you are going this route and have some questions about how to do some specific things.Let me know if this helps!
Cheers!
Hello! Thanks a lot for your answer. One more question : do you confirm that Company should implement User Interface, as a authenticated 'user'? Or not. Thanks again!
Yes it should!
I'm curious in this example, why you have potentially one user - many tokens relationship?
Wouldn't one user - one token suffice ?
Hey @MildDisaster ,
That's how GitHub access tokens, you can create many tokens on GitHub for your account. First of all, you can create different tokens with different scopes, like some tokens may have access to the private repos while others - only public ones. That's the most common and flexible structure I think :) Moreover, you can use one token e.g. for Composer and another token for your website that sends API requests to GitHub. And it's convenient this way, as you can e.g. remove that second token but the first one will still work.
But of course it depends on your personal use case, and if you're happy with OneToOne relation - go for it, there's nothing wrong with it, just some limitations I mentioned above :)
Cheers!
I think I understand. So the relationship is more (but not exclusively) between tokens and apps ( that a user will use to access api with ) ?
What is the preferred way to handle tokens once an associated account is changed, ie (password recovered) ?
Should one invalidate existing tokens, or just let them be ?
I noticed in the symfonycasts github there are projects for account registration and password recovery. Without double checking, don't think there was mention of token cleanup in them.
Thanks !
Hey @MildDisaster ,
Mostly yes, I suppose, but once again, it depends on your specific use case. But from my experience it seems more like this :)
Good question. I think it also depends on your specific use case and your strategy. I don't think GitHub invalidate all tokens if you changed password... but in some cases this might be a good idea. It depends on how much your users will be annoyed by it :)
Yeah, we support those bundles. And yeah, probably it's a good use case when you have to clean up password reset tokens on password change :)
Cheers!
How to authenticate jwt token string (First Way to make token). I have done react login page. Successfully logged in, But not getting Authenticated.
Hey @Someswara-rao-J!
Hmm. Did you create an access token authenticator like we did? https://symfonycasts.com/screencast/api-platform-security/access-token-authenticator
If so, here is what I would do to debug:
A) After making the AJAX request to "log in", find the Symfony profiler for that request and open it (reminder: you can always go to
/_profilerto see a list of the most recent requests to your site - and you can find the AJAX request you just made there. When you get to the profiler for that request, click on the "Security" tab. Does it show you as authenticated?B) If it does NOT show you as authenticated, then look more closely at your access token authenticator system: make sure your
ApiTokenHandleris being called, etc. If it DOES show that you are authenticated... but then future AJAX requests appear to be not authenticated, it means that you are "losing" authentication between requests.There are a few things that could cause this:
1) If you have
stateless: trueon your firewall, this will happen. This is because this tells your firewall to NOT use session/cookie storage.2) If your frontend and backend are on different subdomains this could happen. For example, if your API is api.example.com and your frontend is just frontend.example.com, by default, Symfony will create a cookie for api.example.com and (iirc) that will not be usable by your frontend. You can adjust your cookie domain in the Symfony settings if this is the cause
3) In some situations, if you've added some custom serialization logic to your
Userclass, you can "lose" authentication on the 2nd request. If you check the logs on the first request after the successful login, you should see something like:Let me know if this helps!
"Houston: no signs of life"
Start the conversation!