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_URLdefault 'redis://localhost:6378/1' # depracatedREDIS_AUTH_HOSTdefault '127.0.0.1'REDIS_AUTH_PORTdefault 6379REDIS_AUTH_PASSWORDdefault NoneREDIS_AUTH_DBdefault 0REDIS_AUTH_ACCESS_PREFIXdefault = 'access'REDIS_AUTH_REFRESH_PREFIXdefault = 'refresh'REDIS_AUTH_TOTP_PREFIXdefault = 'totp'REDIS_AUTH_PROFILE_PREFIXdefault = 'profile'REDIS_AUTH_TOKEN_STORAGEdefault = '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.extuser and request.profie
# you can add request user or profile in apps django config <app_name>.apps.py
class BloggerConfig(AppConfig):
name = 'apps.base'
def ready(self):
HttpRequest.extuser = property(get_user)
HttpRequest.profile = property(get_profile)
def get_user(self):
from barter_auth.auth import get_token_from_cookies, get_token_from_header
from barter_auth.providers import RedisAccessClient
from django.contrib.auth.models import AnonymousUser
from django.conf import settings
token = get_token_from_header(self)
if not token:
token = get_token_from_cookies(self)
if token:
token_service = RedisAccessClient(host=settings.REDIS_AUTH_HOST)
user = token_service.get_user(token)
return user or AnonymousUser()
return AnonymousUser()
def get_profile(self):
from barter_auth.models import AnonymousProfile
extuser = self.extuser
if extuser.is_authenticated:
try:
profile = RedisProfileClient().get_model(key=self.headers.get('Profile'))
except:
profile = AnonymousProfile()
if profile.user_id and profile.user_id == extuser.id:
# print(f'profile: {profile.id}')
return profile
return AnonymousProfile()
# in settings
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
"barter_auth.auth.ApiTokenRedisAuthentication",
],
"EXCEPTION_HANDLER": '<your path>.api_exception_handler',
# ...
}
# file with api_exception_handler
from rest_framework.response import Response
from rest_framework.views import exception_handler, set_rollback
from barter_auth.exceptions import BAuthAPIException
def api_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
if isinstance(exc, BAuthAPIException):
headers = {}
if getattr(exc, 'auth_header', None):
headers['WWW-Authenticate'] = exc.auth_header
if getattr(exc, 'wait', None):
headers['Retry-After'] = '%d' % exc.wait
if isinstance(exc.detail, (list, dict)):
data = exc.detail
else:
data = {'detail': exc.detail}
set_rollback()
return Response(data, status=exc.status_code, headers=headers)
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None:
response.data['status_code'] = response.status_code
return response
Metadata
Release files for barter-auth 0.3.43
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| barter-auth-0.3.43.tar.gz | 12.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| barter_auth-0.3.43-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.7 kB
Release files / barter-auth-0.3.43.tar.gz
| Download URL | barter-auth-0.3.43.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
05474dfefc1874319f1553c91653aef16f109d6b4a10e1e5c169076a0f670822
|
|
BLAKE2b-256 checksum How to use checksums |
70abcebc5576f3a8ef3984c5cb90e4e285df860ec68deaa3f530dfc6ffc30edb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / barter_auth-0.3.43-py3-none-any.whl
| Download URL | barter_auth-0.3.43-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b9c620c54e5b1cd762911e465c1f2d02519c2d7ca6b3938f935fbf77dc2b4f53
|
|
BLAKE2b-256 checksum How to use checksums |
132eb3b82efac60b5d6fb470bfc1d88922b5220f7b591ed9296fd942959b85f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for barter-auth 0.3.43
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.