2017-07-18 20:21:03 +02:00
|
|
|
import { _ } from 'lib/locale.js'
|
|
|
|
|
2017-07-06 20:58:01 +02:00
|
|
|
const tcpPortUsed = require('tcp-port-used');
|
|
|
|
const http = require("http");
|
|
|
|
const urlParser = require("url");
|
|
|
|
const FormData = require('form-data');
|
|
|
|
const enableServerDestroy = require('server-destroy');
|
|
|
|
|
|
|
|
class OneDriveApiNodeUtils {
|
|
|
|
|
|
|
|
constructor(api) {
|
|
|
|
this.api_ = api;
|
|
|
|
}
|
|
|
|
|
|
|
|
api() {
|
|
|
|
return this.api_;
|
|
|
|
}
|
|
|
|
|
|
|
|
possibleOAuthDancePorts() {
|
|
|
|
return [1917, 9917, 8917];
|
|
|
|
}
|
|
|
|
|
|
|
|
async oauthDance(targetConsole = null) {
|
|
|
|
if (targetConsole === null) targetConsole = console;
|
|
|
|
|
|
|
|
this.api().setAuth(null);
|
|
|
|
|
|
|
|
let ports = this.possibleOAuthDancePorts();
|
|
|
|
let port = null;
|
|
|
|
for (let i = 0; i < ports.length; i++) {
|
|
|
|
let inUse = await tcpPortUsed.check(ports[i]);
|
|
|
|
if (!inUse) {
|
|
|
|
port = ports[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:49:47 +02:00
|
|
|
if (!port) throw new Error(_('All potential ports are in use - please report the issue at %s', 'https://github.com/laurent22/joplin'));
|
2017-07-06 20:58:01 +02:00
|
|
|
|
|
|
|
let authCodeUrl = this.api().authCodeUrl('http://localhost:' + port);
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let server = http.createServer();
|
|
|
|
let errorMessage = null;
|
|
|
|
|
|
|
|
server.on('request', (request, response) => {
|
|
|
|
const query = urlParser.parse(request.url, true).query;
|
|
|
|
|
|
|
|
function writeResponse(code, message) {
|
|
|
|
response.writeHead(code, {"Content-Type": "text/html"});
|
|
|
|
response.write(message);
|
|
|
|
response.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!query.code) return writeResponse(400, '"code" query parameter is missing');
|
|
|
|
|
2017-07-06 21:29:09 +02:00
|
|
|
this.api().execTokenRequest(query.code, 'http://localhost:' + port.toString()).then(() => {
|
2017-07-18 20:21:03 +02:00
|
|
|
writeResponse(200, _('The application has been authorised - you may now close this browser tab.'));
|
2017-07-06 21:29:09 +02:00
|
|
|
targetConsole.log('');
|
2017-07-18 20:21:03 +02:00
|
|
|
targetConsole.log(_('The application has been successfully authorised.'));
|
2017-07-06 21:29:09 +02:00
|
|
|
server.destroy();
|
|
|
|
}).catch((error) => {
|
|
|
|
writeResponse(400, error.message);
|
|
|
|
targetConsole.log('');
|
|
|
|
targetConsole.log(error.message);
|
|
|
|
server.destroy();
|
2017-07-06 20:58:01 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.on('close', () => {
|
|
|
|
if (errorMessage) {
|
|
|
|
reject(new Error(errorMessage));
|
|
|
|
} else {
|
|
|
|
resolve(this.api().auth());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(port);
|
|
|
|
|
|
|
|
enableServerDestroy(server);
|
|
|
|
|
2017-07-18 20:21:03 +02:00
|
|
|
targetConsole.log(_('Please open this URL in your browser to authentify the application:'));
|
2017-07-06 20:58:01 +02:00
|
|
|
targetConsole.log('');
|
|
|
|
targetConsole.log(authCodeUrl);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export { OneDriveApiNodeUtils };
|