-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaooauth.cpp
More file actions
72 lines (60 loc) · 1.86 KB
/
aooauth.cpp
File metadata and controls
72 lines (60 loc) · 1.86 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
#include "aooauth.h"
#include <QDesktopServices>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QOAuth2AuthorizationCodeFlow>
#include <QOAuthHttpServerReplyHandler>
#include <QUrlQuery>
AOOAuth::AOOAuth(const QHostAddress &bind_addr, int port, QObject *parent)
: QObject{parent}
, m_oauth(new QOAuth2AuthorizationCodeFlow(this))
, m_handler(new QOAuthHttpServerReplyHandler(bind_addr, port, this))
, m_networkManager(new QNetworkAccessManager(this))
{
qDebug() << "Created AOOAuth Instance";
m_oauth->setReplyHandler(m_handler);
}
QString AOOAuth::token() const
{
return m_oauth->token();
}
AOOAuth::~AOOAuth() {}
void AOOAuth::setAuthorizationUrl(const QUrl &url)
{
m_authorization_url = url;
}
void AOOAuth::setTokenUrl(const QUrl &url)
{
m_token_url = url;
}
void AOOAuth::setRevocationUrl(const QUrl &url)
{
m_revoke_url = url;
}
void AOOAuth::setClientId(const QString &id)
{
m_client_id = id;
}
void AOOAuth::requestToken()
{
m_oauth->setAuthorizationUrl(m_authorization_url);
m_oauth->setAccessTokenUrl(m_token_url);
m_oauth->setClientIdentifier(m_client_id);
m_oauth->setScope("identify");
connect(m_oauth, &QAbstractOAuth::authorizeWithBrowser, &QDesktopServices::openUrl);
connect(m_oauth, &QAbstractOAuth::granted, this, [this]() {
qDebug() << "OAuth granted! Token:" << m_oauth->token();
m_handler->close();
emit authenticated();
});
connect(m_oauth, &QAbstractOAuth::requestFailed, this, [this](QAbstractOAuth::Error error) {
qDebug() << QVariant::fromValue(error).toString();
});
if (m_handler->isListening()) {
qDebug() << "Starting OAuth on port" << m_handler->port();
m_oauth->grant();
} else {
qDebug() << "Failed to start server on port" << m_handler->port();
emit authenticationFailed();
}
}