Skip to content

Commit

Permalink
internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 693845926
  • Loading branch information
ftsui authored and copybara-github committed Nov 6, 2024
1 parent 291780f commit c41add7
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 158 deletions.
45 changes: 45 additions & 0 deletions internal/platform/implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@
# limitations under the License.
licenses(["notice"])

cc_library(
name = "auth_status",
hdrs = ["auth_status.h"],
visibility = [
"//internal/auth:__pkg__",
"//internal/platform/implementation:__subpackages__",
"//internal/test:__subpackages__",
"//sharing:__subpackages__",
],
)

cc_library(
name = "account_info",
hdrs = ["account_info.h"],
visibility = [
"//internal/account:__pkg__",
"//internal/auth:__pkg__",
"//internal/platform/implementation:__subpackages__",
"//internal/test:__subpackages__",
"//location/nearby/cpp/sharing/clients/cpp:__subpackages__",
"//sharing:__subpackages__",
],
)

cc_library(
name = "account_manager",
hdrs = ["account_manager.h"],
Expand All @@ -26,12 +50,33 @@ cc_library(
"//sharing:__subpackages__",
],
deps = [
":account_info",
":signin_attempt",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings:string_view",
],
)

cc_library(
name = "signin_attempt",
hdrs = ["signin_attempt.h"],
visibility = [
"//internal/account:__pkg__",
"//internal/auth:__pkg__",
"//internal/platform/implementation:__subpackages__",
"//internal/test:__subpackages__",
"//location/nearby/cpp/sharing/clients/cpp:__subpackages__",
"//sharing:__subpackages__",
],
deps = [
":account_info",
":auth_status",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/strings:string_view",
],
)

cc_library(
name = "types",
hdrs = [
Expand Down
35 changes: 35 additions & 0 deletions internal/platform/implementation/account_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_ACCOUNT_INFO_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_ACCOUNT_INFO_H_

#include <string>

namespace nearby {

// Describes a Nearby account. The account class will have more properties
// and methods in the future based on the new feature added.
struct AccountInfo {
std::string id; // The unique identify of the account.
std::string display_name;
std::string family_name;
std::string given_name;
std::string picture_url;
std::string email;
};

} // namespace nearby

#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_ACCOUNT_INFO_H_
35 changes: 10 additions & 25 deletions internal/platform/implementation/account_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,24 @@
#ifndef PLATFORM_API_ACCOUNT_MANAGER_H_
#define PLATFORM_API_ACCOUNT_MANAGER_H_

#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/account_info.h"
#include "internal/platform/implementation/signin_attempt.h"

namespace nearby {

// AccountManager manages the accounts are used to access Nearby backend.
// In current design, AccountManager only support one active account.
class AccountManager {
public:
// Describes a Nearby account. The account class will have more properties
// and methods in the future based on the new feature added.
struct Account {
std::string id; // The unique identify of the account.
std::string display_name;
std::string family_name;
std::string given_name;
std::string picture_url;
std::string email;
};
using Account = AccountInfo;

// Observes the activity of the account manager.
class Observer {
Expand All @@ -56,24 +50,11 @@ class AccountManager {
// Gets current active account. If no login user, return std::nullopt.
virtual std::optional<Account> GetCurrentAccount() = 0;

// Initializes the login process for a Google account from 1P client.
// |login_success_callback| is called when the login succeeded. Account
// information is passed to callback.
// |login_failure_callback| is called when the login fails.
virtual void Login(
absl::AnyInvocable<void(Account)> login_success_callback,
absl::AnyInvocable<void(absl::Status)> login_failure_callback) = 0;

// Initializes the login process for a Google account from an oauth client.
// |client_id| GCP client_id of the client
// |client_secret| GCP client_secret of the client
// |login_success_callback| is called when the login succeeded. Account
// information is passed to callback.
// |login_failure_callback| is called when the login fails.
virtual void Login(
absl::string_view client_id, absl::string_view client_secret,
absl::AnyInvocable<void(Account)> login_success_callback,
absl::AnyInvocable<void(absl::Status)> login_failure_callback) = 0;
virtual std::unique_ptr<SigninAttempt> Login(
absl::string_view client_id, absl::string_view client_secret) = 0;

// Logs out current active account. |logout_callback| is called when logout is
// completed.
Expand All @@ -97,6 +78,10 @@ class AccountManager {

virtual void AddObserver(Observer* observer) = 0;
virtual void RemoveObserver(Observer* observer) = 0;

virtual void SaveAccountPrefs(absl::string_view user_id,
absl::string_view client_id,
absl::string_view client_secret) = 0;
};

} // namespace nearby
Expand Down
84 changes: 84 additions & 0 deletions internal/platform/implementation/auth_status.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_AUTH_STATUS_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_AUTH_STATUS_H_

namespace nearby {

enum AuthStatus {
AUTH_STATUS_UNSPECIFIED = 0,
// Request completed successfully, the results should be in the correct order
// up to the given count.
SUCCESS = 1,

// Request encountered a generic error.
GENERIC_ERROR = 2,

// Request as specified is not supported.
UNSUPPORTED = 3,

// Request failed and should be retried soon.
TEMPORARILY_UNAVAILABLE = 4,

// Request failed due to an unavailable resource.
UNAVAILABLE_RESOURCE = 5,

// The request failed due to an invalid argument.
INVALID_ARGUMENT = 6,

// In case the status could not be retrieved.
UNKNOWN_STATUS = 7,

// Currently used as a way to signal an ETag mismatch.
PRECONDITION_FAILED = 8,

// The operation failed due to insufficient permissions.
PERMISSION_DENIED = 9,

// The resource exists, but the requested attribute of it does not.
MISSING_ATTRIBUTE = 10,

// The method was interrupted and the caller should exit the current unit of
// work immediately.
INTERRUPTED = 11,

// User signed in with an unexpected account.
SIGNED_IN_WITH_WRONG_ACCOUNT = 12,

// Used when data cannot be parsed properly.
PARSE_ERROR = 13,

// Used to report that the local HTTP server for receiving the authorization
// code cannot be created.
CANT_CREATE_AUTH_SERVER = 14,

// Used to report that the system browser for authenticating the user cannot
// be open.
CANT_OPEN_BROWSER_FOR_AUTH = 15,

// Used to report that the authorization code cannot be received.
CANT_RECEIVE_AUTH_CODE = 16,

// Used to report that the account is blocked (e.g. CAA).
ACCOUNT_BLOCKED = 17,

// Receiving the authorization code failed because it took longer than the
// timeout.
AUTH_CODE_TIMEOUT_EXCEEDED = 18,
};

} // namespace nearby

#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_AUTH_STATUS_H_
45 changes: 45 additions & 0 deletions internal/platform/implementation/signin_attempt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_SIGNIN_ATTEMPT_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_SIGNIN_ATTEMPT_H_

#include <string>

#include "absl/functional/any_invocable.h"
#include "internal/platform/implementation/account_info.h"
#include "internal/platform/implementation/auth_status.h"

namespace nearby {

class SigninAttempt {
public:
SigninAttempt() = default;
virtual ~SigninAttempt() = default;

// Starts a new sign-in attempt.
// `callback` is called with the status of the request and the user_id if the
// request is successful.
// Returns the auth url if the request is successful.
virtual std::string Start(
absl::AnyInvocable<void(AuthStatus, const AccountInfo&)> callback) = 0;

// Tears down the machinery set up to request auth tokens, including the HTTP
// server.
virtual void Close() = 0;
};

} // namespace nearby

#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_SIGNIN_ATTEMPT_H_
2 changes: 2 additions & 0 deletions internal/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//internal/platform/implementation:account_manager",
"//internal/platform/implementation:signin_attempt",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings:string_view",
Expand Down Expand Up @@ -60,6 +61,7 @@ cc_library(
"//internal/platform:comm",
"//internal/platform:types",
"//internal/platform/implementation:account_manager",
"//internal/platform/implementation:signin_attempt",
"//internal/platform/implementation:types",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
Expand Down
52 changes: 6 additions & 46 deletions internal/test/fake_account_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "internal/test/fake_account_manager.h"

#include <memory>
#include <optional>
#include <string>
#include <utility>
Expand All @@ -22,45 +23,17 @@
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/account_manager.h"
#include "internal/platform/implementation/signin_attempt.h"

namespace nearby {

std::optional<AccountManager::Account> FakeAccountManager::GetCurrentAccount() {
if (user_name_.has_value()) {
return account_;
}
return std::nullopt;
return account_;
}

void FakeAccountManager::Login(
absl::AnyInvocable<void(Account)> login_success_callback,
absl::AnyInvocable<void(absl::Status)> login_failure_callback) {
if (account_.has_value()) {
UpdateCurrentUser(account_->id);
NotifyLogin(account_->id);
// Invoke callback after all operations have been performed since test cases
// may rely on the callback for synchronization.
login_success_callback(*account_);
return;
}

login_failure_callback(absl::InternalError("No account."));
}

void FakeAccountManager::Login(
absl::string_view client_id, absl::string_view client_secret,
absl::AnyInvocable<void(Account)> login_success_callback,
absl::AnyInvocable<void(absl::Status)> login_failure_callback) {
if (account_.has_value()) {
UpdateCurrentUser(account_->id);
NotifyLogin(account_->id);
// Invoke callback after all operations have been performed since test cases
// may rely on the callback for synchronization.
login_success_callback(*account_);
return;
}

login_failure_callback(absl::InternalError("No account."));
std::unique_ptr<SigninAttempt> FakeAccountManager::Login(
absl::string_view client_id, absl::string_view client_secret) {
return nullptr;
}

void FakeAccountManager::Logout(
Expand Down Expand Up @@ -97,19 +70,6 @@ FakeAccountManager::GetOAuthClientCredential() {

void FakeAccountManager::SetAccount(std::optional<Account> account) {
account_ = account;
if (account_.has_value()) {
UpdateCurrentUser(account_->id);
} else {
ClearCurrentUser();
}
}

void FakeAccountManager::UpdateCurrentUser(absl::string_view current_user) {
user_name_ = current_user;
}

void FakeAccountManager::ClearCurrentUser() {
user_name_.reset();
}

void FakeAccountManager::AddObserver(Observer* observer) {
Expand Down
Loading

0 comments on commit c41add7

Please sign in to comment.