ClientToken.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Twilio\Jwt;
  3. use Twilio\Jwt\Client\ScopeURI;
  4. /**
  5. * Twilio Capability Token generator
  6. */
  7. class ClientToken {
  8. public $accountSid;
  9. public $authToken;
  10. /** @var ScopeURI[] $scopes */
  11. public $scopes;
  12. /**
  13. * Create a new TwilioCapability with zero permissions. Next steps are to
  14. * grant access to resources by configuring this token through the
  15. * functions allowXXXX.
  16. *
  17. * @param string $accountSid the account sid to which this token is granted
  18. * access
  19. * @param string $authToken the secret key used to sign the token. Note,
  20. * this auth token is not visible to the user of the token.
  21. */
  22. public function __construct($accountSid, $authToken) {
  23. $this->accountSid = $accountSid;
  24. $this->authToken = $authToken;
  25. $this->scopes = array();
  26. $this->clientName = false;
  27. }
  28. /**
  29. * If the user of this token should be allowed to accept incoming
  30. * connections then configure the TwilioCapability through this method and
  31. * specify the client name.
  32. *
  33. * @param $clientName
  34. * @throws \InvalidArgumentException
  35. */
  36. public function allowClientIncoming($clientName) {
  37. // clientName must be a non-zero length alphanumeric string
  38. if (preg_match('/\W/', $clientName)) {
  39. throw new \InvalidArgumentException(
  40. 'Only alphanumeric characters allowed in client name.');
  41. }
  42. if (strlen($clientName) == 0) {
  43. throw new \InvalidArgumentException(
  44. 'Client name must not be a zero length string.');
  45. }
  46. $this->clientName = $clientName;
  47. $this->allow('client', 'incoming',
  48. array('clientName' => $clientName));
  49. }
  50. /**
  51. * Allow the user of this token to make outgoing connections.
  52. *
  53. * @param string $appSid the application to which this token grants access
  54. * @param mixed[] $appParams signed parameters that the user of this token
  55. * cannot overwrite.
  56. */
  57. public function allowClientOutgoing($appSid, array $appParams = array()) {
  58. $this->allow('client', 'outgoing', array(
  59. 'appSid' => $appSid,
  60. 'appParams' => http_build_query($appParams, '', '&')));
  61. }
  62. /**
  63. * Allow the user of this token to access their event stream.
  64. *
  65. * @param mixed[] $filters key/value filters to apply to the event stream
  66. */
  67. public function allowEventStream(array $filters = array()) {
  68. $this->allow('stream', 'subscribe', array(
  69. 'path' => '/2010-04-01/Events',
  70. 'params' => http_build_query($filters, '', '&'),
  71. ));
  72. }
  73. /**
  74. * Generates a new token based on the credentials and permissions that
  75. * previously has been granted to this token.
  76. *
  77. * @param int $ttl the expiration time of the token (in seconds). Default
  78. * value is 3600 (1hr)
  79. * @return ClientToken the newly generated token that is valid for $ttl
  80. * seconds
  81. */
  82. public function generateToken($ttl = 3600) {
  83. $payload = array(
  84. 'scope' => array(),
  85. 'iss' => $this->accountSid,
  86. 'exp' => time() + $ttl,
  87. );
  88. $scopeStrings = array();
  89. foreach ($this->scopes as $scope) {
  90. if ($scope->privilege == "outgoing" && $this->clientName)
  91. $scope->params["clientName"] = $this->clientName;
  92. $scopeStrings[] = $scope->toString();
  93. }
  94. $payload['scope'] = implode(' ', $scopeStrings);
  95. return JWT::encode($payload, $this->authToken, 'HS256');
  96. }
  97. protected function allow($service, $privilege, $params) {
  98. $this->scopes[] = new ScopeURI($service, $privilege, $params);
  99. }
  100. }