
    {iG                    d   S r SSKJr  SSKrSSKJrJr  SSKJrJ	r	J
r
  SSKJr  SSKJr  SS	KJr  SS
KJrJrJr  \(       a  SSKJr  SSKJr   " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r  " S S\5      r!g)z2Provides Authentication and Authorization classes.    )annotationsN)ABCabstractmethod)TYPE_CHECKINGAnyCallable)Request)codes   )const)InvalidInvocationOAuthExceptionResponseException)Response)	Requestorc                      \ rS rSrSr\S 5       r S       SS jjr\S   4       SS jjr	 S         SS jjr
SSS	 jjrS
rg)BaseAuthenticator   zEProvide the base authenticator object that stores OAuth2 credentials.c                    g N selfs    G/home/james-whalen/.local/lib/python3.13/site-packages/prawcore/auth.py_authBaseAuthenticator._auth   s        Nc                (    Xl         X l        X0l        g)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
_requestor	client_idredirect_uri)r   	requestorr!   r"   s       r   __init__BaseAuthenticator.__init__   s    " $"(r   okc           	         U R                   R                  SUU R                  5       [        UR	                  5       5      SS0S9nUR
                  U:w  a  [        U5      eU$ )Npost
Connectionclose)authdataheaders)r    requestr   sorteditemsstatus_coder   )r   urlsuccess_statusr,   responses        r   _postBaseAuthenticator._post0   sb     ??**

%!7+ + 
 >1#H--r   c                   U R                   c  Sn[        U5      eU(       a"  [        U [        5      (       d  Sn[        U5      eU(       a  US:w  a  Sn[        U5      eU R                  UU R                   U(       a  SOSSR                  U5      US.nU R                  R                  [        R                  -   n[        S	XvS
9nUR                  5       R                  $ )a  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"``.

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!   durationr"   response_typescopestateGET)params)r"   r   
isinstanceUntrustedAuthenticatorr!   joinr    
reddit_urlr   AUTHORIZATION_PATHr	   preparer2   )	r   r=   scopesr@   implicitmsgrB   r2   r.   s	            r   authorize_urlBaseAuthenticator.authorize_url>   s    < $-C#C((Jt-CDDX  $C((K/RC#C((   --(0WfXXf%
 oo((5+C+CC%4 $$$r   c                    SU0nUb  X#S'   U R                   R                  [        R                  -   nU R                  " U40 UD6  g)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"``.

r:   Ntoken_type_hint)r    rF   r   REVOKE_TOKEN_PATHr5   )r   r:   
token_typer,   r2   s        r   revoke_tokenBaseAuthenticator.revoke_tokent   sH     !&0"#oo((5+B+BB

3$r   r   r   )r#   r   r!   strr"   
str | NonereturnNone)r2   rT   r3   intr,   r   rV   r   F)
r=   rT   rI   z	list[str]r@   rT   rJ   boolrV   rT   )r:   rT   rQ   rU   rV   rW   )__name__
__module____qualname____firstlineno____doc__r   r   r$   r
   r5   rL   rR   __static_attributes__r   r   r   r   r      s    O  $(	)) ) !	)
 
), /4Dk(+CF	& 4%4% 4% 	4%
 4% 
4%l   r   r   c                  d    \ rS rSr% Sr\rS\S'   SS jrSS jr	SS jr
SS jrSS	 jrSS
 jrSrg)BaseAuthorizer   z6Superclass for OAuth2 authorization tokens and scopes.ztuple | typeAUTHENTICATOR_CLASSc                P    Xl         U R                  5         U R                  5         g)zvRepresent a single authorization to Reddit's API.

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

N)_authenticator_clear_access_token_validate_authenticator)r   authenticators     r   r$   BaseAuthorizer.__init__   s"     ,  "$$&r   c                $    U   S U l         S U l        g r   )access_tokenrI   r   s    r   rg   "BaseAuthorizer._clear_access_token   s    (,'+r   c                   U R                   R                  R                  [        R                  -   n[
        R
                  " 5       nU R                   R                  " S
SU0UD6nUR                  5       nSU;   a  [        XES   UR                  S5      5      eUS-
  US   -   U l
        US   U l        SU;   a
  US   U l        [        US   R                  S	5      5      U l        g )Nr2   errorerror_description
   
expires_inrl   refresh_tokenr?   r<   r   )rf   r    rF   r   ACCESS_TOKEN_PATHtimer5   jsonr   get_expiration_timestamprl   rs   setsplitrI   )r   r,   r2   pre_request_timer4   payloads         r   _request_tokenBaseAuthorizer._request_token   s    !!,,77%:Q:QQ99;&&,,===--/g '*GKK8K,L  &6%:W\=R%R"#N3g%!(!9D''*0056r   c                d   [        U R                  U R                  5      (       d  Sn[        U R                  [        5      (       a  USU R                  R                   S3-  nO<USSR                  U R                   Vs/ s H  o"R                  PM     sn5       S3-  n[        U5      eg s  snf )Nz!Must use an authenticator of typer<   .z or )rC   rf   rd   typer[   rE   r   )r   rK   is      r   rh   &BaseAuthorizer._validate_authenticator   s    $--t/G/GHH5C$22D99433<<=Q??9Q9Q$R9QAZZ9Q$RSTTUV $C(( I %Ss    B-c                p    U R                   SL=(       a"    [        R                  " 5       U R                  :  $ )zReturn 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)rl   ru   rx   r   s    r   is_validBaseAuthorizer.is_valid   s-     T)VdiikD<V<V.V	
r   c                    U R                   c  Sn[        U5      eU R                  R                  U R                   S5        U R	                  5         g)z!Revoke the current Authorization.Nzno token available to revokerl   )rl   r   rf   rR   rg   r   rK   s     r   revokeBaseAuthorizer.revoke   sF    $0C#C(((():):NK  "r   )rf   rx   rl   rs   rI   N)ri   r   rV   rW   rV   rW   )r,   r   rV   rW   )rV   rZ   )r[   r\   r]   r^   r_   r   rd   __annotations__r$   rg   r}   rh   r   r   r`   r   r   r   rb   rb      s.    @(99',
7 	)	
#r   rb   c                  b   ^  \ rS rSr% SrSrS\S'    S	         S
U 4S jjjrSS jrSr	U =r
$ )TrustedAuthenticator   zEStore OAuth2 authentication credentials for web, or script type apps.r;   rT   RESPONSE_TYPEc                2   > [         TU ]  XU5        X0l        g)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 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$   client_secret)r   r#   r!   r   r"   	__class__s        r   r$   TrustedAuthenticator.__init__   s    & 	|<*r   c                2    U R                   U R                  4$ r   )r!   r   r   s    r   r   TrustedAuthenticator._auth   s    ~~t1111r   )r   r   )
r#   r   r!   rT   r   rT   r"   rU   rV   rW   rV   ztuple[str, str])r[   r\   r]   r^   r_   r   r   r$   r   r`   __classcell__r   s   @r   r   r      sY    OM3 $(++ + 	+
 !+ 
+ +,2 2r   r   c                  "    \ rS rSrSrSS jrSrg)rD      zCStore OAuth2 authentication credentials for installed applications.c                    U R                   S4$ )N )r!   r   s    r   r   UntrustedAuthenticator._auth   s    ~~r!!r   r   Nr   )r[   r\   r]   r^   r_   r   r`   r   r   r   rD   rD      s
    M"r   rD   c                  v   ^  \ rS rSrSrSSSS.         S
U 4S jjjrSS jrSS jrSSU 4S jjjrS	r	U =r
$ )
Authorizer   z/Manages OAuth2 authorization tokens and scopes.N)post_refresh_callbackpre_refresh_callbackrs   c               H   > [         TU ]  U5        X l        X0l        X@l        g)aV  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)r   r$   _post_refresh_callback_pre_refresh_callbackrs   )r   ri   r   r   rs   r   s        r   r$   Authorizer.__init__   s$    0 	'&;#%9"*r   c                    U R                   R                  c  Sn[        U5      eU R                  USU R                   R                  S9  g)zObtain and set authorization tokens based on ``code``.

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

Nr8   authorization_code)r;   
grant_typer"   )rf   r"   r   r}   )r   r;   rK   s      r   	authorizeAuthorizer.authorize  sN     ++3-C#C((+,,99 	 	
r   c                    U R                   (       a  U R                  U 5        U R                  c  Sn[        U5      eU R                  SU R                  S9  U R                  (       a  U R	                  U 5        gg)z1Obtain a new access token from the refresh_token.Nzrefresh token not providedrs   )r   rs   )r   rs   r   r}   r   r   s     r   refreshAuthorizer.refresh  sq    %%&&t,%.C#C((&d6H6H 	 	
 &&''- 'r   c                   > U(       d  U R                   c  [        TU ]	  5         gU R                  R	                  U R                   S5        U R                  5         SU l         g)zRevoke 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.

Nrs   )rs   r   r   rf   rR   rg   )r   only_accessr   s     r   r   Authorizer.revoke+  sM     $,,4GN,,T-?-?Q$$&!%Dr   )r   r   rs   )
ri   r   r   #Callable[[Authorizer], None] | Noner   r   rs   rU   rV   rW   )r;   rT   rV   rW   r   rY   )r   rZ   rV   rW   )r[   r\   r]   r^   r_   r$   r   r   r   r`   r   r   s   @r   r   r      sc    9 FJDH$(+(+  C	+
 B+ "+ 
+ +:
 .& &r   r   c                  H   ^  \ rS rSrSr\r          SU 4S jjrSrU =r	$ )ImplicitAuthorizeri=  z3Manages implicit installed-app type authorizations.c                   > [         TU ]  U5        [        R                  " 5       U-   U l        X l        [        UR                  S5      5      U l        g)a  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)r   r$   ru   rx   rl   ry   rz   rI   )r   ri   rl   rr   r?   r   s        r   r$   ImplicitAuthorizer.__init__B  s@    * 	'%)YY[:%="(%++c*+r   )rx   rl   rI   )
ri   rD   rl   rT   rr   rX   r?   rT   rV   rW   )
r[   r\   r]   r^   r_   rD   rd   r$   r`   r   r   s   @r   r   r   =  sF    =0,-, , 	,
 , 
, ,r   r   c                  N   ^  \ rS rSrSr\r S     SU 4S jjjrSS jrSr	U =r
$ )	ReadOnlyAuthorizeri]  zManages 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.

c                0   > [         TU ]  U5        X l        g)zRepresent 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)r   r$   _scopes)r   ri   rI   r   s      r   r$   ReadOnlyAuthorizer.__init__g  s     	'r   c                    0 nU R                   (       a  SR                  U R                   5      US'   U R                  " SSS0UD6  g)z#Obtain a new ReadOnly access token.r<   r?   r   client_credentialsNr   )r   rE   r}   )r   additional_kwargss     r   r   ReadOnlyAuthorizer.refreshu  s?    <<),$,,)?g&Q';Q?PQr   )r   r   )ri   r   rI   list[str] | NonerV   rW   r   r[   r\   r]   r^   r_   r   rd   r$   r   r`   r   r   s   @r   r   r   ]  sG     /
 $(( ! 
	 R Rr   r   c                  \   ^  \ rS rSrSr\r  S           SU 4S jjjrSS jrSr	U =r
$ )	ScriptAuthorizeri}  zManages personal-use script type authorizations.

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

c                T   > [         TU ]  U5        X0l        XPl        X@l        X l        g)ak  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)r   r$   	_passwordr   _two_factor_callback	_username)r   ri   usernamepasswordtwo_factor_callbackrI   r   s         r   r$   ScriptAuthorizer.__init__  s'    ( 	'!$7!!r   c                   0 nU R                   (       a  SR                  U R                   5      US'   U R                  =(       a    U R                  5       nU(       a  X!S'   U R                  " SSU R                  U R
                  S.UD6  g)z3Obtain a new personal-use script type access token.r<   r?   otpr   )r   r   r   Nr   )r   rE   r   r}   r   r   )r   r   two_factor_codes      r   r   ScriptAuthorizer.refresh  sy    <<),$,,)?g&33S8Q8Q8S'6e$ 	
!^^^^	
  		
r   )r   r   r   r   NN)ri   r   r   rU   r   rU   r   zCallable | NonerI   r   rV   rW   r   r   r   s   @r   r   r   }  sf     / 04#'"(" " 	"
 -" !" 
" "4
 
r   r   c                  X   ^  \ rS rSrSr\\4r  S       SU 4S jjjrSS jr	Sr
U =r$ )	DeviceIDAuthorizeri  zManages 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.

c                F   > Uc  Sn[         TU ]  U5        X l        X0l        g)az  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)r   r$   
_device_idr   )r   ri   	device_idrI   r   s       r   r$   DeviceIDAuthorizer.__init__  s'    $ 2I'#r   c                    0 nU R                   (       a  SR                  U R                   5      US'   SnU R                  " SUU R                  S.UD6  g)zObtain a new access token.r<   r?   z0https://oauth.reddit.com/grants/installed_client)r   r   Nr   )r   rE   r}   r   )r   r   r   s      r   r   DeviceIDAuthorizer.refresh  sS    <<),$,,)?g&G
 	
!oo	
  	
r   )r   r   r   )ri   r   r   rU   rI   r   rV   rW   r   )r[   r\   r]   r^   r_   r   rD   rd   r$   r   r`   r   r   s   @r   r   r     sX     01GH
 !%#'	(  !	
 
 0

 

r   r   )"r_   
__future__r   ru   abcr   r   typingr   r   r   requestsr	   requests.status_codesr
   r   r   
exceptionsr   r   r   requests.modelsr   prawcore.requestorr   r   rb   r   rD   r   r   r   r   r   r   r   r   <module>r      s    8 "  # / /  '  L L(,m  m `A#S A#H2, 2>". "L& L&^, ,@R R@1
z 1
h,
 ,
r   