Skip to content

Commit a3e7290

Browse files
authored
Merge pull request #123 from codesy/check-auth-change
add auth change check for codesy/codesy…
2 parents 7db2ee7 + 08d9755 commit a3e7290

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/csp.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ function makeCspAppender (domain='') {
22
const csp_names = ['CONTENT-SECURITY-POLICY','X-WEBKIT-CSP']
33
const name_finder = (name) => (csp_name) => csp_name === name.toUpperCase()
44
const if_csp = (name) => csp_names.find(name_finder(name)) ? true : false
5-
65
const codesy_types = 'connect-src frame-src child-src script-src style-src';
76
const is_codesy = (type) => codesy_types.indexOf(type) !== -1;
87
const add_codesy = (accum, word) =>`${accum} ${word} ${is_codesy(word) ? domain : '' }`;
@@ -21,20 +20,17 @@ function makeCspAppender (domain='') {
2120

2221
let codesyAppender = new makeCspAppender()
2322

24-
const githubFilter = {
25-
urls: ["*://*.github.com/*"],
26-
types: ["main_frame"]
27-
};
28-
29-
const headerOptions = ["responseHeaders", "blocking"]
3023

3124
function setCodesyAppender (domain) {
25+
const filter = {urls:["*://*.github.com/*"], types:["main_frame"]};
26+
const options = ["responseHeaders", "blocking"]
27+
3228
if (chrome.webRequest.onHeadersReceived.hasListener(codesyAppender)) {
3329
chrome.webRequest.onHeadersReceived.removeListener(codesyAppender);
3430
}
3531
codesyAppender = new makeCspAppender(domain);
3632
chrome.webRequest.onHeadersReceived.addListener(
37-
codesyAppender, githubFilter, headerOptions
33+
codesyAppender, filter, options
3834
);
3935
};
4036

src/on_install.js

+40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2+
// TODO: consolated csp.js and on_install.js into single background.js file
3+
4+
// functions for refreshing tabs after an extension install:
15
function find_these (query) {
26
return new Promise((resolve)=>{
37
if (Object.keys(query).includes('title')) {
@@ -55,3 +59,39 @@ function when_installed ({reason}) {
5559
}
5660

5761
chrome.runtime.onInstalled.addListener(when_installed);
62+
63+
// functions for handling auth changes:
64+
65+
const auth_header ='x-codesy-auth-changed'
66+
const has_auth_header = ({name})=>{ return name.startsWith(auth_header) }
67+
68+
function check_codesy_auth ({responseHeaders}) {
69+
auth_headers = responseHeaders.filter(has_auth_header)
70+
if ( auth_headers.length > 0 ) {
71+
find_these( {url: "*://*.github.com/*"} )
72+
.then(reload_them)
73+
}
74+
}
75+
76+
function listen_for_auth (domain) {
77+
const filter = {urls:[`${domain}/*`], types:["main_frame"]};
78+
const options = ["responseHeaders"]
79+
if (chrome.webRequest.onHeadersReceived.hasListener(check_codesy_auth)) {
80+
chrome.webRequest.onHeadersReceived.removeListener(check_codesy_auth);
81+
}
82+
chrome.webRequest.onHeadersReceived.addListener(
83+
check_codesy_auth, filter, options
84+
);
85+
};
86+
87+
chrome.storage.local.get(null,
88+
({domain = "https://www.codesy.io" })=>{
89+
if (domain) listen_for_auth(domain);
90+
}
91+
);
92+
93+
chrome.storage.onChanged.addListener(
94+
({domain: {newValue: domain} }, namespace)=>{
95+
if (domain) listen_for_auth(domain);
96+
}
97+
);

0 commit comments

Comments
 (0)