[![Python package](https://github.com/joegasewicz/flask-jwt-router/actions/workflows/python-package.yml/badge.svg)](https://github.com/joegasewicz/flask-jwt-router/actions/workflows/python-package.yml)
[![PyPI version](https://badge.fury.io/py/flask-jwt-router.svg)](https://badge.fury.io/py/flask-jwt-router)
[![codecov](https://codecov.io/gh/joegasewicz/Flask-JWT-Router/branch/master/graph/badge.svg)](https://codecov.io/gh/joegasewicz/Flask-JWT-Router)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/c15de26af47d48448392eaa5e0e41bcf)](https://www.codacy.com/manual/joegasewicz/Flask-JWT-Router?utm_source=github.com&utm_medium=referral&utm_content=joegasewicz/Flask-JWT-Router&utm_campaign=Badge_Grade)
[![Documentation Status](https://readthedocs.org/projects/flask-jwt-router/badge/?version=latest)](https://flask-jwt-router.readthedocs.io/en/latest/?badge=latest)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/flask-jwt-router)
[![GitHub license](https://img.shields.io/github/license/joegasewicz/flask-jwt-router)](https://github.com/joegasewicz/flask-jwt-router/blob/master/LICENSE.txt)
![Flask JWT Router](assets/logo.png?raw=true "Title")
Read the docs: [Flask-JWT-Router](https://flask-jwt-router.readthedocs.io/en/latest/)
# Flask JWT Router
Flask JWT Router is a Python library that adds authorised routes to a Flask app. Both basic & Google's OAuth2.0 authentication
is supported.
## ![Google-Cloud](assets/Google-Cloud1.png) Google's OAuth2.0 supported
### Google OAuth 2.0 Quick Start
Read the detailed instructions here: [Flask-JWT-Router](https://flask-jwt-router.readthedocs.io/en/latest/google.html)
```python
oauth_options = {
"client_id": "<CLIENT_ID>",
"client_secret": "<CLIENT_SECRET>",
"redirect_uri": "http://localhost:3000",
"tablename": "users",
"email_field": "email",
"expires_in": 3600,
}
jwt_routes.init_app(app, google_oauth=oauth_options)
```
Now your front end needs a token. Create an endpoint &
return a new access token from the clients header *code*.
For Example::
```python
from flask import request
@app.routes("/login", methods=["POST"])
def login():
jwt_routes.google.oauth_login(request) # Pass in Flask's request
```
Now, the next time your front-end requests authorised resources
flask-jwt-router will authenticate with this access token until
it expires.
## Installation
Stable version
```bash
pip install flask-jwt-router
```
## Basic Usage
```python
from flask import Flask
from flask_jwt_router import JwtRoutes
app = Flask(__name__)
# You are required to always set a unique SECRET_KEY for your app
app.config["SECRET_KEY"] = "your_app_secret_key"
JwtRoutes(app)
# If you're using the Flask factory pattern:
jwt_routes = JwtRoutes() # Example with *entity_model - see below
def create_app(config):
...
jwt_routes.init_app(app)
```
## Whitelist Routes
```python
app.config["WHITE_LIST_ROUTES"] = [
("POST", "/register"),
]
@app.route("/register", methods=["POST"])
def register():
return "I don't need authorizing!"
```
## Prefix your api name to whitelisted routes
```python
# All routes will
app.config["JWT_ROUTER_API_NAME"] = "/api/v1"
app.config["WHITE_LIST_ROUTES"] = [
("POST", "/register"),
]
@app.route("/api/v1/register", methods=["POST"])
def register():
return "I don't need authorizing!"
```
## Bypass Flask-JWT-Router on specified routes
```python
# Define homepage template routes for example on JWT_IGNORE_ROUTES
# & still get to use the api name on request handlers returning resources
app.config["IGNORED_ROUTES"] = [
("GET", "/")
]
```
## Declare an entity model
```python
# Create your entity model (example uses Flask-SqlAlchemy)
class UserModel(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
# You can define the primary key name with `ENTITY_KEY` on Flask's config
app.config["ENTITY_KEY"] = "user_id"
# (`id` is used by default)
JwtRoutes(app, entity_models=[UserModel, TeacherModel, ...etc])
# Or pass later with `init_app`
def create_app(config):
...
jwt_routes.init_app(app, entity_models=[UserModel, TeacherModel, ...etc])
```
## Authorization
```python
from your_app import jwt_routes
# white list the routes
app.config["WHITE_LIST_ROUTES"] = [
("POST", "/register"),
("POST", "/login"),
]
@app.route("/login", methods=["POST"])
def register():
"""I'm registering a new user & returning a token!"""
return jsonify({
"token": jwt_routes.create_token(entity_id=1, table_name='users')
})
@app.route("/your_resource", methods=["POST"])
def login():
"""I'm authorized & updating my token!"""
return jsonify({
"token": jwt_routes.update_token(entity_id=1)
})
```
*Warning: The `table_name` must be the same as your tablename or `__tablename__` attribute's value.
(With SqlAlchemy, you can define a `__tablename__` attribute directly or else
the name is derived from your entity’s database table name).
## Setting the Token Expire Duration
There are two ways to set the expire duration of the JWT.
from your app config
```python
# Set the token expire duration to 7 days
app.config["JWT_EXPIRE_DAYS"] = 7
```
calling the `set_exp`
```python
# Set the token expire duration to 14 days
jwt_routes = JwtRoutes()
# jwt_routes.init_app( ...etc
jwt_routes.set_exp(expire_days=14)
```
By default the expire duration is set to 30 days
## Create & update Tokens on Routes
Create a new entity & return a new token
```python
@app.route("/register", methods=["POST"])
def register():
user_data = request.get_json()
try:
user = UserModel(**user_data)
user.create_user() # your entity creation logic
# Here we pass the id as a kwarg to `create_token`
token: str = jwt_routes.create_token(entity_id=user.id, table_name="users")
# Now we can return a new token!
return {
"message": "User successfully created.",
"token": str(token), # casting is optional
}, 200
```
Access entity on Flask's global context
```python
from app import app, jwt_routes
# Example uses Marshmallow to serialize entity object
class EntitySchema(Schema):
id = fields.Integer()
name = fields.String()
@app.route("/login", methods=["GET"])
def login():
user_data = g.get("users") # This is your SqlAlchemy `__tablename__` or the default name.
try:
user_dumped = UserSchema().dump(user_data)
except ValidationError as _:
return {
"error": "User requested does not exist."
}, 401
return {
"data": user_dumped,
"token": jwt_routes.update_token(entity_id=user_data.id),
}, 200
```
If you are handling a request with a token in the headers you can call::
```python
jwt_routes.update_token(entity_id=user_data.id)
```
If you are handling a request without a token in the headers you can call::
```python
jwt_routes.create_token(entity_id=user_data.id, table_name="users")
```
An Example configuration for registering & logging in users of different types:
```python
app.config["IGNORED_ROUTES"] = [("GET", "/")]
app.config["JWT_ROUTER_API_NAME"] = "/api/v1"
app.config["WHITE_LIST_ROUTES"] = [
("POST", "/auth/user"), ("POST", "/auth/user/login"),
("POST", "/auth/teacher"), ("POST", "/auth/teacher/login"),
]
# Optionally, you can pass your models to Flask's config:
app.config["ENTITY_MODELS"] = [ UserModel, TeacherModel, ...etc ]
```
## JSON Web Token setup
To send the JSON web token from your front end, you will need to pass a `Bearer` string in your authorization header.
For example:
```javascript
fetch(url, {
headers: {
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共36个文件
py:15个
rst:7个
txt:5个
资源分类:Python库 所属语言:Python 资源全名:flask-jwt-router-0.1.8a3.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
flask-jwt-router-0.1.8a3.tar.gz (36个子文件)
flask-jwt-router-0.1.8a3
MANIFEST.in 163B
PKG-INFO 14KB
docs
make.bat 763B
Makefile 640B
source
config.rst 88B
routing.rst 83B
conf.py 2KB
__init__.py 0B
jwt_routes.rst 86B
google.rst 92B
entity.rst 80B
authentication.rst 104B
index.rst 679B
Pipfile 262B
flask_jwt_router.egg-info
PKG-INFO 14KB
requires.txt 29B
SOURCES.txt 905B
top_level.txt 17B
dependency_links.txt 1B
Pipfile.lock 39KB
setup.cfg 38B
setup.py 1002B
README.md 11KB
flask_jwt_router
_routing.py 8KB
_entity.py 8KB
_authentication.py 4KB
oauth2
_base.py 605B
http_requests.py 2KB
__init__.py 66B
_urls.py 191B
_exceptions.py 566B
google.py 12KB
__init__.py 113B
_jwt_routes.py 10KB
_config.py 2KB
LICENSE.txt 1KB
共 36 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功