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

Cached guardian's subscriber credentials #15702

Merged
merged 3 commits into from
Nov 2, 2022
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
18 changes: 10 additions & 8 deletions components/brave_vpn/brave_vpn_api_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,20 @@ void BraveVpnAPIRequest::GetSubscriberCredentialV12(
{{"Brave-Payments-Environment", environment}});
}

void BraveVpnAPIRequest::CreateSupportTicket(ResponseCallback callback,
const std::string& email,
const std::string& subject,
const std::string& body) {
void BraveVpnAPIRequest::CreateSupportTicket(
ResponseCallback callback,
const std::string& email,
const std::string& subject,
const std::string& body,
const std::string& subscriber_credential) {
auto internal_callback =
base::BindOnce(&BraveVpnAPIRequest::OnCreateSupportTicket,
weak_ptr_factory_.GetWeakPtr(), std::move(callback));

OAuthRequest(
GetURLWithPath(kVpnHost, kCreateSupportTicket), "POST",
CreateJSONRequestBody(GetValueWithTicketInfos(email, subject, body)),
std::move(internal_callback));
OAuthRequest(GetURLWithPath(kVpnHost, kCreateSupportTicket), "POST",
CreateJSONRequestBody(GetValueWithTicketInfos(
email, subject, body, subscriber_credential)),
std::move(internal_callback));
}

void BraveVpnAPIRequest::OAuthRequest(
Expand Down
3 changes: 2 additions & 1 deletion components/brave_vpn/brave_vpn_api_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class BraveVpnAPIRequest {
void CreateSupportTicket(ResponseCallback callback,
const std::string& email,
const std::string& subject,
const std::string& body);
const std::string& body,
const std::string& subscriber_credential);

private:
using URLRequestCallback = base::OnceCallback<void(APIRequestResult)>;
Expand Down
3 changes: 3 additions & 0 deletions components/brave_vpn/brave_vpn_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ constexpr char kCreateSubscriberCredentialV12[] =
"api/v1.2/subscriber-credential/create";
constexpr int kP3AIntervalHours = 24;

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

#if !BUILDFLAG(IS_ANDROID)
constexpr char kTokenNoLongerValid[] = "Token No Longer Valid";
#endif // !BUILDFLAG(IS_ANDROID)
Expand Down
52 changes: 4 additions & 48 deletions components/brave_vpn/brave_vpn_os_connection_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ void BraveVPNOSConnectionAPI::CheckConnection() {
CheckConnectionImpl(target_vpn_entry_name_);
}

void BraveVPNOSConnectionAPI::SetSkusCredential(const std::string& credential) {
skus_credential_ = credential;
}

void BraveVPNOSConnectionAPI::ResetConnectionInfo() {
VLOG(2) << __func__;
connection_info_.Reset();
Expand Down Expand Up @@ -333,7 +329,7 @@ std::string BraveVPNOSConnectionAPI::GetSelectedRegion() const {
}

std::string BraveVPNOSConnectionAPI::GetCurrentEnvironment() const {
return local_prefs_->GetString(prefs::kBraveVPNEEnvironment);
return local_prefs_->GetString(prefs::kBraveVPNEnvironment);
}

void BraveVPNOSConnectionAPI::FetchHostnamesForRegion(const std::string& name) {
Expand Down Expand Up @@ -402,59 +398,19 @@ void BraveVPNOSConnectionAPI::ParseAndCacheHostnames(
<< hostname_->display_name << ", " << hostname_->is_offline << ", "
<< hostname_->capacity_score;

if (skus_credential_.empty()) {
VLOG(2) << __func__ << " : skus_credential is empty";
UpdateAndNotifyConnectionStateChange(ConnectionState::CONNECT_FAILED);
return;
}

if (!GetAPIRequest()) {
CHECK_IS_TEST();
return;
}

// Get subscriber credentials and then get EAP credentials with it to create
// OS VPN entry.
VLOG(2) << __func__ << " : request subscriber credential:"
// Get profile credentials it to create OS VPN entry.
VLOG(2) << __func__ << " : request profile credential:"
simonhong marked this conversation as resolved.
Show resolved Hide resolved
<< GetBraveVPNPaymentsEnv(GetCurrentEnvironment());
GetAPIRequest()->GetSubscriberCredentialV12(
base::BindOnce(&BraveVPNOSConnectionAPI::OnGetSubscriberCredentialV12,
base::Unretained(this)),
skus_credential_, GetBraveVPNPaymentsEnv(GetCurrentEnvironment()));
}

void BraveVPNOSConnectionAPI::OnGetSubscriberCredentialV12(
const std::string& subscriber_credential,
bool success) {
if (cancel_connecting_) {
UpdateAndNotifyConnectionStateChange(ConnectionState::DISCONNECTED);
cancel_connecting_ = false;
return;
}

if (!success) {
VLOG(2) << __func__ << " : failed to get subscriber credential";
if (subscriber_credential == kTokenNoLongerValid) {
for (auto& obs : observers_)
obs.OnGetInvalidToken();
}
UpdateAndNotifyConnectionStateChange(ConnectionState::CONNECT_FAILED);
return;
}

VLOG(2) << __func__ << " : received subscriber credential";

if (!GetAPIRequest()) {
CHECK_IS_TEST();
return;
}

// TODO(bsclifton): consider storing `subscriber_credential` for
// support ticket use-case (see `CreateSupportTicket`).
simonhong marked this conversation as resolved.
Show resolved Hide resolved
GetAPIRequest()->GetProfileCredentials(
base::BindOnce(&BraveVPNOSConnectionAPI::OnGetProfileCredentials,
base::Unretained(this)),
subscriber_credential, hostname_->hostname);
GetSubscriberCredential(local_prefs_), hostname_->hostname);
bsclifton marked this conversation as resolved.
Show resolved Hide resolved
}

void BraveVPNOSConnectionAPI::OnGetProfileCredentials(
Expand Down
5 changes: 0 additions & 5 deletions components/brave_vpn/brave_vpn_os_connection_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class BraveVPNOSConnectionAPI : public base::PowerSuspendObserver,
public:
class Observer : public base::CheckedObserver {
public:
virtual void OnGetInvalidToken() = 0;
virtual void OnConnectionStateChanged(mojom::ConnectionState state) = 0;

protected:
Expand Down Expand Up @@ -74,7 +73,6 @@ class BraveVPNOSConnectionAPI : public base::PowerSuspendObserver,
void ToggleConnection();
void RemoveVPNConnection();
void CheckConnection();
void SetSkusCredential(const std::string& credential);
void ResetConnectionInfo();
std::string GetHostname() const;

Expand Down Expand Up @@ -121,8 +119,6 @@ class BraveVPNOSConnectionAPI : public base::PowerSuspendObserver,
bool success);
void ParseAndCacheHostnames(const std::string& region,
const base::Value::List& hostnames_value);
void OnGetSubscriberCredentialV12(const std::string& subscriber_credential,
bool success);
void OnGetProfileCredentials(const std::string& profile_credential,
bool success);

Expand All @@ -134,7 +130,6 @@ class BraveVPNOSConnectionAPI : public base::PowerSuspendObserver,
bool reconnect_on_resume_ = false;
bool prevent_creation_ = false;
std::string target_vpn_entry_name_;
std::string skus_credential_;
mojom::ConnectionState connection_state_ =
mojom::ConnectionState::DISCONNECTED;
BraveVPNConnectionInfo connection_info_;
Expand Down
Loading