Moodle authentication plugin for Macaroons

Caveat.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Macaroons;
  3. class Caveat
  4. {
  5. private $caveat_id;
  6. private $verification_id;
  7. private $caveat_location;
  8. public function __construct($caveatId, $verificationId = NULL, $caveatLocation = NULL)
  9. {
  10. $this->caveat_id = $caveatId;
  11. $this->verification_id = $verificationId;
  12. $this->caveat_location = $caveatLocation;
  13. }
  14. public function getCaveatId()
  15. {
  16. return $this->caveat_id;
  17. }
  18. public function getCaveatLocation()
  19. {
  20. return $this->caveat_location;
  21. }
  22. public function getVerificationId()
  23. {
  24. return $this->verification_id;
  25. }
  26. public function setCaveatLocation($caveatLocation)
  27. {
  28. $this->caveat_location = $caveatLocation;
  29. }
  30. public function setVerificationId($verificationId)
  31. {
  32. $this->verification_id = $verificationId;
  33. }
  34. public function isFirstParty()
  35. {
  36. return $this->verification_id === NULL;
  37. }
  38. public function isThirdParty()
  39. {
  40. return !$this->isFirstParty();
  41. }
  42. public function toArray()
  43. {
  44. $caveatKeys = array('cid' => $this->getCaveatId());
  45. if ($this->isThirdParty())
  46. {
  47. $caveatKeys = array_merge(
  48. $caveatKeys,
  49. array(
  50. 'vid' => $this->getVerificationId(),
  51. 'cl' => $this->getCaveatLocation()
  52. )
  53. );
  54. }
  55. return $caveatKeys;
  56. }
  57. public function __toString()
  58. {
  59. $caveatAsArray = $this->toArray();
  60. if ($this->isThirdParty())
  61. $caveatAsArray['vid'] = Utils::hexlify($caveatAsArray['vid']);
  62. return join("\n", array_map(function($key, $value) {
  63. return "$key $value";
  64. }, array_keys($caveatAsArray), $caveatAsArray));
  65. }
  66. }