How can I get Basic authorization header and authenticate it for API by using PHP. I had created a unique key which is encoded and stored in database. I am using Slim Framework for rest API.
I can get the header by using $request->headers();
in Slim Framework. It returns all the headers specified, but how do i check that key with my database token.
Is there any proper way to do this?
I did it like this in Slim Framework 3:
I added the Middleware to all of the requests and checked the Authorization header for certain string and if the Authorization header is not set or doesnt match my string i just return 400 HTTP code.
$app->add(function($request,$response,$next){
$authorization_header = $request->getHeader("Authorization");
if(empty($authorization_header) || ($authorization_header[0]!="test")){ //you can check the header for a certain string or you can check if what is in the Authorization header exists in your database
return $response->withStatus(400);
}
$response = $next($request,$response);
return $response;
});