/
login.py
78 lines (59 loc) · 2.31 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
import urllib
from urlparse import urlparse
import httplib
from Cookie import SimpleCookie
from devilryclient.utils import findconffolder
from getpass import getpass
from os.path import join, exists
from os import chmod
import stat
import ConfigParser
class LoginError(Exception):
"""Raised on login error"""
def get_session_cookie():
if exists(join(findconffolder(), 'session')):
session = open(join(findconffolder(), 'session'), 'r')
cookieout = session.read()
session.close()
return cookieout
else:
return login()
def login():
confdir = findconffolder()
conf = ConfigParser.ConfigParser()
conf.read(join(confdir, 'config'))
# make the url and credentials
url = join(conf.get('resources', 'url'), 'authenticate/login')
username = raw_input("Username: ")
password = getpass("Password: ")
creds = urllib.urlencode({'username': username, 'password': password})
parsed_url = urlparse(url)
host = parsed_url.netloc
if parsed_url.scheme == "https":
conn = httplib.HTTPSConnection(host)
else:
conn = httplib.HTTPConnection(host)
response = conn.request('POST', parsed_url.path, creds, {'Content-type': "application/x-www-form-urlencoded"})
response = conn.getresponse()
if response.status > 400:
raise LoginError("Login to %s failed with the following message: %s %s (%s)" % (
url, response.status, response.reason, response.msg))
response.read()
setcookie = response.getheader('Set-Cookie')
if setcookie == None:
raise LoginError("Login failed. This is usually because of "
"invalid username/password, but might be "
"caused by wrong login urlprefix or server errors. "
"Technical error message: Login urlprefix did not "
"respond with any authorization cookies.")
cookie = SimpleCookie()
cookie.load(setcookie)
cookieout = cookie.output().replace('Set-Cookie: ', '')
session = open(join(confdir, 'session'), 'w')
session.write(cookieout)
session.close()
chmod(join(confdir, 'session'), stat.S_IRUSR | stat.S_IWUSR)
conf.set('resources', 'user', username)
with open(join(confdir, 'config'), 'wb') as f:
conf.write(f)
return cookieout