Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache skus credential till we get valid subs credential #17789

Merged
merged 3 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions components/brave_vpn/browser/brave_vpn_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ void BraveVpnService::CheckInitialState() {

ScheduleBackgroundRegionDataFetch();
#endif
} else if (HasValidSkusCredential(local_prefs_)) {
// If we have valid skus creds during the startup, we can try to get subs
// credential in advance.
ReloadPurchasedState();
} else {
// Try to reload purchased state if cached credential is not valid because
// it could be invalidated when not running.
Expand Down Expand Up @@ -643,9 +647,28 @@ void BraveVpnService::LoadPurchasedState(const std::string& domain) {
return;
}

if (HasValidSkusCredential(local_prefs_)) {
// We can reach here if we fail to get subscriber credential from guardian.
VLOG(2) << __func__
<< " Try to get subscriber credential with valid cached skus "
"credential.";

if (GetCurrentEnvironment() != requested_env) {
SetCurrentEnvironment(requested_env);
}

api_request_.GetSubscriberCredentialV12(
base::BindOnce(&BraveVpnService::OnGetSubscriberCredentialV12,
base::Unretained(this),
GetExpirationTimeForSkusCredential(local_prefs_)),
GetSkusCredential(local_prefs_),
GetBraveVPNPaymentsEnv(GetCurrentEnvironment()));
return;
}

VLOG(2) << __func__
<< ": Checking purchased state as we doesn't have valid subscriber "
"credentials";
<< ": Checking purchased state as we doesn't have valid skus or "
"subscriber credentials";
ClearSubscriberCredential(local_prefs_);

EnsureMojoConnected();
Expand Down Expand Up @@ -765,6 +788,8 @@ void BraveVpnService::OnPrepareCredentialsPresentation(
return;
}

SetSkusCredential(local_prefs_, credential, time);

if (GetCurrentEnvironment() != env) {
// Change environment because we have successfully authorized with new one.
SetCurrentEnvironment(env);
Expand All @@ -788,12 +813,14 @@ void BraveVpnService::OnGetSubscriberCredentialV12(
if (subscriber_credential == kTokenNoLongerValid) {
SetPurchasedState(GetCurrentEnvironment(), PurchasedState::INVALID);
} else {
SetPurchasedState(GetCurrentEnvironment(), PurchasedState::NOT_PURCHASED);
SetPurchasedState(GetCurrentEnvironment(), PurchasedState::FAILED);
}
#endif
return;
}

// Previously cached skus credential is cleared and fetched subscriber
// credential is cached.
SetSubscriberCredential(local_prefs_, subscriber_credential, expiration_time);

// Launch one-shot timer for refreshing subscriber_credential before it's
Expand Down
24 changes: 24 additions & 0 deletions components/brave_vpn/browser/brave_vpn_service_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,28 @@ void ClearSubscriberCredential(PrefService* local_prefs) {
local_prefs->ClearPref(prefs::kBraveVPNSubscriberCredential);
}

void SetSkusCredential(PrefService* local_prefs,
const std::string& skus_credential,
const base::Time& expiration_time) {
base::Value::Dict cred_dict;
cred_dict.Set(kSkusCredentialKey, skus_credential);
cred_dict.Set(kSubscriberCredentialExpirationKey,
base::TimeToValue(expiration_time));
local_prefs->SetDict(prefs::kBraveVPNSubscriberCredential,
std::move(cred_dict));
}

base::Time GetExpirationTimeForSkusCredential(PrefService* local_prefs) {
CHECK(HasValidSkusCredential(local_prefs));

const base::Value::Dict& sub_cred_dict =
local_prefs->GetDict(prefs::kBraveVPNSubscriberCredential);

const base::Value* expiration_time_value =
sub_cred_dict.Find(kSubscriberCredentialExpirationKey);

CHECK(expiration_time_value);
return *base::ValueToTime(expiration_time_value);
}

} // namespace brave_vpn
4 changes: 4 additions & 0 deletions components/brave_vpn/browser/brave_vpn_service_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ void SetSubscriberCredential(PrefService* local_prefs,
const std::string& subscriber_credential,
const base::Time& expiration_time);
void ClearSubscriberCredential(PrefService* local_prefs);
void SetSkusCredential(PrefService* local_prefs,
const std::string& skus_credential,
const base::Time& expiration_time);
base::Time GetExpirationTimeForSkusCredential(PrefService* local_prefs);

} // namespace brave_vpn

Expand Down
23 changes: 23 additions & 0 deletions components/brave_vpn/browser/brave_vpn_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,29 @@ TEST_F(BraveVPNServiceTest, RegionDataTest) {
EXPECT_EQ(regions()[0], device_region());
}

TEST_F(BraveVPNServiceTest, SkusCredentialCacheTest) {
std::string env = skus::GetDefaultEnvironment();
std::string domain = skus::GetDomain("vpn", env);

SetPurchasedState(env, PurchasedState::LOADING);
OnCredentialSummary(
domain, R"({ "active": true, "remaining_credential_count": 1 } )");
EXPECT_EQ(PurchasedState::LOADING, GetPurchasedStateSync());
OnPrepareCredentialsPresentation(
domain, "credential=abcdefghijk; Expires=Wed, 21 Oct 2050 07:28:00 GMT");
EXPECT_TRUE(HasValidSkusCredential(&local_pref_service_));
OnGetSubscriberCredentialV12("inalid", false);
EXPECT_EQ(PurchasedState::FAILED, GetPurchasedStateSync());

// Trying again with valid subscriber credential.
// Check cached skus credential is gone and we have valid subs credential
// instead.
SetPurchasedState(env, PurchasedState::LOADING);
OnGetSubscriberCredentialV12("valid-subs-credentials", true);
EXPECT_FALSE(HasValidSkusCredential(&local_pref_service_));
EXPECT_TRUE(HasValidSubscriberCredential(&local_pref_service_));
}

TEST_F(BraveVPNServiceTest, LoadPurchasedStateSessionExpiredTest) {
std::string env = skus::GetDefaultEnvironment();
std::string domain = skus::GetDomain("vpn", env);
Expand Down
1 change: 1 addition & 0 deletions components/brave_vpn/common/brave_vpn_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ constexpr char kCreateSubscriberCredentialV12[] =
constexpr int kP3AIntervalHours = 24;

constexpr char kSubscriberCredentialKey[] = "credential";
constexpr char kSkusCredentialKey[] = "skus_credential";
constexpr char kSubscriberCredentialExpirationKey[] = "expiration";

#if !BUILDFLAG(IS_ANDROID)
Expand Down
38 changes: 38 additions & 0 deletions components/brave_vpn/common/brave_vpn_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,42 @@ std::string GetSubscriberCredential(PrefService* local_prefs) {
return *cred;
}

bool HasValidSkusCredential(PrefService* local_prefs) {
const base::Value::Dict& sub_cred_dict =
local_prefs->GetDict(prefs::kBraveVPNSubscriberCredential);
if (sub_cred_dict.empty()) {
return false;
}

const std::string* skus_cred = sub_cred_dict.FindString(kSkusCredentialKey);
const base::Value* expiration_time_value =
sub_cred_dict.Find(kSubscriberCredentialExpirationKey);

if (!skus_cred || !expiration_time_value) {
return false;
}

if (skus_cred->empty()) {
return false;
}

auto expiration_time = base::ValueToTime(expiration_time_value);
if (!expiration_time || expiration_time < base::Time::Now()) {
return false;
}

return true;
}

std::string GetSkusCredential(PrefService* local_prefs) {
CHECK(HasValidSkusCredential(local_prefs))
<< "Don't call when there is no valid skus credential.";

const base::Value::Dict& sub_cred_dict =
local_prefs->GetDict(prefs::kBraveVPNSubscriberCredential);
const std::string* skus_cred = sub_cred_dict.FindString(kSkusCredentialKey);
DCHECK(skus_cred);
return *skus_cred;
}

} // namespace brave_vpn
2 changes: 2 additions & 0 deletions components/brave_vpn/common/brave_vpn_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
void RegisterAndroidProfilePrefs(PrefRegistrySimple* registry);
bool HasValidSubscriberCredential(PrefService* local_prefs);
std::string GetSubscriberCredential(PrefService* local_prefs);
bool HasValidSkusCredential(PrefService* local_prefs);
std::string GetSkusCredential(PrefService* local_prefs);

} // namespace brave_vpn

Expand Down