-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoauth.js
70 lines (64 loc) · 3.48 KB
/
oauth.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
require("shelljs/global");
var shared = require("./shared");
module.exports = {
/**
* Open a browser that performs an oauth access token request
* This generates a new 'offline' oauth token.
* Took a while to get right because google apparently reissues the already existing token
* when you have requested a token other than 'offline' earlier.
* You *HAVE* to de-auth the existing token you have in google acounts when the refresh_tokens keep expiring!
*/
getApprovalPromptURL: function() {
var credentials = shared.getCredentials();
return 'https://accounts.google.com/o/oauth2/auth?response_type=code&scope=https://www.googleapis.com/auth/chromewebstore&client_id=' + credentials.CHROME_WEBSTORE_CLIENT_ID + '&redirect_uri=urn:ietf:wg:oauth:2.0:oob&access_type=offline&approval_prompt=force&state=' + new Date().getTime();
},
generateInitialToken: function() {
var credentials = shared.getCredentials();
var response = JSON.parse(exec('curl "https://accounts.google.com/o/oauth2/token" -d "client_secret=' + credentials.CHROME_WEBSTORE_CLIENT_SECRET + '&client_id=' + credentials.CHROME_WEBSTORE_CLIENT_ID + '&code=' + code.trim() + '&grant_type=authorization_code&redirect_uri=urn:ietf:wg:oauth:2.0:oob"'));
if (response.error) {
process.exit();
}
credentials.CHROME_WEBSTORE_REFRESH_TOKEN = response.refresh_token;
credentials.CHROME_WEBSTORE_CODE = response.access_token;
credentials.CHROME_WEBSTORE_REFRESH_TOKEN_MAX_AGE = Math.floor(new Date().getTime() / 1000) + response.expires_in;
shared.putCredentials(credentials);
return response;
},
refreshTokenIfNeeded: function() {
var credentials = shared.getCredentials();
if (Math.floor(new Date().getTime() / 1000) > credentials.CHROME_WEBSTORE_REFRESH_TOKEN_MAX_AGE) {
var response = JSON.parse(exec('curl "https://accounts.google.com/o/oauth2/token" -d "client_id=' + credentials.CHROME_WEBSTORE_CLIENT_ID + '&client_secret=' + credentials.CHROME_WEBSTORE_CLIENT_SECRET + '&refresh_token=' + credentials.CHROME_WEBSTORE_REFRESH_TOKEN.trim() + '&grant_type=refresh_token"'));
if (response.error) {
process.exit();
}
credentials.CHROME_WEBSTORE_CODE = response.access_token;
credentials.CHROME_WEBSTORE_REFRESH_TOKEN_MAX_AGE = Math.floor(new Date().getTime() / 1000) + response.expires_in;
shared.putCredentials(credentials);
echo("Oauth access token refreshed.");
}
},
/**
* Perform oAuth authenticated PUT request to upload new zipfile to web store
*/
uploadBinary: function(APP_ID, zipFile) {
return exec(['curl',
'-H "Authorization: Bearer ' + shared.getCredentials().CHROME_WEBSTORE_CODE + '"',
'-H "x-goog-api-version: 2"',
'-X PUT -T ' + zipFile,
'https://www.googleapis.com/upload/chromewebstore/v1.1/items/' + APP_ID
].join(" "));
},
/**
* Publish the uploaded draft
*/
publishDraft: function(APP_ID) {
return exec(['curl',
'-H "Authorization: Bearer ' + shared.getCredentials().CHROME_WEBSTORE_CODE + '"',
'-H "x-goog-api-version: 2"',
'-H "Content-Type: application/json"',
'-X POST',
"-d '{\"target\": \"default\"}'",
'https://www.googleapis.com/chromewebstore/v1.1/items/' + APP_ID + '/publish'
].join(" "));
}
}