Skip to content
Snippets Groups Projects
Commit 72a843be authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

hide metager key when anonymous tokens are enabled

parent 59504a7d
No related branches found
No related tags found
No related merge requests found
...@@ -92,7 +92,9 @@ export class TokenManager { ...@@ -92,7 +92,9 @@ export class TokenManager {
async handleRequestHeaders(details) { async handleRequestHeaders(details) {
await this.init(); await this.init();
return details.requestHeaders; let requestHeaders = details.requestHeaders;
requestHeaders = this.hideMetaGerKey(requestHeaders);
return requestHeaders;
} }
async handleResponseHeaders(details) { async handleResponseHeaders(details) {
...@@ -100,6 +102,50 @@ export class TokenManager { ...@@ -100,6 +102,50 @@ export class TokenManager {
return details.responseHeaders; return details.responseHeaders;
} }
/**
* Removes the MetaGer key from the request
* and replaces it with the headers for anonymous tokens
* https://gitlab.metager.de/open-source/MetaGer/-/wikis/Anonymous%20Token%20System
*/
hideMetaGerKey(requestHeaders) {
if (this._anonymous_tokens_enabled == false) return requestHeaders;
let new_headers = [];
let cookie_header = null;
for (let header of requestHeaders) {
if (header.name.match(/cookie/i)) {
cookie_header = header;
} else {
new_headers.push(header);
}
}
let new_cookies = [];
if (cookie_header) {
let cookies = cookie_header.value.split(";");
for (let cookie of cookies) {
let cookie_array = cookie.split("=");
if (cookie_array.length != 2) continue;
let name = cookie_array[0].trim();
let value = decodeURIComponent(cookie_array[1].trim());
if (name != "key") {
new_cookies.push(name + "=" + value);
}
}
}
// Set the tokenauthorization cookie instead of the actual key
let token_authorization = "empty";
if (this._key_charge > 30) {
token_authorization = "full";
} else if (this._key_charge > 0) {
token_authorization = "low";
}
new_cookies.push(`tokenauthorization=${token_authorization}`);
new_headers.push({ name: "Cookie", value: new_cookies.join("; ") });
return new_headers;
}
/** /**
* Refills locally stored tokens * Refills locally stored tokens
*/ */
...@@ -108,7 +154,12 @@ export class TokenManager { ...@@ -108,7 +154,12 @@ export class TokenManager {
for (let cost of this._recent_costs) { for (let cost of this._recent_costs) {
required_token_count += cost; required_token_count += cost;
} }
if (this._tokens.length >= this._max_cost || this._key == null) return; if (
this._tokens.length >= this._max_cost ||
this._key == null ||
this._anonymous_tokens_enabled == false
)
return;
let public_key = await fetch(this._api_base + "/token/pubkey").then( let public_key = await fetch(this._api_base + "/token/pubkey").then(
(response) => response.json() (response) => response.json()
); );
......
...@@ -7,6 +7,7 @@ module.exports = { ...@@ -7,6 +7,7 @@ module.exports = {
assert: false, assert: false,
crypto: require.resolve("crypto-browserify"), crypto: require.resolve("crypto-browserify"),
constants: false, constants: false,
vm: false,
stream: require.resolve("stream-browserify"), stream: require.resolve("stream-browserify"),
buffer: require.resolve("buffer/"), buffer: require.resolve("buffer/"),
}, },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment