Skip to content

Commit 9088690

Browse files
committed
fix libre-linkup connection
1 parent d9431df commit 9088690

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

App/Modules/SensorConnector/LibreConnection/LibreLinkUpConnection.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class LibreLinkUpConnection: SensorBluetoothConnection, IsSensor {
224224
DirectLog.info("LibreLinkUp processLogin, apiRegion: \(apiRegion)")
225225
DirectLog.info("LibreLinkUp processLogin, authExpires: \(authExpires)")
226226

227-
let connectResponse = try await connect(apiRegion: apiRegion, authToken: authToken)
227+
let connectResponse = try await connect(userID: userID, apiRegion: apiRegion, authToken: authToken)
228228

229229
guard let patientID = connectResponse.data?.first(where: { $0.patientID == userID })?.patientID ?? connectResponse.data?.first?.patientID else {
230230
disconnectConnection()
@@ -234,7 +234,7 @@ class LibreLinkUpConnection: SensorBluetoothConnection, IsSensor {
234234

235235
DirectLog.info("LibreLinkUp processLogin, patientID: \(patientID)")
236236

237-
lastLogin = LibreLinkLogin(patientID: patientID, apiRegion: apiRegion, authToken: authToken, authExpires: authExpires)
237+
lastLogin = LibreLinkLogin(userID: userID, patientID: patientID, apiRegion: apiRegion, authToken: authToken, authExpires: authExpires)
238238
}
239239
}
240240

@@ -376,7 +376,7 @@ class LibreLinkUpConnection: SensorBluetoothConnection, IsSensor {
376376
throw LibreLinkError.unknownError
377377
}
378378

379-
private func connect(apiRegion: String, authToken: String) async throws -> LibreLinkResponse<[LibreLinkResponseConnect]> {
379+
private func connect(userID: String, apiRegion: String, authToken: String) async throws -> LibreLinkResponse<[LibreLinkResponseConnect]> {
380380
DirectLog.info("LibreLinkUp connect")
381381

382382
guard let url = URL(string: "https://api-\(apiRegion).libreview.io/llu/connections") else {
@@ -387,6 +387,7 @@ class LibreLinkUpConnection: SensorBluetoothConnection, IsSensor {
387387

388388
var request = URLRequest(url: url)
389389
request.setValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization")
390+
request.setValue(userID.toSha256(), forHTTPHeaderField: "Account-Id")
390391

391392
for (header, value) in requestHeaders {
392393
request.setValue(value, forHTTPHeaderField: header)
@@ -423,6 +424,7 @@ class LibreLinkUpConnection: SensorBluetoothConnection, IsSensor {
423424

424425
var request = URLRequest(url: url)
425426
request.setValue("Bearer \(lastLogin.authToken)", forHTTPHeaderField: "Authorization")
427+
request.setValue(lastLogin.userID.toSha256(), forHTTPHeaderField: "Account-Id")
426428

427429
for (header, value) in requestHeaders {
428430
request.setValue(value, forHTTPHeaderField: header)
@@ -595,7 +597,8 @@ private struct LibreLinkResponseAuthentication: Codable {
595597
private struct LibreLinkLogin {
596598
// MARK: Lifecycle
597599

598-
init(patientID: String, apiRegion: String, authToken: String, authExpires: Double) {
600+
init(userID: String, patientID: String, apiRegion: String, authToken: String, authExpires: Double) {
601+
self.userID = userID
599602
self.patientID = patientID
600603
self.apiRegion = apiRegion.lowercased()
601604
self.authToken = authToken
@@ -604,6 +607,7 @@ private struct LibreLinkLogin {
604607

605608
// MARK: Internal
606609

610+
let userID: String
607611
let patientID: String
608612
let apiRegion: String
609613
let authToken: String

Library/Extensions/String.swift

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import CommonCrypto
77
import Foundation
8+
import CryptoKit
89

910
extension String {
1011
func capitalizingFirstLetter() -> String {
@@ -14,6 +15,13 @@ extension String {
1415
func asMinuteChange() -> String {
1516
return String(format: LocalizedString("%1$@/min."), self)
1617
}
18+
19+
func toSha256() -> String {
20+
let data = Data(self.utf8)
21+
22+
let hashedData = SHA256.hash(data: data)
23+
return hashedData.map { String(format: "%02hhx", $0) }.joined()
24+
}
1725

1826
func toSha1() -> String {
1927
let data = Data(self.utf8)

0 commit comments

Comments
 (0)