首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

接着昨天发布的开放平台原理,今天详细的跟着代码一步一步分析

目前的开放平台大致都是创建一个应用然后由系统自动分配相关信息:如

点击创建应用:

填写好后提交,系统接收到自动分配相关信息:

这些都是在之后进行校验所需要的重要信息。

好的到这里我们看看代码具体经过了哪些:

大致步骤是,首先自行设计一个app_id,app_key,app_secret生成算法,然后保存至数据库内。数据库表结构根据自己的需要进行设计。以备之后接口调用进行合法校验。重要的东西来了

classAuth2

{

//API接口请求方式

private$action='Get';

//API接口名称

private$method;

//应用的app_secret

private$app_secret;

//应用的app_key

private$app_key;

//签名

private$sign;

//时间戳,格式为yyyy-MM-dd HH:mm:ss,例如:2011-06-16 13:23:30。

API服务端允许客户端请求时间误差为6分钟

private$timestamp;

//暂时只支持json

private$format='json';

//API协议版本,可选值:2.0

private$v='2.0';

private$param= [];

private$_request=[];

private$request_param;

private$error= [

10000=>'parameter illegality',

10001=>'request method must be get/post',

20001=>'client_id is empty',

20002=>'app_key is empty',

20003=>'action is empty',

20004=>'timestamp is empty',

20005=>'sign is empty',

20006=>'format is empty',

20007=>'v is empty',

20008=>'method is empty',

30000=>'OAUTH SERVER ERROR:xxxxx',

30001=>'Can not find the client_id:xxxxx',

];

public functionverify($request)

{

$this->request_param=$request;

$this->app_key=$this->existsParam('app_key',20002);

$this->param['app_key'] =$this->app_key;

$this->action=$this->existsParam('action',20003);

$this->param['action'] =$this->action;

$this->sign=$this->existsParam('sign',20005);

$this->format=$this->existsParam('format',20006);

$this->param['format'] =$this->format;

$this->timestamp=$this->existsParam('timestamp',20004);

$this->param['timestamp'] =$this->timestamp;

$this->v=$this->existsParam('v',20007);

$this->param['v'] =$this->v;

$this->method=$this->existsParam('method',20008);

$this->param['method'] =$this->method;

$this->app_secret=$this->getSecret();

return$this->verifySign($this->sign

($this->generate_sign()));

}

//签名拼装

private functiongenerate_sign()

{

//对参数进行排序

ksort($this->param);

//根据api要求md5加密需在前后加上密钥

$string_to_be_signed=$this->app_secret;

foreach($this->paramas$k=>$v){

if(is_string($v) &&"@"!= substr($v,,1))

{

$string_to_be_signed.="$k$v";

}

}

unset($k,$v);

$string_to_be_signed.=$this->app_secret;

return$string_to_be_signed;

}

//签名

private functionsign($string_to_sign)

{

returnstrtoupper(md5($string_to_sign));

}

public functionrequest()

{

//保存api实例对象

if(isset($this->_request[$this->method])){

return$this->_request[$this->method];

}else{

$class='\\app\\api\\request\\'.$this->method;

//实例化api接口

$this->_request[$this->method] =new$class;;

}

//统一调用api接口方法

return$this->_request[$this->method]->getJson();

}

//错误返回

private functionerrorCode($code)

{

Restful::request()

->setData(['code'=>$code,'error_msg'=>

$this->error[$code]])

->send();

}

//判断是否存在必要参数

private functionexistsParam($param,$code)

{

//判断参数是否全部传入

if(array_key_exists($param,$this->request_param)){

//判断参数是否为空

if($this->request_param[$param] ==''

is_null($this->request_param[$param])){

$this->errorCode($code);

}

return$this->request_param[$param];

}else{

$this->errorCode(10000);

}

}

//sign验证

private functionverifySign($sign)

{

if(function_exists('hash_equals')) {

returnhash_equals($this->sign,$sign);

}

returnstrcmp($this->sign,$sign);

}

//获取用户私钥

private functiongetSecret()

{

try{

$secret= Access::getAppSecret($this->app_key);

if(!$secret){

$this->errorCode(30001);

}

return$secret;

}catch(AccessException$e) {

//

$this->errorCode(30000);

}

}

}

该类中提供了校验所需要的用到的参数以及算法,提供两个verify以及sign方法,前者为验证接收请求者发送过来的参数进行签名校验,后者提供签名方法。这个类有待优化,还有很多安全问题以及合理性需要调整更改。大致逻辑就是以上,根据具体业务来自己组织。最后一个获取用户私钥方法是根据请求来的app_key去查询数据库然后获取对应的私钥。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180208G0JRJ000?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券