Barter authentication package
Project description
Barter authentication package
This package allows you to authorize users through a shared redis
Install package
pip install barter-auth
Define env variables
REDIS_AUTH_URL
default 'redis://localhost:6378/1' # depracatedREDIS_AUTH_HOST
default '127.0.0.1'REDIS_AUTH_PORT
default 6379REDIS_AUTH_PASSWORD
default NoneREDIS_AUTH_DB
default 0REDIS_AUTH_ACCESS_PREFIX
default = 'access'REDIS_AUTH_REFRESH_PREFIX
default = 'refresh'REDIS_AUTH_TOTP_PREFIX
default = 'totp'REDIS_AUTH_PROFILE_PREFIX
default = 'profile'REDIS_AUTH_TOKEN_STORAGE
default = 'headers'
Use in view
# in django
from rest_framework.permissions import AllowAny, IsAuthenticated
from barter_auth.auth import ApiTokenRedisAuthentication
class SomeView(APIView):
authentication_classes = [ApiTokenRedisAuthentication]
permission_classes = [IsAuthenticated]
# barter_auth BaseUser() is available in request.user in DRF APIView
Use in AppConfig for request.profie
# you can add request user or profile in apps django config <app_name>.apps.py
from django.apps import AppConfig
from django.http import HttpRequest
def get_profile(self):
from barter_auth.providers import RedisProfileClient
from barter_auth.models import AnonymousProfile
if self.user.is_authenticated:
try:
return RedisProfileClient().get_profile(uuid=self.headers.get('Profile'))
except:
pass
return AnonymousProfile()
class ProfilesConfig(AppConfig):
name = "apps.<appp_name>"
def ready(self):
HttpRequest.profile = property(get_profile)
in the same way for request.extuser
# you can add request user or profile in apps django config <app_name>.apps.py
from django.apps import AppConfig
from django.http import HttpRequest
def get_user(self):
from barter_auth.providers import RedisAccessClient
from barter_auth.auth import (
get_token_from_header, get_token_from_cookies,
)
from django.contrib.auth.models import AnonymousUser
token = get_token_from_header(self)
if not token:
token = get_token_from_cookies(self)
if token:
token_service = RedisAccessClient()
user = token_service.get_user(token)
return user or AnonymousUser()
return AnonymousUser()
class ProfilesConfig(AppConfig):
name = "apps.<appp_name>"
def ready(self):
HttpRequest.extuser = property(get_user)
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
barter_auth-0.1.5.tar.gz
(9.0 kB
view hashes)
Built Distribution
Close
Hashes for barter_auth-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2b70962896c934a0f74715d6a68a4b20d6ed82dd9d1daa1cf44b7d057093279 |
|
MD5 | ad84f53952df5199c07e32e6cc652109 |
|
BLAKE2b-256 | 7709f3d2a18dd37e1d2690e94f9a63e935abb01a5dc45c7d34f334221486b279 |