task_change_password.cpp 3.32 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 13 14
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

24 25
#include <QThread>

26
#include "src/datovka_shared/log/log.h"
27 28
#include "src/net/isds_const.h"
#include "src/worker/task_change_password.h"
29 30
#include "src/xml/xml_base.h"
#include "src/xml/xml_login.h"
31 32 33 34 35

TaskChangePassword::TaskChangePassword(IsdsSession::IsdsContext &ctx,
    NetLayer *netLayer, const QString &dbOTPType, const QString &oldPwd,
    const QString &newPwd)
    : m_result(DL_ERR),
36
    m_isdsText(),
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    m_ctx(ctx),
    m_netLayer(netLayer),
    m_dbOTPType(dbOTPType),
    m_oldPwd(oldPwd),
    m_newPwd(newPwd)
{
}

void TaskChangePassword::run(void)
{
	if (Q_NULLPTR == m_netLayer) {
		Q_ASSERT(0);
		return;
	}

	/* ### Worker task begin. ### */

54 55
	logDebugLv0NL("%s", "--------------CHANGE PASSWORD TASK-------------");
	logDebugLv0NL("Starting in thread '%p'", QThread::currentThreadId());
56

57 58
	m_result = changePassword(m_ctx, m_netLayer, m_dbOTPType,
	    m_oldPwd, m_newPwd);
59
	m_isdsText = m_ctx.last_isds_msg;
60

61 62
	logDebugLv0NL("Finished in thread '%p'", QThread::currentThreadId());
	logDebugLv0NL("%s", "-----------------------------------------------");
63

64
	/* ### Worker task end. ### */
65 66 67 68 69 70 71
}

enum TaskChangePassword::Result TaskChangePassword::changePassword(
    IsdsSession::IsdsContext &ctx, NetLayer *netLayer, const QString &dbOTPType,
    const QString &oldPwd, const QString &newPwd)
{
	if (ctx.username.isEmpty()) {
72
		logErrorNL("%s", "Account user name missing!");
73 74 75 76
		Q_ASSERT(0);
		return DL_ERR;
	}

77 78 79
	logDebugLv1NL("Sending from account '%s'",
	    ctx.account_name.toUtf8().constData());

80 81 82 83 84
	QByteArray xmlDataOut;

	/* Send SOAP request */
	if (dbOTPType == OTP_NO_OTP) {
		if (!netLayer->createChangePwdRequest(ctx, DB_SERVICE,
85
		    Xml::xmlCreateChangeISDSPasswordSoapRequest(oldPwd,
86
		    newPwd), xmlDataOut)) {
87 88
			logErrorNL("ISDS returns: '%s'",
			    ctx.last_isds_msg.toUtf8().constData());
89 90 91 92
			return DL_ISDS_ERROR;
		}
	} else {
		if (!netLayer->createChangePwdRequest(ctx, OTP_CHNG_PWD_SERVICE,
93
		    Xml::xmlCreateChangePasswordOTPSoapRequest(dbOTPType,
94
		    oldPwd, newPwd), xmlDataOut)) {
95 96
			logErrorNL("ISDS returns: '%s'",
			    ctx.last_isds_msg.toUtf8().constData());
97 98 99 100 101
			return DL_ISDS_ERROR;
		}
	}

	/* Parse SOAP response */
102
	if (!Xml::parseDbStatusFromXml(xmlDataOut, ctx.last_isds_msg)) {
103 104
		logErrorNL("ISDS returns: '%s'",
		    ctx.last_isds_msg.toUtf8().constData());
105 106 107
		return DL_XML_ERROR;
	}

108 109
	logDebugLv1NL("%s", "Password has been changed");

110 111
	return DL_SUCCESS;
}