Securing your API with OAuth2
In web applications, securing endpoints from unauthorized users is crucial. OAuth2 is a common authorization framework that enables applications to be accessed by user accounts with restricted permissions. It works by issuing tokens instead of credentials. This recipe will show how to use OAuth2 in our Task Manager API to protect endpoints.
Getting ready…
FastAPI provides support for OAuth2 with a password, including the use of external tokens. Data compliance regulations require that passwords are not stored in plain text. Instead, a usual method is to store the outcome of the hashing operation, which changes the plain text into a string that is not readable by humans and cannot be reversed.
Important note
With the only purpose of showing the functionality, we will fake the hashing mechanism as well the token creation with trivial ones. For obvious security reasons, do not use it in a production environment.