summaryrefslogtreecommitdiff
path: root/plugins/CloudFile/src
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2017-04-23 23:07:03 +0300
committeraunsane <aunsane@gmail.com>2017-04-23 23:14:45 +0300
commit856629d0e757c73720097fd89d598c097df86c2b (patch)
tree716255c90dda5538f367d464f0dbbc968b55c3a5 /plugins/CloudFile/src
parent71a8d5202cc65a6e714956e78852a7b87ce51b35 (diff)
CloudFile: fix for token refresh
Diffstat (limited to 'plugins/CloudFile/src')
-rw-r--r--plugins/CloudFile/src/Services/dropbox_service.cpp4
-rw-r--r--plugins/CloudFile/src/Services/google_api.h15
-rw-r--r--plugins/CloudFile/src/Services/google_service.cpp49
-rw-r--r--plugins/CloudFile/src/Services/yandex_api.h15
-rw-r--r--plugins/CloudFile/src/Services/yandex_service.cpp52
5 files changed, 114 insertions, 21 deletions
diff --git a/plugins/CloudFile/src/Services/dropbox_service.cpp b/plugins/CloudFile/src/Services/dropbox_service.cpp
index 26c0ff6af3..1feeddce47 100644
--- a/plugins/CloudFile/src/Services/dropbox_service.cpp
+++ b/plugins/CloudFile/src/Services/dropbox_service.cpp
@@ -24,7 +24,9 @@ HANDLE CDropboxService::GetIcon() const
bool CDropboxService::IsLoggedIn()
{
ptrA token(db_get_sa(NULL, GetModule(), "TokenSecret"));
- return token != NULL;
+ if (!token || token[0] == 0)
+ return false;
+ return true;
}
void CDropboxService::Login()
diff --git a/plugins/CloudFile/src/Services/google_api.h b/plugins/CloudFile/src/Services/google_api.h
index a48e9e878b..cf405dda36 100644
--- a/plugins/CloudFile/src/Services/google_api.h
+++ b/plugins/CloudFile/src/Services/google_api.h
@@ -26,6 +26,21 @@ namespace GDriveAPI
}
};
+ class RefreshTokenRequest : public HttpRequest
+ {
+ public:
+ RefreshTokenRequest(const char *refreshToken) :
+ HttpRequest(REQUEST_POST, "https://www.googleapis.com/oauth2/v4/token")
+ {
+ AddHeader("Content-Type", "application/x-www-form-urlencoded");
+
+ CMStringA data(CMStringDataFormat::FORMAT,
+ "client_id=%s&client_secret=%s&grant_type=refresh_token&refresh_token=%s",
+ GOOGLE_APP_ID, GOOGLE_CLIENT_SECRET, refreshToken);
+ SetData(data.GetBuffer(), data.GetLength());
+ }
+ };
+
class RevokeAccessTokenRequest : public HttpRequest
{
public:
diff --git a/plugins/CloudFile/src/Services/google_service.cpp b/plugins/CloudFile/src/Services/google_service.cpp
index 03b4880bd0..3c88ffd366 100644
--- a/plugins/CloudFile/src/Services/google_service.cpp
+++ b/plugins/CloudFile/src/Services/google_service.cpp
@@ -24,12 +24,35 @@ HANDLE CGDriveService::GetIcon() const
bool CGDriveService::IsLoggedIn()
{
ptrA token(db_get_sa(NULL, GetModule(), "TokenSecret"));
- return token != NULL;
+ if (!token || token[0] == 0)
+ return false;
+ time_t now = time(NULL);
+ time_t expiresIn = db_get_dw(NULL, GetModule(), "ExpiresIn");
+ return now < expiresIn;
}
void CGDriveService::Login()
{
- COAuthDlg(this, GOOGLE_OAUTH "/auth?response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file&redirect_uri=urn:ietf:wg:oauth:2.0:oob&client_id=" GOOGLE_APP_ID, RequestAccessTokenThread).DoModal();
+ ptrA token(db_get_sa(NULL, GetModule(), "TokenSecret"));
+ ptrA refreshToken(db_get_sa(NULL, GetModule(), "RefreshToken"));
+ if (token && refreshToken && refreshToken[0]) {
+ GDriveAPI::RefreshTokenRequest request(refreshToken);
+ NLHR_PTR response(request.Send(hConnection));
+
+ JSONNode root = GetJsonResponse(response);
+
+ JSONNode node = root.at("access_token");
+ db_set_s(NULL, GetModule(), "TokenSecret", node.as_string().c_str());
+
+ node = root.at("expires_in");
+ time_t expiresIn = time(NULL) + node.as_int();
+ db_set_dw(NULL, GetModule(), "ExpiresIn", expiresIn);
+
+ return;
+ }
+
+ COAuthDlg dlg(this, GOOGLE_OAUTH "/auth?response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file&redirect_uri=urn:ietf:wg:oauth:2.0:oob&client_id=" GOOGLE_APP_ID, RequestAccessTokenThread);
+ dlg.DoModal();
}
void CGDriveService::Logout()
@@ -78,7 +101,13 @@ unsigned CGDriveService::RequestAccessTokenThread(void *owner, void *param)
node = root.at("access_token");
db_set_s(NULL, service->GetModule(), "TokenSecret", node.as_string().c_str());
- ProtoBroadcastAck(MODULE, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_OFFLINE, (WPARAM)ID_STATUS_ONLINE);
+
+ node = root.at("expires_in");
+ time_t expiresIn = time(NULL) + node.as_int();
+ db_set_dw(NULL, service->GetModule(), "ExpiresIn", expiresIn);
+
+ node = root.at("refresh_token");
+ db_set_s(NULL, service->GetModule(), "RefreshToken", node.as_string().c_str());
SetDlgItemTextA(hwndDlg, IDC_OAUTH_CODE, "");
@@ -186,15 +215,15 @@ void CGDriveService::CreateSharedLink(const char *fileId, char *url)
UINT CGDriveService::Upload(FileTransferParam *ftp)
{
- if (!IsLoggedIn())
- Login();
+ try {
+ if (!IsLoggedIn())
+ Login();
- if (!IsLoggedIn()) {
- ftp->SetStatus(ACKRESULT_FAILED);
- return ACKRESULT_FAILED;
- }
+ if (!IsLoggedIn()) {
+ ftp->SetStatus(ACKRESULT_FAILED);
+ return ACKRESULT_FAILED;
+ }
- try {
const wchar_t *folderName = ftp->GetFolderName();
if (folderName) {
char path[MAX_PATH], link[MAX_PATH];
diff --git a/plugins/CloudFile/src/Services/yandex_api.h b/plugins/CloudFile/src/Services/yandex_api.h
index f70964bc78..2a83abb634 100644
--- a/plugins/CloudFile/src/Services/yandex_api.h
+++ b/plugins/CloudFile/src/Services/yandex_api.h
@@ -24,6 +24,21 @@ namespace YandexAPI
}
};
+ class RefreshTokenRequest : public HttpRequest
+ {
+ public:
+ RefreshTokenRequest(const char *refreshToken) :
+ HttpRequest(REQUEST_POST, YANDEX_OAUTH "/token")
+ {
+ AddHeader("Content-Type", "application/x-www-form-urlencoded");
+
+ CMStringA data(CMStringDataFormat::FORMAT,
+ "client_id=%s&client_secret=%s&grant_type=refresh_token&refresh_token=%s",
+ YANDEX_APP_ID, YADISK_CLIENT_SECRET, refreshToken);
+ SetData(data.GetBuffer(), data.GetLength());
+ }
+ };
+
class RevokeAccessTokenRequest : public HttpRequest
{
public:
diff --git a/plugins/CloudFile/src/Services/yandex_service.cpp b/plugins/CloudFile/src/Services/yandex_service.cpp
index d7b2e8b9e6..4ef0a5e115 100644
--- a/plugins/CloudFile/src/Services/yandex_service.cpp
+++ b/plugins/CloudFile/src/Services/yandex_service.cpp
@@ -24,12 +24,38 @@ HANDLE CYandexService::GetIcon() const
bool CYandexService::IsLoggedIn()
{
ptrA token(db_get_sa(NULL, GetModule(), "TokenSecret"));
- return token != NULL;
+ if (!token || token[0] == 0)
+ return false;
+ time_t now = time(NULL);
+ time_t expiresIn = db_get_dw(NULL, GetModule(), "ExpiresIn");
+ return now < expiresIn;
}
void CYandexService::Login()
{
- COAuthDlg(this, YANDEX_OAUTH "/authorize?response_type=code&client_id=" YANDEX_APP_ID, RequestAccessTokenThread).DoModal();
+ ptrA token(db_get_sa(NULL, GetModule(), "TokenSecret"));
+ ptrA refreshToken(db_get_sa(NULL, GetModule(), "RefreshToken"));
+ if (token && refreshToken && refreshToken[0]) {
+ YandexAPI::RefreshTokenRequest request(refreshToken);
+ NLHR_PTR response(request.Send(hConnection));
+
+ JSONNode root = GetJsonResponse(response);
+
+ JSONNode node = root.at("access_token");
+ db_set_s(NULL, GetModule(), "TokenSecret", node.as_string().c_str());
+
+ node = root.at("expires_in");
+ time_t expiresIn = time(NULL) + node.as_int();
+ db_set_dw(NULL, GetModule(), "ExpiresIn", expiresIn);
+
+ node = root.at("refresh_token");
+ db_set_s(NULL, GetModule(), "RefreshToken", node.as_string().c_str());
+
+ return;
+ }
+
+ COAuthDlg dlg(this, YANDEX_OAUTH "/authorize?response_type=code&client_id=" YANDEX_APP_ID, RequestAccessTokenThread);
+ dlg.DoModal();
}
void CYandexService::Logout()
@@ -78,7 +104,13 @@ unsigned CYandexService::RequestAccessTokenThread(void *owner, void *param)
node = root.at("access_token");
db_set_s(NULL, service->GetModule(), "TokenSecret", node.as_string().c_str());
- ProtoBroadcastAck(MODULE, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_OFFLINE, (WPARAM)ID_STATUS_ONLINE);
+
+ node = root.at("expires_in");
+ time_t expiresIn = time(NULL) + node.as_int();
+ db_set_dw(NULL, service->GetModule(), "ExpiresIn", expiresIn);
+
+ node = root.at("refresh_token");
+ db_set_s(NULL, service->GetModule(), "RefreshToken", node.as_string().c_str());
SetDlgItemTextA(hwndDlg, IDC_OAUTH_CODE, "");
@@ -165,15 +197,15 @@ void CYandexService::CreateSharedLink(const char *path, char *url)
UINT CYandexService::Upload(FileTransferParam *ftp)
{
- if (!IsLoggedIn())
- Login();
+ try {
+ if (!IsLoggedIn())
+ Login();
- if (!IsLoggedIn()) {
- ftp->SetStatus(ACKRESULT_FAILED);
- return ACKRESULT_FAILED;
- }
+ if (!IsLoggedIn()) {
+ ftp->SetStatus(ACKRESULT_FAILED);
+ return ACKRESULT_FAILED;
+ }
- try {
const wchar_t *folderName = ftp->GetFolderName();
if (folderName) {
char path[MAX_PATH], link[MAX_PATH];