Browse Source

Merge branch 'message_v1' of https://dev.33.cn/yanglin/logSystem-advance into message_v1

lin 6 years ago
parent
commit
f12eba1c1a
2 changed files with 39 additions and 9 deletions
  1. 2 1
      backend/controllers/BaseController.php
  2. 37 8
      components/Secret.php

+ 2 - 1
backend/controllers/BaseController.php

@@ -28,7 +28,8 @@ class BaseController extends Controller
 	{
 		return ArrayHelper::merge(parent::behaviors(), [
 			'bearerAuth' => [
-				'class' => \yii\filters\auth\HttpBearerAuth::className(),
+				'class' => \common\filters\auth\BearerAuth::className(),
+				//'class' => \yii\filters\auth\HttpBearerAuth::className(),
 				'optional' => [
 					'token',
 					'signature',

+ 37 - 8
components/Secret.php

@@ -2,12 +2,13 @@
 namespace components;
 
 use Yii;
+use yii\base\Component;
 
 /**
  * Class Secret
  * @package components
  */
-class Secret
+class Secret extends Component
 {
 	public $showErrorInfo = true;
 
@@ -38,14 +39,14 @@ class Secret
 		if (!$cone)
 			static::throwError("client id avail.");
 
-		$sKey = isset($client_id->client_key) ? $client_id->client_key : '';
+		$sKey = isset($cone->client_key) ? $cone->client_key : '';
 
 		$headers = Yii::$app->request->getHeaders();
 		if (!$headers)
 			static::throwError("headers format avail.");
 
-		$strVerb = $headers->get('verb');
-		if (!$strVerb || !in_array((strtoupper($strVerb)), ['POST', 'GET']))
+		$strVerb =  strtoupper($headers->get('verb'));
+		if (!$strVerb || !in_array(($strVerb), ['POST', 'GET']))
 			static::throwError("headers verb avail.");
 
 		$strMd5 = $headers->get('content-md5');
@@ -56,8 +57,10 @@ class Secret
 		if ($strContentType !== $this->_contentType)
 			static::throwError("headers content type avail.");
 
-		$strDate = strtotime($headers->get('date'));
-		if ($strDate == false || $strDate-time() > 0 || time()-$strDate > $this->_timeout)
+		//$strDate = gmdate("D, d M Y H:i:s \G\M\T");
+		$strDate = $headers->get('date');
+		$strTime = strtotime($strDate);
+		if ($strDate == false || $strTime-time() > 0 || time()-$strTime > $this->_timeout)
 			static::throwError("headers date avail.");
 
 		$strSM = $headers->get('signature-method');
@@ -100,14 +103,40 @@ class Secret
 		if ($verb == 'GET' && $md5 != '')
 			return false;
 
-		ksort($post);
-		$this->_contentMd5 = md5(json_encode($post));
+		if (!is_array($post))
+			return true;
+
+		$s = static::kSortParams($post);
+		if (!$s)
+			return true;
+
+		$this->_contentMd5 = md5(json_encode($s));
 		if ( strtolower($md5) != strtolower($this->_contentMd5))
 			return false;
 
 		return true;
 	}
 
+	/**
+	 * kSortParams
+	 * @param $arr
+	 * @return mixed
+	 */
+	public static function kSortParams($arr)
+	{
+		foreach ($arr as $k => $v) {
+			if ($v === [] || $v === '' || $v === null) {
+				unset($arr[$k]);
+				continue;
+			}
+
+			if (is_array($v))
+				$arr[$k] = static::kSortParams($v);
+		}
+		ksort($arr);
+		return $arr;
+	}
+
 	/**
 	 * throwError
 	 * @param $msg