Skip to content

Commit 58d051a

Browse files
committed
Manage HTTPS for cookie based authentication
Should fix #14
1 parent 7b5b13b commit 58d051a

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

dokuwiki.py

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import base64
2121
import weakref
2222
from xml.parsers.expat import ExpatError
23-
if sys.version_info[0] == 3:
24-
from xmlrpc.client import ServerProxy, Binary, Fault, Transport
23+
24+
PY_VERSION = sys.version_info[0]
25+
if PY_VERSION == 3:
26+
from xmlrpc.client import ServerProxy, Binary, Fault, Transport, SafeTransport
2527
from urllib.parse import urlencode
2628
else:
27-
from xmlrpclib import ServerProxy, Binary, Fault, Transport
29+
from xmlrpclib import ServerProxy, Binary, Fault, Transport, SafeTransport
2830
from urllib import urlencode
2931

3032
from datetime import datetime, timedelta
@@ -59,53 +61,59 @@ class DokuWikiError(Exception):
5961
"""Exception raised by this module when there is an error."""
6062
pass
6163

62-
class CookiesTransport(Transport):
63-
"""A Python3 xmlrpc.client.Transport subclass that retains cookies."""
64-
def __init__(self):
65-
Transport.__init__(self)
66-
self._cookies = dict()
67-
68-
def send_headers(self, connection, headers):
69-
if self._cookies:
70-
cookies = map(lambda x: x[0] + '=' + x[1], self._cookies.items())
71-
connection.putheader("Cookie", "; ".join(cookies))
72-
Transport.send_headers(self, connection, headers)
73-
74-
def parse_response(self, response):
75-
"""parse and store cookie"""
76-
try:
77-
for header in response.msg.get_all("Set-Cookie"):
78-
cookie = header.split(";", 1)[0]
79-
cookieKey, cookieValue = cookie.split("=", 1)
80-
self._cookies[cookieKey] = cookieValue
81-
finally:
82-
return Transport.parse_response(self, response)
83-
84-
class CookiesTransport2(Transport):
85-
"""A Python2 xmlrpclib.Transport subclass that retains cookies."""
86-
def __init__(self):
87-
Transport.__init__(self)
88-
self._cookies = dict()
89-
90-
def send_request(self, connection, handler, request_body):
91-
Transport.send_request(self, connection, handler, request_body)
92-
# set cookie below handler
93-
if self._cookies:
94-
cookies = map(lambda x: x[0] + '=' + x[1], self._cookies.items())
95-
connection.putheader("Cookie", "; ".join(cookies))
96-
97-
def parse_response(self, response):
98-
"""parse and store cookie"""
99-
try:
100-
for header in response.getheader("set-cookie").split(", "):
101-
# filter 'expire' information
102-
if not header.startswith("D"):
103-
continue
104-
cookie = header.split(";", 1)[0]
105-
cookieKey, cookieValue = cookie.split("=", 1)
106-
self._cookies[cookieKey] = cookieValue
107-
finally:
108-
return Transport.parse_response(self, response)
64+
def CookiesTransport(proto='https'):
65+
"""Generate transport class when using cookie based authentication."""
66+
_TransportClass_ = Transport if proto == 'http' else SafeTransport
67+
68+
class CookiesTransport(_TransportClass_):
69+
"""A Python3 xmlrpc.client.Transport subclass that retains cookies."""
70+
def __init__(self):
71+
_TransportClass_.__init__(self)
72+
self._cookies = dict()
73+
74+
def send_headers(self, connection, headers):
75+
if self._cookies:
76+
cookies = map(lambda x: x[0] + '=' + x[1], self._cookies.items())
77+
connection.putheader('Cookie', '; '.join(cookies))
78+
_TransportClass_.send_headers(self, connection, headers)
79+
80+
def parse_response(self, response):
81+
"""parse and store cookie"""
82+
try:
83+
for header in response.msg.get_all("Set-Cookie"):
84+
cookie = header.split(";", 1)[0]
85+
cookieKey, cookieValue = cookie.split("=", 1)
86+
self._cookies[cookieKey] = cookieValue
87+
finally:
88+
return _TransportClass_.parse_response(self, response)
89+
90+
class CookiesTransport2(_TransportClass_):
91+
"""A Python2 xmlrpclib.Transport subclass that retains cookies."""
92+
def __init__(self):
93+
_TransportClass_.__init__(self)
94+
self._cookies = dict()
95+
96+
def send_request(self, connection, handler, request_body):
97+
_TransportClass_.send_request(self, connection, handler, request_body)
98+
# set cookie below handler
99+
if self._cookies:
100+
cookies = map(lambda x: x[0] + '=' + x[1], self._cookies.items())
101+
connection.putheader("Cookie", "; ".join(cookies))
102+
103+
def parse_response(self, response):
104+
"""parse and store cookie"""
105+
try:
106+
for header in response.getheader("set-cookie").split(", "):
107+
# filter 'expire' information
108+
if not header.startswith("D"):
109+
continue
110+
cookie = header.split(";", 1)[0]
111+
cookieKey, cookieValue = cookie.split("=", 1)
112+
self._cookies[cookieKey] = cookieValue
113+
finally:
114+
return _TransportClass_.parse_response(self, response)
115+
116+
return CookiesTransport2() if PY_VERSION == 2 else CookiesTransport()
109117

110118
class DokuWiki(object):
111119
"""Initialize a connection to a DokuWiki wiki. *url*, *user* and
@@ -147,10 +155,7 @@ def __init__(self, url, user, password, cookieAuth=False, **kwargs):
147155
if cookieAuth == False:
148156
self.proxy = ServerProxy(url, **kwargs)
149157
else:
150-
if sys.version_info[0] == 3:
151-
self.proxy = ServerProxy(url, CookiesTransport(), **kwargs)
152-
else:
153-
self.proxy = ServerProxy(url, CookiesTransport2(), **kwargs)
158+
self.proxy = ServerProxy(url, CookiesTransport(params['proto']), **kwargs)
154159

155160
# Force login to check the connection.
156161
if not self.login(user, password):

0 commit comments

Comments
 (0)