
    ,OfD                        d dl Z d dlZd dlZd dl mZ d dlmZmZmZ d dlmZm	Z	m
Z
mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZmZ erd d	lmZ d
Z ee      Z G d de j                        Z G d dee j                        Z G d de      Z G d de      Z G d dee      Z G d de      Z G d de      Z  G d deed         Z! G d deed         Z" G d deed         Z#y)     N)ABC)datetime	timedeltatimezone)TYPE_CHECKINGDictOptionalUnion)utils)Consts)InstallationAuthorization)	RequesterWithRequesterGithubIntegration   )secondsc                       e Zd ZdZeej                  defd              Zeej                  defd              Z	de
ddfdZde
ddfdZedefd	       Zy)
AuthzS
    This class is the base class of all authentication methods for Requester.
    returnc                      y)z
        The type of the auth token as used in the HTTP Authorization header, e.g. Bearer or Basic.

        :return: token type

        N selfs    +lib/python3.12/site-packages/github/Auth.py
token_typezAuth.token_type9           c                      y)zc
        The auth token as used in the HTTP Authorization header.

        :return: token

        Nr   r   s    r   tokenz
Auth.tokenC   r   r   headersNc                 @    | j                    d| j                   |d<   y)z3
        Add authorization to the headers.
         AuthorizationN)r   r    r   r!   s     r   authenticationzAuth.authenticationM   s"     '+oo%6a

|#D r   c                 "    | j                   |d<   y)z0
        Mask header, e.g. for logging.
        r$   N)_masked_tokenr%   s     r   mask_authenticationzAuth.mask_authenticationS   s     $(#5#5 r   c                      y)Nz(unknown auth removed)r   r   s    r   r(   zAuth._masked_tokenY   s    'r   )__name__
__module____qualname____doc__propertyabcabstractmethodstrr   r    dictr&   r)   r(   r   r   r   r   r   4   s     C    s   Ed Et E64 6D 6 (s ( (r   r   c                       e Zd Zeej
                  defd              Zeej
                  defd              Zedefd       Z	edefd       Z
edefd       Zy)HTTPBasicAuthr   c                      y)z
        The username.
        Nr   r   s    r   usernamezHTTPBasicAuth.username_   r   r   c                      y)z
        The password.
        Nr   r   s    r   passwordzHTTPBasicAuth.passwordf   r   r   c                      y)NBasicr   r   s    r   r   zHTTPBasicAuth.token_typem       r   c                     t        j                  | j                   d| j                   j	                               j                  d      j                  dd      S )N:utf-8
 )base64	b64encoder7   r9   encodedecodereplacer   s    r   r    zHTTPBasicAuth.tokenq   sJ    4==/4==/ B I I KLSST[\ddeikmnnr   c                      y)Nz"Basic (login and password removed)r   r   s    r   r(   zHTTPBasicAuth._masked_tokenu   s    3r   N)r+   r,   r-   r/   r0   r1   r2   r7   r9   r   r    r(   r   r   r   r5   r5   ^   s    #   
 #   
 C   os o o 4s 4 4r   r5   c                   b    e Zd ZdZdedefdZedefd       Zedefd       Zedefd       Z	y	)
LoginzE
    This class is used to authenticate with login and password.
    loginr9   c                     t        |t              sJ t        |      dkD  sJ t        |t              sJ t        |      dkD  sJ || _        || _        y Nr   )
isinstancer2   len_login	_password)r   rJ   r9   s      r   __init__zLogin.__init__   sR    %%%%5zA~~(C(((8}q   !r   r   c                     | j                   S NrO   r   s    r   rJ   zLogin.login       {{r   c                     | j                   S rS   rJ   r   s    r   r7   zLogin.username       zzr   c                     | j                   S rS   rP   r   s    r   r9   zLogin.password   s    ~~r   N)
r+   r,   r-   r.   r2   rQ   r/   rJ   r7   r9   r   r   r   rI   rI   z   sk    "c "S " s   #   #  r   rI   c                   ^    e Zd ZdZdefdZedefd       Zedefd       Zedefd       Z	y)	TokenzJ
    This class is used to authenticate with a single constant token.
    r    c                 V    t        |t              sJ t        |      dkD  sJ || _        y rL   rM   r2   rN   _tokenr   r    s     r   rQ   zToken.__init__   )    %%%%5zA~~r   r   c                      yNr    r   r   s    r   r   zToken.token_type   r<   r   c                     | j                   S rS   r_   r   s    r   r    zToken.token   rU   r   c                      yNztoken (oauth token removed)r   r   s    r   r(   zToken._masked_token       ,r   N)
r+   r,   r-   r.   r2   rQ   r/   r   r    r(   r   r   r   r\   r\      sd    c 
 C   s   -s - -r   r\   c                   &    e Zd ZdZedefd       Zy)JWTz
    This class is the base class to authenticate with a JSON Web Token (JWT).

    https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app

    r   c                      y)NBearerr   r   s    r   r   zJWT.token_type   s    r   N)r+   r,   r-   r.   r/   r2   r   r   r   r   rj   rj      s"     C  r   rj   c                      e Zd ZdZej
                  ej                  ej                  fdee	e
f   de
de	de	de
f
dZedee	e
f   fd	       Zede
fd
       Zede
fd       Z	 	 dde	deee
e
f      dee   ddfdZddee	   de
fdZy)AppAuthz
    This class is used to authenticate as a GitHub App.

    https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app

    app_idprivate_key
jwt_expiryjwt_issued_atjwt_algorithmc                    t        |t        t        f      sJ |       t        |t              rt        |      dkD  sJ d       t        |t              sJ t        |      dkD  sJ d       t        |t              sJ |       t        j
                  |cxk  rt        j                  k  sJ |        J |       || _        || _        || _	        || _
        || _        y )Nr   zapp_id must not be emptyzprivate_key must not be empty)rM   intr2   rN   r   MIN_JWT_EXPIRYMAX_JWT_EXPIRY_app_id_private_key_jwt_expiry_jwt_issued_at_jwt_algorithm)r   ro   rp   rq   rr   rs   s         r   rQ   zAppAuth.__init__   s     &3*-5v5-fc"v;?>$>>?+s+++;!#D%DD#*c*6J6*$$
Kf6K6KKWZWKWZWK'%++r   r   c                     | j                   S rS   )rx   r   s    r   ro   zAppAuth.app_id   s    ||r   c                     | j                   S rS   )ry   r   s    r   rp   zAppAuth.private_key   s       r   c                 "    | j                         S rS   )
create_jwtr   s    r   r    zAppAuth.token   s      r   Ninstallation_idtoken_permissions	requesterAppInstallationAuthc                     t        | |||      S )a  
        Creates a github.Auth.AppInstallationAuth instance for an installation.

        :param installation_id: installation id
        :param token_permissions: optional permissions
        :param requester: optional requester with app authentication
        :return:

        )r   )r   r   r   r   s       r   get_installation_authzAppAuth.get_installation_auth   s     #4:KYWWr   
expirationc                    |Mt        |t              sJ |       t        j                  |cxk  rt        j                  k  sJ |        J |       t        t        j
                               }|| j                  z   |||n| j                  z   | j                  d}t        j                  || j                  | j                        }t        |t              r|j                  d      S |S )z
        Create a signed JWT
        https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-a-github-app

        :return string: jwt
        )iatexpiss)key	algorithmr?   )rM   ru   r   rv   rw   timer{   rz   rx   jwtrD   rp   r|   bytesrE   )r   r   nowpayload	encrypteds        r   r   zAppAuth.create_jwt   s     !j#.:
:.((JO&:O:OO[Q[[O[Q[[O$))+,,,
(>*DDTDTU<<

 JJwD,<,<H[H[\	i'##G,,r   NNrS   )r+   r,   r-   r.   r   DEFAULT_JWT_EXPIRYDEFAULT_JWT_ISSUED_ATDEFAULT_JWT_ALGORITHMr
   ru   r2   rQ   r/   ro   rp   r    r	   r   r   r   r   r   r   r   rn   rn      s    !33#99#99,c3h, , 	,
 , ,, c3h   !S ! ! !s ! ! 7;)-	XX $DcN3X I&	X
 
X"Xc] c r   rn   c                   2    e Zd ZdZdefdZedefd       Zy)AppAuthTokenz
    This class is used to authenticate as a GitHub App with a single constant JWT.

    https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app

    r    c                 V    t        |t              sJ t        |      dkD  sJ || _        y rL   r^   r`   s     r   rQ   zAppAuthToken.__init__  ra   r   r   c                     | j                   S rS   re   r   s    r   r    zAppAuthToken.token  rU   r   N)r+   r,   r-   r.   r2   rQ   r/   r    r   r   r   r   r     s.    c 
 s  r   r   c            
       d    e Zd ZU dZdZed   ed<   dZee   ed<   	 	 dde	de
deeeef      d	ee   f fd
Zd	edd f fdZedee
ef   fd       Zedefd       Zede
fd       Zedeeeef      fd       Zedefd       Zedefd       Zedefd       ZdefdZedefd       Z xZS )r   z
    This class is used to authenticate as a GitHub App Installation.

    https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation

    Nr   !_AppInstallationAuth__integration0_AppInstallationAuth__installation_authorizationapp_authr   r   r   c                 4   t         |           t        |t              sJ |       t        |t              sJ |       |t        |t
              sJ |       |t        |t              sJ |       || _        || _        || _	        || j                  |       y y rS   )superrQ   rM   rn   ru   r3   r   	_app_auth_installation_id_token_permissionswithRequester)r   r   r   r   r   	__class__s        r   rQ   zAppInstallationAuth.__init__*  s     	(G,6h6,/3/@@/ (J7H$,ObQbbO Jy)$DOiOD! /"3 y) !r   r   c                     t        |t              sJ |       t        |   |j	                  | j
                               ddlm}  |di | j                  j                  | _
        | S )Nr   r   r   )rM   r   r   r   withAuthr   github.GithubIntegrationr   r   kwargsr   )r   r   r   r   s      r   r   z!AppInstallationAuth.withRequester?  sX    )Y/::/i00@A 	?.G1F1FGr   c                 .    | j                   j                  S rS   )r   ro   r   s    r   ro   zAppInstallationAuth.app_idJ  s    ~~$$$r   c                 .    | j                   j                  S rS   )r   rp   r   s    r   rp   zAppInstallationAuth.private_keyN  s    ~~)))r   c                     | j                   S rS   )r   r   s    r   r   z#AppInstallationAuth.installation_idR  s    $$$r   c                     | j                   S rS   )r   r   s    r   r   z%AppInstallationAuth.token_permissionsV  s    &&&r   c                      yrc   r   r   s    r   r   zAppInstallationAuth.token_typeZ  r<   r   c                     | j                   | j                  r| j                         | _         | j                   j                  S rS   )r   _is_expired_get_installation_authorizationr    r   s    r   r    zAppInstallationAuth.token^  s9    ,,48H8H040T0T0VD-00666r   c                     | j                   J | j                   j                  t        z
  }|t        j                  t
        j                        k  S rS   )r   
expires_at!TOKEN_REFRESH_THRESHOLD_TIMEDELTAr   r   r   utc)r   token_expires_ats     r   r   zAppInstallationAuth._is_expiredd  sD    00<<<<<GGJkk(,,x||"<<<r   c                     | j                   J d       | j                   j                  | j                  | j                        S )N4Method withRequester(Requester) must be called first)permissions)r   get_access_tokenr   r   r   s    r   r   z3AppInstallationAuth._get_installation_authorizationj  sJ    !!-e/ee-!!22!!// 3 
 	
r   c                      yrg   r   r   s    r   r(   z!AppInstallationAuth._masked_tokenq  rh   r   r   )r+   r,   r-   r.   r   r	   __annotations__r   r   rn   ru   r   r2   r   rQ   r   r/   r
   ro   rp   r   r   r   r    boolr   r   r(   __classcell__r   s   @r   r   r     s~    48M8/07HL (+D"EL 7;)-** * $DcN3	*
 I&**	y 	5J 	 %c3h % % *S * * % % % '8DcN#; ' ' C   7s 7 7
 =T = =

1J 
 -s - -r   r   c                       e Zd ZU dZeed<   eed<   eed<   eed<   ee   ed<   ee   ed<   ee   ed<   ee   ed	<   d
dlm	Z	 e	ed<   	 	 	 	 	 d!dedededee   dee   dee   dee   dee
   ddf fdZedefd       Zedefd       Zde
dd f fdZedefd       Zd"dZedee   fd       Zedee   fd       Zedee   fd       Zedefd        Z xZS )#AppUserAuthz
    This class is used to authenticate as a GitHub App on behalf of a user.

    https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-with-a-github-app-on-behalf-of-a-user

    
_client_id_client_secretr_   _type_scope_expires_at_refresh_token_refresh_expires_atr   ApplicationOAuth_AppUserAuth__appN	client_idclient_secretr    r   r   refresh_tokenrefresh_expires_atr   r   c	                    t         	|           t        |t              rt	        |      dkD  sJ t        |t              rt	        |      dkD  sJ t        |t              rt	        |      dkD  sJ |%t        |t              rt	        |      dkD  sJ |       |t        |t
              sJ |       | t        |t              rt	        |      dkD  sJ |t        |t
              sJ |       |t        |t              sJ |       || _        || _        || _	        |xs d| _
        || _        || _        || _        || j                  |       y y )Nr   bearer)r   rQ   rM   r2   rN   r   r   r   r   r_   r   r   r   r   r   )
r   r   r   r    r   r   r   r   r   r   s
            r   rQ   zAppUserAuth.__init__  sM    	)S)c)nq.@@@--#m2Dq2HHH%%#e*q.88!Z
C%@S_WXEXdZddX!Z
H%EQzQE$
=#(F3}K]`aKaaa!)Z8JH-UiWiiU Jy)$DOiOD#++8
%+#5  y) !r   c                     | j                   S rS   )r   r   s    r   r   zAppUserAuth.token_type  rX   r   c                 R    | j                   r| j                          | j                  S rS   )r   _refreshr_   r   s    r   r    zAppUserAuth.token  s    MMO{{r   c                     t        |t              sJ |       t        |   |j	                  d              ddlm}  |t        |   i | j                  | j                  dd      | _
        | S )Nr   r   )r   r   F)r!   
attributes	completed)rM   r   r   r   r   github.ApplicationOAuthr   r   r   r   r   )r   r   r   r   s      r   r   zAppUserAuth.withRequester  sm    )Y/::/i0067 	=%G!__!%!4!4 	

 r   c                     | j                   d uxr0 | j                   t        j                  t        j                        k  S rS   )r   r   r   r   r   r   s    r   r   zAppUserAuth._is_expired  s3    t+]0@0@8<<PXP\P\C]0]]r   c                    | j                   t        d      | j                  ;| j                  t        j                  t
        j                        k  rt        d      | j                  j                  | j                         }|j                  | _
        |j                  | _        |j                  | _        |j                  | _        |j"                  | _         |j$                  | _        y )NzGCannot refresh expired token because no refresh token has been providedz?Cannot refresh expired token because refresh token also expired)r   RuntimeErrorr   r   r   r   r   r   refresh_access_tokenr    r_   typer   scoper   r   r   r   r   r`   s     r   r   zAppUserAuth._refresh  s    &hii##/D4L4Lx||\d\h\hOi4i`aa 

//0C0CD kkZZ
kk ++#11#(#;#; r   c                     | j                   S rS   )r   r   s    r   r   zAppUserAuth.expires_at  s    r   c                     | j                   S rS   )r   r   s    r   r   zAppUserAuth.refresh_token  s    """r   c                     | j                   S rS   )r   r   s    r   r   zAppUserAuth.refresh_expires_at  s    '''r   c                      y)NzBearer (jwt removed)r   r   s    r   r(   zAppUserAuth._masked_token  s    %r   )NNNNNr   N)r+   r,   r-   r.   r2   r   r	   r   r   r   r   rQ   r/   r   r    r   r   r   r   r   r   r   r(   r   r   s   @r   r   r   v  s    OKJSM(##SM!!(++ 9 %))-'+15)-** * 	*
 SM* X&*  }* %X.* I&* 
*B C   s  
y ] ( ^T ^ ^<"  HX.     #x} # # (HX$6 ( ( &s & &r   r   c                   z     e Zd ZdZd	 fdZedefd       Zedefd       Zedefd       Z	de
dd f fdZ xZS )
	NetrcAuthz8
    This class is used to authenticate via .netrc.
    r   c                 >    t         |           d | _        d | _        y rS   )r   rQ   rO   rP   )r   r   s    r   rQ   zNetrcAuth.__init__  s    %)(,r   c                     | j                   S rS   rW   r   s    r   r7   zNetrcAuth.username  rX   r   c                 @    | j                   J d       | j                   S Nr   rT   r   s    r   rJ   zNetrcAuth.login   s"    {{&^(^^&{{r   c                 @    | j                   J d       | j                   S r   rZ   r   s    r   r9   zNetrcAuth.password  s"    ~~)a+aa)~~r   r   c                     t        |t              sJ |       t        |   |       t	        j
                  |j                  d      }|t        d|j                         |\  | _	        | _
        | S )NT)raise_errorsz.Could not get credentials from netrc for host )rM   r   r   r   r   get_netrc_authbase_urlr   hostnamerO   rP   )r   r   authr   s      r   r   zNetrcAuth.withRequester
  sp    )Y/::/i(##I$6$6TJ<!OPYPbPbOcdee&*#T^r   r   )r+   r,   r-   r.   rQ   r/   r2   r7   rJ   r9   r   r   r   r   s   @r   r   r     sv    - #   s   #  
y 
[ 
 
r   r   )$r0   rB   r   r   r   r   r   typingr   r   r	   r
   r   requestsr   githubr    github.InstallationAuthorizationr   github.Requesterr   r   r   r   &ACCESS_TOKEN_REFRESH_THRESHOLD_SECONDSr   r   r5   rI   r\   rj   rn   r   r   r   r   r   r   r   <module>r      s   :     2 2 7 7 
   F 5: *, &$-6\$] !'(377 '(T4D#'' 48M 6-D -.
$ 
Pc Pf3 $U-$.C D U-px&$m4 x&v#}[9 #r   