A simple token-based auth plugin for Sanic
Project description
Gotta get your API protected!
=============================
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
```python
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:
```bash
$ 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
=============================
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
```python
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:
```bash
$ 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sanic_token_auth-0.1.1.tar.gz
.
File metadata
- Download URL: sanic_token_auth-0.1.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50218e5fa215be21dc829d3d8927f060e67a9c69a9411f8bfa2e125517cbeb2f |
|
MD5 | b64d3f2453aba12de5cb4a01cdf7b09b |
|
BLAKE2b-256 | da20e8f0087c0d10c71b6245b39eac73725968eadcccc2be17a5cba74c82b899 |
File details
Details for the file sanic_token_auth-0.1.1-py2.py3-none-any.whl
.
File metadata
- Download URL: sanic_token_auth-0.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8462a60e8909507aa4ff885db0eb8b33e26e2ca5fa4c10448247df49db20eb1 |
|
MD5 | 59cda04ca00bb6e4721253bffde6ed4e |
|
BLAKE2b-256 | 90b868e8bd5960537b0dddbe0d07a344eec7bc96688581ac388f38b13239e61e |