QXmppNonSASLAuth.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Manjeet Dahiya
  6. *
  7. * Source:
  8. * http://code.google.com/p/qxmpp
  9. *
  10. * This file is a part of QXmpp library.
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. */
  23. #include <QCryptographicHash>
  24. #include <QDomElement>
  25. #include <QXmlStreamWriter>
  26. #include "QXmppConstants.h"
  27. #include "QXmppNonSASLAuth.h"
  28. #include "QXmppUtils.h"
  29. QXmppNonSASLAuthIq::QXmppNonSASLAuthIq()
  30. : QXmppIq(QXmppIq::Set)
  31. {
  32. }
  33. QString QXmppNonSASLAuthIq::username() const
  34. {
  35. return m_username;
  36. }
  37. void QXmppNonSASLAuthIq::setUsername( const QString &username )
  38. {
  39. m_username = username;
  40. }
  41. QByteArray QXmppNonSASLAuthIq::digest() const
  42. {
  43. return m_digest;
  44. }
  45. void QXmppNonSASLAuthIq::setDigest(const QString &streamId, const QString &password)
  46. {
  47. m_digest = QCryptographicHash::hash(streamId.toUtf8() + password.toUtf8(), QCryptographicHash::Sha1);
  48. }
  49. QString QXmppNonSASLAuthIq::password() const
  50. {
  51. return m_password;
  52. }
  53. void QXmppNonSASLAuthIq::setPassword( const QString &password )
  54. {
  55. m_password = password;
  56. }
  57. QString QXmppNonSASLAuthIq::resource() const
  58. {
  59. return m_resource;
  60. }
  61. void QXmppNonSASLAuthIq::setResource(const QString &resource)
  62. {
  63. m_resource = resource;
  64. }
  65. /// \cond
  66. bool QXmppNonSASLAuthIq::isNonSASLAuthIq(const QDomElement &element)
  67. {
  68. QDomElement queryElement = element.firstChildElement("query");
  69. return queryElement.namespaceURI() == ns_auth;
  70. }
  71. void QXmppNonSASLAuthIq::parseElementFromChild(const QDomElement &element)
  72. {
  73. QDomElement queryElement = element.firstChildElement("query");
  74. m_username = queryElement.firstChildElement("username").text();
  75. m_password = queryElement.firstChildElement("password").text();
  76. m_digest = QByteArray::fromHex(queryElement.firstChildElement("digest").text().toAscii());
  77. m_resource = queryElement.firstChildElement("resource").text();
  78. }
  79. void QXmppNonSASLAuthIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  80. {
  81. writer->writeStartElement("query");
  82. writer->writeAttribute("xmlns", ns_auth);
  83. if (!m_username.isEmpty())
  84. writer->writeTextElement("username", m_username);
  85. if (!m_digest.isEmpty())
  86. writer->writeTextElement("digest", m_digest.toHex());
  87. if (!m_password.isEmpty())
  88. writer->writeTextElement("password", m_password);
  89. if (!m_resource.isEmpty())
  90. writer->writeTextElement("resource", m_resource);
  91. writer->writeEndElement();
  92. }
  93. /// \endcond