Coder Social home page Coder Social logo

thatsuseful / guardpost Goto Github PK

View Code? Open in Web Editor NEW

This project forked from neoteroi/guardpost

0.0 0.0 0.0 87 KB

Classes to handle authentication and authorization for async Python application.

License: MIT License

Python 98.74% Makefile 1.26%

guardpost's Introduction

Build pypi versions license codecov

Authentication and authorization framework for Python apps

Basic framework to handle authentication and authorization in asynchronous Python applications.

Features:

  • strategy to implement authentication (who or what is using a service?)
  • strategy to implement authorization (is the acting identity authorized to do a certain action?)
  • support for dependency injection for classes handling authentication and authorization requirements
  • built-in support for JSON Web Tokens (JWTs) authentication

This library is freely inspired by authorization in ASP.NET Core; although its implementation is extremely different.

Installation

pip install guardpost

To install with support for JSON Web Tokens (JWTs) validation:

pip install guardpost[jwt]

Examples

For examples, refer to the examples folder.

Functions to validate JWTs

GuardPost includes functions to validate JSON Web Tokens (JWTs) and handle JSON Web Keys Sets (JWKS).

The built-in validator class can retrieve automatically JWKS from identity providers and handle automatically caching and keys rotation. Caching is useful to not incur in useless performance fees (e.g. downloading JWKS at each web request), and keys rotation is important because identity providers can periodically change the keys they use to sign JWTs.

To use these features, install to include additional dependencies:

pip install guardpost[jwt]

The following example shows how to use guardpost to validate tokens:

import asyncio
from guardpost.jwts import JWTValidator


async def main():
    validator = JWTValidator(
        authority="YOUR_AUTHORITY",
        valid_issuers=["YOUR_ISSUER_VALUE"],
        valid_audiences=["YOUR_AUDIENCE"],
    )

    # keys are fetched when necessary
    data = await validator.validate_jwt("YOUR_TOKEN")

    print(data)


asyncio.run(main())

An example value for authority, to validate access tokens issued by Azure Active Directory could be: https://sts.windows.net/YOUR_TENANT_ID/.

GuardPost is used in BlackSheep and has been tested with:

  • Auth0
  • Azure Active Directory
  • Azure Active Directory B2C
  • Okta

If you have doubts about authentication vs authorization...

Authentication answers the question: Who is the user who is initiating the action?, or more in general: Who is the user, or what is the service, that is initiating the action?.

Authorization answers the question: Is the user, or service, authorized to do something?.

Usually, to implement authorization, is necessary to have the context of the entity that is executing the action.

Usage in BlackSheep

guardpost is used in the BlackSheep web framework, to implement authentication and authorization strategies for request handlers.

To see how guardpost is used in blacksheep web framework, read:

Documentation

Under construction. ๐Ÿšง

guardpost's People

Contributors

robertoprevato 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.