o
    ËÝ2gßG  ã                   @  s$  d Z ddlmZ ddlZddlmZmZ ddl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mZmZ erHddlmZ ddlmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ G dd„ deƒZ!dS )z2Provides Authentication and Authorization classes.é    )ÚannotationsN)ÚABCÚabstractmethod)ÚTYPE_CHECKINGÚAnyÚCallable)ÚRequest)Úcodesé   )Úconst)ÚInvalidInvocationÚOAuthExceptionÚResponseException)ÚResponse)Ú	Requestorc                   @  sV   e Zd ZdZedd„ ƒZ	d%d&dd„Zed fd'dd„Z	d(d)dd „Z	d%d*d#d$„Z
dS )+ÚBaseAuthenticatorzEProvide the base authenticator object that stores OAuth2 credentials.c                 C  s   d S ©N© ©Úselfr   r   úE/home/garg/my-data/venv/lib/python3.10/site-packages/prawcore/auth.pyÚ_auth   s   zBaseAuthenticator._authNÚ	requestorr   Ú	client_idÚstrÚredirect_uriú
str | NoneÚreturnÚNonec                 C  s   || _ || _|| _dS )a%  Represent a single authentication to Reddit's API.

        :param requestor: An instance of :class:`.Requestor`.
        :param client_id: The OAuth2 client ID to use with the session.
        :param redirect_uri: The redirect URI exactly as specified in your OAuth
            application settings on Reddit. This parameter is required if you want to
            use the :meth:`~.Authorizer.authorize_url` method, or the
            :meth:`~.Authorizer.authorize` method of the :class:`.Authorizer` class
            (default: ``None``).

        N)Ú
_requestorr   r   )r   r   r   r   r   r   r   Ú__init__   s   
zBaseAuthenticator.__init__ÚokÚurlÚsuccess_statusÚintÚdatar   r   c                 K  s<   | j jd||  ¡ t| ¡ ƒddid}|j|krt|ƒ‚|S )NÚpostÚ
ConnectionÚclose)Úauthr%   Úheaders)r   Úrequestr   ÚsortedÚitemsÚstatus_coder   )r   r"   r#   r%   Úresponser   r   r   Ú_post0   s   
û
zBaseAuthenticator._postFÚdurationÚscopesú	list[str]ÚstateÚimplicitÚboolc           	      C  s’   | j du rd}t|ƒ‚|rt| tƒsd}t|ƒ‚|r$|dkr$d}t|ƒ‚| j|| j |r-dndd |¡|d	œ}| jjtj	 }t
d
||d}| ¡ jS )an  Return the URL used out-of-band to grant access to your application.

        :param duration: Either ``"permanent"`` or ``"temporary"``. ``"temporary"``
            authorizations generate access tokens that last only 1 hour. ``"permanent"``
            authorizations additionally generate a refresh token that can be
            indefinitely used to generate new hour-long access tokens. Only
            ``"temporary"`` can be specified if ``implicit`` is set to ``True``.
        :param scopes: A list of OAuth scopes to request authorization for.
        :param state: A string that will be reflected in the callback to
            ``redirect_uri``. Elements must be printable ASCII characters in the range
            ``0x20`` through ``0x7E`` inclusive. This value should be temporarily unique
            to the client for whom the URL was generated.
        :param implicit: Use the implicit grant flow (default: ``False``). This flow is
            only available for ``UntrustedAuthenticators``.

        :returns: URL to be used out-of-band for granting access to your application.

        :raises: :class:`.InvalidInvocation` if ``redirect_uri`` is not provided, if
            ``implicit`` is ``True`` and an authenticator other than
            :class:`.UntrustedAuthenticator` is used, or ``implicit`` is ``True`` and
            ``duration`` is ``"permanent"``.

        Núredirect URI not providedzFOnly UntrustedAuthenticator instances can use the implicit grant flow.Ú	temporaryz>The implicit grant flow only supports temporary access tokens.ÚtokenÚcodeú )r   r1   r   Úresponse_typeÚscoper4   ÚGET)Úparams)r   r   Ú
isinstanceÚUntrustedAuthenticatorr   Újoinr   Ú
reddit_urlr   ÚAUTHORIZATION_PATHr   Úpreparer"   )	r   r1   r2   r4   r5   Úmsgr?   r"   r+   r   r   r   Úauthorize_url>   s(   
ÿ
ú
zBaseAuthenticator.authorize_urlr9   Ú
token_typec                 C  s<   d|i}|dur||d< | j jtj }| j|fi |¤Ž dS )a;  Ask Reddit to revoke the provided token.

        :param token: The access or refresh token to revoke.
        :param token_type: When provided, hint to Reddit what the token type is for a
            possible efficiency gain. The value can be either ``"access_token"`` or
            ``"refresh_token"``.

        r9   NÚtoken_type_hint)r   rC   r   ÚREVOKE_TOKEN_PATHr0   )r   r9   rH   r%   r"   r   r   r   Úrevoke_tokent   s
   	zBaseAuthenticator.revoke_tokenr   )r   r   r   r   r   r   r   r   )r"   r   r#   r$   r%   r   r   r   ©F)
r1   r   r2   r3   r4   r   r5   r6   r   r   )r9   r   rH   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r	   r0   rG   rK   r   r   r   r   r      s    
üÿû6r   c                   @  sZ   e Zd ZU dZeZded< ddd	„Zdd
d„Zddd„Z	ddd„Z
ddd„Zddd„ZdS )ÚBaseAuthorizerz6Superclass for OAuth2 authorization tokens and scopes.ztuple | typeÚAUTHENTICATOR_CLASSÚauthenticatorr   r   r   c                 C  s   || _ |  ¡  |  ¡  dS )z†Represent a single authorization to Reddit's API.

        :param authenticator: An instance of :class:`.BaseAuthenticator`.

        N)Ú_authenticatorÚ_clear_access_tokenÚ_validate_authenticator)r   rS   r   r   r   r    ‰   s   zBaseAuthorizer.__init__c                 C  s   |  d | _ d | _d S r   )Úaccess_tokenr2   r   r   r   r   rU   “   s   
z"BaseAuthorizer._clear_access_tokenr%   r   c                 K  sš   | j jjtj }t ¡ }| j jd
d|i|¤Ž}| ¡ }d|v r*t||d | 	d¡ƒ‚|d |d  | _
|d | _d|v rA|d | _t|d  d	¡ƒ| _d S )Nr"   ÚerrorÚerror_descriptioné
   Ú
expires_inrW   Úrefresh_tokenr=   r;   r   )rT   r   rC   r   ÚACCESS_TOKEN_PATHÚtimer0   Újsonr   ÚgetÚ_expiration_timestamprW   r\   ÚsetÚsplitr2   )r   r%   r"   Úpre_request_timer/   Úpayloadr   r   r   Ú_request_token˜   s   ÿ

zBaseAuthorizer._request_tokenc                 C  sh   t | j| jƒs2d}t | jtƒr|d| jj› d7 }t|ƒ‚|dd dd„ | jD ƒ¡› d7 }t|ƒ‚d S )Nz!Must use an authenticator of typer;   Ú.z or c                 S  s   g | ]}|j ‘qS r   )rM   )Ú.0Úir   r   r   Ú
<listcomp>¯   s    z:BaseAuthorizer._validate_authenticator.<locals>.<listcomp>)r@   rT   rR   ÚtyperM   rB   r   ©r   rF   r   r   r   rV   ¨   s   ýÿøz&BaseAuthorizer._validate_authenticatorr6   c                 C  s   | j duot ¡ | jk S )zÌReturn whether the :class`.Authorizer` is ready to authorize requests.

        A ``True`` return value does not guarantee that the ``access_token`` is actually
        valid on the server side.

        N)rW   r^   ra   r   r   r   r   Úis_valid³   s   ÿzBaseAuthorizer.is_validc                 C  s2   | j du rd}t|ƒ‚| j | j d¡ |  ¡  dS )z!Revoke the current Authorization.Nzno token available to revokerW   )rW   r   rT   rK   rU   rl   r   r   r   Úrevoke¾   s
   
zBaseAuthorizer.revokeN)rS   r   r   r   ©r   r   )r%   r   r   r   )r   r6   )rM   rN   rO   rP   r   rR   Ú__annotations__r    rU   rf   rV   rm   rn   r   r   r   r   rQ   „   s   
 





rQ   c                      s>   e Zd ZU dZdZded< 	dd‡ fdd„Zddd„Z‡  ZS )ÚTrustedAuthenticatorzEStore OAuth2 authentication credentials for web, or script type apps.r:   r   ÚRESPONSE_TYPENr   r   r   Úclient_secretr   r   r   r   c                   s   t ƒ  |||¡ || _dS )au  Represent a single authentication to Reddit's API.

        :param requestor: An instance of :class:`.Requestor`.
        :param client_id: The OAuth2 client ID to use with the session.
        :param client_secret: The OAuth2 client secret to use with the session.
        :param redirect_uri: The redirect URI exactly as specified in your OAuth
            application settings on Reddit. This parameter is required if you want to
            use the :meth:`~.Authorizer.authorize_url` method, or the
            :meth:`~.Authorizer.authorize` method of the :class:`.Authorizer` class
            (default: ``None``).

        N)Úsuperr    rs   )r   r   r   rs   r   ©Ú	__class__r   r   r    Í   s   
zTrustedAuthenticator.__init__útuple[str, str]c                 C  s   | j | jfS r   )r   rs   r   r   r   r   r   ã   s   zTrustedAuthenticator._authr   )
r   r   r   r   rs   r   r   r   r   r   ©r   rw   )	rM   rN   rO   rP   rr   rp   r    r   Ú__classcell__r   r   ru   r   rq   È   s   
 ûrq   c                   @  s   e Zd ZdZddd„ZdS )rA   zCStore OAuth2 authentication credentials for installed applications.r   rw   c                 C  s
   | j dfS )NÚ )r   r   r   r   r   r   ê   s   
zUntrustedAuthenticator._authNrx   )rM   rN   rO   rP   r   r   r   r   r   rA   ç   s    rA   c                      sP   e Zd ZdZddddœd‡ fdd„Zddd„Zddd„Zdd‡ fdd„Z‡  ZS )Ú
Authorizerz/Manages OAuth2 authorization tokens and scopes.N)Úpost_refresh_callbackÚpre_refresh_callbackr\   rS   r   r|   ú#Callable[[Authorizer], None] | Noner}   r\   r   r   r   c                  s"   t ƒ  |¡ || _|| _|| _dS )a¾  Represent a single authorization to Reddit's API.

        :param authenticator: An instance of a subclass of :class:`.BaseAuthenticator`.
        :param post_refresh_callback: When a single-argument function is passed, the
            function will be called prior to refreshing the access and refresh tokens.
            The argument to the callback is the :class:`.Authorizer` instance. This
            callback can be used to inspect and modify the attributes of the
            :class:`.Authorizer`.
        :param pre_refresh_callback: When a single-argument function is passed, the
            function will be called after refreshing the access and refresh tokens. The
            argument to the callback is the :class:`.Authorizer` instance. This callback
            can be used to inspect and modify the attributes of the
            :class:`.Authorizer`.
        :param refresh_token: Enables the ability to refresh the authorization.

        N)rt   r    Ú_post_refresh_callbackÚ_pre_refresh_callbackr\   )r   rS   r|   r}   r\   ru   r   r   r    ñ   s   
zAuthorizer.__init__r:   r   c                 C  s0   | j jdu rd}t|ƒ‚| j|d| j jd dS )z§Obtain and set authorization tokens based on ``code``.

        :param code: The code obtained by an out-of-band authorization request to
            Reddit.

        Nr7   Úauthorization_code)r:   Ú
grant_typer   )rT   r   r   rf   )r   r:   rF   r   r   r   Ú	authorize  s   
ýzAuthorizer.authorizec                 C  sN   | j r|   | ¡ | jdu rd}t|ƒ‚| jd| jd | jr%|  | ¡ dS dS )z1Obtain a new access token from the refresh_token.Nzrefresh token not providedr\   )r‚   r\   )r€   r\   r   rf   r   rl   r   r   r   Úrefresh  s   

ÿÿzAuthorizer.refreshFÚonly_accessr6   c                   s>   |s| j du rtƒ  ¡  dS | j | j d¡ |  ¡  d| _ dS )a  Revoke the current Authorization.

        :param only_access: When explicitly set to ``True``, do not evict the refresh
            token if one is set.

        Revoking a refresh token will in-turn revoke all access tokens associated with
        that authorization.

        Nr\   )r\   rt   rn   rT   rK   rU   )r   r…   ru   r   r   rn   +  s
   

zAuthorizer.revoke)
rS   r   r|   r~   r}   r~   r\   r   r   r   )r:   r   r   r   ro   rL   )r…   r6   r   r   )	rM   rN   rO   rP   r    rƒ   r„   rn   ry   r   r   ru   r   r{   î   s    ú

r{   c                      s&   e Zd ZdZeZd‡ fdd„Z‡  ZS )ÚImplicitAuthorizerz3Manages implicit installed-app type authorizations.rS   rA   rW   r   r[   r$   r=   r   r   c                   s4   t ƒ  |¡ t ¡ | | _|| _t| d¡ƒ| _dS )aU  Represent a single implicit authorization to Reddit's API.

        :param authenticator: An instance of :class:`.UntrustedAuthenticator`.
        :param access_token: The access_token obtained from Reddit via callback to the
            authenticator's ``redirect_uri``.
        :param expires_in: The number of seconds the ``access_token`` is valid for. The
            origin of this value was returned from Reddit via callback to the
            authenticator's redirect uri. Note, you may need to subtract an offset
            before passing in this number to account for a delay between when Reddit
            prepared the response, and when you make this function call.
        :param scope: A space-delimited string of Reddit OAuth2 scope names as returned
            from Reddit in the callback to the authenticator's redirect uri.

        r;   N)rt   r    r^   ra   rW   rb   rc   r2   )r   rS   rW   r[   r=   ru   r   r   r    B  s   zImplicitAuthorizer.__init__)
rS   rA   rW   r   r[   r$   r=   r   r   r   )rM   rN   rO   rP   rA   rR   r    ry   r   r   ru   r   r†   =  s    r†   c                      s4   e Zd ZdZeZ	dd‡ fd	d
„Zddd„Z‡  ZS )ÚReadOnlyAuthorizerzÔManages authorizations that are not associated with a Reddit account.

    While the ``"*"`` scope will be available, some endpoints simply will not work due
    to the lack of an associated Reddit account.

    NrS   r   r2   úlist[str] | Noner   r   c                   s   t ƒ  |¡ || _dS )zìRepresent a ReadOnly authorization to Reddit's API.

        :param scopes: A list of OAuth scopes to request authorization for (default:
            ``None``). The scope ``"*"`` is requested when the default argument is used.

        N)rt   r    Ú_scopes)r   rS   r2   ru   r   r   r    g  s   
zReadOnlyAuthorizer.__init__c                 C  s2   i }| j rd | j ¡|d< | jdddi|¤Ž dS )z#Obtain a new ReadOnly access token.r;   r=   r‚   Úclient_credentialsNr   )r‰   rB   rf   )r   Úadditional_kwargsr   r   r   r„   u  s   zReadOnlyAuthorizer.refreshr   )rS   r   r2   rˆ   r   r   ro   ©	rM   rN   rO   rP   rq   rR   r    r„   ry   r   r   ru   r   r‡   ]  s    ýr‡   c                      s6   e Zd ZdZeZ		dd‡ fdd„Zddd„Z‡  ZS )ÚScriptAuthorizerzšManages personal-use script type authorizations.

    Only users who are listed as developers for the application will be granted access
    tokens.

    NrS   r   Úusernamer   ÚpasswordÚtwo_factor_callbackúCallable | Noner2   rˆ   r   r   c                   s(   t ƒ  |¡ || _|| _|| _|| _dS )a³  Represent a single personal-use authorization to Reddit's API.

        :param authenticator: An instance of :class:`.TrustedAuthenticator`.
        :param username: The Reddit username of one of the application's developers.
        :param password: The password associated with ``username``.
        :param two_factor_callback: A function that returns OTPs (One-Time Passcodes),
            also known as 2FA auth codes. If this function is provided, prawcore will
            call it when authenticating.
        :param scopes: A list of OAuth scopes to request authorization for (default:
            ``None``). The scope ``"*"`` is requested when the default argument is used.

        N)rt   r    Ú	_passwordr‰   Ú_two_factor_callbackÚ	_username)r   rS   rŽ   r   r   r2   ru   r   r   r    ‡  s
   
zScriptAuthorizer.__init__c                 C  sT   i }| j rd | j ¡|d< | jo|  ¡ }|r||d< | jdd| j| jdœ|¤Ž dS )z3Obtain a new personal-use script type access token.r;   r=   Úotpr   )r‚   rŽ   r   Nr   )r‰   rB   r“   rf   r”   r’   )r   r‹   Útwo_factor_coder   r   r   r„   ¡  s   ý
üzScriptAuthorizer.refresh©NN)rS   r   rŽ   r   r   r   r   r‘   r2   rˆ   r   r   ro   rŒ   r   r   ru   r   r   }  s    úr   c                      s:   e Zd ZdZeefZ		dd‡ fdd„Zddd„Z‡  Z	S )ÚDeviceIDAuthorizerzÄManages app-only OAuth2 for 'installed' applications.

    While the ``"*"`` scope will be available, some endpoints simply will not work due
    to the lack of an associated Reddit account.

    NrS   r   Ú	device_idr   r2   rˆ   r   r   c                   s(   |du rd}t ƒ  |¡ || _|| _dS )aÂ  Represent an app-only OAuth2 authorization for 'installed' apps.

        :param authenticator: An instance of :class:`.UntrustedAuthenticator` or
            :class:`.TrustedAuthenticator`.
        :param device_id: A unique ID (20-30 character ASCII string) (default:
            ``None``). ``device_id`` is set to ``"DO_NOT_TRACK_THIS_DEVICE"`` when the
            default argument is used. For more information about this parameter, see:
            https://github.com/reddit/reddit/wiki/OAuth2#application-only-oauth
        :param scopes: A list of OAuth scopes to request authorization for (default:
            ``None``). The scope ``"*"`` is requested when the default argument is used.

        NÚDO_NOT_TRACK_THIS_DEVICE)rt   r    Ú
_device_idr‰   )r   rS   r™   r2   ru   r   r   r    »  s
   
zDeviceIDAuthorizer.__init__c                 C  s:   i }| j rd | j ¡|d< d}| jd|| jdœ|¤Ž dS )zObtain a new access token.r;   r=   z0https://oauth.reddit.com/grants/installed_client)r‚   r™   Nr   )r‰   rB   rf   r›   )r   r‹   r‚   r   r   r   r„   Ó  s   þ
ýzDeviceIDAuthorizer.refreshr—   )rS   r   r™   r   r2   rˆ   r   r   ro   )
rM   rN   rO   rP   rq   rA   rR   r    r„   ry   r   r   ru   r   r˜   ±  s    ür˜   )"rP   Ú
__future__r   r^   Úabcr   r   Útypingr   r   r   Úrequestsr   Úrequests.status_codesr	   rz   r   Ú
exceptionsr   r   r   Úrequests.modelsr   Úprawcore.requestorr   r   rQ   rq   rA   r{   r†   r‡   r   r˜   r   r   r   r   Ú<module>   s*    pDO  4