Skip to main content

Token-based authentication is the most common way of protecting your APIs from unwanted folk.

Sometimes you need to do things fast (you know, get it to prod yesterday) and you do not really have time to implement a proper authentication layer.

Okay, are you fine with a temporary solution? There’s nothing more permanent than temporary, right.

If you can:

  • provide a simple async token verifier (say, checking it in memcached or Redis)

    or

  • hard-code a token in your app prototype,

    and also

  • sent the token in a request header

  • then we are ready to go.

Usage example

from sanic import Sanic
from sanic.response import text

from sanic_token_auth import SanicTokenAuth

app = Sanic()
auth = SanicTokenAuth(app, secret_key='utee3Quaaxohh1Oo', header='X-My-App-Auth-Token')


@app.route("/")
async def index(request):
    return text("Go to /protected")


@app.route("/protected")
@auth.auth_required
async def protected(request):
    return text("Welcome!")


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, debug=True)

And let’s try it:

$ curl http://localhost:8000/protected -H "X-My-App-Auth-Token: utee3Quaaxohh1Oo"

Welcome!

If you omit the header argument, you can instead send a token in either Authorization: Bearer <yourtoken> or Authorization: Token <yourtoken> header.


TODO:

[ ] Document token_verifier and implement examples of using of

[ ] Implement “protect all” behaviour

Release files for sanic-token-auth 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-token-auth 0.2.0
File Size Uploaded
sanic_token_auth-0.2.0.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sanic-token-auth 0.2.0
File Interpreter ABI Platform
sanic_token_auth-0.2.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 7.2 kB

Release files / sanic_token_auth-0.2.0.tar.gz

Download URL sanic_token_auth-0.2.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
87f9cd1a05f242a2664b4137e18fe868efb846228af1c76516dec279c1e96e76
BLAKE2b-256 checksum
How to use checksums
7ea2565fe111db696a982ea31fe6d4cdc9976b94d5d633d4fc2402e26e7390fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / sanic_token_auth-0.2.0-py2.py3-none-any.whl

Download URL sanic_token_auth-0.2.0-py2.py3-none-any.whl
Size 3.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
12058acf7e6bda20fd76d444442f5c7580f31bd47fe2cdf77738e43633c582c5
BLAKE2b-256 checksum
How to use checksums
72f97e4a936699e3d43f9c5a64fbc5de72a110c6787fcc0e75cf6477ae5d0f55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page