Browse Source

签名方式细节更新

lbk 6 years ago
parent
commit
3f0a9643c3
1 changed files with 32 additions and 9 deletions
  1. 32 9
      components/Secret.php

+ 32 - 9
components/Secret.php

@@ -39,14 +39,14 @@ class Secret extends Component
 		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');
@@ -57,8 +57,10 @@ class Secret extends Component
 		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');
@@ -101,16 +103,37 @@ class Secret extends Component
 		if ($verb == 'GET' && $md5 != '')
 			return false;
 
-		ksort($post);
-		$k = array_keys($post);
-		$s = $k ? implode('&', $k) : '';
-		$this->_contentMd5 = md5($s);
+		if (!is_array($post) || !$post)
+			return true;
+
+		$s = static::kSortParams($post);
+		$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