How to secure a REST Api on flask

24,058

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.

Share:
24,058
Pusheen_the_dev
Author by

Pusheen_the_dev

Very interested in neural-nets and generally in machine learning. I know : Python-2 | Python-3 | C | C++ | PHP5

Updated on July 05, 2022

Comments

  • Pusheen_the_dev
    Pusheen_the_dev almost 2 years

    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