Skip to content

Commit 3e78c5e

Browse files
committed
api returns the token back to firefox addon 🚀
1 parent 2de88f3 commit 3e78c5e

14 files changed

Lines changed: 313 additions & 18 deletions

File tree

background/all.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

background/authorize.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* exported getAccessToken */
2+
3+
const REDIRECT_URL = browser.identity.getRedirectURL();
4+
const CLIENT_ID = "12428";
5+
const KEY = "f26RUH3uoCiokrEYNeDf9Q(("
6+
const SCOPES = ["read_inbox", "noexpire"];
7+
const AUTH_URL =
8+
`https://stackoverflow.com/oauth/dialog?
9+
client_id=${CLIENT_ID}&key=${KEY}&redirect_uri=${encodeURIComponent(REDIRECT_URL)}
10+
&scope=${encodeURIComponent(SCOPES.join(' '))}`;
11+
12+
const VALIDATION_BASE_URL="https://api.stackexchange.com/2.2/";
13+
14+
function extractAccessToken(redirectUri) {
15+
let m = redirectUri.match(/[#?](.*)/);
16+
if (!m || m.length < 1)
17+
return null;
18+
let params = new URLSearchParams(m[1].split("#")[0]);
19+
return params.get("access_token");
20+
}
21+
22+
/**
23+
Validate the token contained in redirectURL.
24+
This follows essentially the process here:
25+
https://developers.google.com/identity/protocols/OAuth2UserAgent#tokeninfo-validation
26+
- make a GET request to the validation URL, including the access token
27+
- if the response is 200, and contains an "aud" property, and that property
28+
matches the clientID, then the response is valid
29+
- otherwise it is not valid
30+
31+
Note that the Google page talks about an "audience" property, but in fact
32+
it seems to be "aud".
33+
*/
34+
function validate(redirectURL) {
35+
const accessToken = extractAccessToken(redirectURL);
36+
console.log(accessToken+ "access")
37+
if (!accessToken) {
38+
throw "Authorization failure";
39+
}
40+
const validationURL = `${VALIDATION_BASE_URL}access_tokens/${accessToken}`;
41+
const validationRequest = new Request(validationURL, {
42+
method: "GET"
43+
});
44+
45+
function checkResponse(response) {
46+
return new Promise((resolve, reject) => {
47+
if (response.status != 200) {
48+
reject("Token validation error");
49+
}
50+
response.json().then((json) => {
51+
if (json.items) {
52+
resolve(accessToken);
53+
} else {
54+
reject("Token validation error");
55+
}
56+
});
57+
});
58+
}
59+
60+
return fetch(validationRequest).then(checkResponse);
61+
}
62+
63+
/**
64+
Authenticate and authorize using browser.identity.launchWebAuthFlow().
65+
If successful, this resolves with a redirectURL string that contains
66+
an access token.
67+
*/
68+
function authorize() {
69+
console.log(AUTH_URL)
70+
return browser.identity.launchWebAuthFlow({
71+
interactive: true,
72+
url: AUTH_URL
73+
});
74+
}
75+
76+
function getAccessToken() {
77+
return authorize()
78+
}

background/main.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*global getAccessToken*/
2+
3+
// function notifyUser(user) {
4+
// browser.notifications.create({
5+
// "type": "basic",
6+
// "title": "Google info",
7+
// "message": `Hi ${user.name}`
8+
// });}
9+
10+
function logError(error) {
11+
console.error(`Error: ${error}`);
12+
}
13+
14+
/**
15+
When the button's clicked:
16+
- get an access token using the identity API
17+
- use it to get the user's info
18+
- show a notification containing some of it
19+
*/
20+
// browser.browser.addListener(function() {
21+
// console.log("clicked")
22+
// getAccessToken()
23+
// // .then(getUserInfo)
24+
// // .then(notifyUser)
25+
// .catch(logError);
26+
// });

background/stackinit.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('test '+browser.identity.getRedirectURL())
2+

background/userinfo.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// /**
2+
// Fetch the user's info, passing in the access token in the Authorization
3+
// HTTP request header.
4+
// */
5+
6+
// /* exported getUserInfo */
7+
8+
// function getUserInfo(accessToken) {
9+
// const requestURL = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
10+
// const requestHeaders = new Headers();
11+
// requestHeaders.append('Authorization', 'Bearer ' + accessToken);
12+
// const driveRequest = new Request(requestURL, {
13+
// method: "GET",
14+
// headers: requestHeaders
15+
// });
16+
17+
// return fetch(driveRequest).then((response) => {
18+
// if (response.status === 200) {
19+
// return response.json();
20+
// } else {
21+
// throw response.status;
22+
// }
23+
// });
24+
25+
// }

content_script/stackzilla.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(function() {
2+
/**
3+
* Check and set a global guard variable.
4+
* If this content script is injected into the same page again,
5+
* it will do nothing next time.
6+
*/
7+
if (window.hasRun) {
8+
return;
9+
}
10+
window.hasRun = true;
11+
12+
})

manifest.json

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
11
{
2-
32
"description": "Eazy access for your StackOverflow account",
43
"manifest_version": 2,
54
"name": "StackZilla",
65
"version": "1.0",
76
"homepage_url": "https://github.com/namila007/StackZilla",
87
"icons": {
9-
"64": "icons/addon.png"
8+
"64": "icons/addon.png",
9+
"32": "icons/toolbar.png"
1010
},
11-
"permissions": [
12-
"activeTab"
13-
],
14-
"browser_action": {
15-
"default_icon": "icons/toolbar.png",
16-
"default_title": "StackZilla",
17-
"default_popup": "popup/index.html"
11+
"permissions": [
12+
"notifications",
13+
"identity",
14+
"*://api.stackexchange.com/*",
15+
"*://*.stackoverflow.com/*"
16+
],
17+
"applications": {
18+
"gecko": {
19+
"id": "stackzilla1@mozilla.org"
20+
}
1821
},
19-
20-
"content_scripts": [
21-
{
22-
"matches": ["<all_urls>"],
23-
"js": ["stackzilla.js"]
24-
}
25-
]
22+
"browser_action": {
23+
"default_icon": "icons/toolbar.png",
24+
"default_title": "StackZilla",
25+
"default_popup": "popup/index.html"
26+
},
2627

28+
"content_scripts": [
29+
{
30+
"matches": ["<all_urls>"],
31+
"js": ["stackzilla.js"]
32+
}
33+
],
34+
"background": {
35+
"scripts": [
36+
"background/authorize.js",
37+
"background/main.js",
38+
"background/stackinit.js"
39+
]
40+
},
41+
"options_ui": {
42+
"page": "options/options.html"
43+
},
44+
"author": "Namila Bandara"
45+
46+
2747
}

options/options.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
</head>
7+
8+
<body>
9+
10+
<div>Redirect URL: <span id="redirect-url"></span></div>
11+
12+
<script src="options.js"></script>
13+
14+
</body>
15+
16+
</html>

options/options.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
Display the redirect URL.
3+
*/
4+
document.querySelector("#redirect-url").textContent = browser.identity.getRedirectURL()

popup/blank.html

Whitespace-only changes.

0 commit comments

Comments
 (0)