-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorization.cpp
58 lines (52 loc) · 1.82 KB
/
authorization.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
#include "authorization.h"
#include <QNetworkRequest>
#include <QNetworkReply>
Authorization::Authorization(Fact *parent):
Fact(parent, "authorization", "Authorization", "", Fact::NoFlags, "login")
{
m_timer.setSingleShot(false);
m_timer.setInterval(5000);
m_tokenExpireTimer.setSingleShot(true);
connect(&m_manager, &QNetworkAccessManager::finished, this, &Authorization::onRequestFinished);
connect(&m_timer, &QTimer::timeout, this, &Authorization::onTimerTimeout);
connect(&m_tokenExpireTimer, &QTimer::timeout, this, &Authorization::requestBearerToken);
requestBearerToken();
m_timer.start();
setValue("Not authorized");
}
QString Authorization::getBearerToken() const
{
return m_bearerToken;
}
void Authorization::requestBearerToken()
{
QNetworkRequest request(URL);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QString body = QString("grant_type=client_credentials"
"&scope=openid"
"&client_id=%1"
"&client_secret=%2")
.arg(CLIENT_ID)
.arg(CLIENT_SECRET);
m_manager.post(request, body.toUtf8());
}
void Authorization::onRequestFinished(QNetworkReply *reply)
{
auto doc = QJsonDocument::fromJson(reply->readAll());
auto root = doc.object();
int expiresSecs = root["expires_in"].toInt();
m_bearerToken = root["access_token"].toString();
if(!m_bearerToken.isEmpty()) {
setValue("OK");
m_tokenExpireTimer.start(std::clamp(expiresSecs, 10, expiresSecs - 10) * 1000);
emit bearerTokenReceived(m_bearerToken);
} else {
setValue("Not authorized");
}
}
void Authorization::onTimerTimeout()
{
if(m_bearerToken.isEmpty()) {
requestBearerToken();
}
}