Skip to main content

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' # depracated
  • REDIS_AUTH_HOST default '127.0.0.1'
  • REDIS_AUTH_PORT default 6379
  • REDIS_AUTH_PASSWORD default None
  • REDIS_AUTH_DB default 0
  • REDIS_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.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

Project details


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.3.41.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

barter_auth-0.3.41-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file barter_auth-0.3.41.tar.gz.

File metadata

  • Download URL: barter_auth-0.3.41.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for barter_auth-0.3.41.tar.gz
Algorithm Hash digest
SHA256 82aa16a06f35fdcef09c5b2331f40823b2f063c8239c12b43cad97d903bb36b2
MD5 b945321015ccf6157ffc478c0eb3a0ad
BLAKE2b-256 bb7e48c0e4cfdc252212933a6f12b5224c83bb75a89a64b4d0530c7674686e5a

See more details on using hashes here.

File details

Details for the file barter_auth-0.3.41-py3-none-any.whl.

File metadata

  • Download URL: barter_auth-0.3.41-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for barter_auth-0.3.41-py3-none-any.whl
Algorithm Hash digest
SHA256 9e5b325df8a892182251ca24e29688088322edb1511d280604734546082b12e9
MD5 474bcaa8e8435e04892d0f9d3250d811
BLAKE2b-256 faeb26725f34c0ea8f0265cc40b4ce99859c38dec738be969e174fc836795b23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page