Skip to content

Commit 6570c30

Browse files
committed
Use authorization header when getting access token
1 parent 15e785b commit 6570c30

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

src/background.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
import { setTwitchClientId } from './clientIdManager';
1+
import { setTwitchClientId, setTwitchOauthToken } from './storageManager';
22

33
chrome.webRequest.onSendHeaders.addListener(
44
async function (details) {
5+
let oauthTokenFound = false;
56
const headers = details.requestHeaders || [];
67
for (const header of headers) {
7-
if ('client-id' === header.name.toLocaleLowerCase()) {
8+
const headerName = header.name.toLocaleLowerCase()
9+
if ('client-id' === headerName) {
810
const clientId = header.value;
911
await setTwitchClientId(clientId);
1012
}
13+
else if ('authorization' === headerName) {
14+
const oauthToken = header.value;
15+
if (oauthToken) {
16+
await setTwitchOauthToken(oauthToken);
17+
oauthTokenFound = true;
18+
// console.log('Authorization found', header.value);
19+
}
20+
}
21+
}
22+
// If user logged out
23+
if (!oauthTokenFound) {
24+
// console.log('Authorization not found');
25+
await setTwitchOauthToken(null);
1126
}
1227
},
1328
{ urls: ['*://gql.twitch.tv/gql/*'] },

src/clientIdManager.ts

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

src/fetch.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import log from 'loglevel';
22

33
import { buildUsherUrl, parseAudioOnlyUrl } from './url';
44
import { AccessTokenGqlPayload } from './accessToken';
5-
import { getTwitchClientId } from './clientIdManager';
5+
import { getTwitchClientId, getTwitchOauthToken } from './storageManager';
66

77
const GQL_ENDPOINT_URL: string = 'https://gql.twitch.tv/gql';
88

@@ -52,16 +52,27 @@ export async function fetchJson(url: string) {
5252
return null;
5353
}
5454

55+
async function getGqlHeaders() {
56+
const twitchClientId = await getTwitchClientId();
57+
const twitchOauthToken = await getTwitchOauthToken();
58+
59+
const basicHeader = {
60+
'Client-ID': twitchClientId,
61+
'Content-Type': 'text/plain; charset=UTF-8',
62+
};
63+
64+
if (twitchOauthToken) {
65+
Object.assign(basicHeader, { 'Authorization': twitchOauthToken });
66+
}
67+
return basicHeader;
68+
}
69+
5570
// Run GQL query
5671
export async function fetchGql(payload: any) {
5772
try {
58-
const twitchClientId = await getTwitchClientId();
5973
const postResponse = await fetch(GQL_ENDPOINT_URL, {
6074
method: 'POST',
61-
headers: {
62-
'Client-ID': twitchClientId,
63-
'Content-Type': 'text/plain; charset=UTF-8',
64-
},
75+
headers: await getGqlHeaders(),
6576
body: JSON.stringify(payload),
6677
});
6778
const respJson = await postResponse.json();

src/storageManager.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Code shared by both service workers and content script
2+
3+
const TWITCH_CLIENT_ID_KEY = 'twitchClientId';
4+
const TWITCH_OAUTH_TOKEN_KEY = 'twitchOauthToken';
5+
6+
export async function getTwitchClientId(): Promise<string | null> {
7+
const result = await chrome.storage.local.get([TWITCH_CLIENT_ID_KEY]);
8+
return result[TWITCH_CLIENT_ID_KEY] || null;
9+
}
10+
11+
export async function setTwitchClientId(twitchClientId: string) {
12+
await chrome.storage.local.set({ [TWITCH_CLIENT_ID_KEY]: twitchClientId });
13+
}
14+
15+
export async function getTwitchOauthToken(): Promise<string | null> {
16+
const result = await chrome.storage.local.get([TWITCH_OAUTH_TOKEN_KEY]);
17+
return result[TWITCH_OAUTH_TOKEN_KEY] || null;
18+
}
19+
20+
export async function setTwitchOauthToken(twitchOauthToken: string | null) {
21+
await chrome.storage.local.set({ [TWITCH_OAUTH_TOKEN_KEY]: twitchOauthToken });
22+
}

0 commit comments

Comments
 (0)