public function invoke($api, $param) { if (empty($api) || strpos($api, '.') === false) { return 'api not exist'; } $api_arr = explode('.', $api); $class = $api_arr[0]; $method = $api_arr[1]; require_once APP_ROOT . "/data/services/{$class}.php"; $ins = new $class(); require_once FRAMEWORK . '/rest/Signature.class.php'; $param['t'] = time(); $sig_key = get_app_config()->getGlobalConfig(AppConfig::SIGNATURE_KEY); $param['k'] = Signature::sign($param, $sig_key, 'k'); if (method_exists($ins, $method)) { return $ins->{$method}($param); } else { return "method {$method} not exist in class {$class}."; } }
/** * Sign the request, setting the `oauth_signature_method` and `oauth_signature`. * * @param Signature signature * @param Consumer consumer * @param Token token * @return $this * @uses Signature::sign */ public function sign(Signature $signature, Consumer $consumer, Token $token = NULL) { // Create a new signature class from the method $this->param('oauth_signature_method', $signature->name); // Sign the request using the consumer and token $this->param('oauth_signature', $signature->sign($this, $consumer, $token)); return $this; }
/** * Sign request using proxy object * * @return string */ public function sign($options = array()) { $params = $this->parameters(); $params['oauth_signature'] = Signature::sign($this, $options); $this->setParameters($params); $this->signed = true; return $this->signature(); }
/** * 验证参数的签名是否正确 * @param $params * @return void */ protected function checkSignature($params) { // 签名的key必须不为空 if (empty($params['k'])) { $this->throwException('signature error', GameStatusCode::DATA_ERROR); } // action_id,need_check_sleep是在happyranch.php中添加的参数,不需要作验证的 if (isset($params['action_id'])) { unset($params['action_id']); } if (isset($params['need_check_sleep'])) { unset($params['need_check_sleep']); } $sig_key = get_app_config()->getGlobalConfig(AppConfig::SIGNATURE_KEY); require_once FRAMEWORK . '/rest/Signature.class.php'; $sig = Signature::sign($params, $sig_key, 'k'); if ($sig != $params['k']) { $this->throwException("signature error", GameStatusCode::DATA_ERROR); } }
/** * 验证rest服务必须的参数 */ protected function validate() { $params =& $_REQUEST; if (!isset($params['sig'], $params['timestamp'], $params['method'], $params['sig_appkey'])) { $this->errorMessage(ELEX_API_CODE_PARAMETER_ERROR, 'request parameters error.'); } $request_time = $_SERVER['REQUEST_TIME']; if (empty($request_time)) { $request_time = time(); } // 验证时间戳 $timestamp = getGPC('timestamp', 'int'); if (abs($timestamp - $request_time) > 30) { $this->errorMessage(ELEX_API_CODE_PARAMETER_ERROR, 'timestamp error'); } // 验证签名 require_once FRAMEWORK . '/rest/Signature.class.php'; $sign = Signature::sign($params, API_SIG_KEY); $sig_request = $params['sig']; if ($sign != $sig_request) { $this->errorMessage(ELEX_API_CODE_SIGNATURE_ERROR, 'signature error.'); } }
/** * testGetRequestSignature * * @return void */ public function testGetRequestSignature() { $options = array('consumer' => $this->consumer, 'token' => $this->ConsumerToken, 'uri' => 'http://photos.example.net/photos', 'privateCert' => $this->pem, 'publicCert' => '', 'privateCertPass' => ''); $signature = Signature::sign($this->request, $options); $this->assertEqual('jvTp/wX1TYtByB1m+Pbyo0lnCOLIsyGCH7wke8AUs3BpnwZJtAuEJkvQL2/9n4s5wUmUl4aCI4BwpraNx4RtEXMe5qg5T1LVTGliMRpKasKsW//e+RinhejgCuzoH26dyF8iY2ZZ/5D1ilgeijhV/vBka5twt399mXwaYdCwFYE=', $signature); }
/** * Get request signature * * @param array $extraOptions * @return string */ public function signature($extraOptions = array()) { $options = array('uri' => $this->options['request_uri'], 'consumer' => $this->options['consumer'], 'token' => $this->options['token']); $this->__updateExtraOption($extraOptions); $options = array_merge($options, $extraOptions); return Signature::sign($this->request, $options); }
/** * @param $config Easypay_config * @param $data string * @param $http_request_method string * @param $path string * @param $req_id string * @return mixed */ private static function _process_request($config, $data, $http_request_method, $path, $req_id) { $content_type = $http_request_method === 'GET' ? '' : 'application/vnd.ch.swisscom.easypay.direct.payment+json'; $accept = $http_request_method === 'GET' ? "application/vnd.ch.swisscom.easypay.direct.payment+json" : "application/vnd.ch.swisscom.easypay.message.list+json"; $date = gmdate('D, d M Y H:i:s +0000', time()); $url = "https://" . $config->get_host() . $config->get_basepath() . $path; $md5Hash = isset($data) ? base64_encode(Signature::hash($data)) : ""; $hash_string = self::create_hash_string($http_request_method, $md5Hash, $content_type, $date, $path); $signature = Signature::sign($hash_string, $config->get_easypay_secret()); //Signature::Sign($hashString, base64_decode($config->getEasypaySecret())); $headers = array("Content-Type: " . $content_type, "X-SCS-Date: " . $date, "X-Request-Id: " . $req_id, "X-Merchant-Id: " . $config->get_merchant_id(), "X-CE-Client-Specification-Version: 1.1", "X-SCS-Signature: " . base64_encode($signature), "Accept: " . $accept, "Date: " . $date); if (isset($data)) { array_push($headers, "Content-MD5: " . $md5Hash); } $options = array(CURLOPT_CUSTOMREQUEST => $http_request_method, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => $data, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0); $ch = curl_init($url); curl_setopt_array($ch, $options); $response = curl_exec($ch); $http_code = curl_getinfo($ch)["http_code"]; $payment_response = new Payment_response(); echo "response: " . $response . "<br/>"; if (curl_errno($ch) != 0 || $http_code !== 200) { $payment_response->setSuccess(false); if (curl_errno($ch) != 0) { $error = curl_error($ch); $payment_response->set_exception($error); } else { $messages = json_decode($response, true); foreach ($messages["messages"] as $msg) { $message_obj = new Message(); if (array_key_exists("code", $msg)) { $message_obj->set_code($msg["code"]); } if (array_key_exists("field", $msg)) { $message_obj->set_field($msg["field"]); } if (array_key_exists("message", $msg)) { $message_obj->set_message($msg["message"]); } if (array_key_exists("requestId", $msg)) { $message_obj->set_request_id($msg["requestId"]); } $payment_response->add_message($message_obj); } } } else { $payment_response->setSuccess(true); $statusResponse = json_decode($response, true); if (isset($statusResponse)) { $payment_response = new Status_response(); if (array_key_exists("orderID", $statusResponse)) { $payment_response->set_orderID($statusResponse["orderID"]); } if (array_key_exists("paymentInfo", $statusResponse)) { $payment_response->set_payment_info($statusResponse["paymentInfo"]); } if (array_key_exists("isSilentAuthenticated", $statusResponse)) { $payment_response->set_is_silent_authenticated($statusResponse["isSilentAuthenticated"]); } if (array_key_exists("amount", $statusResponse)) { $payment_response->set_amount($statusResponse["amount"]); } if (array_key_exists("msisdn", $statusResponse)) { $payment_response->set_msisdn($statusResponse["msisdn"]); } if (array_key_exists("status", $statusResponse)) { $payment_response->set_status($statusResponse["status"]); } if (array_key_exists("formattedMsisdn", $statusResponse)) { $payment_response->set_formatted_msisdn($statusResponse["formattedMsisdn"]); } } } curl_close($ch); return $payment_response; }