Giter Site home page Giter Site logo

sanic-token-auth's Introduction

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

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

sanic-token-auth's People

Contributors

saabeilin avatar

Watchers

James Cloos 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.