Skip to content

Commit

Permalink
Merge branch 'main' into cecilia/BITS-2951/add-tests-for-playlog-4
Browse files Browse the repository at this point in the history
  • Loading branch information
ceciliasaraiva committed Aug 5, 2024
2 parents af27a74 + dd447e6 commit a222884
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.27] - 2024-07-30
### New
- Use [swift-log](https://github.com/apple/swift-log) library for logging different events (Auth)

### Fixed
- Fixed crash for the new cache implementation in AVQueuePlayerWrapper (Player)

## [0.3.26] - 2024-07-23
### Fixed
- Made access to the database queue thread-safe (EventProducer)
Expand Down
30 changes: 22 additions & 8 deletions Sources/Auth/Storage/DefaultTokensStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ final class DefaultTokensStore: TokensStore {

private var latestTokens: Tokens?

private let credentialsQueue = DispatchQueue(label: "com.tidal.auth.credentialsQueue")

init(credentialsKey: String, credentialsAccessGroup: String?) {
storageKey = "\(credentialsKey)_\(PREFS_FILE_NAME)"
self.credentialsKey = credentialsKey
Expand All @@ -20,8 +22,12 @@ final class DefaultTokensStore: TokensStore {
}

func getLatestTokens() throws -> Tokens? {
latestTokens = try latestTokens ?? loadTokens()
return latestTokens
try credentialsQueue.sync {
if latestTokens == nil {
latestTokens = try loadTokens()
}
return latestTokens
}
}

private func loadTokens() throws -> Tokens? {
Expand All @@ -38,7 +44,7 @@ final class DefaultTokensStore: TokensStore {
// try to decode legacy tokens, convert them to tokens and save tokens
print("Failed to decode tokens. Attempting to decode legacy tokens")
if let convertedLegacyTokens = try? decoder.decode(LegacyTokens.self, from: data).toTokens() {
try saveTokens(tokens: convertedLegacyTokens)
try saveTokensUnsafe(tokens: convertedLegacyTokens)
return convertedLegacyTokens
} else {
throw error
Expand All @@ -48,14 +54,22 @@ final class DefaultTokensStore: TokensStore {
}

func saveTokens(tokens: Tokens) throws {
try credentialsQueue.sync {
try saveTokensUnsafe(tokens: tokens)
}
}

func eraseTokens() throws {
try credentialsQueue.sync {
latestTokens = nil
try encryptedStorage.removeAll()
}
}

private func saveTokensUnsafe(tokens: Tokens) throws {
let data = try JSONEncoder().encode(tokens)
encryptedStorage[credentialsKey] = String(data: data, encoding: .utf8)
latestTokens = tokens
// TODO: emit credentails update: `bus.emit(CredentialsUpdatedMessage)`
}

func eraseTokens() throws {
latestTokens = nil
try encryptedStorage.removeAll()
}
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.26
0.3.27

0 comments on commit a222884

Please sign in to comment.