File indexing completed on 2025-01-19 03:55:37
0001 #include <QNetworkAccessManager> 0002 #include <QNetworkRequest> 0003 #include <QNetworkReply> 0004 #include <QDesktopServices> 0005 #include <QMetaEnum> 0006 #include <QDebug> 0007 #include <QRegExp> 0008 0009 #include "msgraphdemo.h" 0010 #include "o0globals.h" 0011 #include "o0settingsstore.h" 0012 #include "o2requestor.h" 0013 0014 const char MSGRAPH_APP_ID[] = "YOUR_MSGRAPH_APP_ID"; 0015 const char MSGRAPH_APP_SECRET[] = "YOUR_MSGRAPH_APP_SECRET"; 0016 const char MSGRAPH_SCOPE[] = "User.Read"; 0017 const char MSGRAPH_USER_INFO_URL[] = "https://graph.microsoft.com/v1.0/me"; 0018 0019 const int localPort = 8888; 0020 0021 #define QENUM_NAME(o,e,v) (o::staticMetaObject.enumerator(o::staticMetaObject.indexOfEnumerator(#e)).valueToKey((v))) 0022 #define GRANTFLOW_STR(v) QString(QENUM_NAME(O2, GrantFlow, v)) 0023 0024 MsgraphDemo::MsgraphDemo(QObject *parent) : 0025 QObject(parent), requestId_(0) { 0026 o2Msgraph_ = new O2Msgraph(this); 0027 0028 o2Msgraph_->setClientId(MSGRAPH_APP_ID); 0029 o2Msgraph_->setClientSecret(MSGRAPH_APP_SECRET); 0030 o2Msgraph_->setLocalPort(localPort); 0031 o2Msgraph_->setScope(MSGRAPH_SCOPE); 0032 0033 // Create a store object for writing the received tokens 0034 O0SettingsStore *store = new O0SettingsStore(O2_ENCRYPTION_KEY); 0035 store->setGroupKey("msgraph"); 0036 o2Msgraph_->setStore(store); 0037 0038 connect(o2Msgraph_, SIGNAL(linkedChanged()), this, SLOT(onLinkedChanged())); 0039 connect(o2Msgraph_, SIGNAL(linkingFailed()), this, SIGNAL(linkingFailed())); 0040 connect(o2Msgraph_, SIGNAL(linkingSucceeded()), this, SLOT(onLinkingSucceeded())); 0041 connect(o2Msgraph_, SIGNAL(openBrowser(QUrl)), this, SLOT(onOpenBrowser(QUrl))); 0042 connect(o2Msgraph_, SIGNAL(closeBrowser()), this, SLOT(onCloseBrowser())); 0043 } 0044 0045 void MsgraphDemo::doOAuth(O2::GrantFlow grantFlowType) { 0046 qDebug() << "Starting OAuth 2 with grant flow type" << GRANTFLOW_STR(grantFlowType) << "..."; 0047 o2Msgraph_->setGrantFlow(grantFlowType); 0048 o2Msgraph_->unlink(); 0049 o2Msgraph_->link(); 0050 } 0051 0052 void MsgraphDemo::getUserPrincipalName() { 0053 if (!o2Msgraph_->linked()) { 0054 qWarning() << "ERROR: Application is not linked!"; 0055 emit linkingFailed(); 0056 return; 0057 } 0058 0059 QString userInfoURL = QString(MSGRAPH_USER_INFO_URL); 0060 QNetworkRequest request = QNetworkRequest(QUrl(userInfoURL)); 0061 QNetworkAccessManager *mgr = new QNetworkAccessManager(this); 0062 O2Requestor *requestor = new O2Requestor(mgr, o2Msgraph_, this); 0063 requestor->setAddAccessTokenInQuery(false); 0064 requestor->setAccessTokenInAuthenticationHTTPHeaderFormat("Bearer %1"); 0065 requestId_ = requestor->get(request); 0066 connect(requestor, SIGNAL(finished(int, QNetworkReply::NetworkError, QByteArray)), 0067 this, SLOT(onFinished(int, QNetworkReply::NetworkError, QByteArray)) 0068 ); 0069 qDebug() << "Getting user info... Please wait."; 0070 } 0071 0072 void MsgraphDemo::onOpenBrowser(const QUrl &url) { 0073 QDesktopServices::openUrl(url); 0074 } 0075 0076 void MsgraphDemo::onCloseBrowser() { 0077 } 0078 0079 void MsgraphDemo::onLinkedChanged() { 0080 qDebug() << "Link changed!"; 0081 } 0082 0083 void MsgraphDemo::onLinkingSucceeded() { 0084 O2Msgraph *o2t = qobject_cast<O2Msgraph *>(sender()); 0085 if (!o2t->linked()) { 0086 return; 0087 } 0088 QVariantMap extraTokens = o2t->extraTokens(); 0089 if (!extraTokens.isEmpty()) { 0090 emit extraTokensReady(extraTokens); 0091 qDebug() << "Extra tokens in response:"; 0092 foreach (QString key, extraTokens.keys()) { 0093 qDebug() << "\t" << key << ":" << (extraTokens.value(key).toString().left(3) + "..."); 0094 } 0095 } 0096 emit linkingSucceeded(); 0097 } 0098 0099 void MsgraphDemo::onFinished(int requestId, QNetworkReply::NetworkError error, QByteArray replyData) { 0100 if (requestId != requestId_) 0101 return; 0102 0103 if (error != QNetworkReply::NoError) { 0104 qWarning() << "Reply error:" << error; 0105 emit userPrincipalNameFailed(); 0106 return; 0107 } 0108 0109 QString reply(replyData); 0110 bool errorFound = reply.contains("error"); 0111 if (errorFound) { 0112 qDebug() << "Request failed"; 0113 emit userPrincipalNameFailed(); 0114 return; 0115 } 0116 0117 QRegExp userPrincipalNameRE("\"userPrincipalName\":\"([^\"]+)\""); 0118 if (userPrincipalNameRE.indexIn(reply) == -1) { 0119 qDebug() << "Can not parse reply:" << reply; 0120 emit userPrincipalNameFailed(); 0121 return; 0122 } 0123 0124 qInfo() << "userPrincipalName: " << userPrincipalNameRE.cap(1); 0125 emit userPrincipalNameReceived(); 0126 } 0127 0128 #include "moc_msgraphdemo.cpp"