o
    RDi/                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZmZ ddl	m
Z
 ddlmZ ddlmZ e d	Zg d
ZG dd dZG dd dZG dd deZG dd deeZG dd deZG dd dZG dd deZdS )    N)settings)ImproperlyConfiguredSuspiciousOperation)HttpResponseForbiddenHttpResponseNotFound   )FatalClientError)get_scopes_backend)oauth2_settingsoauth2_provider)GETHEADOPTIONSc                   @   s   e Zd ZdZdZdZdZedd Zedd Z	edd Z
ed	d
 Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )OAuthLibMixina  
    This mixin decouples Django OAuth Toolkit from OAuthLib.

    Users can configure the Server, Validator and OAuthlibCore
    classes used by this mixin by setting the following class
    variables:

      * server_class
      * validator_class
      * oauthlib_backend_class

    If these class variables are not set, it will fall back to using the classes
    specified in oauth2_settings (OAUTH2_SERVER_CLASS, OAUTH2_VALIDATOR_CLASS
    and OAUTH2_BACKEND_CLASS).
    Nc                 C      | j du rtjS | j S )z9
        Return the OAuthlib server class to use
        N)server_classr
   OAUTH2_SERVER_CLASScls r   U/var/www/Datamplify/venv/lib/python3.10/site-packages/oauth2_provider/views/mixins.pyget_server_class&      
zOAuthLibMixin.get_server_classc                 C   r   )zI
        Return the RequestValidator implementation class to use
        N)validator_classr
   OAUTH2_VALIDATOR_CLASSr   r   r   r   get_validator_class0   r   z!OAuthLibMixin.get_validator_classc                 C   r   )zE
        Return the OAuthLibCore implementation class to use
        N)oauthlib_backend_classr
   OAUTH2_BACKEND_CLASSr   r   r   r   get_oauthlib_backend_class:   r   z(OAuthLibMixin.get_oauthlib_backend_classc                 C   s(   |   }|  }tj}|| fi |S )zj
        Return an instance of `server_class` initialized with a `validator_class`
        object
        )r   r   r
   server_kwargs)r   r   r   r   r   r   r   
get_serverD   s   zOAuthLibMixin.get_serverc                 C   s0   t | drtjr|  }|  }||| _| jS )z
        Cache and return `OAuthlibCore` instance so it will be created only on first request
        unless ALWAYS_RELOAD_OAUTHLIB_CORE is True.
        _oauthlib_core)hasattrr
   ALWAYS_RELOAD_OAUTHLIB_COREr    r   r!   )r   server
core_classr   r   r   get_oauthlib_coreO   s
   
zOAuthLibMixin.get_oauthlib_corec                 C      |   }||S )z
        A wrapper method that calls validate_authorization_request on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        )r&   validate_authorization_requestselfrequestcorer   r   r   r(   [      
z,OAuthLibMixin.validate_authorization_requestc                 C   s*   |r| dng }|  }|||||S )a  
        A wrapper method that calls create_authorization_response on `server_class`
        instance.

        :param request: The current django.http.HttpRequest object
        :param scopes: A space-separated string of provided scopes
        :param credentials: Authorization credentials dictionary containing
                           `client_id`, `state`, `redirect_uri` and `response_type`
        :param allow: True if the user authorize the client, otherwise False
         )splitr&   create_authorization_response)r*   r+   scopescredentialsallowr,   r   r   r   r0   d   s   z+OAuthLibMixin.create_authorization_responsec                 C   r'   )z
        A wrapper method that calls create_token_response on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        )r&   create_token_responser)   r   r   r   r4   u   r-   z#OAuthLibMixin.create_token_responsec                 C   r'   )z
        A wrapper method that calls create_revocation_response on the
        `server_class` instance.

        :param request: The current django.http.HttpRequest object
        )r&   create_revocation_responser)   r   r   r   r5   ~      
z(OAuthLibMixin.create_revocation_responsec                 C   r'   )z
        A wrapper method that calls create_userinfo_response on the
        `server_class` instance.

        :param request: The current django.http.HttpRequest object
        )r&   create_userinfo_responser)   r   r   r   r7      r6   z&OAuthLibMixin.create_userinfo_responsec              
   C   sL   |   }z
|j||  dW S  ty% } zt|dkr t| d}~ww )z
        A wrapper method that calls verify_request on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        )r1   z%Invalid hex encoding in query string.N)r&   verify_request
get_scopes
ValueErrorstrr   )r*   r+   r,   errorr   r   r   r8      s   zOAuthLibMixin.verify_requestc                 C   s   g S )z
        This should return the list of scopes required to access the resources.
        By default it returns an empty list.
        r   )r*   r   r   r   r9      s   zOAuthLibMixin.get_scopesc                 K   s`   |j }|jpd}d|v rdnd}||| |j d}|| t|tr*d}||fS d}||fS )z
        Return an error to be displayed to the resource owner if anything goes awry.

        :param error: :attr:`OAuthToolkitError`
         ?&)r<   urlFT)oauthlib_errorredirect_uri
urlencodedupdate
isinstancer   )r*   r<   kwargsrA   rB   	separatorerror_responseredirectr   r   r   rH      s   


zOAuthLibMixin.error_responsec                 C   r'   )zReturns a boolean representing if client is authenticated with client credentials
        method. Returns `True` if authenticated.

        :param request: The current django.http.HttpRequest object
        )r&   authenticate_clientr)   r   r   r   rJ      r-   z!OAuthLibMixin.authenticate_client)__name__
__module____qualname____doc__r   r   r   classmethodr   r   r   r    r&   r(   r0   r4   r5   r7   r8   r9   rH   rJ   r   r   r   r   r      s0    
	
	
	


		

r   c                   @   s   e Zd ZdZdZdd ZdS )ScopedResourceMixinzB
    Helper mixin that implements "scopes handling" behaviour
    Nc                 O   s   | j du r	td| j S )z
        Return the scopes needed to access the resource

        :param args: Support scopes injections from the outside (not yet implemented)
        NzoProtectedResourceMixin requires either a definition of 'required_scopes' or an implementation of 'get_scopes()')required_scopesr   r*   argsrF   r   r   r   r9      s
   
zScopedResourceMixin.get_scopes)rK   rL   rM   rN   rQ   r9   r   r   r   r   rP      s    rP   c                           e Zd ZdZ fddZ  ZS )ProtectedResourceMixinz{
    Helper mixin that implements OAuth2 protection on request dispatch,
    specially useful for Django Generic Views
    c                    sb   |j  dkrt j|g|R i |S | |\}}|r.|j|_t j|g|R i |S t S Nr   )methoduppersuperdispatchr8   userresource_ownerr   r*   r+   rS   rF   validr	__class__r   r   rZ      s   zProtectedResourceMixin.dispatchrK   rL   rM   rN   rZ   __classcell__r   r   r`   r   rU          rU   c                       s@   e Zd ZdZg ZdZ fddZ fddZ fddZ  Z	S )	ReadWriteScopedResourceMixinzG
    Helper mixin that implements "read and write scopes" behavior
    Nc                    sP   t   }tjtjg}t|t|std|t	 j
| g|R i |S )NzmReadWriteScopedResourceMixin requires following scopes {} to be in OAUTH2_PROVIDER["SCOPES"] list in settings)r	   get_all_scopesr
   
READ_SCOPEWRITE_SCOPEsetissubsetr   formatrY   __new__)r   rS   rF   provided_scopesread_write_scopesr`   r   r   rl      s   
z$ReadWriteScopedResourceMixin.__new__c                    s:   |j  tv rtj| _ntj| _t j|g|R i |S N)	rW   rX   SAFE_HTTP_METHODSr
   rg   read_write_scoperh   rY   rZ   )r*   r+   rS   rF   r`   r   r   rZ   
  s   
z%ReadWriteScopedResourceMixin.dispatchc                    s   t  j|i |}|| jg S ro   )rY   r9   rq   )r*   rS   rF   r1   r`   r   r   r9     s   z'ReadWriteScopedResourceMixin.get_scopes)
rK   rL   rM   rN   rQ   rq   rl   rZ   r9   rc   r   r   r`   r   re      s    re   c                       rT   )ClientProtectedResourceMixinzMixin for protecting resources with client authentication as mentioned in rfc:`3.2.1`
    This involves authenticating with any of: HTTP Basic Auth, Client Credentials and
    Access token in that order. Breaks off after first validation.
    c                    s   |j  dkrt j|g|R i |S | |}|s8| |\}}|r5|j|_t j|g|R i |S t S t j|g|R i |S rV   )	rW   rX   rY   rZ   rJ   r8   r[   r\   r   r]   r`   r   r   rZ     s   
z%ClientProtectedResourceMixin.dispatchrb   r   r   r`   r   rr     rd   rr   c                       $   e Zd ZdZdZ fddZ  ZS )OIDCOnlyMixinz
    Mixin for views that should only be accessible when OIDC is enabled.

    If OIDC is not enabled:

    * if DEBUG is True, raises an ImproperlyConfigured exception explaining why
    * otherwise, returns a 404 response, logging the same warning
    zgdjango-oauth-toolkit OIDC views are not enabled unless you have configured OIDC_ENABLED in the settingsc                    :   t jstjrt| jt| j t S t	 j
|i |S ro   )r
   OIDC_ENABLEDr   DEBUGr   debug_error_messagelogwarningr   rY   rZ   rR   r`   r   r   rZ   A     
zOIDCOnlyMixin.dispatchrK   rL   rM   rN   rx   rZ   rc   r   r   r`   r   rt   2  
    
rt   c                       rs   )OIDCLogoutOnlyMixina  
    Mixin for views that should only be accessible when OIDC and OIDC RP-Initiated Logout are enabled.

    If either is not enabled:

    * if DEBUG is True, raises an ImproperlyConfigured exception explaining why
    * otherwise, returns a 404 response, logging the same warning
    zThe django-oauth-toolkit OIDC RP-Initiated Logout view is not enabled unless you have configured OIDC_RP_INITIATED_LOGOUT_ENABLED in the settingsc                    ru   ro   )r
    OIDC_RP_INITIATED_LOGOUT_ENABLEDr   rw   r   rx   ry   rz   r   rY   rZ   rR   r`   r   r   rZ   Y  r{   zOIDCLogoutOnlyMixin.dispatchr|   r   r   r`   r   r~   J  r}   r~   )loggingdjango.confr   django.core.exceptionsr   r   django.httpr   r   
exceptionsr   r1   r	   r
   	getLoggerry   rp   r   rP   rU   re   rr   rt   r~   r   r   r   r   <module>   s"    
 <#