o
    RDi                     @   s   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	 ddl
mZ ddlmZ dd	lmZ d
eefddZd
eefddZd
S )    wraps)ImproperlyConfigured)HttpResponseForbidden)Server   )OAuthLibCore)OAuth2Validator)get_scopes_backend)oauth2_settingsNc                       | pg   fdd}|S )a  
    Decorator to protect views by providing OAuth2 authentication out of the box,
    optionally with scope handling.

        @protected_resource()
        def my_view(request):
            # An access token is required to get here...
            # ...
            pass
    c                       t   fdd}|S )Nc                    sL    }t |}|j|  d\}}|r#|j| _| g|R i |S t S )Nscopes)r   verify_requestuserresource_ownerr   )requestargskwargs	validatorcorevalidoauthlib_req_scopes
server_clsvalidator_cls	view_func S/var/www/Datamplify/venv/lib/python3.10/site-packages/oauth2_provider/decorators.py	_validate   s   z8protected_resource.<locals>.decorator.<locals>._validater   r   r!   r   r   r   r   r    	decorator   s   	z%protected_resource.<locals>.decoratorr   r   r   r   r%   r   r#   r    protected_resource   s   r'   c                    r   )a  
    Decorator to protect views by providing OAuth2 authentication and read/write scopes
    out of the box.
    GET, HEAD, OPTIONS http methods require "read" scope. Otherwise "write" scope is required.

        @rw_protected_resource()
        def my_view(request):
            # If this is a POST, you have to provide 'write' scope to get here...
            # ...
            pass

    c                    r   )Nc           	         s   t   }tjtjg}t|t|std|| j	
 dv r) tj n tj  }t|}|j|  d\}}|rR|j| _| g|R i |S t S )Nzqrw_protected_resource decorator requires following scopes {0} to be in OAUTH2_PROVIDER['SCOPES'] list in settings)GETHEADOPTIONSr   )r
   get_all_scopesr   
READ_SCOPEWRITE_SCOPEsetissubsetr   formatmethodupperappendr   r   r   r   r   )	r   r   r   provided_scopesread_write_scopesr   r   r   r   r   r   r    r!   :   s"   
z;rw_protected_resource.<locals>.decorator.<locals>._validater   r"   r#   r$   r    r%   9   s   z(rw_protected_resource.<locals>.decoratorr   r&   r   r#   r    rw_protected_resource*   s   r6   )	functoolsr   django.core.exceptionsr   django.httpr   oauthlib.oauth2r   oauth2_backendsr   oauth2_validatorsr	   r   r
   settingsr   r'   r6   r   r   r   r    <module>   s    