4

Here is one of my functions:

def connect():
    c = xmlrpclib.ServerProxy('http://username:password@host',
                allow_none=True,
            )
    return c

How do I check if the username and password are correct in this method before returning c?

BPm
  • 2,924
  • 11
  • 33
  • 51

1 Answers1

5

You can check if the provided credentials are valid by using this trick (provided that the plone site has wsapi4plone correctly installed):

>>> server = xmlrpclib.ServerProxy("http://admin:admin@localhost:8080/plone")
>>> server.get_schema('Document')
{'creators': {'required': False, 'type': 'lines'}, 'description': ...
>>> baduser_server = xmlrpclib.ServerProxy("http://bad:bad@localhost:8080/plone")
>>> baduser_server.get_schema('Document')
Traceback (most recent call last):
...
ProtocolError: <ProtocolError for bad:bad@localhost:8080/plone: 401 Unauthorized>

So the corresponding code would be:

from xmlrpclib import ServerProxy
from xmlrpclib import ProtocolError
try:
    server = ServerProxy("http://admin:admin@localhost:8080/plone")
    server.get_schema('Document')
    return server
except ProtocolError:
    return None
Giacomo Spettoli
  • 4,476
  • 1
  • 16
  • 24
  • so there's no specific method to check the username and password for plone? hmm I'll try this trick for now – BPm Jan 04 '12 at 00:37