Skip to content

Commit 5d4d43a

Browse files
committed
working token
1 parent 3e78c5e commit 5d4d43a

7 files changed

Lines changed: 145 additions & 97 deletions

File tree

background/authorize.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* exported getAccessToken */
22

3-
const REDIRECT_URL = browser.identity.getRedirectURL();
4-
const CLIENT_ID = "12428";
3+
// const REDIRECT_URL = browser.identity.getRedirectURL();
4+
// const CLIENT_ID = "12428";
55
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(' '))}`;
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(' '))}`;
1111

1212
const VALIDATION_BASE_URL="https://api.stackexchange.com/2.2/";
1313

@@ -33,11 +33,13 @@ it seems to be "aud".
3333
*/
3434
function validate(redirectURL) {
3535
const accessToken = extractAccessToken(redirectURL);
36-
console.log(accessToken+ "access")
36+
console.log(accessToken + " access")
3737
if (!accessToken) {
3838
throw "Authorization failure";
3939
}
40-
const validationURL = `${VALIDATION_BASE_URL}access_tokens/${accessToken}`;
40+
const validationURL = `${VALIDATION_BASE_URL}access-tokens/${accessToken}?key=${KEY}`;
41+
42+
console.log(validationURL)
4143
const validationRequest = new Request(validationURL, {
4244
method: "GET"
4345
});
@@ -60,19 +62,11 @@ function validate(redirectURL) {
6062
return fetch(validationRequest).then(checkResponse);
6163
}
6264

65+
66+
6367
/**
6468
Authenticate and authorize using browser.identity.launchWebAuthFlow().
6569
If successful, this resolves with a redirectURL string that contains
6670
an access token.
6771
*/
68-
function authorize() {
69-
console.log(AUTH_URL)
70-
return browser.identity.launchWebAuthFlow({
71-
interactive: true,
72-
url: AUTH_URL
73-
});
74-
}
7572

76-
function getAccessToken() {
77-
return authorize()
78-
}

background/main.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
/*global getAccessToken*/
22

3+
var token
34
// function notifyUser(user) {
45
// browser.notifications.create({
56
// "type": "basic",
67
// "title": "Google info",
78
// "message": `Hi ${user.name}`
89
// });}
910

11+
function sendnow (tabs) {
12+
browser.tabs.sendMessage(tabs[0].id, {
13+
command: "token",
14+
token: token
15+
});
16+
}
17+
18+
19+
async function sendToken (data) {
20+
console.log(data.url)
21+
token = await validate(data.url)
22+
23+
console.log("FINAL "+token)
24+
25+
browser.tabs.query({active: true, currentWindow: true},
26+
function (tabs) {
27+
browser.tabs.sendMessage(tabs[0].id, {
28+
command: "token",
29+
token: token
30+
});
31+
})
32+
33+
.catch(logError);
34+
35+
}
36+
1037
function logError(error) {
1138
console.error(`Error: ${error}`);
1239
}
@@ -17,10 +44,23 @@ When the button's clicked:
1744
- use it to get the user's info
1845
- show a notification containing some of it
1946
*/
20-
// browser.browser.addListener(function() {
21-
// console.log("clicked")
22-
// getAccessToken()
23-
// // .then(getUserInfo)
24-
// // .then(notifyUser)
25-
// .catch(logError);
26-
// });
47+
browser.runtime.onMessage.addListener(async function (message,sender,res){
48+
console.log(message.url)
49+
token = await validate(message.url)
50+
51+
console.log("FINAL "+token)
52+
53+
browser.tabs.query({active: true, currentWindow: true},
54+
function (tabs) {
55+
browser.tabs.sendMessage(tabs[0].id, {
56+
command: "token",
57+
token: token
58+
});
59+
})
60+
61+
.catch(logError);
62+
63+
});
64+
65+
66+

background/stackinit.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

manifest.json

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
11
{
2-
"description": "Eazy access for your StackOverflow account",
3-
"manifest_version": 2,
4-
"name": "StackZilla",
5-
"version": "1.0",
6-
"homepage_url": "https://github.com/namila007/StackZilla",
7-
"icons": {
8-
"64": "icons/addon.png",
9-
"32": "icons/toolbar.png"
2+
"description": "Eazy access for your StackOverflow account",
3+
"manifest_version": 2,
4+
"name": "StackZilla",
5+
"version": "1.0",
6+
"homepage_url": "https://github.com/namila007/StackZilla",
7+
"icons": {
8+
"64": "icons/addon.png",
9+
"32": "icons/toolbar.png"
10+
},
11+
"permissions": [
12+
"notifications",
13+
"identity",
14+
"tabs",
15+
"activeTab",
16+
"*://api.stackexchange.com/*",
17+
"*://*.stackoverflow.com/*"
18+
],
19+
"applications": {
20+
"gecko": {
21+
"id": "stackzilla1@mozilla.org"
22+
}
23+
},
24+
"browser_action": {
25+
"default_icon": "icons/toolbar.png",
26+
"default_title": "StackZilla",
27+
"default_popup": "popup/index.html"
1028
},
11-
"permissions": [
12-
"notifications",
13-
"identity",
14-
"*://api.stackexchange.com/*",
15-
"*://*.stackoverflow.com/*"
16-
],
17-
"applications": {
18-
"gecko": {
19-
"id": "stackzilla1@mozilla.org"
20-
}
21-
},
22-
"browser_action": {
23-
"default_icon": "icons/toolbar.png",
24-
"default_title": "StackZilla",
25-
"default_popup": "popup/index.html"
26-
},
27-
28-
"content_scripts": [
29-
{
30-
"matches": ["<all_urls>"],
31-
"js": ["stackzilla.js"]
32-
}
33-
],
3429
"background": {
3530
"scripts": [
3631
"background/authorize.js",
37-
"background/main.js",
38-
"background/stackinit.js"
32+
"background/userinfo.js",
33+
"background/main.js"
3934
]
4035
},
41-
"options_ui": {
42-
"page": "options/options.html"
43-
},
44-
"author": "Namila Bandara"
45-
46-
36+
37+
"content_scripts": [
38+
{
39+
"matches": ["*://*/*"],
40+
"js": ["popup/stackpopup.js"]
41+
}
42+
],
43+
"options_ui": {
44+
"page": "options/options.html"
45+
},
46+
"author": "Namila Bandara"
47+
48+
4749
}

popup/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828

2929
<script type='text/javascript' src="scripts/jquery-3.3.1.min.js"></script>
30-
<script type='text/javascript' src='../background/all.js'></script>
3130
<script src="stackpopup.js"></script>
3231
</body>
3332

popup/stackpopup.js

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,27 @@ const AUTH_URL =
1010
//&scope=${encodeURIComponent(SCOPES.join(' '))}`;
1111

1212

13+
browser.runtime.onMessage.addListener((message) => {
14+
console.log(message + " mess")
15+
if (message.command === "token") {
16+
tokenSet(message.token);
17+
}
18+
});
1319

1420
document.getElementById('tokenbtn').addEventListener("click", async function getToken () {
15-
16-
// SE.init({
17-
// // Parameters obtained by registering an app, these are specific to the SE
18-
// // documentation site
19-
// clientId: 12428,
20-
// key: 'f26RUH3uoCiokrEYNeDf9Q((',
21-
// // Used for cross domain communication, it will be validated
22-
// channelUrl: 'resource://stackzilla-nb007@mozilla.org-at-jetpack/stackzilla/popup/blank.html',
23-
// // Called when all initialization is finished
24-
// complete: function(data) {
25-
// console.log(data)
26-
// }
27-
// });
28-
// console.log(SE)
29-
30-
// SE.authenticate({
31-
// success: function(data) {
32-
// console.log(data)
33-
// document.getElementById('token').innerHTML() = data
34-
// },
35-
// error: function(data) {
36-
// alert('An error occurred:\n' + data.errorName + '\n' + data.errorMessage);
37-
// },
38-
// networkUsers: true
39-
// })
40-
console.log(AUTH_URL)
41-
const auth = await browser.identity.launchWebAuthFlow({
21+
22+
var tokenurl = await browser.identity.launchWebAuthFlow({
4223
interactive: true,
4324
url: AUTH_URL
44-
});
45-
console.log(auth + "auth")
25+
})
26+
browser.runtime.sendMessage({url: tokenurl});
4627
})
28+
29+
function tokenSet (req) {
30+
document.getElementById('token').innerHTML = req
31+
32+
}
33+
34+
35+
36+

popup/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+
}

0 commit comments

Comments
 (0)