mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
replace secrect_id to id inside presence credential manager
PiperOrigin-RevId: 630558485
This commit is contained in:
committed by
Copybara-Service
parent
dd7dbdf98d
commit
17df967296
@@ -126,10 +126,9 @@ void CredentialStorageImpl::UpdateLocalCredential(
|
||||
NEARBY_LOGS(WARNING) << credentials.status();
|
||||
credentials = std::vector<LocalCredential>();
|
||||
}
|
||||
auto it = std::find_if(credentials->begin(), credentials->end(),
|
||||
[&](const LocalCredential& a) {
|
||||
return a.secret_id() == credential.secret_id();
|
||||
});
|
||||
auto it = std::find_if(
|
||||
credentials->begin(), credentials->end(),
|
||||
[&](const LocalCredential& a) { return a.id() == credential.id(); });
|
||||
if (it == credentials->end()) {
|
||||
credentials->push_back(std::move(credential));
|
||||
} else {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
@@ -27,6 +28,7 @@
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "absl/types/variant.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
#ifdef NEARBY_CHROMIUM
|
||||
#include "crypto/aead.h"
|
||||
@@ -74,6 +76,8 @@ constexpr absl::string_view kEmptyAccountName = "";
|
||||
constexpr int kExpectedValidLocalCredtialSize = 6;
|
||||
// The expiration time in days for a credential.
|
||||
constexpr int kCredentialLifeCycleDays = 5;
|
||||
// The minimum size of bytes to generate credential id.
|
||||
constexpr int kExpectedByteSizeOfCredentialId = 8;
|
||||
|
||||
// Returns a random duration in [0, max_duration] range.
|
||||
absl::Duration RandomDuration(absl::Duration max_duration) {
|
||||
@@ -90,6 +94,29 @@ std::string CustomizeBytesSize(absl::string_view bytes, size_t len) {
|
||||
|
||||
} // namespace
|
||||
|
||||
// Returns a positive long value extracted from a byte array.
|
||||
int64_t GenerateIdFromByteArray(const ByteArray& input) {
|
||||
size_t inputLength = input.size();
|
||||
|
||||
ByteArray processed_bytes(kExpectedByteSizeOfCredentialId);
|
||||
// Only use first 8 bytes if the input is longer than 8 bytes.
|
||||
if (inputLength > kExpectedByteSizeOfCredentialId) {
|
||||
processed_bytes.CopyAt(0, input);
|
||||
} else {
|
||||
// Extend the input with zeros if it's shorter than 8 bytes
|
||||
processed_bytes.CopyAt(kExpectedByteSizeOfCredentialId - inputLength,
|
||||
input);
|
||||
}
|
||||
|
||||
int64_t id = 0;
|
||||
for (int i = 0; i < kExpectedByteSizeOfCredentialId; ++i) {
|
||||
id |= (static_cast<int64_t>(processed_bytes.data()[i]) << (8 * i));
|
||||
}
|
||||
if (id == std::numeric_limits<int64_t>::min())
|
||||
return std::numeric_limits<int64_t>::max();
|
||||
return id;
|
||||
}
|
||||
|
||||
void CredentialManagerImpl::GenerateCredentials(
|
||||
const DeviceIdentityMetaData& device_identity_metadata,
|
||||
absl::string_view manager_app_id,
|
||||
@@ -198,7 +225,7 @@ CredentialManagerImpl::CreateLocalCredential(
|
||||
// empty ByteArray.
|
||||
CHECK(!secret_id.Empty()) << "Crypto::Sha256 failed!";
|
||||
|
||||
private_credential.set_secret_id(std::string(secret_id.AsStringView()));
|
||||
private_credential.set_id(GenerateIdFromByteArray(secret_id));
|
||||
|
||||
std::string alias = Base64Utils::Encode(secret_id);
|
||||
auto prefixedAlias = kPairedKeyAliasPrefix + alias;
|
||||
@@ -241,7 +268,7 @@ SharedCredential CredentialManagerImpl::CreatePublicCredential(
|
||||
RandomDuration(absl::Hours(3));
|
||||
SharedCredential public_credential;
|
||||
public_credential.set_identity_type(private_credential.identity_type());
|
||||
public_credential.set_secret_id(private_credential.secret_id());
|
||||
public_credential.set_id(private_credential.id());
|
||||
public_credential.set_key_seed(private_credential.key_seed());
|
||||
public_credential.set_start_time_millis(absl::ToUnixMillis(start_time));
|
||||
public_credential.set_end_time_millis(absl::ToUnixMillis(end_time));
|
||||
|
||||
@@ -188,7 +188,7 @@ TEST_F(CredentialManagerImplTest, CreateOneCredentialSuccessfully) {
|
||||
LocalCredential private_credential = credentials.first;
|
||||
// Verify the private credential.
|
||||
EXPECT_EQ(private_credential.identity_type(), IDENTITY_TYPE_PRIVATE_GROUP);
|
||||
EXPECT_FALSE(private_credential.secret_id().empty());
|
||||
EXPECT_NE(private_credential.id(), 0);
|
||||
EXPECT_EQ(private_credential.start_time_millis(),
|
||||
absl::ToUnixMillis(kStartTime));
|
||||
EXPECT_EQ(private_credential.end_time_millis(), absl::ToUnixMillis(kEndTime));
|
||||
@@ -201,7 +201,8 @@ TEST_F(CredentialManagerImplTest, CreateOneCredentialSuccessfully) {
|
||||
SharedCredential public_credential = credentials.second;
|
||||
// Verify the public credential.
|
||||
EXPECT_EQ(public_credential.identity_type(), IDENTITY_TYPE_PRIVATE_GROUP);
|
||||
EXPECT_FALSE(public_credential.secret_id().empty());
|
||||
EXPECT_NE(public_credential.id(), 0);
|
||||
EXPECT_EQ(private_credential.id(), public_credential.id());
|
||||
EXPECT_EQ(private_credential.key_seed(), public_credential.key_seed());
|
||||
EXPECT_LE(public_credential.start_time_millis(),
|
||||
absl::ToUnixMillis(kStartTime));
|
||||
@@ -241,7 +242,7 @@ TEST_F(CredentialManagerImplTest, GenerateCredentialsSuccessfully) {
|
||||
for (int i = 0; i < kExpectedPresenceCredentialListSize; i++) {
|
||||
SharedCredential& public_credential = public_credentials->at(i);
|
||||
EXPECT_EQ(public_credential.identity_type(), IDENTITY_TYPE_PRIVATE_GROUP);
|
||||
EXPECT_FALSE(public_credential.secret_id().empty());
|
||||
EXPECT_NE(public_credential.id(), 0);
|
||||
absl::Time start_time_millis =
|
||||
absl::FromUnixMillis(public_credential.start_time_millis());
|
||||
absl::Time end_time_millis =
|
||||
|
||||
Reference in New Issue
Block a user