
    ,Of                         d dl mZ d dlZd dlmZmZ d dlZd dl	Zd dl
mZmZ d dlmZmZ d dlmZmZmZ d dlmZ erd dlmZ d d	l	mZ  G d
 de      Zy)    )annotationsN)TYPE_CHECKINGAny)DEFAULT_BASE_URLDEFAULT_OAUTH_URL)BadCredentialsExceptionGithubException)	AttributeNonCompletableGithubObjectNotSet)	Requester)AccessToken)AppUserAuthc                       e Zd ZdZddZ	 	 	 	 	 	 	 	 	 	 d fdZddZedd       Zedd       Z	ddZ
ddZ	 	 	 d	 	 	 	 	 	 	 dd	Zddd
ZddZddZedd       Z xZS )ApplicationOAuthz
    This class is used for identifying and authorizing users for Github Apps.

    The reference can be found at
    https://docs.github.com/en/developers/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps

    c                0    t         | _        t         | _        y N)r   
_client_id_client_secretselfs    7lib/python3.12/site-packages/github/ApplicationOAuth.py_initAttributesz ApplicationOAuth._initAttributes8   s    *0.4    c                N    |j                  d       }t        | 	  ||||       y )N)auth)withAuthsuper__init__)r   	requesterheaders
attributes	completed	__class__s        r   r   zApplicationOAuth.__init__<   s+     &&D&1	GZCr   c                P    | j                  d| j                  j                  i      S )N	client_id)get__repr__r   valuer   s    r   __repr__zApplicationOAuth.__repr__G   s"    doo.C.C DEEr   c                .    | j                   j                  S r   )r   r(   r   s    r   r&   zApplicationOAuth.client_idJ   s    $$$r   c                .    | j                   j                  S r   )r   r(   r   s    r   client_secretzApplicationOAuth.client_secretN   s    ""(((r   c                z    d|v r| j                  |d         | _        d|v r| j                  |d         | _        y y )Nr&   r,   )_makeStringAttributer   r   )r   r"   s     r   _useAttributeszApplicationOAuth._useAttributesR   sF    *$"77
;8OPDOj("&";";J<W"XD )r   c                    |j                  d      sd| }| j                  j                  t        k(  rt        }n0| j                  j
                   d| j                  j                   d}| | S )N/z://z/login/oauth)
startswith
_requesterbase_urlr   r   schemehostname_and_port)r   pathr4   s      r   get_oauth_urlzApplicationOAuth.get_oauth_urlX   si    s#tf:D??##'77(H//001T__5V5V4WWcdHD6""r   c                8   d| j                   i}|t        |t              sJ |       ||d<   |t        |t              sJ |       ||d<   |t        |t              sJ |       ||d<   t        j                  j                  |      }| j                  d|       S )z_
        Return the URL you need to redirect a user to in order to authorize your App.
        r&   redirect_uristateloginz/authorize?)r&   
isinstancestrurllibparse	urlencoder8   )r   r:   r;   r<   
parametersquerys         r   get_login_urlzApplicationOAuth.get_login_urlb   s     "4>>2
#lC0>,>0)5J~&eS)050)"'JweS)050)"'Jw&&z2!!Kw"788r   c                L   t        |t              sJ |       || j                  | j                  d}|||d<    | j                  | j
                  j                  d| j                  d      ddi|       \  }}t        j                  j                  | j
                  ||d	      S )
z
        :calls: `POST /login/oauth/access_token <https://docs.github.com/en/developers/apps/identifying-and-authorizing-users-for-github-apps>`_
        )coder&   r,   r;   POST/access_tokenAcceptapplication/jsonr!   inputFr    r!   r"   r#   
r=   r>   r&   r,   _checkErrorr3   requestJsonAndCheckr8   githubr   )r   rF   r;   post_parametersr!   datas         r   get_access_tokenz!ApplicationOAuth.get_access_tokenz   s     $$*d*$!//
 ',OG$(((__00""?3!#56%	 1 
 !!--oo	 . 
 	
r   c           
         t         j                  j                  | j                  | j                  |j
                  |j                  |j                  |j                  |j                  | j                        S )N)r&   r,   token
token_type
expires_atrefresh_tokenrefresh_expires_atr    )rQ   Authr   r&   r,   rV   typerX   rY   rZ   r3   )r   rV   s     r   get_app_user_authz"ApplicationOAuth.get_app_user_auth   s^    {{&&nn,,++zz''--$77oo ' 	
 		
r   c                6   t        |t              sJ | j                  | j                  d|d} | j                  | j
                  j                  d| j                  d      ddi|       \  }}t        j                  j                  | j
                  ||d	      S )
z
        :calls: `POST /login/oauth/access_token <https://docs.github.com/en/developers/apps/identifying-and-authorizing-users-for-github-apps>`_
        :param refresh_token: string
        rY   )r&   r,   
grant_typerY   rG   rH   rI   rJ   rK   FrM   rN   )r   rY   rR   r!   rS   s        r   refresh_access_tokenz%ApplicationOAuth.refresh_access_token   s    
 ----!//)*	
 )((__00""?3!#56%	 1 
 !!--oo	 . 
 	
r   c                v    t        |t              r&d|v r"|d   dk(  rt        d||       t        d||       | |fS )Nerrorbad_verification_code   )r=   dictr   r	   )r!   rS   s     r   rO   zApplicationOAuth._checkError   sG    dD!goG} 77-c4AA!#tW55}r   )returnNone)
r    r   r!   dict[str, Any]r"   r   r#   boolrf   rg   )rf   r>   )r"   rh   rf   rg   )r7   r>   rf   r>   )NNN)r:   
str | Noner;   rj   r<   rj   rf   r>   r   )rF   r>   r;   rj   rf   r   )rV   r   rf   r   )rY   r>   rf   r   )r!   rh   rS   r   rf   ztuple[dict[str, Any], Any])__name__
__module____qualname____doc__r   r   r)   propertyr&   r,   r/   r8   rD   rT   r]   r`   staticmethodrO   __classcell__)r$   s   @r   r   r   /   s    5	D	D  	D 		D
 	D 
	DF % % ) )Y# $(  	9 9 9 	9
 
90
<


:  r   r   )
__future__r   urllib.parser?   typingr   r   github.AccessTokenrQ   github.Authgithub.Constsr   r   github.GithubExceptionr   r	   github.GithubObjectr
   r   r   github.Requesterr   r   r   r    r   r   <module>r|      s=   < #  %   = K M M &.'Y1 Yr   