Skip to content

Commit e452140

Browse files
authored
Merge pull request #11 from luminisward/master
Support authentication by Cookie
2 parents 1d7cc73 + 329862e commit e452140

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

dokuwiki.py

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
import weakref
2222
from xml.parsers.expat import ExpatError
2323
if sys.version_info[0] == 3:
24-
from xmlrpc.client import ServerProxy, Binary, Fault
24+
from xmlrpc.client import ServerProxy, Binary, Fault, Transport
2525
from urllib.parse import urlencode
2626
else:
27-
from xmlrpclib import ServerProxy, Binary, Fault
27+
from xmlrpclib import ServerProxy, Binary, Fault, Transport
2828
from urllib import urlencode
2929

3030
from datetime import datetime, timedelta
@@ -59,6 +59,53 @@ class DokuWikiError(Exception):
5959
"""Exception raised by this module when there is an error."""
6060
pass
6161

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)
62109

63110
class DokuWiki(object):
64111
"""Initialize a connection to a DokuWiki wiki. *url*, *user* and
@@ -73,21 +120,32 @@ class DokuWiki(object):
73120
.. code::
74121
75122
try:
76-
wiki = dokuwiki.DokuWiki('URL', 'USER', 'PASSWORD')
123+
wiki = dokuwiki.DokuWiki('URL', 'USER', 'PASSWORD', cookieAuth=False)
77124
except (DokuWikiError, Exception) as err:
78125
print('unable to connect: %s' % err)
79126
"""
80127

81-
def __init__(self, url, user, password, **kwargs):
128+
def __init__(self, url, user, password, cookieAuth=False, **kwargs):
82129
"""Initialize the object by connecting to the XMLRPC server."""
83130
# Initialize XMLRPC client.
84131
try:
85132
params = _URL_RE.search(url).groupdict()
86-
url = '%s://%s:%s@%s%s/lib/exe/xmlrpc.php' % (
87-
params['proto'], user, password, params['host'], params['uri'] or '')
133+
if cookieAuth == False:
134+
url = '%s://%s:%s@%s%s/lib/exe/xmlrpc.php' % (
135+
params['proto'], user, password, params['host'], params['uri'] or '')
136+
else:
137+
url = '%s://%s%s/lib/exe/xmlrpc.php' % (
138+
params['proto'], params['host'], params['uri'] or '')
88139
except AttributeError:
89140
raise DokuWikiError("invalid url '%s'" % url)
90-
self.proxy = ServerProxy(url, **kwargs)
141+
142+
if cookieAuth == False:
143+
self.proxy = ServerProxy(url, **kwargs)
144+
else:
145+
if sys.version_info[0] == 3:
146+
self.proxy = ServerProxy(url, CookiesTransport(), **kwargs)
147+
else:
148+
self.proxy = ServerProxy(url, CookiesTransport2(), **kwargs)
91149

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

0 commit comments

Comments
 (0)