-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl(oauth2): port service account credential prerequisites (#8343)
* impl(oauth2): port service account credential prerequisites
- Loading branch information
Showing
10 changed files
with
478 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2021 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. | ||
|
||
#include "google/cloud/internal/make_jwt_assertion.h" | ||
#include "google/cloud/internal/openssl_util.h" | ||
|
||
namespace google { | ||
namespace cloud { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
namespace internal { | ||
|
||
StatusOr<std::string> MakeJWTAssertionNoThrow(std::string const& header, | ||
std::string const& payload, | ||
std::string const& pem_contents) { | ||
auto const body = | ||
UrlsafeBase64Encode(header) + '.' + UrlsafeBase64Encode(payload); | ||
auto pem_signature = internal::SignUsingSha256(body, pem_contents); | ||
if (!pem_signature) return std::move(pem_signature).status(); | ||
return body + '.' + UrlsafeBase64Encode(*pem_signature); | ||
} | ||
|
||
} // namespace internal | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace cloud | ||
} // namespace google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2021 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 GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_MAKE_JWT_ASSERTION_H | ||
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_MAKE_JWT_ASSERTION_H | ||
|
||
#include "google/cloud/status_or.h" | ||
#include "google/cloud/version.h" | ||
#include <string> | ||
|
||
namespace google { | ||
namespace cloud { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
namespace internal { | ||
StatusOr<std::string> MakeJWTAssertionNoThrow(std::string const& header, | ||
std::string const& payload, | ||
std::string const& pem_contents); | ||
|
||
} // namespace internal | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_MAKE_JWT_ASSERTION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2021 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. | ||
|
||
#include "google/cloud/internal/make_jwt_assertion.h" | ||
#include "google/cloud/testing_util/status_matchers.h" | ||
#include "absl/strings/str_split.h" | ||
#include <gmock/gmock.h> | ||
#include <nlohmann/json.hpp> | ||
|
||
namespace google { | ||
namespace cloud { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
namespace internal { | ||
namespace { | ||
|
||
using ::google::cloud::testing_util::IsOk; | ||
|
||
// This is an invalidated private key. It was created using the Google Cloud | ||
// Platform console, but then the key (and service account) were deleted. | ||
auto constexpr kWellFormattedKey = R"""(-----BEGIN PRIVATE KEY----- | ||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCltiF2oP3KJJ+S | ||
tTc1McylY+TuAi3AdohX7mmqIjd8a3eBYDHs7FlnUrFC4CRijCr0rUqYfg2pmk4a | ||
6TaKbQRAhWDJ7XD931g7EBvCtd8+JQBNWVKnP9ByJUaO0hWVniM50KTsWtyX3up/ | ||
fS0W2R8Cyx4yvasE8QHH8gnNGtr94iiORDC7De2BwHi/iU8FxMVJAIyDLNfyk0hN | ||
eheYKfIDBgJV2v6VaCOGWaZyEuD0FJ6wFeLybFBwibrLIBE5Y/StCrZoVZ5LocFP | ||
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R3zQs1NAInpjI1sOVrNPD7Ymwc | ||
W7mmNzZbxycCUL/yzg1VW4P1a6sBBYGbw1SMtWxun4ZbnuvMc2CTCh+43/1l+FHe | ||
Mmt46kq/2rH2jwx5feTbOE6P6PINVNRJh/9BDWECgYEAsCWcH9D3cI/QDeLG1ao7 | ||
rE2NcknP8N783edM07Z/zxWsIsXhBPY3gjHVz2LDl+QHgPWhGML62M0ja/6SsJW3 | ||
YvLLIc82V7eqcVJTZtaFkuht68qu/Jn1ezbzJMJ4YXDYo1+KFi+2CAGR06QILb+I | ||
lUtj+/nH3HDQjM4ltYfTPUg= | ||
-----END PRIVATE KEY----- | ||
)"""; | ||
|
||
TEST(MakeJWTAssertionNoThrow, Basic) { | ||
auto header = nlohmann::json{ | ||
{"alg", "HS256"}, {"typ", "JWT"}, {"kid", "test-key-name"}}; | ||
auto payload = nlohmann::json{ | ||
{"iss", "[email protected]"}, | ||
{"sub", "[email protected]"}, | ||
{"aud", "https//not-a-service.googleapis.com"}, | ||
{"iat", "1511900000"}, | ||
{"exp", "1511903600"}, | ||
}; | ||
auto const assertion = | ||
MakeJWTAssertionNoThrow(header.dump(), payload.dump(), kWellFormattedKey); | ||
ASSERT_THAT(assertion, IsOk()); | ||
|
||
std::vector<std::string> components = absl::StrSplit(*assertion, '.'); | ||
EXPECT_EQ(components.size(), 3); | ||
} | ||
|
||
TEST(MakeJWTAssertionNoThrow, InvalidKey) { | ||
auto header = nlohmann::json{ | ||
{"alg", "HS256"}, {"typ", "JWT"}, {"kid", "test-key-name"}}; | ||
auto payload = nlohmann::json{ | ||
{"iss", "[email protected]"}, | ||
{"sub", "[email protected]"}, | ||
{"aud", "https//not-a-service.googleapis.com"}, | ||
{"iat", "1511900000"}, | ||
{"exp", "1511903600"}, | ||
}; | ||
auto const assertion = | ||
MakeJWTAssertionNoThrow(header.dump(), payload.dump(), "invalid-key"); | ||
ASSERT_THAT(assertion, Not(IsOk())); | ||
} | ||
|
||
} // namespace | ||
} // namespace internal | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace cloud | ||
} // namespace google |
Oops, something went wrong.