Code Monkey home page Code Monkey logo

oauth2helper's Introduction

This project is no longer maintained.
Please use layabauth module instead.



Validate and extract information from OAuth2 token

pypi version Build status Coverage Code style: black Number of tests Number of downloads

Validating an OAuth2 token

import oauth2helper

headers = {"Authorization": "Bearer YOUR_OAUTH2_TOKEN"}
my_token = headers.get('Authorization')[7:]

# Will raise InvalidTokenError or InvalidKeyError in case validation failed
oauth2helper.validate(my_token, "https://provider_url/common/discovery/keys")

Extracting user from a OAuth2 token

import oauth2helper

headers = {"Authorization": "Bearer YOUR_OAUTH2_TOKEN"}
my_token = headers.get('Authorization')[7:]

json_header, json_body = oauth2helper.validate(my_token, "https://provider_url/common/discovery/keys")
username = oauth2helper.user_name(json_body)

Starlette

A Starlette AuthenticationMiddleware backend is available.

from starlette.applications import Starlette
from starlette.authentication import requires
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware
from oauth2helper.starlette import OAuth2IdTokenBackend, unauthorized


backend = OAuth2IdTokenBackend(
    identity_provider_url="https://identity_provider_url",
    # You can extract scopes per user and validate them on @requires decorator
    scopes_retrieval=lambda json_body: ["authenticated"],
)
app = Starlette(middleware=[Middleware(AuthenticationMiddleware, backend=backend, on_error=unauthorized)])

@app.route("/authenticated_endpoint")
@requires(scopes=["authenticated"])
def endpoint(request):
    pass  # Implement your own logic

How to install

  1. python 3.6+ must be installed
  2. Use pip to install module:
python -m pip install oauth2helper

oauth2helper's People

Contributors

colin-b avatar

Watchers

 avatar  avatar  avatar

oauth2helper's Issues

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.