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 Logger = require('lib/Logger').default;
|
||||
const { _ } = require('lib/locale');
|
||||
const urlUtils = require('lib/urlUtils.js');
|
||||
|
||||
class OneDriveApi {
|
||||
// `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) {
|
||||
const body = new shim.FormData();
|
||||
body.append('client_id', this.clientId());
|
||||
if (!this.isPublic()) body.append('client_secret', this.clientSecret());
|
||||
body.append('code', code);
|
||||
body.append('redirect_uri', redirectUri);
|
||||
body.append('grant_type', 'authorization_code');
|
||||
const body = {};
|
||||
body['client_id'] = this.clientId();
|
||||
if (!this.isPublic()) body['client_secret'] = this.clientSecret();
|
||||
body['code'] = code;
|
||||
body['redirect_uri'] = redirectUri;
|
||||
body['grant_type'] = 'authorization_code';
|
||||
|
||||
const r = await shim.fetch(this.tokenBaseUrl(), {
|
||||
method: 'POST',
|
||||
body: body,
|
||||
body: urlUtils.objectToQueryString(body),
|
||||
headers: {
|
||||
['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.'));
|
||||
}
|
||||
|
||||
const body = new shim.FormData();
|
||||
body.append('client_id', this.clientId());
|
||||
if (!this.isPublic()) body.append('client_secret', this.clientSecret());
|
||||
body.append('refresh_token', this.auth_.refresh_token);
|
||||
body.append('redirect_uri', 'http://localhost:1917');
|
||||
body.append('grant_type', 'refresh_token');
|
||||
const body = {};
|
||||
body['client_id'] = this.clientId();
|
||||
if (!this.isPublic()) body['client_secret'] = this.clientSecret();
|
||||
body['refresh_token'] = this.auth_.refresh_token;
|
||||
body['redirect_uri'] = 'http://localhost:1917';
|
||||
body['grant_type'] = 'refresh_token';
|
||||
|
||||
const options = {
|
||||
const response = await shim.fetch(this.tokenBaseUrl(), {
|
||||
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) {
|
||||
this.setAuth(null);
|
||||
const msg = await response.text();
|
||||
|
|
|
@ -91,4 +91,18 @@ urlUtils.extractResourceUrls = function(text) {
|
|||
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;
|
||||
|
|
Loading…
Reference in New Issue