Skip to content

Commit 98ed799

Browse files
committed
Prevent call to the login method for non cookie based authentication
It seems that making a call to the login method in the constructor fail in some cases even this should not happened (as ids are passed in the URI by default, login should always return true). The login is not necessary for non cookie based authentication but is required for cookie based authentication. Also, it is preferable to ensure the connection work in the constructor (instead of throwing an exception at the first call). For cookie based authentication, the login is still done but for simple authentication this has been replaced by a call retrieving the version. For having coherent message between simple and cookie authentication, the call to the version catch 401 errors to show the same message when ids are not valid. This should fix #15 and #16
1 parent 6127908 commit 98ed799

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

dokuwiki.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
PY_VERSION = sys.version_info[0]
2525
if PY_VERSION == 3:
26-
from xmlrpc.client import ServerProxy, Binary, Fault, Transport, SafeTransport
26+
from xmlrpc.client import ServerProxy, Binary, Fault, Transport, SafeTransport, ProtocolError
2727
from urllib.parse import urlencode
2828
else:
29-
from xmlrpclib import ServerProxy, Binary, Fault, Transport, SafeTransport
29+
from xmlrpclib import ServerProxy, Binary, Fault, Transport, SafeTransport, ProtocolError
3030
from urllib import urlencode
3131

3232
from datetime import datetime, timedelta
@@ -155,19 +155,27 @@ def __init__(self, url, user, password, **kwargs):
155155

156156
# Set auth string or transport for cookie based authentication.
157157
auth = '{:s}:{:s}@'.format(user, password)
158-
if kwargs.pop('cookieAuth', False):
158+
cookie_auth = kwargs.pop('cookieAuth', False)
159+
if cookie_auth:
159160
auth = ''
160161
kwargs['transport'] = CookiesTransport(params['proto'])
161162

162163
xmlrpc_url = '%s://%s%s%s/lib/exe/xmlrpc.php' % (
163164
params['proto'], auth, params['host'], params['uri'] or '')
164165
self.proxy = ServerProxy(xmlrpc_url, **kwargs)
165166

166-
# Force login (required for cookie based authentication and allows
167-
# to check the connection).
168-
if not self.login(user, password):
167+
# Force login for cookie based authentication.
168+
if cookie_auth and not self.login(user, password):
169169
raise DokuWikiError('invalid login or password!')
170170

171+
# Dummy call to ensure the connection is up.
172+
try:
173+
self.version
174+
except ProtocolError as err:
175+
if err.errcode == 401:
176+
raise DokuWikiError('invalid login or password!')
177+
raise
178+
171179
# Set "namespaces" for pages and medias functions.
172180
self.pages = _Pages(weakref.ref(self)())
173181
self.medias = _Medias(weakref.ref(self)())

0 commit comments

Comments
 (0)