This repository has been archived by the owner on Sep 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
preflight.js
52 lines (47 loc) · 1.9 KB
/
preflight.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
function runPreFlightChecks() {
const isHttp = window.location.protocol == 'http:',
isLocalhost = window.location.hostname == 'localhost'
isUnsafe = isHttp && !isLocalhost;
if (isUnsafe) {
const isHttpMessage = [
'This page is running over the web over HTTP.',
'You should use HTTPS and rotate your access token.',
].join(' ');
alert(isHttpMessage);
}
// We will:
// - Make a request to the github rate limit endpoint
// (This will not affect the rate limit)
// - Check what scopes we have access to
const queryVars = new FourthWall.getQueryVariables(),
token = queryVars.token,
extraScopes = (queryVars.extra_scopes || '').split(',').filter(
function(scope) { return scope.length > 0 }
),
ghUrl = 'https://api.github.com/rate_limit',
authGhUrl = ghUrl + '?access_token=' + token;
fetch(authGhUrl)
.then(function (response) { return response.headers; })
.then(function (headers) { return headers.get('x-oauth-scopes') })
.then(function (scopes) { return scopes.split(', '); })
.then(function (scopes) {
const allowedScopes = [
'repo:status', 'repo_deployment', 'read:org'
].concat(extraScopes);
let badScopes = scopes.filter(function(scope) {
return allowedScopes.indexOf(scope) < 0;
});
if (badScopes.length > 0) {
let badScopesString = badScopes.join(' '),
badScopesMessage = [
'You have the following unnecessary scopes: <',
badScopesString,
'>; these scopes should be removed.',
'Clicking accept will reload this page.',
'Reloading the page will show this message unless the scopes are correct.',
].join(' ');
alert(badScopesMessage);
window.location.reload(true);
}
});
}