-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoauth2.cpp
More file actions
88 lines (73 loc) · 2.21 KB
/
oauth2.cpp
File metadata and controls
88 lines (73 loc) · 2.21 KB
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
#include "oauth2.h"
#include "ui_oauth2.h"
#include "../Lib/network.h"
#include <QUrl>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonObject>
#include <QJsonParseError>
#include <QWebEnginePage>
#include <QWebEngineProfile>
#include <QWebEngineCookieStore>
OAuth2::OAuth2(QWidget *parent) :
QDialog(parent),
ui(new Ui::OAuth2)
{
ui->setupUi(this);
connect(ui->web, SIGNAL(urlChanged(const QUrl)), this, SLOT(webPageChanged(const QUrl))) ;
}
OAuth2::~OAuth2()
{
delete ui;
}
//
// Open a web browser, and reurn the authcode, which is extracted
// from the resulting url.
QString OAuth2::getAuthCode(QString url, QString completedurl)
{
accesstoken.clear() ;
authcode.clear() ;
ui->web->setUrl(QUrl(url)) ;
ui->web->page()->profile()->cookieStore()->deleteAllCookies() ;
testurl = completedurl ;
this->exec() ;
return authcode ;
}
void OAuth2::webPageChanged(const QUrl url)
{
QString urltxt = url.toString() ;
int tlen = testurl.length() ;
if (urltxt.left(tlen).compare(testurl)==0) {
QStringList dat = urltxt.mid(tlen).split("&") ;
authcode = dat.at(0) ;
this->done(0) ;
}
}
//
// Request the access token, given the authcode
//
QString OAuth2::getAccessToken(QString requesturl, QString clientid, QString clientsecret, QString authcode)
{
QJsonObject json ;
json.insert("client_id",clientid) ;
json.insert("code", authcode) ;
json.insert("client_secret", clientsecret) ;
json.insert("grant_type", "authorization_code") ;
QJsonDocument doc ;
doc.setObject(json) ;
QString jsontext = doc.toJson() ;
QString header = "Content-type: application/json\n" ;
QString result ;
int status = networkHttp(POST, requesturl, header, jsontext, result) ;
if (status<200 || status>299) {
accesstoken.clear() ;
} else {
QJsonParseError err ;
QJsonDocument resultdoc = QJsonDocument::fromJson(result.toUtf8(), &err) ;
if (err.error != QJsonParseError::NoError) return "Error" ;
if (resultdoc.isNull()) return "Error" ;
if (!resultdoc.isObject()) return "Error" ;
accesstoken = resultdoc.object()["access_token"].toString() ;
}
return accesstoken ;
}