Skip to content

Commit 18a7b87

Browse files
Merge pull request #170 from timoschlueter/bugfix/fix-connection-error
Fix for connection errors starting November 2024
2 parents cfa81b1 + 420b340 commit 18a7b87

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {HttpCookieAgent} from "http-cookie-agent/http";
2323
import {Agent as HttpAgent} from "node:http";
2424
import {Agent as HttpsAgent} from "node:https";
2525
import * as crypto from "crypto";
26+
import {jwtDecode} from "jwt-decode";
27+
import {Jwt} from "./interfaces/librelink/jwt";
2628

2729
// Generate new Ciphers for stealth mode in order to bypass SSL fingerprinting used by Cloudflare.
2830
// The new Ciphers are then used in the HTTPS Agent for Axios.
@@ -95,7 +97,8 @@ const libreLinkUpHttpHeaders: LibreLinkUpHttpHeaders = {
9597
"User-Agent": USER_AGENT,
9698
"Content-Type": "application/json;charset=UTF-8",
9799
"version": LIBRE_LINK_UP_VERSION,
98-
"product": LIBRE_LINK_UP_PRODUCT
100+
"product": LIBRE_LINK_UP_PRODUCT,
101+
"account-id": "",
99102
}
100103

101104
if (config.singleShot)
@@ -141,7 +144,7 @@ async function main(): Promise<void>
141144
export async function login(): Promise<AuthTicket | null>
142145
{
143146
config = readConfig()
144-
147+
145148
try
146149
{
147150
const url = "https://" + LIBRE_LINK_UP_URL + "/llu/auth/login"
@@ -353,6 +356,19 @@ function getLluAuthHeaders(): LibreLinkUpHttpHeaders
353356
{
354357
const authenticatedHttpHeaders = libreLinkUpHttpHeaders;
355358
authenticatedHttpHeaders.Authorization = "Bearer " + getAuthenticationToken();
359+
360+
if (authTicket)
361+
{
362+
try
363+
{
364+
let jwt: Jwt = jwtDecode(authTicket.token);
365+
let hashedAccountId: string = crypto.createHash("sha256").update(jwt.id).digest("hex");
366+
authenticatedHttpHeaders["account-id"] = hashedAccountId;
367+
} catch (error)
368+
{
369+
logger.error("Error getting accountId: ", error);
370+
}
371+
}
356372
logger.debug("authenticatedHttpHeaders: " + JSON.stringify(authenticatedHttpHeaders));
357373
return authenticatedHttpHeaders;
358374
}

src/interfaces/librelink/jwt.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Interfaces for the JSON Web Token (JWT)
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
export interface Jwt
8+
{
9+
id: string;
10+
firstName: string;
11+
lastName: string;
12+
country: string;
13+
region: string;
14+
role: string;
15+
units: number;
16+
practices: Array<string>;
17+
c: number;
18+
s: string;
19+
exp: number;
20+
}

0 commit comments

Comments
 (0)