I'm currently developing a simple API using Laravel for Android devices. I am trying to authenticate every request so that I know the request is made by a legitimate user. To do so , I require every legitimate request to include parameters user id and key. The android application will get the information needed upon user's successful login and store it in the device. Every request from the device will contain the user id and key as parameters.
I use middleware to check every request here's the code :
class AuthenticatedUser {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$input = $request->only('userID','key');
$u = User::where(['id' => $input['userID']])->first();
if($u && Hash::check($input['key'],$u->app_key)){
Session::flash('user',$u);
return $next($request);
}
else{
return response(['status' => 'ERROR' , 'message' => 'AUTH_FAILURE', 'payload' => [] ],401);
}
}
}
Basically, the middleware checks if the a user with the given user id and key exists, and if the user exists, it will store the user in a temporary session. If no such user exists, it will return an error.
My problem is that every request would take 800-1000 ms. After some tinkering I found out that Hash::Check is the cause of the delay. I tried to remove the Hash::Check code and every request would take roughly 300-400ms
Then I tried to deploy this application on two different free VM Workspace services, and the request would take around 350-400ms with the Hash::Check intact. What could possibly cause this ? Why would Hash::Check function takes a very long time on shared host but not on VMs ?
The PHP version used on the shared host is 5.4.41 , and the PHP version of the VM is 5.5.9
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire