Skip to content

Commit 28ab25e

Browse files
committed
Use the default web browser instead of embedded one for Dropbox authorization
1 parent df78faa commit 28ab25e

File tree

4 files changed

+84
-16
lines changed

4 files changed

+84
-16
lines changed

Data/Scripts/Lang/en.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,10 @@
6363
"confirmation" : {
6464
"text": "You need to sign in to your Imgur account in the web browser that has just opened, then copy the confirmation code into the text field below. Please enter the confirmation code:"
6565
}
66+
},
67+
"oauth": {
68+
"success": "Authorization was successful! You can now close this page.",
69+
"failed": "Failed to get a confirmation code",
70+
"title": "Authorization"
6671
}
6772
}

Data/Scripts/Lang/ru.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,10 @@
6363
"confirmation": {
6464
"text": "Вам нужно войти в свою учетную запись Imgur в веб-браузере, который только что открылся, а затем скопировать код подтверждения в текстовое поле ниже. Пожалуйста, введите код подтверждения:"
6565
}
66+
},
67+
"oauth": {
68+
"success": "Авторизация прошла успешно! Теперь вы можете закрыть эту страницу.",
69+
"failed": "Не удалось получить код подтверждения",
70+
"title": "Авторизация"
6671
}
6772
}

Data/Scripts/dropbox.nut

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ appKey <- GetEnvDecode("IU_DROPBOX_APP_KEY");
22
appSecret <- GetEnvDecode("IU_DROPBOX_APP_SECRET");
33
accessType <- "app_folder";
44

5-
redirectUri <- "https://svistunov.dev/blank.html";
6-
redirectUrlEscaped <- "https:\\/\\/svistunov\\.dev\\/blank\\.html";
5+
const REDIRECT_URI = "https://svistunov.dev/blank.html";
6+
const REDIRECT_URI_ESCAPED = "https:\\/\\/svistunov\\.dev\\/blank\\.html";
77

88
authStep1Url <- "https://api.dropbox.com/1/oauth/request_token";
99
authStep2Url <- "https://api.dropbox.com/1/oauth/access_token";
1010

1111
authCode <- "";
12+
redirectUrl <- "";
1213

1314
function _SignRequest(url, token) {
1415
nm.addQueryHeader("Authorization", "Bearer " + token);
@@ -17,7 +18,7 @@ function _SignRequest(url, token) {
1718
}
1819

1920
function OnUrlChangedCallback(data) {
20-
local reg = CRegExp("^" + redirectUrlEscaped, "");
21+
local reg = CRegExp("^" + REDIRECT_URI_ESCAPED, "");
2122
if ( reg.match(data.url) ) {
2223
local br = data.browser;
2324
local regError = CRegExp("error=([^&]+)", "");
@@ -68,7 +69,7 @@ function _ObtainAccessToken() {
6869
nm.setUrl(url);
6970
nm.addQueryParam("code", authCode);
7071
nm.addQueryParam("grant_type", "authorization_code");
71-
nm.addQueryParam("redirect_uri", redirectUri);
72+
nm.addQueryParam("redirect_uri", redirectUrl);
7273
nm.addQueryParam("client_id", appKey);
7374
nm.addQueryParam("client_secret", appSecret);
7475
nm.doPost("");
@@ -97,19 +98,68 @@ function Authenticate() {
9798
return 1;
9899
}
99100

100-
local browser = CWebBrowser();
101-
browser.setTitle(tr("dropbox.browser.title", "Dropbox authorization"));
102-
browser.setOnUrlChangedCallback(OnUrlChangedCallback, null);
101+
local server = WebServer();
102+
local port = 0;
103+
104+
local htmlHead = @"<html>
105+
<head>
106+
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
107+
<title>%s</title>
108+
</head>
109+
<body>";
110+
111+
htmlHead = format(htmlHead, tr("oauth.title", "Authorization"));
103112

104-
local url = "https://www.dropbox.com/oauth2/authorize?" +
113+
local htmlFooter = "</body></html>";
114+
115+
server.resource("^/$", "GET", function(d) {
116+
local responseBody = "";
117+
if ("code" in d.queryParams){
118+
authCode = d.queryParams.code;
119+
responseBody = htmlHead + "<h1>" + tr("oauth.title", "Authorization") + "</h1><p>" + tr("oauth.success", "Success! Now you can close this page.")+"</p>" + htmlFooter;
120+
} else {
121+
responseBody = htmlHead + "<h1>" + tr("oauth.title", "Authorization") + "</h1><p>" + tr("oauth.success", "Failed to obtain confirmation code") + "</p>" + htmlFooter;
122+
}
123+
124+
return {
125+
responseBody = responseBody,
126+
stopDelay = 500
127+
};
128+
}, null);
129+
130+
local ports = [49707, 39517, 22690, 27966, 51502];
131+
132+
foreach (localPort in ports) {
133+
port = server.bind(localPort);
134+
if (port != 0) {
135+
break;
136+
}
137+
}
138+
139+
if (port != 0) {
140+
redirectUrl = "http://127.0.0.1:" + port;
141+
local url = "https://www.dropbox.com/oauth2/authorize?" +
105142
"client_id=" + appKey +
106143
"&response_type=code" +
107144
"&token_access_type=offline" +
108-
"&redirect_uri=" + nm.urlEncode(redirectUri);
145+
"&redirect_uri=" + nm.urlEncode(redirectUrl);
146+
ShellOpenUrl(url);
147+
server.start();
148+
} else {
149+
local browser = CWebBrowser();
150+
browser.setTitle(tr("dropbox.browser.title", "Dropbox authorization"));
151+
browser.setOnUrlChangedCallback(OnUrlChangedCallback, null);
152+
redirectUrl = REDIRECT_URI;
153+
local url = "https://www.dropbox.com/oauth2/authorize?" +
154+
"client_id=" + appKey +
155+
"&response_type=code" +
156+
"&token_access_type=offline" +
157+
"&redirect_uri=" + nm.urlEncode(redirectUrl);
109158

110-
browser.navigateToUrl(url);
111-
browser.showModal();
112-
159+
browser.navigateToUrl(url);
160+
browser.showModal();
161+
}
162+
113163
return _ObtainAccessToken();
114164
}
115165

@@ -359,7 +409,6 @@ function _RegReplace(str, pattern, replace_with) {
359409

360410
function GetServerParamList() {
361411
return {
362-
token = "token"
363412
UploadPath = "Upload Path"
364413
};
365414
}

Source/Core/Scripting/API/WebServer.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include "WebServer.h"
22

3+
//#include <boost/property_tree/ptree.hpp>
4+
35
#include "Core/3rdpart/SimpleWebServer/server_http.hpp"
4-
#include <boost/property_tree/ptree.hpp>
6+
#include "Core/Logging.h"
7+
#include "Core/Utils/CoreUtils.h"
58

69
#include "ScriptAPI.h"
710

@@ -37,7 +40,7 @@ void WebServer::resource(const std::string& path, const std::string& method, Sqr
3740
d_->callbacks_.push_back(callBack);
3841
size_t callbackIndex = d_->callbacks_.size()-1;
3942

40-
using namespace boost::property_tree;
43+
//using namespace boost::property_tree;
4144

4245
d_->server_.resource[path][method] = [this, callbackIndex](std::shared_ptr<HttpServer::Response> response, std::shared_ptr<HttpServer::Request> request) {
4346
try {
@@ -115,7 +118,13 @@ void WebServer::stop() {
115118
int WebServer::bind(int port) {
116119
d_->server_.config.address = "127.0.0.1";
117120
d_->server_.config.port = static_cast<unsigned int>(port);
118-
return d_->server_.bind();
121+
try {
122+
return d_->server_.bind();
123+
}
124+
catch (const std::exception& ex) {
125+
//LOG(ERROR) << IuCoreUtils::SystemLocaleToUtf8(ex.what()) << std::endl;
126+
}
127+
return 0;
119128
}
120129

121130

0 commit comments

Comments
 (0)