-
Notifications
You must be signed in to change notification settings - Fork 327
/
auth.js
70 lines (62 loc) · 1.78 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { remote } from 'electron';
import webdavFs from 'webdav-fs';
import dropboxFs from 'dropbox-fs';
import anyFs from 'any-fs';
const { BrowserWindow } = remote;
const currentWindow = BrowserWindow.getFocusedWindow();
export function authenticateDropbox() {
return new Promise((resolve, reject) => {
let foundToken = null;
const authWin = new BrowserWindow({
parent: currentWindow,
// modal: true,
show: false,
webPreferences: {
nodeIntegration: false,
webSecurity: false,
sandbox: true
}
});
const redirectUri = 'https://buttercup.pw/';
const clientId = '5fstmwjaisrt06t';
const authUri = `https://www.dropbox.com/1/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token`;
authWin.loadURL(authUri);
authWin.show();
const navigateCb = url => {
const match = url.match(/access_token=([^&]*)/);
if (match !== null && match.length > 0) {
foundToken = match[1];
authWin.hide();
}
};
const closeCb = () => {
if (foundToken) {
resolve(foundToken);
} else {
reject(new Error('Auth unsuccessful.'));
}
};
authWin.webContents.on('did-start-navigation', (e, url) => navigateCb(url));
authWin.webContents.on('did-get-redirect-request', (e, oldUrl, newUrl) =>
navigateCb(newUrl)
);
authWin.on('hide', closeCb);
authWin.on('close', closeCb);
});
}
export function getFsInstance(type, settings) {
switch (type) {
case 'dropbox':
return anyFs(
dropboxFs({
apiKey: settings.token
})
);
case 'webdav':
return anyFs(
webdavFs(settings.endpoint, settings.username, settings.password)
);
default:
return null;
}
}