o
    >h                     @   s   d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ edd	 ZedddZedd Zdd ZG dd deZG dd deZdd Zd
S )    )settings)BACKEND_SESSION_KEYHASH_SESSION_KEYSESSION_KEY_get_backendsget_user_modelload_backenduser_logged_inuser_logged_out)constant_time_compare)
LazyObject)database_sync_to_async)BaseMiddleware)CookieMiddlewareSessionMiddlewarec           	      C   s   ddl m} d| vrtd| d }d}z
t|}|t }W n	 ty'   Y n*w |tjv rQt|}|	|}t
|drQ|t}|oHt|| }|sQ|  d}|pU| S )z
    Return the user model instance associated with the given scope.
    If no user is retrieved, return an instance of `AnonymousUser`.
    r   AnonymousUsersessionQCannot find session in scope. You should wrap your consumer in SessionMiddleware.Nget_session_auth_hash)django.contrib.auth.modelsr   
ValueError_get_user_session_keyr   KeyErrorr   AUTHENTICATION_BACKENDSr   get_userhasattrgetr   r   r   flush)	scoper   r   useruser_idbackend_pathbackendsession_hashsession_hash_verified r&   m/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/channels/auth.pyr      s2   




r   Nc                 C   s  d| vrt d| d }d}|du r| dd}|du r t dt|dr)| }t|v rDt||jks?|rCt|td|sC|	  n|
  z|pM|j}W n tyn   tdd	}t|d
krh|d \}}nt dY nw |jj||t< ||t< ||t< || d< tj|jd|d dS )z
    Persist a user id and a backend in the request.
    This way a user doesn't have to re-authenticate on every request.
    Note that data set during the anonymous session is retained when the user
    logs in.
    r   r    Nr    zCUser must be passed as an argument or must be present in the scope.r   T)return_tuples   r   zYou have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.senderrequestr    )r   r   r   r   r   r   pkr   r   r   	cycle_keyr#   AttributeErrorr   len_metavalue_to_stringr   r	   send	__class__)r   r    r#   r   session_auth_hashbackends_r&   r&   r'   login:   sR   
	

r9   c                 C   s   ddl m} d| vrtd| d }| dd}t|dr"|js"d}|dur/tj|jd|d |	  d| v r>| | d< dS dS )	z_
    Remove the authenticated user's ID from the request and flush their session
    data.
    r   r   r   zWLogin cannot find session in scope. You should wrap your consumer in SessionMiddleware.r    Nis_authenticatedr+   )
r   r   r   r   r   r:   r
   r4   r5   r   )r   r   r   r    r&   r&   r'   logoutr   s   r;   c                 C   s   t  jj| t S N)r   r2   r.   	to_pythonr   )r   r&   r&   r'   r      s   r   c                   @   s   e Zd ZdZdd ZdS )UserLazyObjectzc
    Throw a more useful error message when scope['user'] is accessed before
    it's resolved
    c                 C   s   t d)Nz(Accessing scope user before it is ready.)r   )selfr&   r&   r'   _setup   s   zUserLazyObject._setupN)__name__
__module____qualname____doc__r@   r&   r&   r&   r'   r>      s    r>   c                       s0   e Zd ZdZdd Zdd Z fddZ  ZS )AuthMiddlewarezu
    Middleware which populates scope["user"] from a Django session.
    Requires SessionMiddleware to function.
    c                 C   s*   d|vrt dd|vrt |d< d S d S )Nr   zPAuthMiddleware cannot find session in scope. SessionMiddleware must be above it.r    )r   r>   r?   r   r&   r&   r'   populate_scope   s   zAuthMiddleware.populate_scopec                    s   t |I d H |d _d S )Nr    )r   _wrappedrF   r&   r&   r'   resolve_scope   s   zAuthMiddleware.resolve_scopec                    s:   t |}| | | |I d H  t |||I d H S r<   )dictrG   rI   super__call__)r?   r   receiver4   r5   r&   r'   rL      s
   
zAuthMiddleware.__call__)rA   rB   rC   rD   rG   rI   rL   __classcell__r&   r&   rN   r'   rE      s
    rE   c                 C   s   t tt| S r<   )r   r   rE   )innerr&   r&   r'   AuthMiddlewareStack   s   rQ   r<   )django.confr   django.contrib.authr   r   r   r   r   r   r	   r
   django.utils.cryptor   django.utils.functionalr   channels.dbr   channels.middlewarer   channels.sessionsr   r   r   r9   r;   r   r>   rE   rQ   r&   r&   r&   r'   <module>   s"    (

%7

