Skip to content

Repository files navigation

https://img.shields.io/travis/com/theruziev/security_interface.svg?style=flat-squarehttps://img.shields.io/codecov/c/github/theruziev/security_interface.svg?style=flat-squarePyPI

Security Interface

This library provides an easy API for authentication and authorization.

Installation

Install with the following command

pip install security_interface

Usage

First of all you need to implement IdentityPolicyInterface and AuthorizationPolicyInterface interfaces. For example, we can implement JWT Security

importjwtfromsecurity_interfaceimportIdentityPolicyInterface, AuthorizationPolicyInterfaceclassJwtIdentityPolicy(IdentityPolicyInterface):
def__init__(self, secret, algorithm="HS256"):
self.algorithm=algorithmself.secret=secretasyncdefidentify(self, identity):
ifjwtisNone:
raiseTypeError("Please install PyJWT")
try:
returnjwt.decode(
identity,
self.secret,
algorithms=[self.algorithm],
options={"verify_exp": True, "verify_iat": True},
)
exceptExceptionase:
returnNoneclassJwtAuthPolicy(AuthorizationPolicyInterface):
asyncdefcan(self, identity, permission):
returnpermissioninidentity["scope"]

Create security instance with our implementation

fromsecurity_interface.apiimportSecurityjwt_identity=JwtIdentityPolicy("SECRET")
jwt_auth_policy=JwtAuthPolicy()
security=Security(jwt_identity, jwt_auth_policy)
# Checking claimsecurity.identify(CLAIM)
# Checking permissionsecurity.can(CLAIM, "read")
security.can(CLAIM, "write")

For full implementation see DEMO

About

Simple API for authentication and authorization.

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages