• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lorinkoz / django-pgschemas / 10358753143

12 Aug 2024 08:17PM UTC coverage: 58.477%. Remained the same
10358753143

Pull #274

github

web-flow
Merge e6b4f3fce into 5e0fe418f
Pull Request #274: Bump django from 5.0.8 to 5.1

914 of 1563 relevant lines covered (58.48%)

7.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/django_pgschemas/contrib/channels3/auth.py
1
from channels.auth import AuthMiddleware, CookieMiddleware, SessionMiddleware, _get_user_session_key
×
2
from channels.db import database_sync_to_async
×
3
from django.conf import settings
×
4
from django.contrib.auth import BACKEND_SESSION_KEY, HASH_SESSION_KEY, load_backend
×
5
from django.contrib.auth.models import AnonymousUser
×
6
from django.utils.crypto import constant_time_compare
×
7

8

9
@database_sync_to_async
×
10
def get_user(scope):
×
11
    """
12
    Return the user model instance associated with the given scope.
13
    If no user is retrieved, return an instance of `AnonymousUser`.
14
    """
15
    if "session" not in scope:
×
16
        raise ValueError(
×
17
            "Cannot find session in scope. You should wrap your consumer in SessionMiddleware."
18
        )
19
    user = None
×
20
    session = scope["session"]
×
21
    with scope["tenant"]:
×
22
        try:
×
23
            user_id = _get_user_session_key(session)
×
24
            backend_path = session[BACKEND_SESSION_KEY]
×
25
        except KeyError:
×
26
            pass
×
27
        else:
28
            if backend_path in settings.AUTHENTICATION_BACKENDS:
×
29
                backend = load_backend(backend_path)
×
30
                user = backend.get_user(user_id)
×
31
                # Verify the session
32
                if hasattr(user, "get_session_auth_hash"):
×
33
                    session_hash = session.get(HASH_SESSION_KEY)
×
34
                    session_hash_verified = session_hash and constant_time_compare(
×
35
                        session_hash, user.get_session_auth_hash()
36
                    )
37
                    if not session_hash_verified:
×
38
                        session.flush()
×
39
                        user = None
×
40
    return user or AnonymousUser()
×
41

42

43
class TenantAuthMiddleware(AuthMiddleware):
×
44
    async def resolve_scope(self, scope):
×
45
        scope["user"]._wrapped = await get_user(scope)
×
46

47

48
def TenantAuthMiddlewareStack(inner):
×
49
    return CookieMiddleware(SessionMiddleware(TenantAuthMiddleware(inner)))
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc