Skip to content

Commit de5dea5

Browse files
authored
chore: Add script to get chrome refresh token to tools (#6257)
#### Details Adding script used for getting a chrome refresh token to the tools dir. This script is used in our secrets rotation process. ##### Motivation Keep scripts related to this product in a central location #### Pull request checklist <!-- If a checklist item is not applicable to this change, write "n/a" in the checkbox --> - [n/a] Addresses an existing issue: #0000 - [x] Ran `yarn null:autoadd` - [x] Ran `yarn fastpass` - [n/a] Added/updated relevant unit test(s) (and ran `yarn test`) - [n/a] Verified code coverage for the changes made. Check coverage report at: `<rootDir>/test-results/unit/coverage` - [x] PR title *AND* final merge commit title both start with a semantic tag (`fix:`, `chore:`, `feat(feature-name):`, `refactor:`). See `CONTRIBUTING.md`. - [n/a] (UI changes only) Added screenshots/GIFs to description above - [n/a] (UI changes only) Verified usability with NVDA/JAWS
1 parent 1891243 commit de5dea5

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

tools/get-chrome-refresh-token.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
const http = require('http');
4+
const request = require('request');
5+
6+
// This script is used to generate new chrome web store access tokens and refresh tokens from the
7+
// chrome web store API (see docs: https://developer.chrome.com/docs/webstore/using_webstore_api/).
8+
// Parameters:
9+
// 0: client id
10+
// 1: client secret
11+
// Example command line call: node .\get-chrome-refresh-token.js $CLIENT_ID $CLIENT_SECRET
12+
13+
const host = 'localhost';
14+
const port = 8000;
15+
const scope = 'https://www.googleapis.com/auth/chromewebstore';
16+
17+
// Validate command line args
18+
const clientId = process.argv[2];
19+
const clientSecret = process.argv[3];
20+
if (!clientId || !clientSecret) {
21+
throw new Error('Please provide client id and secret command line args');
22+
} else {
23+
console.log('Provided client id: ' + clientId);
24+
console.log('Provided client secret: ' + clientSecret);
25+
}
26+
27+
const constructAccessTokenUrl = function () {
28+
return `https://accounts.google.com/o/oauth2/v2/auth?scope=${scope}&response_type=code&redirect_uri=http%3A//${host}%3A${port}&client_id=${clientId}`;
29+
};
30+
31+
const fetchRefreshToken = function (accessToken) {
32+
var body =
33+
'grant_type=authorization_code&code=' +
34+
accessToken +
35+
'&redirect_uri=http%3A//' +
36+
host +
37+
'%3A' +
38+
scope +
39+
'&client_id=' +
40+
clientId +
41+
'&client_secret=' +
42+
clientSecret;
43+
request.post(
44+
{
45+
url: 'https://accounts.google.com/o/oauth2/token',
46+
form: body,
47+
headers: {
48+
'Content-Type': 'application/x-www-form-urlencoded',
49+
},
50+
},
51+
function (_err, _httpResponse, body) {
52+
console.log('Refresh token: ' + JSON.parse(body).refresh_token);
53+
},
54+
);
55+
};
56+
57+
const requestListener = function (req, res) {
58+
if (req.headers.referer) {
59+
// Parse access token
60+
const url = new URL(req.headers.referer);
61+
const params = new Proxy(new URLSearchParams(url.search), {
62+
get: (searchParams, prop) => searchParams.get(prop),
63+
});
64+
console.log('Access token: ' + params.code);
65+
fetchRefreshToken(params.code);
66+
}
67+
res.writeHead(200);
68+
res.end('See terminal for access token');
69+
};
70+
71+
// Start a server to receive the access token once authentication is completed. This is necessary
72+
// since OOB has been deprecated (https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html#disallowed-oob).
73+
const server = http.createServer(requestListener);
74+
server.listen(port, host, () => {
75+
console.log(`Server is running on http://${host}:${port}`);
76+
console.log(
77+
`Get access token by opening the following URL in a browser: ${constructAccessTokenUrl()}`,
78+
);
79+
});

0 commit comments

Comments
 (0)