1
0

ClientToken.php 4.1 KB

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