credentials.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // MinIO C++ Library for Amazon S3 Compatible Cloud Storage
  2. // Copyright 2022 MinIO, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #ifndef _MINIO_CREDS_CREDENTIALS_H
  16. #define _MINIO_CREDS_CREDENTIALS_H
  17. #include "utils.h"
  18. #include <pugixml.hpp>
  19. namespace minio {
  20. namespace creds {
  21. static bool expired(utils::Time expiration) {
  22. if (!expiration) return false;
  23. utils::Time now = utils::Time::Now();
  24. now.Add(10);
  25. return expiration < now;
  26. }
  27. /**
  28. * Credentials contains access key and secret key with optional session token
  29. * and expiration.
  30. */
  31. struct Credentials {
  32. error::Error err;
  33. std::string access_key;
  34. std::string secret_key;
  35. std::string session_token;
  36. utils::Time expiration;
  37. bool IsExpired() { return expired(expiration); }
  38. operator bool() const {
  39. return !err && !access_key.empty() && expired(expiration);
  40. }
  41. static Credentials ParseXML(std::string_view data, std::string root) {
  42. pugi::xml_document xdoc;
  43. pugi::xml_parse_result result = xdoc.load_string(data.data());
  44. if (!result) return Credentials{error::Error("unable to parse XML")};
  45. auto credentials = xdoc.select_node((root + "/Credentials").c_str());
  46. auto text = credentials.node().select_node("AccessKeyId/text()");
  47. std::string access_key = text.node().value();
  48. text = credentials.node().select_node("SecretAccessKey/text()");
  49. std::string secret_key = text.node().value();
  50. text = credentials.node().select_node("SessionToken/text()");
  51. std::string session_token = text.node().value();
  52. text = credentials.node().select_node("Expiration/text()");
  53. auto expiration = utils::Time::FromISO8601UTC(text.node().value());
  54. return Credentials{error::SUCCESS, access_key, secret_key, session_token,
  55. expiration};
  56. }
  57. }; // class Credentials
  58. } // namespace creds
  59. } // namespace minio
  60. #endif // #ifndef _MINIO_CREDS_CREDENTIALS_H