<?php
namespace App\Http\Controllers\Api5;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Utils\HttpUtil;
use App\AuthSystem;
use DB;
use Illuminate\Http\Response;
/**
* finn
* 20171214
* 爬虫使用 获取token token 过期时间设置
*/
class ApiController extends Controller
{
public $private_key = "xxxx_1681781234";
public $init_value = "168178_xxxxx_123";
function __construct(Request $request)
{
//$name = empty($request->name)?'':$request->name;
// $type = empty($request->type)?1:$request->type;
// $M_R_L = $type==1?MCRYPT_RIJNDAEL_256:MCRYPT_RIJNDAEL_128;
// $key = $type==1?md5($this->private_key):md5($this->private_key);
// $iv = $type==1?md5($this->init_value):$this->init_value;
}
public function getTokenNodeJs(Request $request)
{
$name = empty($request->name)?'':$request->name;//加密数据
if(empty($name))return HttpUtil::retrun_json(['token'=>'','msg'=>'name must not null','code'=>206],200);
$name = strtolower($name);
if(!$this->authName($name))
{
return HttpUtil::retrun_json(['token'=>'','msg'=>'name not in white list','code'=>206],200);
}
$out_time = strtotime("+5 month");
$encrypted_time = $this->nodeJsEncode($out_time);
//加密
$encrypted = $this->nodeJsEncode($name);
$token = base64_encode($encrypted).'_TIME_'.base64_encode($encrypted_time);
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>1120])->first();
if(empty($auth_system)) $auth_system = new AuthSystem();
$auth_system->auth_name = $name;
$auth_system->auth_token = $token;
$auth_system->auth_start_time = date('Y-m-d',time());
$auth_system->auth_session_time = $out_time;
$auth_system->type = 1;
$auth_system->mcrypt_rijndael_model = 1120;
$auth_system->save();
return HttpUtil::retrun_json(['token'=>$token,'code'=>200],200);
}
private function nodeJsEncode($param)
{
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$key256 = "12345678901234561234567890123456";
$iv = '1234567890123456';
mcrypt_generic_init($cipher, $key256, $iv);
// PHP pads with NULL bytes if $plainText is not a multiple of the block size
$cipherText256 = mcrypt_generic($cipher,$param);
mcrypt_generic_deinit($cipher);
$cipherHexText256 =bin2hex($cipherText256);
return $cipherHexText256;
}
/**
* name 企业名称 唯一性
* 初始秘钥private_key
* 初始向量init_value
* name 加密的数据
* token 返回数据
* 加密模式为 MCRYPT_MODE_CBC
* 加密算法 MCRYPT_RIJNDAEL_256 mcrypt_rijndael
* base64_encode TOKEN 和 过期时间 字符串拼接 返回
* 填充模式只有ZeroPadding
* @param Request $request [description]
* @return [type] [description]
*/
public function getToken(Request $request)
{
$name = empty($request->name)?'':$request->name;//加密数据
if(empty($name))return HttpUtil::retrun_json(['token'=>'','msg'=>'name must not null','code'=>206],200);
$name = strtolower($name);
if(!$this->authName($name))
{
return HttpUtil::retrun_json(['token'=>'','msg'=>'name not in white list','code'=>206],200);
}
$name = md5($name);
// echo $name ,'<br/>';
// $out_time = date("Ymd",strtotime("+2 month"));
$out_time = strtotime("+5 month");
$encrypted_time = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($this->private_key), $out_time, MCRYPT_MODE_CBC, md5($this->init_value));
//加密
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($this->private_key), $name, MCRYPT_MODE_CBC, md5($this->init_value));
$token = base64_encode($encrypted).'_TIME_'.base64_encode($encrypted_time);
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>256])->first();
if(empty($auth_system)) $auth_system = new AuthSystem();
$auth_system->auth_name = $name;
// echo $encrypted,'<br/>';
// echo $token,'<br/>';
// dd($out_time);
$auth_system->auth_token = $token;
$auth_system->auth_start_time = date('Y-m-d',time());
$auth_system->auth_session_time = $out_time;
$auth_system->type = 1;
$auth_system->mcrypt_rijndael_model = 256;
$auth_system->save();
return HttpUtil::retrun_json(['token'=>$token,'code'=>200],200);
}
public function getToken128(Request $request)
{
$name = empty($request->name)?'':$request->name;//加密数据
if(empty($name))return HttpUtil::retrun_json(['token'=>'','msg'=>'name must not null','code'=>206],200);
$name = strtolower($name);
if(!$this->authName($name))
{
return HttpUtil::retrun_json(['token'=>'','msg'=>'name not in white list','code'=>206],200);
}
// echo $name ,'<br/>';
// $out_time = date("Ymd",strtotime("+2 month"));
$out_time = strtotime("+5 month");
$encrypted_time = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->private_key, $out_time, MCRYPT_MODE_CBC, $this->init_value);
//加密
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->private_key, $name, MCRYPT_MODE_CBC, $this->init_value);
$token = base64_encode($encrypted).'_TIME_'.base64_encode($encrypted_time);
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>128])->first();
if(empty($auth_system)) $auth_system = new AuthSystem();
$auth_system->auth_name = $name;
// echo $encrypted,'<br/>';
// echo $token,'<br/>';
// dd($out_time);
$auth_system->auth_token = $token;
$auth_system->auth_start_time = date('Y-m-d',time());
$auth_system->auth_session_time = $out_time;
$auth_system->type = 1;
$auth_system->mcrypt_rijndael_model = 128;
$auth_system->save();
return HttpUtil::retrun_json(['token'=>$token,'code'=>200],200);
}
public function authNodeJSToken(Request $request)
{
// $token = empty($request->token)?'':$request->token;
$name = empty($request->name)?'':$request->name;
$mi_string = [];
if(empty($name))
{
$mi_string = ['status'=>false,'out_time'=>0,'msg'=>'name or token must not null'];
return $this->nodeJsEncode($mi_string);
}
$name = strtolower($name);
if(!$this->authName($name))
{
return $this->nodeJsEncode(json_encode(['status'=>false,'out_time'=>0,'msg'=>'name is not white list','code'=>208]));
}
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>1120])->first();
if(empty($auth_system))return $this->nodeJsEncode(json_encode(['status'=>false,'out_time'=>0,'msg'=>'name is not white list','code'=>208]));
// dd($auth_system);
$auth_session_time = $auth_system->auth_session_time;
$now_time = date('Y-m-d',time());
if(strtotime($now_time)>=$auth_session_time){
$auth_system->type = 0;
$auth_system->save();
return $this->nodeJsEncode(json_encode(['status'=>false,'out_time'=>0,'msg'=>'token is out of time','code'=>204]));
}
$auth_token = $auth_system->auth_token;
$auth_token_tmp = explode('_TIME_', $auth_token);
$token = base64_decode($auth_token_tmp[0]);
$out_time = base64_decode($auth_token_tmp[1]);
$json_string = json_encode(['status'=>true,'out_time'=>1,'msg'=>'token auth is ok','code'=>200,'true_token'=>$token]);
return $this->nodeJsEncode($json_string);
}
/**
* 验证TOKEN
* @param Request $request [description]
* @return [type] [description]
*/
public function authToken(Request $request)
{
// $token = empty($request->token)?'':$request->token;
$type = empty($request->type)?1:$request->type;
$M_R_L = $type==1?MCRYPT_RIJNDAEL_256:MCRYPT_RIJNDAEL_128;
$model_length = $type==1?'256':128;
$name = empty($request->name)?'':$request->name;
$key = $type==1?md5($this->private_key):$this->private_key;
$iv = $type==1?md5($this->init_value):$this->init_value;
$mi_string = [];
if(empty($name))
{
$mi_string = ['status'=>false,'out_time'=>0,'msg'=>'name or token must not null'];
$mi_string = $this->mcryptEn($M_R_L,$key,json_encode($mi_string),MCRYPT_MODE_CBC,$iv);
return $mi_string;
}
$name = strtolower($name);
if(!$this->authName($name))
{
$mi_string = $this->mcryptEn($M_R_L,$key,json_encode(['status'=>false,'out_time'=>0,'msg'=>'name is not white list','code'=>208]),MCRYPT_MODE_CBC,$iv);
return $mi_string;
}
$name = $type==1?md5($name):$name;
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>$model_length])->first();
// dd($auth_system);
$auth_session_time = $auth_system->auth_session_time;
$now_time = date('Y-m-d',time());
if(strtotime($now_time)>=$auth_session_time){
$auth_system->type = 0;
$auth_system->save();
$mi_string = $this->mcryptEn($M_R_L,$key,json_encode(['status'=>false,'out_time'=>0,'msg'=>'token is out of time','code'=>204]),MCRYPT_MODE_CBC,$iv);
return $mi_string;
}
$auth_token = $auth_system->auth_token;
$auth_token_tmp = explode('_TIME_', $auth_token);
$token = base64_decode($auth_token_tmp[0]);
$out_time = base64_decode($auth_token_tmp[1]);
$true_token = '';
$true_time = '';
if($type==1)
{
$true_token = mcrypt_decrypt($M_R_L, md5($this->private_key), $token,MCRYPT_MODE_CBC, md5($this->init_value));
$true_time = mcrypt_decrypt($M_R_L, md5($this->private_key), $out_time,MCRYPT_MODE_CBC, md5($this->init_value));
}else{
$true_token = mcrypt_decrypt($M_R_L, $this->private_key, $token,MCRYPT_MODE_CBC, $this->init_value);
$true_time = mcrypt_decrypt($M_R_L, $this->private_key, $out_time,MCRYPT_MODE_CBC, $this->init_value);
}
$true_token = str_replace("\x00", '', $true_token);
$true_token = $type==1?md5($true_token):$true_token;
$json_string = json_encode(['status'=>true,'out_time'=>1,'msg'=>'token auth is ok','code'=>200,'true_token'=>$true_token]);
$mi_string = $this->mcryptEn($M_R_L,$key,$json_string,MCRYPT_MODE_CBC,$iv);
return $mi_string;
// $nn = '68WA8Mw6OGjJ0hicuUjhaw==';
// $true_token_2 = mcrypt_decrypt($M_R_L, $key, base64_decode($nn),MCRYPT_MODE_CBC, $iv);
// //
// dd($true_token_2);
// $true_token_2 = mcrypt_decrypt($M_R_L, $key, base64_decode($mi_string),MCRYPT_MODE_CBC, $iv);
// dd($true_token_2);
}
public function mcryptEn($length,$key,$string,$model,$iv)
{
$string = mcrypt_encrypt($length, $key, $string, $model, $iv);
$base_string = base64_encode($string);
// $base_de_string = mcrypt_decrypt($length, $key, $string, $model, $iv);
return $base_string;
}
/**
* 验证 用户名是否是白名单
* @return [type] [description]
*/
public function authName($name)
{
$auth_name = DB::table('auth_name')->where(['auth_name'=>$name,'type'=>1])->first();
if(!empty($auth_name) && $auth_name->auth_name!=$name)
{
return false;
}
return empty($auth_name)?false:true;
}
}
namespace App\Http\Controllers\Api5;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Utils\HttpUtil;
use App\AuthSystem;
use DB;
use Illuminate\Http\Response;
/**
* finn
* 20171214
* 爬虫使用 获取token token 过期时间设置
*/
class ApiController extends Controller
{
public $private_key = "xxxx_1681781234";
public $init_value = "168178_xxxxx_123";
function __construct(Request $request)
{
//$name = empty($request->name)?'':$request->name;
// $type = empty($request->type)?1:$request->type;
// $M_R_L = $type==1?MCRYPT_RIJNDAEL_256:MCRYPT_RIJNDAEL_128;
// $key = $type==1?md5($this->private_key):md5($this->private_key);
// $iv = $type==1?md5($this->init_value):$this->init_value;
}
public function getTokenNodeJs(Request $request)
{
$name = empty($request->name)?'':$request->name;//加密数据
if(empty($name))return HttpUtil::retrun_json(['token'=>'','msg'=>'name must not null','code'=>206],200);
$name = strtolower($name);
if(!$this->authName($name))
{
return HttpUtil::retrun_json(['token'=>'','msg'=>'name not in white list','code'=>206],200);
}
$out_time = strtotime("+5 month");
$encrypted_time = $this->nodeJsEncode($out_time);
//加密
$encrypted = $this->nodeJsEncode($name);
$token = base64_encode($encrypted).'_TIME_'.base64_encode($encrypted_time);
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>1120])->first();
if(empty($auth_system)) $auth_system = new AuthSystem();
$auth_system->auth_name = $name;
$auth_system->auth_token = $token;
$auth_system->auth_start_time = date('Y-m-d',time());
$auth_system->auth_session_time = $out_time;
$auth_system->type = 1;
$auth_system->mcrypt_rijndael_model = 1120;
$auth_system->save();
return HttpUtil::retrun_json(['token'=>$token,'code'=>200],200);
}
private function nodeJsEncode($param)
{
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$key256 = "12345678901234561234567890123456";
$iv = '1234567890123456';
mcrypt_generic_init($cipher, $key256, $iv);
// PHP pads with NULL bytes if $plainText is not a multiple of the block size
$cipherText256 = mcrypt_generic($cipher,$param);
mcrypt_generic_deinit($cipher);
$cipherHexText256 =bin2hex($cipherText256);
return $cipherHexText256;
}
/**
* name 企业名称 唯一性
* 初始秘钥private_key
* 初始向量init_value
* name 加密的数据
* token 返回数据
* 加密模式为 MCRYPT_MODE_CBC
* 加密算法 MCRYPT_RIJNDAEL_256 mcrypt_rijndael
* base64_encode TOKEN 和 过期时间 字符串拼接 返回
* 填充模式只有ZeroPadding
* @param Request $request [description]
* @return [type] [description]
*/
public function getToken(Request $request)
{
$name = empty($request->name)?'':$request->name;//加密数据
if(empty($name))return HttpUtil::retrun_json(['token'=>'','msg'=>'name must not null','code'=>206],200);
$name = strtolower($name);
if(!$this->authName($name))
{
return HttpUtil::retrun_json(['token'=>'','msg'=>'name not in white list','code'=>206],200);
}
$name = md5($name);
// echo $name ,'<br/>';
// $out_time = date("Ymd",strtotime("+2 month"));
$out_time = strtotime("+5 month");
$encrypted_time = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($this->private_key), $out_time, MCRYPT_MODE_CBC, md5($this->init_value));
//加密
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($this->private_key), $name, MCRYPT_MODE_CBC, md5($this->init_value));
$token = base64_encode($encrypted).'_TIME_'.base64_encode($encrypted_time);
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>256])->first();
if(empty($auth_system)) $auth_system = new AuthSystem();
$auth_system->auth_name = $name;
// echo $encrypted,'<br/>';
// echo $token,'<br/>';
// dd($out_time);
$auth_system->auth_token = $token;
$auth_system->auth_start_time = date('Y-m-d',time());
$auth_system->auth_session_time = $out_time;
$auth_system->type = 1;
$auth_system->mcrypt_rijndael_model = 256;
$auth_system->save();
return HttpUtil::retrun_json(['token'=>$token,'code'=>200],200);
}
public function getToken128(Request $request)
{
$name = empty($request->name)?'':$request->name;//加密数据
if(empty($name))return HttpUtil::retrun_json(['token'=>'','msg'=>'name must not null','code'=>206],200);
$name = strtolower($name);
if(!$this->authName($name))
{
return HttpUtil::retrun_json(['token'=>'','msg'=>'name not in white list','code'=>206],200);
}
// echo $name ,'<br/>';
// $out_time = date("Ymd",strtotime("+2 month"));
$out_time = strtotime("+5 month");
$encrypted_time = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->private_key, $out_time, MCRYPT_MODE_CBC, $this->init_value);
//加密
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->private_key, $name, MCRYPT_MODE_CBC, $this->init_value);
$token = base64_encode($encrypted).'_TIME_'.base64_encode($encrypted_time);
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>128])->first();
if(empty($auth_system)) $auth_system = new AuthSystem();
$auth_system->auth_name = $name;
// echo $encrypted,'<br/>';
// echo $token,'<br/>';
// dd($out_time);
$auth_system->auth_token = $token;
$auth_system->auth_start_time = date('Y-m-d',time());
$auth_system->auth_session_time = $out_time;
$auth_system->type = 1;
$auth_system->mcrypt_rijndael_model = 128;
$auth_system->save();
return HttpUtil::retrun_json(['token'=>$token,'code'=>200],200);
}
public function authNodeJSToken(Request $request)
{
// $token = empty($request->token)?'':$request->token;
$name = empty($request->name)?'':$request->name;
$mi_string = [];
if(empty($name))
{
$mi_string = ['status'=>false,'out_time'=>0,'msg'=>'name or token must not null'];
return $this->nodeJsEncode($mi_string);
}
$name = strtolower($name);
if(!$this->authName($name))
{
return $this->nodeJsEncode(json_encode(['status'=>false,'out_time'=>0,'msg'=>'name is not white list','code'=>208]));
}
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>1120])->first();
if(empty($auth_system))return $this->nodeJsEncode(json_encode(['status'=>false,'out_time'=>0,'msg'=>'name is not white list','code'=>208]));
// dd($auth_system);
$auth_session_time = $auth_system->auth_session_time;
$now_time = date('Y-m-d',time());
if(strtotime($now_time)>=$auth_session_time){
$auth_system->type = 0;
$auth_system->save();
return $this->nodeJsEncode(json_encode(['status'=>false,'out_time'=>0,'msg'=>'token is out of time','code'=>204]));
}
$auth_token = $auth_system->auth_token;
$auth_token_tmp = explode('_TIME_', $auth_token);
$token = base64_decode($auth_token_tmp[0]);
$out_time = base64_decode($auth_token_tmp[1]);
$json_string = json_encode(['status'=>true,'out_time'=>1,'msg'=>'token auth is ok','code'=>200,'true_token'=>$token]);
return $this->nodeJsEncode($json_string);
}
/**
* 验证TOKEN
* @param Request $request [description]
* @return [type] [description]
*/
public function authToken(Request $request)
{
// $token = empty($request->token)?'':$request->token;
$type = empty($request->type)?1:$request->type;
$M_R_L = $type==1?MCRYPT_RIJNDAEL_256:MCRYPT_RIJNDAEL_128;
$model_length = $type==1?'256':128;
$name = empty($request->name)?'':$request->name;
$key = $type==1?md5($this->private_key):$this->private_key;
$iv = $type==1?md5($this->init_value):$this->init_value;
$mi_string = [];
if(empty($name))
{
$mi_string = ['status'=>false,'out_time'=>0,'msg'=>'name or token must not null'];
$mi_string = $this->mcryptEn($M_R_L,$key,json_encode($mi_string),MCRYPT_MODE_CBC,$iv);
return $mi_string;
}
$name = strtolower($name);
if(!$this->authName($name))
{
$mi_string = $this->mcryptEn($M_R_L,$key,json_encode(['status'=>false,'out_time'=>0,'msg'=>'name is not white list','code'=>208]),MCRYPT_MODE_CBC,$iv);
return $mi_string;
}
$name = $type==1?md5($name):$name;
$auth_system = AuthSystem::where(['auth_name'=>$name,'mcrypt_rijndael_model'=>$model_length])->first();
// dd($auth_system);
$auth_session_time = $auth_system->auth_session_time;
$now_time = date('Y-m-d',time());
if(strtotime($now_time)>=$auth_session_time){
$auth_system->type = 0;
$auth_system->save();
$mi_string = $this->mcryptEn($M_R_L,$key,json_encode(['status'=>false,'out_time'=>0,'msg'=>'token is out of time','code'=>204]),MCRYPT_MODE_CBC,$iv);
return $mi_string;
}
$auth_token = $auth_system->auth_token;
$auth_token_tmp = explode('_TIME_', $auth_token);
$token = base64_decode($auth_token_tmp[0]);
$out_time = base64_decode($auth_token_tmp[1]);
$true_token = '';
$true_time = '';
if($type==1)
{
$true_token = mcrypt_decrypt($M_R_L, md5($this->private_key), $token,MCRYPT_MODE_CBC, md5($this->init_value));
$true_time = mcrypt_decrypt($M_R_L, md5($this->private_key), $out_time,MCRYPT_MODE_CBC, md5($this->init_value));
}else{
$true_token = mcrypt_decrypt($M_R_L, $this->private_key, $token,MCRYPT_MODE_CBC, $this->init_value);
$true_time = mcrypt_decrypt($M_R_L, $this->private_key, $out_time,MCRYPT_MODE_CBC, $this->init_value);
}
$true_token = str_replace("\x00", '', $true_token);
$true_token = $type==1?md5($true_token):$true_token;
$json_string = json_encode(['status'=>true,'out_time'=>1,'msg'=>'token auth is ok','code'=>200,'true_token'=>$true_token]);
$mi_string = $this->mcryptEn($M_R_L,$key,$json_string,MCRYPT_MODE_CBC,$iv);
return $mi_string;
// $nn = '68WA8Mw6OGjJ0hicuUjhaw==';
// $true_token_2 = mcrypt_decrypt($M_R_L, $key, base64_decode($nn),MCRYPT_MODE_CBC, $iv);
// //
// dd($true_token_2);
// $true_token_2 = mcrypt_decrypt($M_R_L, $key, base64_decode($mi_string),MCRYPT_MODE_CBC, $iv);
// dd($true_token_2);
}
public function mcryptEn($length,$key,$string,$model,$iv)
{
$string = mcrypt_encrypt($length, $key, $string, $model, $iv);
$base_string = base64_encode($string);
// $base_de_string = mcrypt_decrypt($length, $key, $string, $model, $iv);
return $base_string;
}
/**
* 验证 用户名是否是白名单
* @return [type] [description]
*/
public function authName($name)
{
$auth_name = DB::table('auth_name')->where(['auth_name'=>$name,'type'=>1])->first();
if(!empty($auth_name) && $auth_name->auth_name!=$name)
{
return false;
}
return empty($auth_name)?false:true;
}
}