Skip to content

Commit

Permalink
Cache skus credential till we get valid subs credential
Browse files Browse the repository at this point in the history
fix brave/brave-browser#29345

If we fail to get subs credential from guardian, we should keep
skus credential till it's expired.
  • Loading branch information
simonhong committed Mar 28, 2023
1 parent 06278a9 commit f2dafbf
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
31 changes: 29 additions & 2 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 Down Expand Up @@ -794,6 +819,8 @@ void BraveVpnService::OnGetSubscriberCredentialV12(
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
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

0 comments on commit f2dafbf

Please sign in to comment.