PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/services/Twilio/JWT.php

https://gitlab.com/sschand/WeatherShouldIGo
PHP | 164 lines | 98 code | 11 blank | 55 comment | 19 complexity | 33c2f0420bf97143f692550835750230 MD5 | raw file
  1. <?php
  2. /**
  3. * JSON Web Token implementation
  4. *
  5. * Minimum implementation used by Realtime auth, based on this spec:
  6. * http://self-issued.info/docs/draft-jones-json-web-token-01.html.
  7. *
  8. * @author Neuman Vong <neuman@twilio.com>
  9. */
  10. class JWT
  11. {
  12. /**
  13. * @param string $jwt The JWT
  14. * @param string|null $key The secret key
  15. * @param bool $verify Don't skip verification process
  16. *
  17. * @return object The JWT's payload as a PHP object
  18. */
  19. public static function decode($jwt, $key = null, $verify = true)
  20. {
  21. $tks = explode('.', $jwt);
  22. if (count($tks) != 3) {
  23. throw new UnexpectedValueException('Wrong number of segments');
  24. }
  25. list($headb64, $payloadb64, $cryptob64) = $tks;
  26. if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))
  27. ) {
  28. throw new UnexpectedValueException('Invalid segment encoding');
  29. }
  30. if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($payloadb64))
  31. ) {
  32. throw new UnexpectedValueException('Invalid segment encoding');
  33. }
  34. $sig = JWT::urlsafeB64Decode($cryptob64);
  35. if ($verify) {
  36. if (empty($header->alg)) {
  37. throw new DomainException('Empty algorithm');
  38. }
  39. if ($sig != JWT::sign("$headb64.$payloadb64", $key, $header->alg)) {
  40. throw new UnexpectedValueException('Signature verification failed');
  41. }
  42. }
  43. return $payload;
  44. }
  45. /**
  46. * @param object|array $payload PHP object or array
  47. * @param string $key The secret key
  48. * @param string $algo The signing algorithm
  49. * @param array $additionalHeaders Additional keys/values to add to the header
  50. *
  51. * @return string A JWT
  52. */
  53. public static function encode($payload, $key, $algo = 'HS256', $additionalHeaders = array())
  54. {
  55. $header = array('typ' => 'JWT', 'alg' => $algo);
  56. $header = $header + $additionalHeaders;
  57. $segments = array();
  58. $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
  59. $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
  60. $signing_input = implode('.', $segments);
  61. $signature = JWT::sign($signing_input, $key, $algo);
  62. $segments[] = JWT::urlsafeB64Encode($signature);
  63. return implode('.', $segments);
  64. }
  65. /**
  66. * @param string $msg The message to sign
  67. * @param string $key The secret key
  68. * @param string $method The signing algorithm
  69. *
  70. * @return string An encrypted message
  71. */
  72. public static function sign($msg, $key, $method = 'HS256')
  73. {
  74. $methods = array(
  75. 'HS256' => 'sha256',
  76. 'HS384' => 'sha384',
  77. 'HS512' => 'sha512',
  78. );
  79. if (empty($methods[$method])) {
  80. throw new DomainException('Algorithm not supported');
  81. }
  82. return hash_hmac($methods[$method], $msg, $key, true);
  83. }
  84. /**
  85. * @param string $input JSON string
  86. *
  87. * @return object Object representation of JSON string
  88. */
  89. public static function jsonDecode($input)
  90. {
  91. $obj = json_decode($input);
  92. if (function_exists('json_last_error') && $errno = json_last_error()) {
  93. JWT::handleJsonError($errno);
  94. }
  95. else if ($obj === null && $input !== 'null') {
  96. throw new DomainException('Null result with non-null input');
  97. }
  98. return $obj;
  99. }
  100. /**
  101. * @param object|array $input A PHP object or array
  102. *
  103. * @return string JSON representation of the PHP object or array
  104. */
  105. public static function jsonEncode($input)
  106. {
  107. $json = json_encode($input);
  108. if (function_exists('json_last_error') && $errno = json_last_error()) {
  109. JWT::handleJsonError($errno);
  110. }
  111. else if ($json === 'null' && $input !== null) {
  112. throw new DomainException('Null result with non-null input');
  113. }
  114. return $json;
  115. }
  116. /**
  117. * @param string $input A base64 encoded string
  118. *
  119. * @return string A decoded string
  120. */
  121. public static function urlsafeB64Decode($input)
  122. {
  123. $padlen = 4 - strlen($input) % 4;
  124. $input .= str_repeat('=', $padlen);
  125. return base64_decode(strtr($input, '-_', '+/'));
  126. }
  127. /**
  128. * @param string $input Anything really
  129. *
  130. * @return string The base64 encode of what you passed in
  131. */
  132. public static function urlsafeB64Encode($input)
  133. {
  134. return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
  135. }
  136. /**
  137. * @param int $errno An error number from json_last_error()
  138. *
  139. * @return void
  140. */
  141. private static function handleJsonError($errno)
  142. {
  143. $messages = array(
  144. JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
  145. JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
  146. JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON'
  147. );
  148. throw new DomainException(isset($messages[$errno])
  149. ? $messages[$errno]
  150. : 'Unknown JSON error: ' . $errno
  151. );
  152. }
  153. }