123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- namespace Mall\Framework\Core;
- use \DomainException;
- use \InvalidArgumentException;
- use \UnexpectedValueException;
- use \DateTime;
- class Jwt
- {
-
- public static $leeway = 0;
-
- public static $timestamp = null;
- public static $supported_algs = array(
- 'HS256' => array('hash_hmac', 'SHA256'),
- 'HS512' => array('hash_hmac', 'SHA512'),
- 'HS384' => array('hash_hmac', 'SHA384'),
- 'RS256' => array('openssl', 'SHA256'),
- 'RS384' => array('openssl', 'SHA384'),
- 'RS512' => array('openssl', 'SHA512'),
- );
-
- public static function decode($jwt, $key, $allowed_algs = array())
- {
- $timestamp = is_null(static::$timestamp) ? time() : static::$timestamp;
- if (empty($key)) {
- throw new InvalidArgumentException('Key may not be empty');
- }
- if (!is_array($allowed_algs)) {
- throw new InvalidArgumentException('Algorithm not allowed');
- }
- $tks = explode('.', $jwt);
- if (count($tks) != 3) {
- throw new UnexpectedValueException('Wrong number of segments');
- }
- list($headb64, $bodyb64, $cryptob64) = $tks;
- if (null === ($header = static::jsonDecode(static::urlsafeB64Decode($headb64)))) {
- throw new UnexpectedValueException('Invalid header encoding');
- }
- if (null === $payload = static::jsonDecode(static::urlsafeB64Decode($bodyb64))) {
- throw new UnexpectedValueException('Invalid claims encoding');
- }
- if (false === ($sig = static::urlsafeB64Decode($cryptob64))) {
- throw new UnexpectedValueException('Invalid signature encoding');
- }
- if (empty($header->alg)) {
- throw new UnexpectedValueException('Empty algorithm');
- }
- if (empty(static::$supported_algs[$header->alg])) {
- throw new UnexpectedValueException('Algorithm not supported');
- }
- if (!in_array($header->alg, $allowed_algs)) {
- throw new UnexpectedValueException('Algorithm not allowed');
- }
- if (is_array($key) || $key instanceof \ArrayAccess) {
- if (isset($header->kid)) {
- if (!isset($key[$header->kid])) {
- throw new UnexpectedValueException('"kid" invalid, unable to lookup correct key');
- }
- $key = $key[$header->kid];
- } else {
- throw new UnexpectedValueException('"kid" empty, unable to lookup correct key');
- }
- }
-
- if (!static::verify("$headb64.$bodyb64", $sig, $key, $header->alg)) {
- throw new UnexpectedValueException('Signature verification failed');
- }
-
-
- if (isset($payload->nbf) && $payload->nbf > ($timestamp + static::$leeway)) {
- throw new UnexpectedValueException(
- 'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
- );
- }
-
-
-
- if (isset($payload->iat) && $payload->iat > ($timestamp + static::$leeway)) {
- throw new UnexpectedValueException(
- 'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
- );
- }
-
- if (isset($payload->exp) && ($timestamp - static::$leeway) >= $payload->exp) {
- throw new UnexpectedValueException('Expired token');
- }
- return $payload;
- }
-
- public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $head = null)
- {
- $header = array('typ' => 'JWT', 'alg' => $alg);
- if ($keyId !== null) {
- $header['kid'] = $keyId;
- }
- if ( isset($head) && is_array($head) ) {
- $header = array_merge($head, $header);
- }
- $segments = array();
- $segments[] = static::urlsafeB64Encode(static::jsonEncode($header));
- $segments[] = static::urlsafeB64Encode(static::jsonEncode($payload));
- $signing_input = implode('.', $segments);
- $signature = static::sign($signing_input, $key, $alg);
- $segments[] = static::urlsafeB64Encode($signature);
- return implode('.', $segments);
- }
-
- public static function sign($msg, $key, $alg = 'HS256')
- {
- if (empty(static::$supported_algs[$alg])) {
- throw new DomainException('Algorithm not supported');
- }
- list($function, $algorithm) = static::$supported_algs[$alg];
- switch($function) {
- case 'hash_hmac':
- return hash_hmac($algorithm, $msg, $key, true);
- case 'openssl':
- $signature = '';
- $success = openssl_sign($msg, $signature, $key, $algorithm);
- if (!$success) {
- throw new DomainException("OpenSSL unable to sign data");
- } else {
- return $signature;
- }
- }
- }
-
- private static function verify($msg, $signature, $key, $alg)
- {
- if (empty(static::$supported_algs[$alg])) {
- throw new DomainException('Algorithm not supported');
- }
- list($function, $algorithm) = static::$supported_algs[$alg];
- switch($function) {
- case 'openssl':
- $success = openssl_verify($msg, $signature, $key, $algorithm);
- if ($success === 1) {
- return true;
- } elseif ($success === 0) {
- return false;
- }
-
- throw new DomainException(
- 'OpenSSL error: ' . openssl_error_string()
- );
- case 'hash_hmac':
- default:
- $hash = hash_hmac($algorithm, $msg, $key, true);
- if (function_exists('hash_equals')) {
- return hash_equals($signature, $hash);
- }
- $len = min(static::safeStrlen($signature), static::safeStrlen($hash));
- $status = 0;
- for ($i = 0; $i < $len; $i++) {
- $status |= (ord($signature[$i]) ^ ord($hash[$i]));
- }
- $status |= (static::safeStrlen($signature) ^ static::safeStrlen($hash));
- return ($status === 0);
- }
- }
-
- public static function jsonDecode($input)
- {
- if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
-
- $obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
- } else {
-
- $max_int_length = strlen((string) PHP_INT_MAX) - 1;
- $json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
- $obj = json_decode($json_without_bigints);
- }
- if (function_exists('json_last_error') && $errno = json_last_error()) {
- static::handleJsonError($errno);
- } elseif ($obj === null && $input !== 'null') {
- throw new DomainException('Null result with non-null input');
- }
- return $obj;
- }
-
- public static function jsonEncode($input)
- {
- $json = json_encode($input);
- if (function_exists('json_last_error') && $errno = json_last_error()) {
- static::handleJsonError($errno);
- } elseif ($json === 'null' && $input !== null) {
- throw new DomainException('Null result with non-null input');
- }
- return $json;
- }
-
- public static function urlsafeB64Decode($input)
- {
- $remainder = strlen($input) % 4;
- if ($remainder) {
- $padlen = 4 - $remainder;
- $input .= str_repeat('=', $padlen);
- }
- return base64_decode(strtr($input, '-_', '+/'));
- }
-
- public static function urlsafeB64Encode($input)
- {
- return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
- }
-
- private static function handleJsonError($errno)
- {
- $messages = array(
- JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
- JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
- JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
- JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
- JSON_ERROR_UTF8 => 'Malformed UTF-8 characters'
- );
- throw new DomainException(
- isset($messages[$errno])
- ? $messages[$errno]
- : 'Unknown JSON error: ' . $errno
- );
- }
-
- private static function safeStrlen($str)
- {
- if (function_exists('mb_strlen')) {
- return mb_strlen($str, '8bit');
- }
- return strlen($str);
- }
- }
|