Ë
    OK¸iÓ  ã                   óÄ   — d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ dd	lmZmZ  ej&                  e«      Zd
„ Z G d„ de
j.                  «      Zy)z•
Classes/functions for integrating with Django REST Framework.

http://www.django-rest-framework.org/api-guide/authentication/#custom-authentication
é    N)Úget_backends)ÚImproperlyConfiguredÚSuspiciousOperation)Úimport_string)ÚauthenticationÚ
exceptions)Ú	HTTPError)ÚOIDCAuthenticationBackend)Úimport_from_settingsÚparse_www_authenticate_headerc                  ó0  — t        dd«      } | r/ t        | «      «       }t        |t        «      sd}t	        |«      ‚|S t        «       D �cg c]  }t        |t        «      sŒ|‘Œ }}|sd}t	        |«      ‚t        |«      dkD  rt	        d«      ‚|d   S c c}w )z5
    Get the Django auth backend that uses OIDC.
    ÚOIDC_DRF_AUTH_BACKENDNzTClass configured in OIDC_DRF_AUTH_BACKEND does not extend OIDCAuthenticationBackend!zxNo backends extending OIDCAuthenticationBackend found - add one to AUTHENTICATION_BACKENDS or set OIDC_DRF_AUTH_BACKEND!é   z.More than one OIDCAuthenticationBackend found!r   )r   r   Ú
isinstancer
   r   r   Úlen)Úbackend_settingÚbackendÚmsgÚbÚbackendss        úV/var/www/aura-ras/venv/lib/python3.12/site-packages/mozilla_django_oidc/contrib/drf.pyÚget_oidc_backendr      s®   € ô +Ð+BÀDÓI€OÙØ0”- Ó0Ó2ˆÜ˜'Ô#<Ô=ð=ð ô ' sÓ+Ð+Øˆô (›>ÖV�a¬Z¸Ô;TÕ-U’ÐV€HÐVáðOð 	ô # 3Ó'Ð'Ü
ˆ8ƒ}�qÒÜ"Ð#SÓTÐTØ�A‰;Ðùò Ws   Á
BÁ Bc                   ó.   — e Zd ZdZdZdd„Zd„ Zd„ Zd„ Zy)	ÚOIDCAuthenticationz0
    Provide OpenID authentication for DRF.
    ÚapiNc                 ó*   — |xs
 t        «       | _        y ©N)r   r   )Úselfr   s     r   Ú__init__zOIDCAuthentication.__init__B   s   € ØÒ4Ô"2Ó"4ˆ�ó    c                 óþ  — | j                  |«      }|sy	 | j                  j                  |dd«      }|sd}t        j                  |«      ‚||fS # t        $ rl}|j                  }|j
                  dk(  rKd|j                  v r=t        |j                  d   «      }t        j                  |j                  dd«      «      ‚‚ d}~wt        $ r0}t        j                  d|«       t        j                  d«      ‚d}~ww xY w)	z†
        Authenticate the request and return a tuple of (user, token) or None
        if there was no authentication attempt.
        Ni‘  zwww-authenticateÚerror_descriptionz(no error description in www-authenticatezLogin failed: %szLogin failedz7Login failed: No user found for the given access token.)Úget_access_tokenr   Úget_or_create_userr	   ÚresponseÚstatus_codeÚheadersr   r   ÚAuthenticationFailedÚgetr   ÚLOGGERÚinfo)r   ÚrequestÚaccess_tokenÚuserÚexcÚrespÚdatar   s           r   ÚauthenticatezOIDCAuthentication.authenticateE   sþ   € ð
 ×,Ñ,¨WÓ5ˆáØð	BØ—<‘<×2Ñ2°<ÀÀtÓLˆDñ* ØKˆCÜ×1Ñ1°#Ó6Ð6à�\Ð!Ð!øô1 ò 	Ø—<‘<ˆDð
 ×Ñ 3Ò&Ð+=ÀÇÁÑ+MÜ4°T·\±\ÐBTÑ5UÓV�Ü ×5Ñ5Ø—H‘HØ+Ð-Wóóð ð ûÜ"ò 	BÜ�K‰KÐ*¨CÔ0Ü×1Ñ1°.ÓAÐAûð	Bús$   –A Á	C<ÁA'C Ã C<Ã+C7Ã7C<c                 ó\  — t        j                  |«      }|sy|j                  t         j                  «      }|j	                  «       }|d   j                  «       dk7  ryt        |«      dk(  rd}t        j                  |«      ‚t        |«      dkD  rd}t        j                  |«      ‚|d   S )z»
        Get the access token based on a request.

        Returns None if no authentication details were provided. Raises
        AuthenticationFailed if the token is incorrect.
        Nr   Úbearerr   z1Invalid "bearer" header: No credentials provided.é   zFInvalid "bearer" header: Credentials string should not contain spaces.)	r   Úget_authorization_headerÚdecodeÚHTTP_HEADER_ENCODINGÚsplitÚlowerr   r   r(   )r   r,   ÚheaderÚauthr   s        r   r#   z#OIDCAuthentication.get_access_tokenk   sž   € ô  ×8Ñ8¸ÓAˆÙØØ—‘œ~×BÑBÓCˆà�|‰|‹~ˆà�‰7�=‰=‹?˜hÒ&Øäˆt‹9˜Š>ØEˆCÜ×1Ñ1°#Ó6Ð6Ü�‹Y˜Š]àXð ô ×1Ñ1°#Ó6Ð6à�A‰wˆr    c                 ó    — d| j                   z  S )a  
        If this method returns None, a generic HTTP 403 forbidden response is
        returned by DRF when authentication fails.

        By making the method return a string, a 401 is returned instead. The
        return value will be used as the WWW-Authenticate header.
        zBearer realm="%s")Úwww_authenticate_realm)r   r,   s     r   Úauthenticate_headerz&OIDCAuthentication.authenticate_header‡   s   € ð # T×%@Ñ%@Ñ@Ð@r    r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r>   r   r2   r#   r?   © r    r   r   r   :   s%   „ ñð
 #Ðó5ò$"òLó8Ar    r   )rC   ÚloggingÚdjango.contrib.authr   Údjango.core.exceptionsr   r   Údjango.utils.module_loadingr   Úrest_frameworkr   r   Úrequests.exceptionsr	   Úmozilla_django_oidc.authr
   Úmozilla_django_oidc.utilsr   r   Ú	getLoggerr@   r*   r   ÚBaseAuthenticationr   rD   r    r   ú<module>rO      sV   ðñó å ,ß LÝ 5ß 5Ý )å >÷ð
 
ˆ×	Ñ	˜8Ó	$€òôDUA˜×:Ñ:õ UAr    