1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-12 22:57:38 +02:00

Making oneDrive work on RN

This commit is contained in:
Laurent Cozic
2017-07-06 19:29:09 +00:00
parent bdb250de8a
commit 1c6b569795
8 changed files with 188 additions and 124 deletions

View File

@ -12,6 +12,15 @@ class OneDriveApi {
};
}
static instance() {
if (this.instance_) return this.instance_;
const CLIENT_ID = 'e09fc0de-c958-424f-83a2-e56a721d331b';
const CLIENT_SECRET = 'JA3cwsqSGHFtjMwd5XoF5L5';
this.instance_ = new OneDriveApi(CLIENT_ID, CLIENT_SECRET);
return this.instance_;
}
dispatch(eventName, param) {
let ls = this.listeners_[eventName];
for (let i = 0; i < ls.length; i++) {
@ -47,10 +56,6 @@ class OneDriveApi {
return this.clientSecret_;
}
// possibleOAuthDancePorts() {
// return [1917, 9917, 8917];
// }
async appDirectory() {
let r = await this.execJson('GET', '/drive/special/approot');
return r.parentReference.path + '/' + r.name;
@ -66,6 +71,34 @@ class OneDriveApi {
return 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?' + stringify(query);
}
async execTokenRequest(code, redirectUri, isPublic = false) {
let body = new shim.FormData();
body.append('client_id', this.clientId());
if (!isPublic) body.append('client_secret', this.clientSecret());
body.append('code', code);
body.append('redirect_uri', redirectUri);
body.append('grant_type', 'authorization_code');
const r = await shim.fetch(this.tokenBaseUrl(), {
method: 'POST',
body: body,
})
if (!r.ok) {
const text = await r.text();
throw new Error('Could not retrieve auth code: ' + r.status + ': ' + r.statusText + ': ' + text);
}
try {
const json = await r.json();
this.setAuth(json);
} catch (error) {
const text = await r.text();
error.message += ': ' + text;
throw error;
}
}
oneDriveErrorResponseToError(errorResponse) {
if (!errorResponse) return new Error('Undefined error');
@ -181,90 +214,6 @@ class OneDriveApi {
this.dispatch('authRefreshed', this.auth_);
}
// async oauthDance(targetConsole = null) {
// if (targetConsole === null) targetConsole = console;
// this.auth_ = 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;
// }
// }
// if (!port) throw new Error('All potential ports are in use - please report the issue at https://github.com/laurent22/joplin');
// let authCodeUrl = this.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');
// let body = new shim.FormData();
// body.append('client_id', this.clientId());
// body.append('client_secret', this.clientSecret());
// body.append('code', query.code ? query.code : '');
// body.append('redirect_uri', 'http://localhost:' + port.toString());
// body.append('grant_type', 'authorization_code');
// let options = {
// method: 'POST',
// body: body,
// };
// fetch(this.tokenBaseUrl(), options).then((r) => {
// if (!r.ok) {
// errorMessage = 'Could not retrieve auth code: ' + r.status + ': ' + r.statusText;
// writeResponse(400, errorMessage);
// targetConsole.log('');
// targetConsole.log(errorMessage);
// server.destroy();
// return;
// }
// return r.json().then((json) => {
// this.auth_ = json;
// writeResponse(200, 'The application has been authorised - you may now close this browser tab.');
// targetConsole.log('');
// targetConsole.log('The application has been successfully authorised.');
// server.destroy();
// });
// });
// });
// server.on('close', () => {
// if (errorMessage) {
// reject(new Error(errorMessage));
// } else {
// resolve(this.auth_);
// }
// });
// server.listen(port);
// enableServerDestroy(server);
// targetConsole.log('Please open this URL in your browser to authentify the application:');
// targetConsole.log('');
// targetConsole.log(authCodeUrl);
// });
// }
}
export { OneDriveApi };