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