Commit dc337d8c authored by Philipp Hörist's avatar Philipp Hörist

Register: Add change_password()

parent f28ff1df
Pipeline #4654 passed with stages
in 26 seconds
......@@ -24,9 +24,11 @@ from nbxmpp.protocol import Iq
from nbxmpp.protocol import isResultNode
from nbxmpp.structs import CommonResult
from nbxmpp.structs import RegisterData
from nbxmpp.structs import ChangePasswordResult
from nbxmpp.util import call_on_response
from nbxmpp.util import callback
from nbxmpp.util import raise_error
from nbxmpp.util import get_form
from nbxmpp.const import REGISTER_FIELDS
from nbxmpp.modules.bits_of_binary import parse_bob_data
from nbxmpp.modules.dataforms import extend_form
......@@ -119,3 +121,34 @@ class Register:
fields.append(create_field(typ='hidden', var='fakeform'))
return SimpleDataForm(type_='form', fields=fields)
def change_password(self, password):
hostname = self._client.get_bound_jid().getDomain()
username = self._client.get_bound_jid().getNode()
iq = Iq('set', NS_REGISTER, to=hostname)
query = iq.getQuery()
query.setTagData('username', username)
query.setTagData('password', password)
return iq
def _on_password_change(self, stanza):
if isResultNode(stanza):
return ChangePasswordResult(successful=True)
if stanza.getQuery() is None:
return raise_error(, stanza)
form = get_form(stanza.getQuery(),
if form is None:
return raise_error(, stanza)
return ChangePasswordResult(successful=False, form=form)
def change_password_with_form(self, form):
hostname = self._client.get_bound_jid().getDomain()
iq = Iq('set', NS_REGISTER, to=hostname)
return iq
......@@ -133,6 +133,8 @@ SecurityLabel = namedtuple('SecurityLabel', 'displaymarking')
RegisterData = namedtuple('RegisterData', 'instructions form fields_form oob_url bob_data')
ChangePasswordResult = namedtuple('ChangePasswordResult', 'successful form')
ChangePasswordResult.__new__.__defaults__ = (None,)
class DiscoInfo(namedtuple('DiscoInfo', 'stanza identities features dataforms timestamp')):
......@@ -321,3 +321,19 @@ def compute_caps_hash(info, compare=True):
def generate_id():
return str(uuid.uuid4())
def get_form(stanza, form_type):
forms = stanza.getTags('x', namespace=NS_DATA)
if not forms:
return None
for form in forms:
form = extend_form(node=form)
field = form.vars.get('FORM_TYPE')
if field is None:
if field.value == form_type:
return form
return None
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment