So I’ve had this idea for an API for a while but the problem I keep coming back to is authentication. I’m using rocket to actually code it. I looked through the rocket docs and it looks like the closest thing to API key authentication it has are cookies.

I then went and looked at some other APIs to see if I can copy their layouts and it looks like a lot of them use an API key and then a secret API key for authentication. Did some more googling and stackoverflow said that it’s more secure to use a pair like that.

So that leaves me with the actual question: how do you actually implement this feature? Do you just generate API keys and throw them a database to be looked up later? Should they be written/read to a file to be used later(probably not a good option I’d guess).

Just for reference I’m using rocket, sqlx and postgres.

  • twice_hatch@midwest.social
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 day ago

    Why is the secret key on the server side? Is this for the server to trust the client, or the client to trust the server?

    • solrize@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      23 hours ago

      It’s so the server can generate any of the API keys as needed, instead of having to store them all. This matters more when you want to do the authentication on a low resource device like an HSM, or otherwise keep the authentication process away from the main server app, to lower the attack surface. Again, depending on the application, it might not be worth it.