Link

Basic Usage

Table of contents

  1. Configuration
  2. Create Token
  3. Protect Views
  4. Use Token Object
  5. Full Example Code

Configuration

First, you should initialize and configure JWT through JWT.initialize context manager.

Important

You must specify secret_key or private_key + public_key that needed to encode JWT with algorithm

with JWT.initialize(app) as manager:
    manager.config.secret_key = "secret"

Find more about configuration

Create Token

After JWT initialized and configured. you can create access token through JWT.create_access_token

access_token = JWT.create_access_token(identity=username)

Find more about creating token

Protect Views

By decorate view function(method) with jwt_required or jwt_optional, You can protect view with JWT.

Important

You should specify token keyword argument to view function(method)

@app.route("/protected", methods=["GET"])
@jwt_required
async def protected(request: Request, token: Token):
    ...

Find more about protecting views

Use Token Object

jwt_required and jwt_optional injects Token to your view function/method. by token keyword argument. and given token object contains useful data of given JWT.

token.identity  # identity(sub) of JWT
token.exp  # expiration(exp) of JWT

Find more about token object


Full Example Code

import uuid

from sanic import Sanic
from sanic.response import json
from sanic.request import Request

from sanic_jwt_extended import JWT, jwt_required
from sanic_jwt_extended.tokens import Token

app = Sanic(__name__)


with JWT.initialize(app) as manager:
    manager.config.secret_key = "secret"


@app.route("/login", methods=["POST"])
async def login(request: Request):
    username = request.json.get("username", "user")

    access_token = JWT.create_access_token(identity=username)

    return json(
        dict(access_token=access_token), status=200
    )


@app.route("/protected", methods=["GET"])
@jwt_required
async def protected(request: Request, token: Token):
    return json(dict(identity=token.identity, type=token.type, raw_data=token.raw_data, exp=str(token.exp)))


if __name__ == "__main__":
    app.run()