Reputation: 23
I have written an REST API in Slim framework. Currently the API is open Mode. How Can I add the security to the API. By adding the API KEY or username or password.
Also this API will be used in Mobile application. how can we protect ?
Upvotes: 1
Views: 1302
Reputation: 8738
You could create a route middleware to be sure that the requests have an API key (or whatever).
Example:
<?php
$checkApiKey = function () {
return function () use () {
$app = \Slim\Slim::getInstance();
$apiKey = $app->request->post('apiKey');
if ( Api::check($apiKey) ) { //your magic check on api key
$app->flash('error', 'API Key not available');
$app->redirect('/api/error');
}
};
};
$app = new \Slim\Slim();
$app->group('/api', $checkApiKey(), function ($app) {
//your amazing api routes...
});
Upvotes: 2