B
    Tò†[k  ã               @   s¬   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m	Z	 ddl
mZ ddlmZ ddlmZmZmZmZ e d¡Zd	d
„ Zddd„Zdd„ Zdd„ Zdd„ ZdS )z
Authenticate a user
é    )Úunicode_literalsN)Úurlparse)Úinput)Úerrors)Ú
get_configÚget_server_apiÚstore_tokenÚ
bool_inputzbinstar.loginc          
   K   sœ   y
| f |ŽS  t jk
r– } zl| d¡r„t|jƒdkr„|jd dkr„t dt ¡  ¡ t d¡ t d¡ t	dƒr„d|d< | f |ŽS ‚ W d	d	}~X Y nX d	S )
zg
    Authenticates using the given *authenticate*, retrying if the token needs
    to be replaced.
    Úfail_if_already_existsé   i  z1It appears you are already logged in from host %szyLogging in again will remove the previous token. (This could cause troubles with virtual machines with the same hostname)zPOtherwise you can login again and specify a different hostname with "--hostname"zWould you like to continueFN)
r   ÚBinstarErrorÚgetÚlenÚargsÚloggerZwarningÚsocketZgethostnamer	   )ÚauthenticateÚkwargsÚerr© r   ú\lib/python3.7/site-packages/anaconda_client-1.7.2-py3.7.egg/binstar_client/commands/login.pyÚtry_replace_token   s    
&


r   Tc             C   sÎ  t | j| jƒ}t| jd}d }t| dt ¡ ƒ}| jp<| d¡}| dd¡}d}|rf|dkrf|d| 7 }|d	t 	¡ |f 7 }| 
¡  | ¡ }	|	d
krÆt|j||d tj¡||d}|d krÂt d¡‚nt| dd ƒrÚ| j}
ntdƒ}
t| dd ƒ}xŠtdƒD ]~}yPtj d|
 ¡ |d kr&tjtjd}t|j|
|||d tj¡||d}P W qø tjk
rt   t d¡ d }wøY qøX qøW |d krÊt|ƒ}|j d¡r d}n|j}|j|f}dd|  d }t |¡‚|S )N)ÚsiteÚhostnameZdefault_siteÚurlzhttps://api.anaconda.orgzbinstar_client:)ZbinstarZanacondaz%s:z%s@%sZkerberosú )ÚapplicationÚapplication_urlÚcreated_withr
   r   zUUnable to authenticate via Kerberos. Try refreshing your authentication using `kinit`Úlogin_usernamez
Username: Úlogin_passwordé   z%s's )Ústream)ÚusernameÚpasswordr   r   r   r
   r   z7Invalid Username password combination, please try againzapi.anaconda.orgzanaconda.orgzSorry. Please try again z'(go to %s://%s/account/forgot_password zto reset your password)) r   Útokenr   r   ÚgetattrÚplatformÚnoder   ÚgetpassZgetuserZcheck_serverZauthentication_typer   Zkrb_authenticateÚjoinÚsysÚargvr   r   r   r   ÚrangeÚstderrÚwriter   ZUnauthorizedr   Úerrorr   ÚnetlocÚ
startswithZscheme)r   r
   ZbsZconfigr%   r   r   r   Z	auth_nameZ	auth_typer#   r$   Ú_Z
parsed_urlr1   Z	hostpartsÚmsgr   r   r   Úinteractive_get_token,   sp    








r5   c             C   s    t | ƒ}t|| ƒ t d¡ d S )Nzlogin successful)r5   r   r   Úinfo)r   r%   r   r   r   Úinteractive_login~   s    
r7   c             C   s   t | ƒ d S )N)r7   )r   r   r   r   Úmain„   s    r8   c             C   sT   | j ddtd}|jdt ¡ dd |jddd	d
 |jdddd
 |jtd d S )NZloginzAuthenticate a user)ÚhelpZdescriptionz
--hostnamezQSpecify the host name of this login, this should be unique (default: %(default)s))Údefaultr9   z
--usernamer   zASpecify your username. If this is not given, you will be prompted)Údestr9   z
--passwordr    zASpecify your password. If this is not given, you will be prompted)r8   )Ú
add_parserÚ__doc__Úadd_argumentr'   r(   Zset_defaultsr8   )Z
subparsersZ	subparserr   r   r   r<   ˆ   s    r<   )T)r=   Z
__future__r   r)   Zloggingr'   r   r+   Zsix.moves.urllib.parser   Z	six.movesr   Zbinstar_clientr   Zbinstar_client.utilsr   r   r   r	   Z	getLoggerr   r   r5   r7   r8   r<   r   r   r   r   Ú<module>   s    

R