-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathauthenticator.cpp
277 lines (245 loc) · 10 KB
/
authenticator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Hanghish
Copyright (C) 2015 Daniele Rogora
This file is part of Hangish.
Hangish 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.
Hangish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Nome-Programma. If not, see <http://www.gnu.org/licenses/>
*/
#include "authenticator.h"
#include <QtWidgets/QApplication>
#include <QProcess>
static QString user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.132 Safari/537.36";
QString homeDir;
QString homePath;
static QString SECONDFACTOR_URL = "https://accounts.google.com/SecondFactor";
void Authenticator::cb(QNetworkReply *reply) {
QVariant v = reply->header(QNetworkRequest::SetCookieHeader);
QList<QNetworkCookie> c = qvariant_cast<QList<QNetworkCookie> >(v);
qDebug() << "Got " << c.size() << "from" << reply->url();
sessionCookies.append(c);
if (reply->error() == QNetworkReply::NoError) {
if (auth_phase==1) {
QVariant v = reply->header(QNetworkRequest::SetCookieHeader);
QList<QNetworkCookie> c = qvariant_cast<QList<QNetworkCookie> >(v);
if (c.at(1).name()=="GALX") {
GALXCookie = c.at(1);
delete reply;
//send_credentials(GALXCookie);
}
}
else if (auth_phase==2) {
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()==302) {
QVariant possibleRedirectUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
followRedirection(possibleRedirectUrl.toUrl());
}
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()==200) {
if (amILoggedIn())
getAuthCookies();
else if (reply->url().toString().startsWith(SECONDFACTOR_URL)) {
//2nd factor auth
qDebug() << "Auth failed " << reply->url();
QString ssreply = reply->readAll();
//find secTok
int start = ssreply.indexOf("id=\"secTok\"");
start = ssreply.indexOf("'", start)+1;
int stop = ssreply.indexOf("'", start);
secTok = ssreply.mid(start, stop-start);
//find timeStmp
start = ssreply.indexOf("id=\"timeStmp\"");
start = ssreply.indexOf("'", start)+1;
stop = ssreply.indexOf("'", start);
timeStmp = ssreply.mid(start, stop-start);
qDebug() << ssreply;
qDebug() << secTok;
qDebug() << timeStmp;
emit secondFactorNeeded();
}
else {
//Something went wrong
qDebug() << "Auth failed " << reply->url();
emit authFailed("Wrong uname/passwd");
}
}
else {
//Something went wrong
qDebug() << "Auth failed " << reply->errorString();
emit authFailed(reply->errorString());
}
}
else if (auth_phase==6) {
QString ssreply = reply->readAll();
qDebug() << ssreply;
//2nd factor response
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()==200) {
//TODO: is this really a consistent check for everyone?
if (amILoggedIn())
getAuthCookies();
else {
emit authFailed(reply->url().toString());
}
}
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()==302) {
QVariant possibleRedirectUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
followRedirection(possibleRedirectUrl.toUrl());
}
else {
qDebug() << "2nd factor error";
emit authFailed("Error sending pin");
}
}
}
else {
//failure
delete reply;
}
}
void Authenticator::followRedirection(QUrl url)
{
QNetworkRequest req( url );
req.setRawHeader("User-Agent", user_agent.toLocal8Bit().data());
req.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(sessionCookies));
nam.get(req);
}
void Authenticator::getGalxToken()
{
auth_phase = 1;
QNetworkRequest req( QUrl( QString("https://accounts.google.com/ServiceLogin?passive=true&skipvpage=true&continue=https://talkgadget.google.com/talkgadget/gauth?verify%3Dtrue&authuser=0") ) );
req.setRawHeader("User-Agent", user_agent.toLocal8Bit().data());
nam.get(req);
}
void Authenticator::getAuthCookies()
{
auth_phase = 3;
QJsonObject obj;
foreach(QNetworkCookie cookie, sessionCookies) {
//Let's save the relevant cookies
if (cookie.name()=="S" || !cookie.isSessionCookie() && cookie.domain().contains("google.com") ) {
qDebug() << "GOOD: " << cookie.name() << " - " << cookie.domain() << " - " << cookie.isSessionCookie() << " - " << cookie.expirationDate().toString();
obj[cookie.name()] = QJsonValue( QString(cookie.value()) );
liveSessionCookies.append(cookie);
}
}
QJsonDocument doc(obj);
QFile cookieFile(homePath);
if ( cookieFile.open(QIODevice::WriteOnly))
{
cookieFile.write(doc.toJson(), doc.toJson().size());
}
cookieFile.close();
qDebug() << "Written cookies to " << homePath;
auth_phase=5;
sessionCookies = liveSessionCookies;
liveSessionCookies.clear();
emit(gotCookies());
}
void Authenticator::send_credentials(QString uname, QString passwd)
{
auth_phase = 2;
QString r = QString("GALX="+QString(GALXCookie.value()));
r+=QString("&Email="+uname);
r+=QString("&Passwd="+passwd);
r+="&bgresponse=js_disabled&dnConn=0&signIn=Accedi&checkedDomains=youtube&PersistentCookie=yes&rmShown=1&pstMsg=0&skipvpage=true&continue=https://talkgadget.google.com/talkgadget/gauth?verify=true";
QByteArray reqString(r.toUtf8());
QNetworkRequest req( QUrl( QString("https://accounts.google.com/ServiceLoginAuth") ) );
req.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
nam.post(req, reqString);
}
void Authenticator::send2ndFactorPin(QString pin)
{
auth_phase = 6;
QString r = QString("timeStmp=" + timeStmp + "&secTok="+secTok);
r+=QString("&smsUserPin="+pin);
r+="&smsVerifyPin=Verify&smsToken=&checkedConnection=youtube:73:0&checkedDomains=youtube&PersistentCookie=on&PersistentOptionSelection=1&pstMsg=0&skipvpage=true";
QByteArray reqString(r.toUtf8());
QNetworkRequest req( QUrl( QString("https://accounts.google.com/SecondFactor") ) );
req.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
nam.post(req, reqString);
}
QList<QNetworkCookie> Authenticator::getCookies()
{
return sessionCookies;
}
bool Authenticator::amILoggedIn()
{
int i = 0;
foreach(QNetworkCookie cookie, sessionCookies) {
if (cookie.name()=="APISID" || cookie.name()=="HSID" || cookie.name()=="S" || cookie.name()=="SAPISID" || cookie.name()=="SID" || cookie.name()=="SSID")
i++;
}
if (i>=6)
return true;
return false;
}
void Authenticator::updateCookies(QList<QNetworkCookie> cookies)
{
QJsonObject obj;
sessionCookies.clear();
QFile cookieFile(homePath);
if (cookieFile.exists()) {
cookieFile.open(QIODevice::ReadWrite | QIODevice::Text);
cookieFile.resize(0);
foreach(QNetworkCookie cookie, cookies) {
qDebug() << "GOOD: " << cookie.name() << " - " << cookie.domain() << " - " << cookie.isSessionCookie() << " - " << cookie.expirationDate().toString();
obj[cookie.name()] = QJsonValue( QString(cookie.value()) );
sessionCookies.append(cookie);
}
QJsonDocument doc(obj);
QFile cookieFile(homePath);
if ( cookieFile.open(QIODevice::WriteOnly))
{
cookieFile.write(doc.toJson(), doc.toJson().size());
}
cookieFile.close();
qDebug() << "Written cookies to " << homePath;
auth_phase=5;
emit(gotCookies());
}
}
void Authenticator::auth()
{
QFile cookieFile(homePath);
if (cookieFile.exists()) {
cookieFile.open(QIODevice::ReadOnly | QIODevice::Text);
QString val = cookieFile.readAll();
cookieFile.close();
QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
QJsonObject obj = doc.object();
foreach (QString s, obj.keys())
{
//TODO: check and delete
if (s=="ACCOUNT_CHOOSER" || s=="GALX" || s=="GAPS" || s=="LSID" || s=="NID") continue;
QNetworkCookie tmp;
//qDebug() << s;
tmp.setName(QVariant(s).toByteArray());
tmp.setValue(obj.value(s).toVariant().toByteArray());
tmp.setDomain(".google.com");
sessionCookies.append(tmp);
}
emit(gotCookies());
//qDebug() << "Already in!";
}
else {
auth_phase = 0;
QObject::connect(&nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(cb(QNetworkReply *)));
nam.setCookieJar(&cJar);
emit loginNeeded();
getGalxToken();
}
}
Authenticator::Authenticator()
{
homeDir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
homePath = homeDir + "/cookies.json";
qDebug() << "Making dir " << homeDir;
QDir().mkpath(homeDir);
}