forked from anaconda/anaconda-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.py
113 lines (91 loc) · 3.77 KB
/
login.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'''
Authenticate a user
'''
from __future__ import unicode_literals
import getpass
import logging
import socket
import sys
from binstar_client import errors
from binstar_client.utils import get_config, get_binstar, store_token, \
bool_input
import platform
log = logging.getLogger('binstar.login')
try:
input = raw_input
except NameError:
input = input
def interactive_get_token(args, fail_if_already_exists=True):
bs = get_binstar(args)
config = get_config(remote_site=args.site)
url = config.get('url', 'https://api.anaconda.org')
token = None
hostname = getattr(args, 'hostname', platform.node())
if getattr(args, 'login_username', None):
username = args.login_username
else:
username = input('Username: ')
auth_name = 'binstar_client:'
site = args.site or config.get('default_site')
if site and site != 'binstar':
# For testing with binstar alpha site
auth_name += '%s:' % site
auth_name += '%s@%s' % (getpass.getuser(), hostname)
password = getattr(args, 'login_password', None)
for _ in range(3):
try:
sys.stderr.write("%s's " % username)
if password is None:
password = getpass.getpass(stream=sys.stderr)
token = bs.authenticate(username, password, auth_name, url,
created_with=' '.join(sys.argv),
fail_if_already_exists=fail_if_already_exists,
hostname=hostname)
break
except errors.Unauthorized:
log.error('Invalid Username password combination, please try again')
password = None
continue
except errors.BinstarError as err:
if fail_if_already_exists is True and err.args[1] == 400:
log.warn('It appears you are already logged in from host %s' % socket.gethostname())
log.warn('Logging in again will remove the previous token. '
' (This could cause troubles with virtual machines with the same hostname)')
log.warn('Otherwise you can login again and specify a '
'different hostname with "--hostname"')
if bool_input("Would you like to continue"):
fail_if_already_exists = False
continue
else:
raise
if token is None:
msg = ('Sorry. Please try again '
'(go to https://anaconda.org/account/forgot_password '
'to reset your password)')
raise errors.BinstarError(msg)
return token
def interactive_login(args):
token = interactive_get_token(args)
store_token(token, args)
log.info('login successful')
def main(args):
interactive_login(args)
def add_parser(subparsers):
subparser = subparsers.add_parser('login',
help='Authenticate a user',
description=__doc__)
subparser.add_argument('--hostname', default=platform.node(),
help="Specify the host name of this login, "
"this should be unique (default: %(default)s)"
)
subparser.add_argument('--username',
dest='login_username',
help="Specify your username. "
"If this is not given, you will be prompted"
)
subparser.add_argument('--password',
dest='login_password',
help="Specify your password. "
"If this is not given, you will be prompted"
)
subparser.set_defaults(main=main)