
    =e&              
       $   U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	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 ddlmZ e	j        rddlZ ej        d	          Zd
Zded<    G d de	j                  Z edddd           edddd           edddd           edddd           edddd          fZded<   d%de	j        ej                 dede fd Z!d!e	j"        e	j#        e e	j$        f                  ddfd"Z%d# Z&d$ Z'dS )&z
Manage Authentication tokens

See also:

* [Using Anaconda.org Tokens](
  https://docs.anaconda.org/free/anacondaorg/user-guide/tasks/work-with-accounts/#cloud-accounts-tokens)

    )print_functionN)parse)input)errors)get_server_api)tableszbinstar.authz

Examples

To allow access to only conda downloads from your account you can run:

    anaconda auth --create --scopes 'repos conda:download'

To allow full access to your account:

    anaconda auth --create --scopes 'all'

ztyping_extensions.Final[str]SCOPE_EXAMPLESc                   <    e Zd ZU dZeed<   eed<   eed<   eed<   dS )TimeDeltaGroupa  
    Rule to format timedelta.

    :param amount: maximum allowed amount for this group.

                   if actual value exceeds it - this rule is skipped and next should be processed.

    :param strict: if set to :code:`True` - actual value would be divided by :code:`amount` before checking next rules.

    :param format: format string that should be used to format the output value.

    :param name: name of the units to display.
    amountstrictformatnameN)__name__
__module____qualname____doc__int__annotations__boolstr     Flib/python3.11/site-packages/binstar_client/commands/authorizations.pyr   r   5   s@           KKKLLLKKK
IIIIIr   r   <   Tdsecond)r   r   r   r   minute   z1.1fhour   Fdayz8typing_extensions.Final[typing.Sequence[TimeDeltaGroup]]TIME_DELTA_GROUPSdateexpiredreturnc                    | sdS d}t           j                                                             t          j                  }| |k     r
|rdS | |} }d}| |z
                                  }t          D ]s}||j        cxk    rdk    rn n|j        r
||j        z  })|j	        
                    d          rt          |          }|dk    rd	| }||j	         d
|j         | c S dS )Nnever )tzinfor%   z agor   r      s unknown)datetimeutcnowreplacepytzutctotal_secondsr#   r   r   r   endswithr   r   )r$   r%   resultnowdeltagroups         r   format_timedeltar:   S   s&    wF%.5577??tx?PPCczz 	9#T (,cz&@&@&B&BE" > >5<####!#####| &%<  %% 	JJEA::!\\F====V=====9r   authenticationsc           	         t          j        dd          }| D ]c}i |dt          |d         rt          |d                   nd           i}d}|d         pdD ]"}||d<   |                    |           |rd}i }#dt
                              d	           |                    t           j                  D ]}t
                              |           t
                              d	           d S )
N))idID)applicationApplication)remote_addrzRemote Addr)hostnameHost)expiresz
Expires In)scopesScopesr+   )aliasesheading_rowsrD   TrE   )NO_SCOPEFr)   )	r   SimpleTableWithAliasesr:   
parse_date
append_rowloggerinforenderSIMPLE)r;   tableauthfirstscopelines         r   
show_authsrV   s   s    +1+H
 
, 
, 
,E    

'tI(`
4	?(C(C(C\`aa
 

 (^4} 	 	E"DNT""" 	 KKOOOV]++  D
KKOOOOOr   c                 z   t          | j        | j                  }| j        rV|                                }t
                              d|d                    t
                              d|d                    | j        r#t          |                                           d S | j	        rB| j	        D ]8}|
                    || j                   t
                              d|           9d S | j        r|                                }t          |          D ]W}t
                              |           t
                              d||                    t
                              d           Xt
                              t                     d S | j        r#|                                }	 |                                }|d         }n# t$          $ rt |d	k    rt
                              d
           Y d S d }t(          j                            d           t(          j                                         t1          d          }Y nw xY wd | j        D             }|sVt
                              d           t
                              d| j        p|           t
                              d           t7          d          D ]}		 |d	k    r7|                    d | j        | j        || j        | j        | j         d          }
nlt(          j                            d|z             tC          j!                    }|"                    ||| j        | j        || j        | j        | j         d	  	        }
| j#                            |
            d S # tH          j%        $ r t
                              d           Y w xY wd S d S )NzName: %sr?   zId: %sr=   zRemoved token %sz  %sr)   loginkerberoszKKerberos authentication needed, please use 'anaconda login' to authenticatez
Username: c                 @    g | ]}|                                 D ]}|S r   )split).0rE   rT   s      r   
<listcomp>zmain.<locals>.<listcomp>   s-    NNNFv||~~NNe%NNNNr   zLYou have not specified the scope of this token with the '--scopes' argument.z1This token will grant full access to %s's accountz=Use the --list-scopes option to see a listing of your optionsr!   T)application_urlrE   for_usermax_agestrengthfail_if_already_existszPlease re-enter %s's z7Invalid Username password combination, please try again)&r   tokensiterN   authenticationrM   listrV   r;   removeremove_authenticationorganizationlist_scopessortedr	   createauthentication_typeuser	Exceptionerrorsysstderrwriteflushr   rE   warningrange_authenticater   urlr`   ra   getpassauthenticateoutr   Unauthorized)argsaserver_apidata	auth_namerE   key	auth_typecurrent_userusername_rc   passwords               r   mainr      s    TY77Ky *))++J] 3444Hd4j)))y D;..00111	 A 	7 	7I--i9JKKKKK*I6666		 <((**&>> 	 	CKKKKs,,,KKOOOON#####	 43355		!&++--L#G,HH 	! 	! 	!J&&jkkkLJ\***JRyyHHH	! ONdkNNN 	\NNklllNNOQUQbQnfnoooNNZ[[[q 	 	A
**'55	(,%!%!2 $!%/3 6 	 	EE J$$%<x%GHHH&00H'44 (	(,%!%!2 $!%/3 5 	 	E u%%%&   VWWWe4 4,	 	s,   +G *I5AIICN

)N65N6c                 H   d}|                      d||t          t          j                  }|                    dddt          j                    z  d           |                    d	d
ddd           |                    dd          }|                    dddgdd           |                    ddddd           |                    dddddd           |                    ddd           |                    d t          d!"           |                    d#d$d%d&g '           |                    d(t          j
        t          j        d)          *           |                    d+          }|                    d,-          }|                    d.d/d0d12           |                    d3d4d0d52           |                    d6d7d8d9d:;           |                    d<d=d0d>2           |                    d?d@dAdBd0dCD           |                    t          E           d S )FNzManage Authorization TokensrR   )helpdescriptionepilogformatter_classz-nz--namezbinstar_token:%szdA unique name so you can identify this token later. View your tokens at anaconda.org/settings/access)defaultr   z-oz--orgz--organizationz-Set the token owner (must be an organization)ri   )r   destztoken creation argumentsz9These arguments are only valid with the `--create` actionz
--strengthstrongweakra   )choicesr   r   z--strongstore_constzCreate a longer token (default))actionconstr   r   z-wz--weakzCreate a shorter tokenz--urlzhttp://anaconda.orgz3The url of the application that will use this tokenz	--max-agez<The maximum age in seconds that this token will be valid for)typer   z-sz--scopesappendzScopes for token. For example if you want to limit this token to conda downloads only you would use --scopes "repo conda:download")r   r   r   z--outw)r   r   actionsT)requiredz-xz--list-scopes
store_truezlist all authentication scopes)r   r   z-lz--listz#list all user authentication tokensz-rz--removeNAME+zremove authentication tokens)metavarnargsr   z-cz--createzCreate an authentication tokenz-iz--infoz--current-inforN   z7Show information about the current authentication token)r   r   r   )r   )
add_parserr   argparseRawDescriptionHelpFormatteradd_argumentsocketgethostnameadd_argument_groupr   rq   stdoutFileTypeadd_mutually_exclusive_groupset_defaultsr   )
subparsersr   parsertoken_groupr9   s        r   r   r      s   /K""6(3/:*13;3W	 # Y YF h"f&8&:&:;s     g'7LSa  c c c ++,F,gi iK \Hf3Ex^hiiiZX",3T  V V VT8M",3K  M M M W.C"W  Y Y Y[s9wxxxj/      Wcjx?PQT?U?UVVV%%i00E...==E	t_\Hhiii	tXlAfggg	tZsIghhh	tZCcddd	tX'7f*1j  l l l T"""""r   )T)(r   
__future__r   r   r/   ry   loggingr   rq   typingr2   dateutil.parserr   rK   	six.movesr   binstar_clientr   binstar_client.utilsr   r   TYPE_CHECKINGtyping_extensions	getLoggerrM   r	   r   
NamedTupler   r#   Optionalr   r   r:   SequenceMappingAnyrV   r   r   r   r   r   <module>r      sd     & % % % % %      



   / / / / / /       ! ! ! ! ! ! / / / / / / ' ' ' ' ' '	  
	>	*	*2.       V&   , N"T#HEEEN"T#HEEEN"T&vFFFN!E&uEEEN!E#EBBBQ M    6?8+<=  X[    @sFJ0O P UY    DJ J JZ0# 0# 0# 0# 0#r   