Skip to content

Commit

Permalink
Uplift of #15702 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases committed Nov 2, 2022
1 parent 1e8463b commit 5f6535a
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 167 deletions.
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:"
<< 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`).
GetAPIRequest()->GetProfileCredentials(
base::BindOnce(&BraveVPNOSConnectionAPI::OnGetProfileCredentials,
base::Unretained(this)),
subscriber_credential, hostname_->hostname);
GetSubscriberCredential(local_prefs_), hostname_->hostname);
}

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

0 comments on commit 5f6535a

Please sign in to comment.