verifier.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * verifier.cpp
  3. *
  4. * Created on: Mar 19, 2017
  5. * Author: hp
  6. */
  7. #include <iostream>
  8. #include "macaroon.h"
  9. namespace c_macaroons {
  10. #include "macaroons.h"
  11. }
  12. namespace macaroons {
  13. verifier::verifier() {
  14. v = c_macaroons::macaroon_verifier_create();
  15. }
  16. void verifier::satisfy_exact(const std::string predicate) {
  17. c_macaroons::macaroon_returncode err = c_macaroons::MACAROON_SUCCESS;
  18. c_macaroons::macaroon_verifier_satisfy_exact(v,
  19. (const unsigned char*) predicate.data(), predicate.size(), &err);
  20. }
  21. bool verifier::verify(const macaroon& m, const std::string key) const {
  22. c_macaroons::macaroon_returncode err = c_macaroons::MACAROON_SUCCESS;
  23. int rc = c_macaroons::macaroon_verify(v, m.m,
  24. (const unsigned char*) key.data(), key.size(), NULL, 0, &err);
  25. if (rc == 0)
  26. return true;
  27. return false;
  28. }
  29. bool verifier::verify(const macaroon& m, const std::string key,
  30. macaroon_list third_party_caveats) const {
  31. c_macaroons::macaroon_returncode err = c_macaroons::MACAROON_SUCCESS;
  32. c_macaroons::macaroon** MS =
  33. new c_macaroons::macaroon*[third_party_caveats.size()];
  34. for (size_t i = 0; i < third_party_caveats.size(); ++i) {
  35. MS[i] = third_party_caveats[i].m;
  36. }
  37. int rc = c_macaroons::macaroon_verify(v, m.m,
  38. (const unsigned char*) key.data(), key.size(), MS,
  39. third_party_caveats.size(), &err);
  40. delete[] MS;
  41. if (rc == 0)
  42. return true;
  43. return false;
  44. }
  45. verifier::~verifier() {
  46. c_macaroons::macaroon_verifier_destroy(v);
  47. }
  48. } // namespace macaroons