-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.js
More file actions
138 lines (123 loc) · 3.79 KB
/
authentication.js
File metadata and controls
138 lines (123 loc) · 3.79 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { updateRateLimitDisplay } from "./helpers.js";
export const config = {
github: {
token: "",
useAuth: false,
tokenExpirationMs: 3600000, // 1 hour in milliseconds
},
cache: {
enabled: true,
},
};
//AUTHENTICATION
// Configure authentication for fetch requests
export function getHeaders() {
const headers = {
Accept: "application/vnd.github+json", // Set the Accept header for GitHub API
};
if (config.github.useAuth && config.github.token) {
headers["Authorization"] = `token ${config.github.token}`; // Add token if available
}
return headers;
}
// Save token to Chrome storage
export function saveToken(token) {
const tokenData = {
value: token,
timestamp: Date.now(),
expires: Date.now() + config.github.tokenExpirationMs,
};
chrome.storage.local.set({ github_token: tokenData }, function () {
console.log("Token saved");
setGitHubToken(token);
updateAuthStatus(true);
// Update rate limit display after authentication changes
updateRateLimitDisplay();
});
}
// Load token from Chrome storage
export function loadToken() {
return new Promise((resolve) => {
chrome.storage.local.get(["github_token"], function (result) {
if (result.github_token && result.github_token.value) {
const tokenData = result.github_token;
const currentTime = Date.now();
// Check if token is expired
if (currentTime > tokenData.expires) {
console.log("Token expired, clearing...");
clearToken();
resolve(false);
return;
}
// Token is valid
setGitHubToken(result.github_token);
updateAuthStatus(true);
// Update rate limit display after authentication is loaded
updateRateLimitDisplay();
resolve(true);
} else {
updateAuthStatus(false);
// Update rate limit display even if not authenticated
updateRateLimitDisplay();
resolve(false);
}
});
});
}
// Function to set authentication token
export function setGitHubToken(token) {
if (token && token.trim() !== "") {
config.github.token = token;
config.github.useAuth = true;
console.log("GitHub authentication enabled");
return true;
} else {
config.github.useAuth = false;
console.log("GitHub authentication disabled");
return false;
}
}
// Clear token from storage
export function clearToken() {
chrome.storage.local.remove(["github_token"], function () {
console.log("Token cleared");
setGitHubToken("");
updateAuthStatus(false);
// Update rate limit display after authentication is removed
updateRateLimitDisplay();
});
}
// Update UI to show authentication status
export function updateAuthStatus(isAuthenticated) {
const statusCircle = document.querySelector(".status-circle");
const authText = document.getElementById("auth-text");
if (isAuthenticated) {
statusCircle.classList.remove("unauthenticated");
statusCircle.classList.add("authenticated");
authText.textContent = "Authenticated";
} else {
statusCircle.classList.remove("authenticated");
statusCircle.classList.add("unauthenticated");
authText.textContent = "Not authenticated";
}
}
export function checkTokenExpiration() {
chrome.storage.local.get(["github_token"], function (result) {
if (result.github_token && result.github_token.expires) {
const now = Date.now();
if (now > result.github_token.expires) {
console.log("Token expired during session check");
clearToken();
}
}
});
}
// Periodic token expiration check (every 15 minutes)
export function initializeAuthChecks() {
// Initial check
checkTokenExpiration();
// Set interval for regular checks
setInterval(() => {
checkTokenExpiration();
}, 15 * 60 * 1000); // Check every 15 minutes
}