Skip to content

Commit

Permalink
Internal Code Change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 558715342
Change-Id: I921254b528a4ff706061cfee3330d8a69460fcbe
  • Loading branch information
ise-crypto authored and copybara-github committed Aug 21, 2023
1 parent c9b17da commit 6e0b325
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
25 changes: 16 additions & 9 deletions tink/jwt/internal/jwt_mac_wrapper_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ TEST_F(JwtMacWrapperTest, CannotWrapPrimitivesFromNonRawOrTinkKeys) {
KeysetHandle::GenerateNew(tink_key_template);
EXPECT_THAT(keyset_handle, IsOk());

EXPECT_FALSE((*keyset_handle)->GetPrimitive<JwtMac>().status().ok());
EXPECT_FALSE((*keyset_handle)
->GetPrimitive<crypto::tink::JwtMac>(ConfigGlobalRegistry())
.status()
.ok());
}

TEST_F(JwtMacWrapperTest, GenerateRawComputeVerifySuccess) {
Expand All @@ -121,7 +124,8 @@ TEST_F(JwtMacWrapperTest, GenerateRawComputeVerifySuccess) {
KeysetHandle::GenerateNew(key_template);
EXPECT_THAT(keyset_handle, IsOk());
util::StatusOr<std::unique_ptr<JwtMac>> jwt_mac =
(*keyset_handle)->GetPrimitive<JwtMac>();
(*keyset_handle)
->GetPrimitive<crypto::tink::JwtMac>(ConfigGlobalRegistry());
EXPECT_THAT(jwt_mac, IsOk());

util::StatusOr<RawJwt> raw_jwt =
Expand Down Expand Up @@ -159,7 +163,8 @@ TEST_F(JwtMacWrapperTest, GenerateRawComputeVerifySuccess) {
std::unique_ptr<KeysetHandle> tink_keyset_handle =
KeysetHandleWithTinkPrefix(**keyset_handle);
util::StatusOr<std::unique_ptr<JwtMac>> tink_jwt_mac =
tink_keyset_handle->GetPrimitive<JwtMac>();
tink_keyset_handle->GetPrimitive<crypto::tink::JwtMac>(
ConfigGlobalRegistry());
ASSERT_THAT(tink_jwt_mac, IsOk());

EXPECT_THAT(
Expand All @@ -173,7 +178,8 @@ TEST_F(JwtMacWrapperTest, GenerateTinkComputeVerifySuccess) {
KeysetHandle::GenerateNew(key_template);
EXPECT_THAT(keyset_handle, IsOk());
util::StatusOr<std::unique_ptr<JwtMac>> jwt_mac =
(*keyset_handle)->GetPrimitive<JwtMac>();
(*keyset_handle)
->GetPrimitive<crypto::tink::JwtMac>(ConfigGlobalRegistry());
EXPECT_THAT(jwt_mac, IsOk());

util::StatusOr<RawJwt> raw_jwt =
Expand Down Expand Up @@ -213,7 +219,8 @@ TEST_F(JwtMacWrapperTest, GenerateTinkComputeVerifySuccess) {
std::unique_ptr<KeysetHandle> keyset_handle_with_new_key_id =
KeysetHandleWithNewKeyId(**keyset_handle);
util::StatusOr<std::unique_ptr<JwtMac>> jwt_mac_with_new_key_id =
keyset_handle_with_new_key_id->GetPrimitive<JwtMac>();
keyset_handle_with_new_key_id->GetPrimitive<crypto::tink::JwtMac>(
ConfigGlobalRegistry());
ASSERT_THAT(jwt_mac_with_new_key_id, IsOk());

util::StatusOr<VerifiedJwt> verified_jwt_2 =
Expand All @@ -234,26 +241,26 @@ TEST_F(JwtMacWrapperTest, KeyRotation) {
ASSERT_THAT(manager.SetPrimary(*old_id), IsOk());
std::unique_ptr<KeysetHandle> handle1 = manager.GetKeysetHandle();
util::StatusOr<std::unique_ptr<JwtMac>> jwt_mac1 =
handle1->GetPrimitive<JwtMac>();
handle1->GetPrimitive<crypto::tink::JwtMac>(ConfigGlobalRegistry());
ASSERT_THAT(jwt_mac1, IsOk());

util::StatusOr<uint32_t> new_id = manager.Add(key_template);
ASSERT_THAT(new_id, IsOk());
std::unique_ptr<KeysetHandle> handle2 = manager.GetKeysetHandle();
util::StatusOr<std::unique_ptr<JwtMac>> jwt_mac2 =
handle2->GetPrimitive<JwtMac>();
handle2->GetPrimitive<crypto::tink::JwtMac>(ConfigGlobalRegistry());
ASSERT_THAT(jwt_mac2, IsOk());

ASSERT_THAT(manager.SetPrimary(*new_id), IsOk());
std::unique_ptr<KeysetHandle> handle3 = manager.GetKeysetHandle();
util::StatusOr<std::unique_ptr<JwtMac>> jwt_mac3 =
handle3->GetPrimitive<JwtMac>();
handle3->GetPrimitive<crypto::tink::JwtMac>(ConfigGlobalRegistry());
ASSERT_THAT(jwt_mac3, IsOk());

ASSERT_THAT(manager.Disable(*old_id), IsOk());
std::unique_ptr<KeysetHandle> handle4 = manager.GetKeysetHandle();
util::StatusOr<std::unique_ptr<JwtMac>> jwt_mac4 =
handle4->GetPrimitive<JwtMac>();
handle4->GetPrimitive<crypto::tink::JwtMac>(ConfigGlobalRegistry());
ASSERT_THAT(jwt_mac4, IsOk());

util::StatusOr<RawJwt> raw_jwt =
Expand Down
61 changes: 44 additions & 17 deletions tink/jwt/internal/jwt_public_key_wrappers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,19 @@ TEST_F(JwtPublicKeyWrappersTest, CannotWrapPrimitivesFromNonRawOrTinkKeys) {
KeysetHandle::GenerateNew(tink_key_template);
ASSERT_THAT(keyset_handle, IsOk());
EXPECT_FALSE(
(*keyset_handle)->GetPrimitive<JwtPublicKeySign>().status().ok());
(*keyset_handle)
->GetPrimitive<crypto::tink::JwtPublicKeySign>(ConfigGlobalRegistry())
.status()
.ok());

util::StatusOr<std::unique_ptr<KeysetHandle>> public_handle =
(*keyset_handle)->GetPublicKeysetHandle();
ASSERT_THAT(public_handle, IsOk());
EXPECT_FALSE(
(*public_handle)->GetPrimitive<JwtPublicKeyVerify>().status().ok());
EXPECT_FALSE((*public_handle)
->GetPrimitive<crypto::tink::JwtPublicKeyVerify>(
ConfigGlobalRegistry())
.status()
.ok());
}

TEST_F(JwtPublicKeyWrappersTest, GenerateRawSignVerifySuccess) {
Expand All @@ -137,14 +143,17 @@ TEST_F(JwtPublicKeyWrappersTest, GenerateRawSignVerifySuccess) {
KeysetHandle::GenerateNew(key_template);
ASSERT_THAT(handle, IsOk());
util::StatusOr<std::unique_ptr<JwtPublicKeySign>> jwt_sign =
(*handle)->GetPrimitive<JwtPublicKeySign>();
(*handle)->GetPrimitive<crypto::tink::JwtPublicKeySign>(
ConfigGlobalRegistry());
EXPECT_THAT(jwt_sign, IsOk());

util::StatusOr<std::unique_ptr<KeysetHandle>> public_handle =
(*handle)->GetPublicKeysetHandle();
EXPECT_THAT(public_handle, IsOk());
util::StatusOr<std::unique_ptr<JwtPublicKeyVerify>> jwt_verify =
(*public_handle)->GetPrimitive<JwtPublicKeyVerify>();
(*public_handle)
->GetPrimitive<crypto::tink::JwtPublicKeyVerify>(
ConfigGlobalRegistry());
EXPECT_THAT(jwt_verify, IsOk());

util::StatusOr<RawJwt> raw_jwt =
Expand Down Expand Up @@ -181,7 +190,8 @@ TEST_F(JwtPublicKeyWrappersTest, GenerateRawSignVerifySuccess) {
std::unique_ptr<KeysetHandle> tink_public_handle =
KeysetHandleWithTinkPrefix(**public_handle);
util::StatusOr<std::unique_ptr<JwtPublicKeyVerify>> tink_verify =
tink_public_handle->GetPrimitive<JwtPublicKeyVerify>();
tink_public_handle->GetPrimitive<crypto::tink::JwtPublicKeyVerify>(
ConfigGlobalRegistry());
ASSERT_THAT(tink_verify, IsOk());

EXPECT_THAT((*tink_verify)->VerifyAndDecode(*compact, *validator).status(),
Expand All @@ -194,14 +204,17 @@ TEST_F(JwtPublicKeyWrappersTest, GenerateTinkSignVerifySuccess) {
KeysetHandle::GenerateNew(key_template);
ASSERT_THAT(handle, IsOk());
util::StatusOr<std::unique_ptr<JwtPublicKeySign>> jwt_sign =
(*handle)->GetPrimitive<JwtPublicKeySign>();
(*handle)->GetPrimitive<crypto::tink::JwtPublicKeySign>(
ConfigGlobalRegistry());
EXPECT_THAT(jwt_sign, IsOk());

util::StatusOr<std::unique_ptr<KeysetHandle>> public_handle =
(*handle)->GetPublicKeysetHandle();
EXPECT_THAT(public_handle, IsOk());
util::StatusOr<std::unique_ptr<JwtPublicKeyVerify>> jwt_verify =
(*public_handle)->GetPrimitive<JwtPublicKeyVerify>();
(*public_handle)
->GetPrimitive<crypto::tink::JwtPublicKeyVerify>(
ConfigGlobalRegistry());
EXPECT_THAT(jwt_verify, IsOk());

util::StatusOr<RawJwt> raw_jwt =
Expand Down Expand Up @@ -240,7 +253,9 @@ TEST_F(JwtPublicKeyWrappersTest, GenerateTinkSignVerifySuccess) {
std::unique_ptr<KeysetHandle> public_handle_with_new_key_id =
KeysetHandleWithNewKeyId(**public_handle);
util::StatusOr<std::unique_ptr<JwtPublicKeyVerify>> verify_with_new_key_id =
public_handle_with_new_key_id->GetPrimitive<JwtPublicKeyVerify>();
public_handle_with_new_key_id
->GetPrimitive<crypto::tink::JwtPublicKeyVerify>(
ConfigGlobalRegistry());
ASSERT_THAT(verify_with_new_key_id, IsOk());

util::StatusOr<VerifiedJwt> verified_jwt_2 =
Expand All @@ -261,50 +276,62 @@ TEST_F(JwtPublicKeyWrappersTest, KeyRotation) {
ASSERT_THAT(manager.SetPrimary(*old_id), IsOk());
std::unique_ptr<KeysetHandle> handle1 = manager.GetKeysetHandle();
util::StatusOr<std::unique_ptr<JwtPublicKeySign>> jwt_sign1 =
handle1->GetPrimitive<JwtPublicKeySign>();
handle1->GetPrimitive<crypto::tink::JwtPublicKeySign>(
ConfigGlobalRegistry());
ASSERT_THAT(jwt_sign1, IsOk());
util::StatusOr<std::unique_ptr<KeysetHandle>> public_handle1 =
handle1->GetPublicKeysetHandle();
EXPECT_THAT(public_handle1, IsOk());
util::StatusOr<std::unique_ptr<JwtPublicKeyVerify>> jwt_verify1 =
(*public_handle1)->GetPrimitive<JwtPublicKeyVerify>();
(*public_handle1)
->GetPrimitive<crypto::tink::JwtPublicKeyVerify>(
ConfigGlobalRegistry());
EXPECT_THAT(jwt_verify1, IsOk());

util::StatusOr<uint32_t> new_id = manager.Add(key_template);
ASSERT_THAT(new_id, IsOk());
std::unique_ptr<KeysetHandle> handle2 = manager.GetKeysetHandle();
util::StatusOr<std::unique_ptr<JwtPublicKeySign>> jwt_sign2 =
handle2->GetPrimitive<JwtPublicKeySign>();
handle2->GetPrimitive<crypto::tink::JwtPublicKeySign>(
ConfigGlobalRegistry());
ASSERT_THAT(jwt_sign2, IsOk());
util::StatusOr<std::unique_ptr<KeysetHandle>> public_handle2 =
handle2->GetPublicKeysetHandle();
EXPECT_THAT(public_handle2, IsOk());
util::StatusOr<std::unique_ptr<JwtPublicKeyVerify>> jwt_verify2 =
(*public_handle2)->GetPrimitive<JwtPublicKeyVerify>();
(*public_handle2)
->GetPrimitive<crypto::tink::JwtPublicKeyVerify>(
ConfigGlobalRegistry());
EXPECT_THAT(jwt_verify2, IsOk());

ASSERT_THAT(manager.SetPrimary(*new_id), IsOk());
std::unique_ptr<KeysetHandle> handle3 = manager.GetKeysetHandle();
util::StatusOr<std::unique_ptr<JwtPublicKeySign>> jwt_sign3 =
handle3->GetPrimitive<JwtPublicKeySign>();
handle3->GetPrimitive<crypto::tink::JwtPublicKeySign>(
ConfigGlobalRegistry());
ASSERT_THAT(jwt_sign3, IsOk());
util::StatusOr<std::unique_ptr<KeysetHandle>> public_handle3 =
handle3->GetPublicKeysetHandle();
EXPECT_THAT(public_handle3, IsOk());
util::StatusOr<std::unique_ptr<JwtPublicKeyVerify>> jwt_verify3 =
(*public_handle3)->GetPrimitive<JwtPublicKeyVerify>();
(*public_handle3)
->GetPrimitive<crypto::tink::JwtPublicKeyVerify>(
ConfigGlobalRegistry());
EXPECT_THAT(jwt_verify3, IsOk());

ASSERT_THAT(manager.Disable(*old_id), IsOk());
std::unique_ptr<KeysetHandle> handle4 = manager.GetKeysetHandle();
util::StatusOr<std::unique_ptr<JwtPublicKeySign>> jwt_sign4 =
handle4->GetPrimitive<JwtPublicKeySign>();
handle4->GetPrimitive<crypto::tink::JwtPublicKeySign>(
ConfigGlobalRegistry());
ASSERT_THAT(jwt_sign4, IsOk());
util::StatusOr<std::unique_ptr<KeysetHandle>> public_handle4 =
handle4->GetPublicKeysetHandle();
EXPECT_THAT(public_handle4, IsOk());
util::StatusOr<std::unique_ptr<JwtPublicKeyVerify>> jwt_verify4 =
(*public_handle4)->GetPrimitive<JwtPublicKeyVerify>();
(*public_handle4)
->GetPrimitive<crypto::tink::JwtPublicKeyVerify>(
ConfigGlobalRegistry());
EXPECT_THAT(jwt_verify4, IsOk());

util::StatusOr<RawJwt> raw_jwt =
Expand Down
3 changes: 2 additions & 1 deletion tink/jwt/internal/raw_jwt_hmac_key_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ TEST(RawJwtHmacKeyManagerTest, GetPrimitiveFromNewKeysetHandle) {
ASSERT_TRUE(handle_result.ok()) << handle_result.status();
std::unique_ptr<KeysetHandle> handle = std::move(handle_result.value());

auto mac_result = handle->GetPrimitive<Mac>();
auto mac_result =
handle->GetPrimitive<crypto::tink::Mac>(ConfigGlobalRegistry());
ASSERT_TRUE(mac_result.ok()) << mac_result.status();
std::unique_ptr<Mac> mac = std::move(mac_result.value());
auto tag_or = mac->ComputeMac("some plaintext");
Expand Down

0 comments on commit 6e0b325

Please sign in to comment.