Skip to content

Commit e9b4156

Browse files
authored
Merge pull request #120 from leonhartX/fix-auth
fix access token in query parameter
2 parents d46269b + 15f4157 commit e9b4156

File tree

7 files changed

+1181
-1043
lines changed

7 files changed

+1181
-1043
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "4.0.6",
2+
"version": "4.0.7",
33
"manifest_version": 2,
44
"default_locale": "en",
55
"name": "__MSG_appName__",

src/gas-api.js

Lines changed: 175 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -4,215 +4,229 @@ class Gas {
44
pull(code) {
55
const changed = $('.diff-file:checked').toArray().map(e => e.value);
66
const updatePromises = changed.filter(f => code.scm[f])
7-
.map((file) => {
8-
const regex = new RegExp(`(.*?)(${context.config.filetype}|\.json|\.html)$`)
9-
const match = file.match(regex);
10-
if (!match || !match[1] || !match[2]) {
11-
showAlert('Unsupported file type.', LEVEL_ERROR);
12-
return;
13-
}
14-
if (match[2] === 'json' && match[1] !== 'appsscript') {
15-
showAlert('Unsupported file type', LEVEL_ERROR);
16-
return;
17-
}
18-
const name = match[1];
19-
const type = match[2];
7+
.map((file) => {
8+
const regex = new RegExp(`(.*?)(${context.config.filetype}|\.json|\.html)$`)
9+
const match = file.match(regex);
10+
if (!match || !match[1] || !match[2]) {
11+
showAlert('Unsupported file type.', LEVEL_ERROR);
12+
return;
13+
}
14+
if (match[2] === 'json' && match[1] !== 'appsscript') {
15+
showAlert('Unsupported file type', LEVEL_ERROR);
16+
return;
17+
}
18+
const name = match[1];
19+
const type = match[2];
2020

21-
if (!code.gas[file]) {
22-
return () => this.gasCreateFile(name, type)
23-
.then(() => {
24-
return this.gasUpdateFile(name, code.scm[file]);
25-
})
26-
} else {
27-
return () => this.gasUpdateFile(name, code.scm[file]);
28-
}
29-
})
30-
.filter(n => n != undefined);
21+
if (!code.gas[file]) {
22+
return () => this.gasCreateFile(name, type)
23+
.then(() => {
24+
return this.gasUpdateFile(name, code.scm[file]);
25+
})
26+
} else {
27+
return () => this.gasUpdateFile(name, code.scm[file]);
28+
}
29+
})
30+
.filter(n => n != undefined);
3131

3232
const deletePromises = changed.filter(f => !code.scm[f])
33-
.map((file) => {
34-
const regex = new RegExp(`(.*?)(${context.config.filetype}|\.html)$`)
35-
const match = file.match(regex);
36-
if (!match || !match[1] || !match[2]) {
37-
showAlert('Unknow Error', LEVEL_ERROR);
38-
return;
39-
}
40-
const name = match[1];
41-
return () => this.gasDeleteFile(name);
42-
});
33+
.map((file) => {
34+
const regex = new RegExp(`(.*?)(${context.config.filetype}|\.html)$`)
35+
const match = file.match(regex);
36+
if (!match || !match[1] || !match[2]) {
37+
showAlert('Unknow Error', LEVEL_ERROR);
38+
return;
39+
}
40+
const name = match[1];
41+
return () => this.gasDeleteFile(name);
42+
});
4343

4444
if (updatePromises.length === 0 && deletePromises.length === 0) {
4545
showAlert('Nothing to do', LEVEL_WARN);
4646
return;
4747
}
4848

4949
this.getGasContext()
50-
.then(() => {
51-
return Promise.all(updatePromises.map(f => f()))
5250
.then(() => {
53-
return Promise.all(deletePromises.map(f => f()));
51+
return Promise.all(updatePromises.map(f => f()))
52+
.then(() => {
53+
return Promise.all(deletePromises.map(f => f()));
54+
})
5455
})
55-
})
56-
.then(() => {
57-
showAlert('Successfully pulled from scm');
58-
location.reload();
59-
})
60-
.catch((err) => {
61-
showAlert(err.message, LEVEL_ERROR);
62-
});
56+
.then(() => {
57+
showAlert('Successfully pulled from scm');
58+
location.reload();
59+
})
60+
.catch((err) => {
61+
showAlert(err.message, LEVEL_ERROR);
62+
});
6363
}
6464

6565
/*
66-
* get project context with google rpc
67-
* this is very volatile since it is just inferred from code
68-
*/
66+
* get project context with google rpc
67+
* this is very volatile since it is just inferred from code
68+
*/
6969
getGasContext() {
7070
return new Promise((resolve, reject) => {
71-
chrome.storage.local.get(['requestUrl' ,'requestHeaders', 'requestBody', 'gasToken'], resolve);
72-
})
73-
.then((param) => {
74-
context.gasUrl = param.requestUrl;
75-
context.gasHeaders = param.requestHeaders;
76-
context.gasToken = param.gasToken;
77-
return param.requestBody;
78-
});
71+
chrome.storage.local.get(['requestUrl', 'requestHeaders', 'requestBody', 'gasToken'], resolve);
72+
})
73+
.then((param) => {
74+
context.gasUrl = param.requestUrl;
75+
context.gasHeaders = param.requestHeaders;
76+
context.gasToken = param.gasToken;
77+
return param.requestBody;
78+
});
7979
}
8080

8181
getGasCode() {
8282
return this.getGasContext()
83-
.then((requestBody) => {
84-
return $.ajax({
85-
url: context.gasUrl,
86-
headers: context.gasHeaders,
87-
method: 'POST',
88-
crossDomain: true,
89-
data: requestBody,
90-
dataType: 'text'
83+
.then((requestBody) => {
84+
return $.ajax({
85+
url: context.gasUrl,
86+
headers: context.gasHeaders,
87+
method: 'POST',
88+
crossDomain: true,
89+
data: requestBody,
90+
dataType: 'text'
91+
})
9192
})
92-
})
93-
.then((response) => {
94-
if (!response.startsWith('//OK')) throw new Error('Init failed');
95-
//evil eval, but it's simple to get the object since it's not valid json object
96-
const initData = eval(response.slice(4)).filter((e) => {
97-
return typeof(e) === 'object';
98-
})[0];
99-
const ids = initData.filter((data) => { return /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/.test(data) });
100-
context.projectId = initData[initData.indexOf(ids[0]) + 2];
101-
if (context.projectId === context.id) {
102-
// for bounded script
103-
context.id = initData[initData.indexOf(ids[0]) + 1];
104-
}
93+
.then((response) => {
94+
if (!response.startsWith('//OK')) throw new Error('Init failed');
95+
//evil eval, but it's simple to get the object since it's not valid json object
96+
const initData = eval(response.slice(4)).filter((e) => {
97+
return typeof (e) === 'object';
98+
})[0];
99+
const ids = initData.filter((data) => {
100+
return /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/.test(data)
101+
});
102+
context.projectId = initData[initData.indexOf(ids[0]) + 2];
103+
if (context.projectId === context.id) {
104+
// for bounded script
105+
context.id = initData[initData.indexOf(ids[0]) + 1];
106+
}
107+
108+
if (context.projectId.length != 33) {
109+
reject(new Error('cant not get project ID'));
110+
}
111+
const promises = ids.map((id) => {
112+
return new Promise((resolve, reject) => {
113+
const payload = `7|1|9|${getBaseUrl()}\|${context.gasToken}|_|getFileContent|l|${id}|${context.id}|${context.projectId}|m|1|2|3|4|1|5|5|6|7|8|9|0|0|`;
114+
$.ajax({
115+
url: context.gasUrl,
116+
headers: context.gasHeaders,
117+
method: 'POST',
118+
crossDomain: true,
119+
data: payload,
120+
dataType: 'text'
121+
})
122+
.then((response) => {
123+
if (!response.startsWith('//OK')) reject(new Error('get apps script code failed'));
124+
//evil eval, but it's simple to get the object since it's not valid json object
125+
const codeContent = eval(response.slice(4)).filter((e) => {
126+
return typeof (e) === 'object';
127+
})[0];
128+
resolve({
129+
file: codeContent[codeContent.length - 7],
130+
content: codeContent[codeContent.length - 10],
131+
id: id
132+
});
133+
})
134+
.fail(reject);
135+
})
136+
});
137+
return Promise.all(promises);
138+
})
139+
.then((responses) => {
140+
context.fileIds = responses.reduce((hash, elem) => {
141+
if (elem) hash[elem.file] = elem.id;
142+
return hash;
143+
}, {});
144+
return responses;
145+
})
146+
}
105147

106-
if (context.projectId.length != 33) {
107-
reject(new Error('cant not get project ID'));
108-
}
109-
const promises = ids.map((id) => {
110-
return new Promise((resolve, reject) => {
111-
const payload = `7|1|9|${getBaseUrl()}\|${context.gasToken}|_|getFileContent|l|${id}|${context.id}|${context.projectId}|m|1|2|3|4|1|5|5|6|7|8|9|0|0|`;
112-
$.ajax({
148+
gasCreateFile(file, type) {
149+
const typeId = type === context.config.filetype ? 0 : 2;
150+
const payload = `7|1|7|${getBaseUrl()}\|${context.gasToken}|_|makeNewFile|1a|i|${file}|1|2|3|4|2|5|6|7|6|${typeId}|`;
151+
return new Promise((resolve, reject) => {
152+
$.ajax({
113153
url: context.gasUrl,
114154
headers: context.gasHeaders,
115155
method: 'POST',
116156
crossDomain: true,
117157
data: payload,
118158
dataType: 'text'
119159
})
120-
.then((response) => {
121-
if (!response.startsWith('//OK')) reject(new Error('get apps script code failed'));
122-
//evil eval, but it's simple to get the object since it's not valid json object
123-
const codeContent = eval(response.slice(4)).filter((e) => {
124-
return typeof(e) === 'object';
125-
})[0];
126-
resolve({file : codeContent[codeContent.length - 7], content: codeContent[codeContent.length - 10], id : id });
160+
.then(resolve)
161+
.fail((err) => {
162+
reject(new Error('Create file failed'))
127163
})
128-
.fail(reject);
129-
})
130-
});
131-
return Promise.all(promises);
132-
})
133-
.then((responses) => {
134-
context.fileIds = responses.reduce((hash, elem) => {
135-
if (elem) hash[elem.file] = elem.id;
136-
return hash;
137-
}, {});
138-
return responses;
139-
})
140-
}
141-
142-
gasCreateFile(file, type) {
143-
const typeId = type === context.config.filetype ? 0 : 2;
144-
const payload = `7|1|7|${getBaseUrl()}\|${context.gasToken}|_|makeNewFile|1a|i|${file}|1|2|3|4|2|5|6|7|6|${typeId}|`;
145-
return new Promise((resolve, reject) => {
146-
$.ajax({
147-
url: context.gasUrl,
148-
headers: context.gasHeaders,
149-
method: 'POST',
150-
crossDomain: true,
151-
data: payload,
152-
dataType: 'text'
153164
})
154-
.then(resolve)
155-
.fail((err) => {reject(new Error('Create file failed'))})
156-
})
157-
.then((response) => {
158-
if (!response.startsWith('//OK')) throw(new Error(`Create file '${file}.${type}' failed`));
159-
const responseData = eval(response.slice(4)).filter((e) => {
160-
return typeof(e) === 'object';
161-
})[0];
162-
const id = responseData.filter((data) => { return /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/.test(data) });
163-
if (id.length > 0) {
164-
context.fileIds[file] = id[0];
165-
return id[0];
166-
} else {
167-
throw new Error('can not parse response');
168-
}
169-
});
165+
.then((response) => {
166+
if (!response.startsWith('//OK')) throw (new Error(`Create file '${file}.${type}' failed`));
167+
const responseData = eval(response.slice(4)).filter((e) => {
168+
return typeof (e) === 'object';
169+
})[0];
170+
const id = responseData.filter((data) => {
171+
return /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/.test(data)
172+
});
173+
if (id.length > 0) {
174+
context.fileIds[file] = id[0];
175+
return id[0];
176+
} else {
177+
throw new Error('can not parse response');
178+
}
179+
});
170180
}
171181

172182
gasUpdateFile(file, code) {
173183
const escapedCode = code.replace(/\\/g, '\\\\').replace(/\|/g, '\\!');
174184
const payload = `7|1|7|${getBaseUrl()}\|${context.gasToken}|_|updateFile|1a|${file}|${escapedCode}|1|2|3|4|2|5|5|6|7|`;
175185
let headers = context.gasHeaders;
176-
Object.assign(headers, { 'file-id': context.fileIds[file]});
186+
Object.assign(headers, {
187+
'file-id': context.fileIds[file]
188+
});
177189
return new Promise((resolve, reject) => {
178190
$.ajax({
179-
url: context.gasUrl,
180-
headers: headers,
181-
method: 'POST',
182-
crossDomain: true,
183-
data: payload,
184-
dataType: 'text'
185-
})
186-
.then((response) => {
187-
if (!response.startsWith('//OK')) reject(new Error('Update file failed'));
188-
resolve();
189-
})
190-
.fail((err) => {
191-
reject(new Error('Update file failed'));
192-
});
191+
url: context.gasUrl,
192+
headers: headers,
193+
method: 'POST',
194+
crossDomain: true,
195+
data: payload,
196+
dataType: 'text'
197+
})
198+
.then((response) => {
199+
if (!response.startsWith('//OK')) reject(new Error('Update file failed'));
200+
resolve();
201+
})
202+
.fail((err) => {
203+
reject(new Error('Update file failed'));
204+
});
193205
});
194206
}
195207

196208
gasDeleteFile(file) {
197209
const payload = `7|1|4|${getBaseUrl()}\|${context.gasToken}|_|deleteFile|1|2|3|4|0|`;
198210
let headers = context.gasHeaders;
199-
Object.assign(headers, { 'file-id': context.fileIds[file]});
211+
Object.assign(headers, {
212+
'file-id': context.fileIds[file]
213+
});
200214
return new Promise((resolve, reject) => {
201215
$.ajax({
202-
url: context.gasUrl,
203-
headers: headers,
204-
method: 'POST',
205-
crossDomain: true,
206-
data: payload,
207-
dataType: 'text'
208-
})
209-
.then((response) => {
210-
if (!response.startsWith('//OK')) reject(new Error('Delete file failed'));
211-
resolve();
212-
})
213-
.fail((err) => {
214-
reject(new Error('Update file failed'));
215-
});
216+
url: context.gasUrl,
217+
headers: headers,
218+
method: 'POST',
219+
crossDomain: true,
220+
data: payload,
221+
dataType: 'text'
222+
})
223+
.then((response) => {
224+
if (!response.startsWith('//OK')) reject(new Error('Delete file failed'));
225+
resolve();
226+
})
227+
.fail((err) => {
228+
reject(new Error('Update file failed'));
229+
});
216230
});
217231
}
218232
}

0 commit comments

Comments
 (0)