-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraktauthenticator.cpp
112 lines (92 loc) · 2.82 KB
/
traktauthenticator.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
#include "traktauthenticator.h"
#include <QDateTime>
#include <QDebug>
#include <QNetworkRequest>
#include <QUrl>
#include <QUrlQuery>
#include <QVariant>
#include "traktrequest.h"
#include "traktreply.h"
TraktAuthenticator::TraktAuthenticator(const QString clientId, const QString &clientSecret, const QString &redirectUrl, QObject *parent) :
QObject(parent),
m_clientId(clientId),
m_clientSecret(clientSecret),
m_redirectUrl(redirectUrl),
m_authorized(false),
m_authorizing(false)
{
}
QString TraktAuthenticator::buildAuthorizeUrl() const
{
QUrlQuery query;
query.addQueryItem("response_type", "code");
query.addQueryItem("client_id", m_clientId);
query.addQueryItem("redirect_uri", m_redirectUrl);
QUrl url(TRAKT_WEB_URL);
url.setPath("/oauth/authorize");
url.setQuery(query);
return url.toString();
}
void TraktAuthenticator::authorize(GrantType type, const QString &token)
{
if (token.isEmpty()) {
return;
}
QVariantMap data;
data["client_id"] = m_clientId;
data["client_secret"] = m_clientSecret;
data["redirect_uri"] = m_redirectUrl;
if (type == GrantAccessCode) {
data["code"] = token;
data["grant_type"] = "authorization_code";
} else if (type == GrantRefreshToken) {
data["refresh_token"] = token;
data["grant_type"] = "refresh_token";
}
TraktRequest *request = new TraktRequest(this);
request->setOperation(TraktRequest::OperationPost);
request->setPath("/oauth/token");
request->setData(data);
request->send();
connect(request, &TraktRequest::replyReceived, this, &TraktAuthenticator::onTokenReceived);
m_authorizing = true;
emit authorizingChanged();
}
bool TraktAuthenticator::authorized() const
{
return m_authorized;
}
bool TraktAuthenticator::authorizing() const
{
return m_authorizing;
}
void TraktAuthenticator::appendHeaders(QNetworkRequest &request) const
{
request.setRawHeader("trakt-api-key", m_clientId.toLocal8Bit());
if (!m_accessToken.isEmpty()) {
request.setRawHeader("Authorization", QString("Bearer %1").arg(m_accessToken).toLocal8Bit());
}
}
void TraktAuthenticator::onTokenReceived(TraktReply *reply)
{
reply->deleteLater();
QVariantMap data = reply->asMap();
if (reply->statusCode() != 200) {
qDebug() << "authorization failed" << data.value("error_description").toString();
if (m_authorized) {
m_authorized = false;
emit authorizedChanged();
}
return;
}
m_accessToken = data.value("access_token").toString();
emit tokensReceived(data.value("refresh_token").toString());
if (!m_authorized) {
m_authorized = true;
emit authorizedChanged();
}
if (m_authorizing) {
m_authorizing = false;
emit authorizingChanged();
}
}