Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a C++ sample plugin for HMAC token authorization. #114

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

walves-cit
Copy link
Contributor

This plugin is a HMAC token authorization showcase.

Technically, this is performed by ensuring that the request query string has a valid HMAC token.

This examples contains only a C++ version.

@walves-cit walves-cit marked this pull request as ready for review September 4, 2024 14:56
@walves-cit walves-cit requested a review from a team as a code owner September 4, 2024 14:56
Copy link

snippet-bot bot commented Sep 4, 2024

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment


FilterHeadersStatus onRequestHeaders(uint32_t headers,
bool end_of_stream) override {
boost::system::result<boost::urls::url> url =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to handle the case where the boost::system::result contains an error, i.e. has_value() returns false (or equivalently, has_error() returns true).

This applies to a few other existing plugins as well, e.g. plugins/samples/jwt_auth/plugin.cc and plugins/samples/ab_testing/plugin.cc, which both dereference a result without first checking if it is non-error. I'm guessing that would trigger an exception or undefined behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it... Added a URL parsing error check.

return FilterHeadersStatus::ContinueAndEndStream;
}

const auto token = (*it).value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand auto into actual type, since it's not obvious from the expression alone

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const auto token = (*it).value;
// Strip the token from the URL.
url->params().erase(it);
const auto path = url->buffer();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand auto into actual type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


private:
// Helper function to convert binary data to a hexadecimal string.
std::string toHexString(const unsigned char* data, size_t length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could absl::BytesToHexString() be used instead?

If this method needs to be retained, suggest passing data and length as a std::string_view

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@walves-cit walves-cit requested a review from mpwarres January 16, 2025 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants