forked from mawie81/electron-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
154 lines (131 loc) · 4.35 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const Promise = require('pinkie-promise');
const queryString = require('querystring');
const fetch = require('node-fetch');
const objectAssign = require('object-assign');
const nodeUrl = require('url');
const electron = require('electron');
const BrowserWindow = electron.BrowserWindow || electron.remote.BrowserWindow;
var generateRandomString = function (length) {
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
module.exports = function (config, windowParams) {
function getAuthorizationCode(opts) {
opts = opts || {};
if (!config.redirectUri) {
config.redirectUri = 'urn:ietf:wg:oauth:2.0:oob';
}
var urlParams = {
response_type: 'code',
redirect_uri: config.redirectUri,
client_id: config.clientId,
state: generateRandomString(16)
};
if (opts.scope) {
urlParams.scope = opts.scope;
}
if (opts.accessType) {
urlParams.access_type = opts.accessType;
}
var url = config.authorizationUrl + '?' + queryString.stringify(urlParams);
return new Promise(function (resolve, reject) {
const authWindow = new BrowserWindow(windowParams || { 'use-content-size': true });
authWindow.loadURL(url);
authWindow.show();
authWindow.on('closed', function () {
reject(new Error('window was closed by user'));
});
function onCallback(url) {
var url_parts = nodeUrl.parse(url, true);
var query = url_parts.query;
var code = query.code;
var error = query.error;
if (error !== undefined) {
reject(error);
authWindow.removeAllListeners('closed');
setImmediate(function () {
setTimeout(function () {
authWindow.webContents.session.clearStorageData({
storages: ['appcache', 'cookies', 'filesystem', 'shadercache'],
quotas: ['persistent', 'syncable']
}, function () {
authWindow.close();
authWindow.destroy()
});
}, 100)
});
} else if (code) {
resolve(code);
authWindow.removeAllListeners('closed');
setImmediate(function () {
setTimeout(function () {
authWindow.webContents.session.clearStorageData({
storages: ['appcache', 'cookies', 'filesystem', 'shadercache'],
quotas: ['persistent', 'syncable']
}, function () {
authWindow.close();
authWindow.destroy()
});
}, 100)
});
}
}
authWindow.webContents.on('will-navigate', function (event, url) {
onCallback(url);
});
authWindow.webContents.on('did-get-redirect-request', function (event, oldUrl, newUrl) {
onCallback(newUrl);
});
});
}
function tokenRequest(data) {
const header = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
};
if (config.useBasicAuthorizationHeader) {
header.Authorization = 'Basic ' + new Buffer(config.clientId + ':' + config.clientSecret).toString('base64');
} else {
objectAssign(data, {
client_id: config.clientId,
client_secret: config.clientSecret
});
}
return fetch(config.tokenUrl, {
method: 'POST',
headers: header,
body: queryString.stringify(data)
}).then(function (res) {
return res.json();
});
}
function getAccessToken(opts) {
return getAuthorizationCode(opts)
.then(function (authorizationCode) {
var tokenRequestData = {
code: authorizationCode,
grant_type: 'authorization_code',
redirect_uri: config.redirectUri
};
tokenRequestData = Object.assign(tokenRequestData, opts.additionalTokenRequestData);
return tokenRequest(tokenRequestData);
});
}
function refreshToken(refreshToken) {
return tokenRequest({
refresh_token: refreshToken,
grant_type: 'refresh_token',
redirect_uri: config.redirectUri
});
}
return {
getAuthorizationCode: getAuthorizationCode,
getAccessToken: getAccessToken,
refreshToken: refreshToken
};
};