diff --git a/src/authentication.cpp b/src/authentication.cpp index e69de29b..ce46e7e1 100644 --- a/src/authentication.cpp +++ b/src/authentication.cpp @@ -0,0 +1,122 @@ +#include "ruqola.h" +#include "authentication.h" + + +#include +#include +#include +#include + +Authentication::Authentication(){ + + m_google->setScope("email"); +// connect(m_google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, this, &QDesktopServices::openUrl); +// connect(&m_google, &QOAuth2AuthorizationCodeFlow::granted, this, &Authentication::onGranted()); + + +} + + + +void Authentication::OAuthLogin() { + QJsonObject authKeys; + authKeys["credentialToken"] = m_client_id; + authKeys["credentialSecret"] = m_client_secret; + + Ruqola::self()->ddp()->method("login", QJsonDocument(authKeys)); + +} + + +void Authentication::getDataFromJson(){ + + QDir cacheDir(":/src/client_secret.json"); + if (!cacheDir.exists(cacheDir.path())) { + cacheDir.mkpath(cacheDir.path()); + } + + QFile f(cacheDir.absoluteFilePath("client_secret.json")); + + QString val; + if (f.open(QIODevice::ReadOnly | QIODevice::Text)) { + val = f.readAll(); + } + + QJsonDocument document = QJsonDocument::fromJson(val.toUtf8()); + QJsonObject object = document.object(); + const auto settingsObject = object["web"].toObject(); + const QUrl authUri(settingsObject["auth_uri"].toString()); + const auto clientId = settingsObject["client_id"].toString(); + const QUrl tokenUri(settingsObject["token_uri"].toString()); + const auto clientSecret(settingsObject["client_secret"].toString()); + const auto redirectUris = settingsObject["redirect_uris"].toArray(); + const QUrl redirectUri(redirectUris[0].toString()); + const auto port = static_cast(redirectUri.port()); + + m_google->setAuthorizationUrl(authUri); + m_google->setClientIdentifier(clientId); + m_google->setAccessTokenUrl(tokenUri); + m_google->setClientIdentifierSharedKey(clientSecret); + + auto replyHandler = new QOAuthHttpServerReplyHandler(port, this); + m_google->setReplyHandler(replyHandler); + + // If user grants the permissions, client receives a QOAuth2AuthorizationCodeFlow::granted signal + // and can then start sending authorized requests. + m_google->grant(); + +} + +void Authentication::onGranted() +{ + m_authGranted = true; +} + +void Authentication::sendApiRequest() +{ + if(!m_authGranted) return; + + //Try sending a request using https://www.googleapis.com/plus/v1/people/me + auto reply = m_google->get(QUrl("https://www.googleapis.com/plus/v1/people/me")); +} + + +/* + * Handle the OAuth 2.0 server response + * Exchange authorization code for refresh and access tokens + * + * + * + * REQUEST + * ------------------------ + * POST /oauth2/v4/token HTTP/1.1 + Host: www.googleapis.com + Content-Type: application/x-www-form-urlencoded + + code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7& + client_id=your_client_id& + client_secret=your_client_secret& + redirect_uri=https://oauth2.example.com/code& + grant_type=authorization_code + + +Google responds to this request by returning a JSON object that contains a short-lived access token and a refresh token. + + *RESPONSE + * ---------------------------- + * The following snippet shows a sample response: + + { + "access_token":"1/fFAGRNJru1FTz70BzhT3Zg", + "expires_in":3920, + "token_type":"Bearer", + "refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI" + } + + + *CALLING GOOGLE APIs + * ------------------------------- + GET https://www.googleapis.com/drive/v2/files?access_token= + + +*/ diff --git a/src/authentication.h b/src/authentication.h index 399b763b..8406ab6c 100644 --- a/src/authentication.h +++ b/src/authentication.h @@ -1,4 +1,32 @@ #ifndef AUTHENTICATION_H #define AUTHENTICATION_H + +#include +#include + +class Authentication : public QObject +{ + Q_OBJECT +public: + Authentication(); + void getDataFromJson(); + void OAuthLogin(); + void sendApiRequest(); + +private slots: + void onGranted(); + + +private: + bool m_authGranted; + QString m_client_id; + QString m_client_secret; + QOAuth2AuthorizationCodeFlow * m_google; + +}; + + + + #endif // AUTHENTICATION_H diff --git a/src/client_secret.json b/src/client_secret.json new file mode 100644 index 00000000..dd0e3d15 --- /dev/null +++ b/src/client_secret.json @@ -0,0 +1 @@ +{"web":{"client_id":"143580046552-s4rmnq5mg008u76id0d3rl63od985hc6.apps.googleusercontent.com","project_id":"ruqola-161705","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"nyVm19iOjjtldcCZJ-7003xg","redirect_uris":["http://localhost:8080/cb","http://127.0.0.1:3000"]}} \ No newline at end of file