mirror of https://github.com/laurent22/joplin.git
All: Fixed OneDrive authentication
parent
ece7ffadd6
commit
98c933fdb7
|
@ -3,6 +3,7 @@ const { stringify } = require('query-string');
|
||||||
const { time } = require('lib/time-utils.js');
|
const { time } = require('lib/time-utils.js');
|
||||||
const Logger = require('lib/Logger').default;
|
const Logger = require('lib/Logger').default;
|
||||||
const { _ } = require('lib/locale');
|
const { _ } = require('lib/locale');
|
||||||
|
const urlUtils = require('lib/urlUtils.js');
|
||||||
|
|
||||||
class OneDriveApi {
|
class OneDriveApi {
|
||||||
// `isPublic` is to tell OneDrive whether the application is a "public" one (Mobile and desktop
|
// `isPublic` is to tell OneDrive whether the application is a "public" one (Mobile and desktop
|
||||||
|
@ -90,16 +91,16 @@ class OneDriveApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
async execTokenRequest(code, redirectUri) {
|
async execTokenRequest(code, redirectUri) {
|
||||||
const body = new shim.FormData();
|
const body = {};
|
||||||
body.append('client_id', this.clientId());
|
body['client_id'] = this.clientId();
|
||||||
if (!this.isPublic()) body.append('client_secret', this.clientSecret());
|
if (!this.isPublic()) body['client_secret'] = this.clientSecret();
|
||||||
body.append('code', code);
|
body['code'] = code;
|
||||||
body.append('redirect_uri', redirectUri);
|
body['redirect_uri'] = redirectUri;
|
||||||
body.append('grant_type', 'authorization_code');
|
body['grant_type'] = 'authorization_code';
|
||||||
|
|
||||||
const r = await shim.fetch(this.tokenBaseUrl(), {
|
const r = await shim.fetch(this.tokenBaseUrl(), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: body,
|
body: urlUtils.objectToQueryString(body),
|
||||||
headers: {
|
headers: {
|
||||||
['Content-Type']: 'application/x-www-form-urlencoded',
|
['Content-Type']: 'application/x-www-form-urlencoded',
|
||||||
},
|
},
|
||||||
|
@ -366,19 +367,21 @@ class OneDriveApi {
|
||||||
throw new Error(_('Cannot refresh token: authentication data is missing. Starting the synchronisation again may fix the problem.'));
|
throw new Error(_('Cannot refresh token: authentication data is missing. Starting the synchronisation again may fix the problem.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = new shim.FormData();
|
const body = {};
|
||||||
body.append('client_id', this.clientId());
|
body['client_id'] = this.clientId();
|
||||||
if (!this.isPublic()) body.append('client_secret', this.clientSecret());
|
if (!this.isPublic()) body['client_secret'] = this.clientSecret();
|
||||||
body.append('refresh_token', this.auth_.refresh_token);
|
body['refresh_token'] = this.auth_.refresh_token;
|
||||||
body.append('redirect_uri', 'http://localhost:1917');
|
body['redirect_uri'] = 'http://localhost:1917';
|
||||||
body.append('grant_type', 'refresh_token');
|
body['grant_type'] = 'refresh_token';
|
||||||
|
|
||||||
const options = {
|
const response = await shim.fetch(this.tokenBaseUrl(), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: body,
|
body: urlUtils.objectToQueryString(body),
|
||||||
};
|
headers: {
|
||||||
|
['Content-Type']: 'application/x-www-form-urlencoded',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const response = await shim.fetch(this.tokenBaseUrl(), options);
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
this.setAuth(null);
|
this.setAuth(null);
|
||||||
const msg = await response.text();
|
const msg = await response.text();
|
||||||
|
|
|
@ -91,4 +91,18 @@ urlUtils.extractResourceUrls = function(text) {
|
||||||
return output;
|
return output;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
urlUtils.objectToQueryString = function(query) {
|
||||||
|
if (!query) return '';
|
||||||
|
|
||||||
|
let queryString = '';
|
||||||
|
const s = [];
|
||||||
|
for (const k in query) {
|
||||||
|
if (!query.hasOwnProperty(k)) continue;
|
||||||
|
s.push(`${encodeURIComponent(k)}=${encodeURIComponent(query[k])}`);
|
||||||
|
}
|
||||||
|
queryString = s.join('&');
|
||||||
|
|
||||||
|
return queryString;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = urlUtils;
|
module.exports = urlUtils;
|
||||||
|
|
Loading…
Reference in New Issue