AccessToken.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Twilio\Jwt;
  3. use Twilio\Jwt\Grants\Grant;
  4. class AccessToken {
  5. private $signingKeySid;
  6. private $accountSid;
  7. private $secret;
  8. private $ttl;
  9. private $identity;
  10. private $nbf;
  11. /** @var Grant[] $grants */
  12. private $grants;
  13. /** @var string[] $customClaims */
  14. private $customClaims;
  15. public function __construct($accountSid, $signingKeySid, $secret, $ttl = 3600, $identity = null) {
  16. $this->signingKeySid = $signingKeySid;
  17. $this->accountSid = $accountSid;
  18. $this->secret = $secret;
  19. $this->ttl = $ttl;
  20. if (!is_null($identity)) {
  21. $this->identity = $identity;
  22. }
  23. $this->grants = array();
  24. $this->customClaims = array();
  25. }
  26. /**
  27. * Set the identity of this access token
  28. *
  29. * @param string $identity identity of the grant
  30. *
  31. * @return $this updated access token
  32. */
  33. public function setIdentity($identity) {
  34. $this->identity = $identity;
  35. return $this;
  36. }
  37. /**
  38. * Returns the identity of the grant
  39. *
  40. * @return string the identity
  41. */
  42. public function getIdentity() {
  43. return $this->identity;
  44. }
  45. /**
  46. * Set the nbf of this access token
  47. *
  48. * @param integer $nbf nbf in epoch seconds of the grant
  49. *
  50. * @return $this updated access token
  51. */
  52. public function setNbf($nbf) {
  53. $this->nbf = $nbf;
  54. return $this;
  55. }
  56. /**
  57. * Returns the nbf of the grant
  58. *
  59. * @return integer the nbf in epoch seconds
  60. */
  61. public function getNbf() {
  62. return $this->nbf;
  63. }
  64. /**
  65. * Add a grant to the access token
  66. *
  67. * @param Grant $grant to be added
  68. *
  69. * @return $this the updated access token
  70. */
  71. public function addGrant(Grant $grant) {
  72. $this->grants[] = $grant;
  73. return $this;
  74. }
  75. /**
  76. * Allows to set custom claims, which then will be encoded into JWT payload.
  77. *
  78. * @param string $name
  79. * @param string $value
  80. */
  81. public function addClaim($name, $value) {
  82. $this->customClaims[$name] = $value;
  83. }
  84. public function toJWT($algorithm = 'HS256') {
  85. $header = array(
  86. 'cty' => 'twilio-fpa;v=1',
  87. 'typ' => 'JWT'
  88. );
  89. $now = time();
  90. $grants = array();
  91. if ($this->identity) {
  92. $grants['identity'] = $this->identity;
  93. }
  94. foreach ($this->grants as $grant) {
  95. $payload = $grant->getPayload();
  96. if (empty($payload)) {
  97. $payload = json_decode('{}');
  98. }
  99. $grants[$grant->getGrantKey()] = $payload;
  100. }
  101. if (empty($grants)) {
  102. $grants = json_decode('{}');
  103. }
  104. $payload = array_merge($this->customClaims, array(
  105. 'jti' => $this->signingKeySid . '-' . $now,
  106. 'iss' => $this->signingKeySid,
  107. 'sub' => $this->accountSid,
  108. 'exp' => $now + $this->ttl,
  109. 'grants' => $grants
  110. ));
  111. if (!is_null($this->nbf)) {
  112. $payload['nbf'] = $this->nbf;
  113. }
  114. return JWT::encode($payload, $this->secret, $algorithm, $header);
  115. }
  116. public function __toString() {
  117. return $this->toJWT();
  118. }
  119. }