Giter Club home page Giter Club logo

flask-jwt's Introduction

Flask-JWT-Extended

Flask Python JWT

Description

This project demonstrates a simple implementation of JWT (JSON Web Token) authentication using Flask and Flask-JWT-Extended. It includes user authentication, token generation, and protected routes.

Prerequisites

  • Python 3.x
  • Flask 3.0.0 Flask
  • Flask-JWT-Extended 4.5.3 JWT
  • Flask-SQLAlchemy
  • Flask-Marshmallow
  • MySQL database

Installation

  1. Clone the repository:

    git clone https://github.com/MartinCantillo/Flask-jwt 
  2. Install dependencies:

    pip install -r requirements.txt
  3. Set up the MySQL database. Update the app.config['SQLALCHEMY_DATABASE_URI'] in app.py with your database connection details.

  4. Run the application:

    python app.py

Configuration

  • MySQL Database: Update the app.config['SQLALCHEMY_DATABASE_URI'] in app.py with your database connection details.

  • JWT Configuration: Update the app.config["JWT_SECRET_KEY"] in app.py with a secure secret key.

Usage

  1. Login without Cookies:

    POST /login_without_cookies

    Authenticate and receive a JWT token.

  2. Login with Cookies:

    POST /login_with_cookies

    Authenticate and receive a JWT token, which is also stored in the cookies.

  3. Logout with Cookies:

    POST /logout_with_cookies

    Log out and remove the JWT token from cookies.

  4. Protected Route:

    GET /protected

    Access a protected route by providing a valid JWT token (can be in headers, cookies, JSON, or query string).

  5. Protected Route with Headers:

    GET /only_headers

    Access a protected route by providing a valid JWT token only in headers.

Models

  • Role Model:

    • id_rol: Integer, primary key
    • nombre_rol: String(50)
  • User Model:

    • id: Integer, primary key
    • username: Text, unique
    • full_name: String(50)
    • password: String(50)
    • rol_id: Integer, foreign key referencing tblRol.id_rol

Schemas

  • Role Schema:

    • id_rol
    • nombre_rol
  • User Schema:

    • id
    • username
    • full_name
    • password
    • rol_id
    • rol

Routes

  • /login_without_cookies: Authenticate without storing the token in cookies.
  • /login_with_cookies: Authenticate and store the token in cookies.
  • /logout_with_cookies: Log out and remove the token from cookies.
  • /protected: Access a protected route (token can be in headers, cookies, JSON, or query string).
  • /only_headers: Access a protected route with the token only in headers.

Additional Notes

  • Ensure that your MySQL server is running and the database is created.

  • Customize the JWT secret key and database URI according to your security requirements.

  • This project is intended as a basic demonstration and may need further enhancements for a production environment.

License

This project is licensed under the MIT License - see the LICENSE file for details.

flask-jwt's People

Contributors

martincantillo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.