Files
Hippolyzer/pyogp/lib/base/credentials.py
tao.takashi 63a0469cdc refactored serialization handling in capabilities and credentials.
It all goes via one interface ISerialization which has a serialize() method which serializes the context into whatever output format and it has a content_type attribute which defines the content type of the output (default is LLSD).

There is a ISerialization adapter configured for both capabilities (from dicts) and PlainPasswordCredentials. See agentdomain.py for how to use it.

For deserialization there is a IDeserialization utility now. It is registered under the name of the content type it can deserialize. It produces dicts always. Use it like this:

deserializer = getUtility(IDeserialization,name="application/llsd-xml")
dict = deserializer.deserialize_string(s)

Tests for this component will be added later but they are indirectly tested by the login test which passes.
2008-07-19 14:21:18 +00:00

55 lines
1.5 KiB
Python

from zope.interface import implements
from zope.component import adapts
from indra.base import llsd
from interfaces import IPlainPasswordCredential, ISerialization
class PlainPasswordCredential(object):
"""a plain password credential"""
implements(IPlainPasswordCredential)
def __init__(self, firstname, lastname, password):
"""initialize this credential"""
self.firstname = firstname
self.lastname = lastname
self.password = password
# an adapter to serialize this to LLSD
class PlainPasswordLLSDSerializer(object):
"""converts a plain password credential to LLSD"""
implements(ISerialization)
adapts(IPlainPasswordCredential)
def __init__(self, context):
"""initialize this adapter by storing the context (the credential)"""
self.context = context
def serialize(self):
"""return the credential as a string"""
loginparams={
'password' : self.context.password,
'lastname' : self.context.lastname,
'firstname' : self.context.firstname
}
llsdlist = llsd.format_xml(loginparams)
return llsdlist
@property
def content_type(self):
"""return HTTP headers needed here"""
return "application/llsd+xml"
# now we register this adapter so it can be used later:
from zope.component import provideAdapter
# register adapters for the HTML node
provideAdapter(PlainPasswordLLSDSerializer)