| 48 | } |
| 49 | |
| 50 | std::unordered_set<std::string> LoadRootTokens(uint32_t accountId, uint32_t appId) { |
| 51 | auto localTokens = LocalMetadataStore::LoadRootTokens(accountId, appId); |
| 52 | if (!localTokens.empty() || !g_tokenProvider || !g_tokenProvider->IsAuthenticated()) |
| 53 | return localTokens; |
| 54 | |
| 55 | InflightSyncScope guard; |
| 56 | if (!guard) return localTokens; |
| 57 | |
| 58 | std::vector<uint8_t> cloudData; |
| 59 | bool usedLegacyRootTokens = false; |
| 60 | if (!DownloadCloudMetadataWithLegacyFallback(accountId, appId, |
| 61 | kRootTokenFilename, kLegacyRootTokenFilename, |
| 62 | cloudData, &usedLegacyRootTokens)) |
| 63 | return localTokens; |
| 64 | |
| 65 | std::unordered_set<std::string> cloudTokens; |
| 66 | std::istringstream iss(std::string(cloudData.begin(), cloudData.end())); |
| 67 | std::string line; |
| 68 | while (std::getline(iss, line)) { |
| 69 | while (!line.empty() && (line.back() == '\r' || line.back() == '\n')) |
| 70 | line.pop_back(); |
| 71 | if (!line.empty()) cloudTokens.insert(line); |
| 72 | } |
| 73 | |
| 74 | if (cloudTokens.empty()) return localTokens; |
| 75 | |
| 76 | if (!LocalMetadataStore::SaveRootTokens(accountId, appId, cloudTokens)) { |
| 77 | LOG("[TokenStore] LoadRootTokens app %u: failed to persist cloud token fallback locally", appId); |
| 78 | return localTokens; |
| 79 | } |
| 80 | |
| 81 | if (usedLegacyRootTokens) { |
| 82 | std::string cleaned; |
| 83 | for (const auto& token : cloudTokens) cleaned += token + "\n"; |
| 84 | if (UploadCloudMetadataText(accountId, appId, kRootTokenFilename, cleaned)) |
| 85 | RemoveCloudMetadataIfPresent(accountId, appId, kLegacyRootTokenFilename); |
| 86 | else |
| 87 | LOG("[TokenStore] LoadRootTokens app %u: failed to migrate legacy cloud root tokens", appId); |
| 88 | } else { |
| 89 | RemoveCloudMetadataIfPresent(accountId, appId, kLegacyRootTokenFilename); |
| 90 | } |
| 91 | |
| 92 | return cloudTokens; |
| 93 | } |
| 94 | |
| 95 | bool SaveFileTokens(uint32_t accountId, uint32_t appId, |
| 96 | const std::unordered_map<std::string, std::string>& fileTokens) { |
no test coverage detected