I need to develop a Rest API on my app (Based on Flask)
But I don't really know how I should secure it.
Currently, I have a normal authentication for users who are coming from a browser. (Using the session etc.)
But for the API users, should I ask the username/password at every API request ? Is it really secured ? I know than a lot of web API use tokens for API calls, is it a best way ?
And in this case, how to implement it ? (This is not really my field of expertise..) Thanks a lot
Integrate the Security Library The code for a working OAuth secured Python Flask API is provided below: The OAuth filter is configured to run before API requests. The filter verifies the token signature and the expected issuer / audience claims. API routes can then access JWT claims in the request object.
Install packages using pip Now it's time to install packages such as the flask framework and PyJWT which we will use to build the rest API and other necessary packages for our API project. Create a requirements. txt file with the following packages. Install them with pip.
Lastly, Flask also has extensive documentation that address everything that developers need to start. Being lightweight, easy to adopt, well-documented, and popular, Flask is a very good option for developing RESTful APIs.
You should use token based authentication
technique to secure your API
, the concept is simple once your user signs in, your site should save it somewhere and you send back that token to your user.
For each call to your API, user should send token with every API request and you should validate the encoded toke and either deny or send back the response.
Have a look here: https://realpython.com/blog/python/token-based-authentication-with-flask/
Check this too http://flask-jwt-extended.readthedocs.io/en/latest/
For better performance, you can store your session tokens in a NOSQL
database like Redis
.
To support logins with social media sites, you should use OAuth
which is working in the same way except it send back a couple of more tokens to the client.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With