API Design: HTTP Basic Authentication vs API Token

前端 未结 4 1524
走了就别回头了
走了就别回头了 2021-01-29 22:37

I\'m currently creating an authentication system on front of a public web API for a web application. Given that each user account has an API key and each request must be authent

相关标签:
4条回答
  • 2021-01-29 22:52

    I would prefer using the token solution. If you don't have actual users with their own username and password, then it feels like you are using the Basic Auth construct not as intended. Not that that's necessarily wrong, but not as clean, IMO. It also removes the need to use custom headers and I think it makes implementation on both sides easier and cleaner. The next question I would be asking is if you should be using two-factor authentication or if you need to manage sessions at all.

    0 讨论(0)
  • 2021-01-29 22:55

    I think that HTTP Basic Auth should be OK but just for really simple needs.

    The complete (and final) solution IMHO is to implement an OAuth provider. It's not complex, it's a simple protocol and gives you lots of flexibility. In addition it seems to be the current trend as many big players implement it and it's supported from many many libraries.

    0 讨论(0)
  • 2021-01-29 22:57

    Many times I had to think about how to authenticate users/requests onto APIs and after comparing more solutions I ended up with using the Amazon's solution where I don't need or I can't use OAuth. This solution is based on signatures that prevents from "man in the middle" problems as Basic Auth and passing a simple token are sending plain text data. Yes you can add ssl but this will add complexity to the system...

    0 讨论(0)
  • 2021-01-29 23:09

    Best bet might be using an API key in the header (e.g. 'Authorization: Token MY_API_KEY') instead of as a url param:

    Advantages over HTTP Basic Auth:

    • More convenient, as you can easily expire or regenerate tokens without affecting the user's account password.
    • If compromised, vulnerability limited to API, not the user's master account
    • You can have multiple keys per account (e.g. users can have "test" and "production" keys side by side.)

    Advantages over API key in URL:

    • Provides extra measure of security by preventing users from inadvertently sharing URLs with their credentials embedded in them. (Also, URL can wind up in things like server logs)
    0 讨论(0)
提交回复
热议问题