source: SMSSender/gateways/Post/src/business/account_task_login.cpp @ 315:e6c8bb244ecd

separation-frontend-backend
Last change on this file since 315:e6c8bb244ecd was 315:e6c8bb244ecd, checked in by Sämy Zehnder <saemy.zehnder@…>, 5 years ago
  • Separates the frontend from the backend code.
  • At this point the schoolnet gateway is converted only.
  • Removes bc & da prefixes from the filenames.
File size: 13.2 KB
Line 
1/*
2  Swiss Post gateway plugin - The smssender plugin for the Swiss Post platform.
3  Copyright (C) 2011-2012, gorrión. See http://smssender.gorrion.ch
4
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "account_tasks.h"
19
20#include <exceptions/eloginaccountloginexception.h>
21#include <network/snetworkreply.h>
22
23#include "business/gateway.h"
24#include "library.h"
25
26namespace Post {
27
28AccountLoginTask::AccountLoginTask(Account *account)
29    : AbstractAccountLoginTask(account)
30{
31}
32
33void AccountLoginTask::doLogin() {
34    setProgress(10);
35
36    QString html;
37
38    html = postLogin();
39    setProgress(35);
40
41    html = postSamlRequest(html);
42    setProgress(65);
43
44    html = postSamlResponse(html);
45
46    account()->reloadOnlineParams(); // TODO can html be used in this call to avoid
47                                     // an additional web-poll in there?
48}
49
50QString AccountLoginTask::postLogin() {
51    QString html = httpHelper()->syncGet(URL_LOGIN)->readAll();
52
53    QList<IParam*> posts;
54    posts.append(new SKeyValueParam("isiwebuserid", account()->username().toUtf8()));
55    posts.append(new SKeyValueParam("isiwebpasswd", account()->password().toUtf8()));
56    posts.append(new SKeyValueParam("checkIsSubmitted", "fromFormLoginhtml"));
57
58    html = httpHelper()->syncPost(URL_LOGIN, posts)->readAll();
59
60    // Check for login error
61    QRegExp rx("<div class=\"errorBoxMessage\">\\s*([^\\n]+)\\s*</div>");
62    if (rx.indexIn(html) > -1) {
63        LoginAccount::ELoginException(LoginAccount::ELoginException::InvalidLoginData)
64                .addDebugInfo("reason",  httpHelper()->htmldecodeString(rx.cap(1)))
65                .raise();
66    }
67
68    return html;
69}
70
71QString AccountLoginTask::postSamlRequest(QString lastHtml) {
72    /* Find URL */
73    QRegExp rx("<form method=\"post\" action=\"([^\"]+)\">");
74    if (rx.indexIn(lastHtml) == -1) {
75        LoginAccount::ELoginException(LoginAccount::ELoginException::RequestError)
76                .addDebugInfo("reason", "Could not find the post url.")
77                .addDebugInfo("html", lastHtml)
78                .raise();
79    }
80    QString url = rx.cap(1);
81
82    /* Find SAMLResponse */
83    rx.setPattern("<input type=\"hidden\" name=\"SAMLRequest\" value=\"([^\"]+)\" />");
84    if (rx.indexIn(lastHtml) == -1) {
85        LoginAccount::ELoginException(LoginAccount::ELoginException::RequestError)
86                .addDebugInfo("reason", "Could not find the SAMLRequest field.")
87                .addDebugInfo("html", lastHtml)
88                .raise();
89    }
90    QString samlRequest = rx.cap(1);
91
92    /* Find RelayState */
93    rx.setPattern("<input type=\"hidden\" name=\"RelayState\" value=\"([^\"]+)\" />");
94    if (rx.indexIn(lastHtml) == -1) {
95        LoginAccount::ELoginException(LoginAccount::ELoginException::RequestError)
96                .addDebugInfo("reason", "Could not find the RelayState field.")
97                .addDebugInfo("html", lastHtml)
98                .raise();
99    }
100    QString relayState = rx.cap(1);
101
102
103    // Post the saml request
104    QList<IParam*> posts;
105    posts.append(new SKeyValueParam("SAMLRequest", samlRequest.toUtf8()));
106    posts.append(new SKeyValueParam("RelayState", relayState.toUtf8()));
107
108    return httpHelper()->syncPost(url, posts)->readAll();
109}
110
111QString AccountLoginTask::postSamlResponse(QString lastHtml) {
112    /*
113                <form id="saml_form" action="https://organizer.sso.post.ch/sso-navi/assertion_consumer.php" method="POST" enctype="application/x-www-form-urlencoded">
114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" type="submit" value="Go"/>
116                <input type="hidden" name="RelayState" value="https://postsso-int.daybyday.de/sso-navi/assertion_consumer.php"/></form></div>
117    */
118    /* Find URL */
119    QRegExp rx("<form id=\"saml_form\" action=\"([^\"]+)\"");
120    if (rx.indexIn(lastHtml) == -1) {
121        LoginAccount::ELoginException(LoginAccount::ELoginException::RequestError)
122                .addDebugInfo("reason", "Could not find the post url.")
123                .addDebugInfo("html", lastHtml)
124                .raise();
125    }
126    QString url = rx.cap(1);
127
128    /* Find SAMLResponse */
129    rx.setPattern("<input type=\"hidden\" name=\"SAMLResponse\" value=\"([^\"]+)\"/>");
130    if (rx.indexIn(lastHtml) == -1) {
131        LoginAccount::ELoginException(LoginAccount::ELoginException::RequestError)
132                .addDebugInfo("reason", "Could not find the SAMLResquest field.")
133                .addDebugInfo("html", lastHtml)
134                .raise();
135    }
136    QString samlRequest = rx.cap(1);
137
138    /* Find RelayState */
139    rx.setPattern("<input type=\"hidden\" name=\"RelayState\" value=\"([^\"]+)\"/>");
140    if (rx.indexIn(lastHtml) == -1) {
141        LoginAccount::ELoginException(LoginAccount::ELoginException::RequestError)
142                .addDebugInfo("reason", "Could not find the RelayState field.")
143                .addDebugInfo("html", lastHtml)
144                .raise();
145    }
146    QString relayState = rx.cap(1);
147
148
149    // Post the saml response
150    QList<IParam*> posts;
151    posts.append(new SKeyValueParam("SAMLResponse", samlRequest.toUtf8()));
152    posts.append(new SKeyValueParam("RelayState", relayState.toUtf8()));
153
154    return httpHelper()->syncPost(url, posts)->readAll();
155}
156
157
158Account* AccountLoginTask::account() const {
159    return static_cast<Account*>(AbstractAccountLoginTask::account());
160}
161
162SNetworkHelper *AccountLoginTask::httpHelper() const {
163    return account()->httpHelper();
164}
165
166} // namespace Post
Note: See TracBrowser for help on using the repository browser.