REST-full API's work on something called a handshake, so where security is concerned, we must ensure we are using SSL encryption in the data transfer.
When we talk about an API Key we're really saying a random but unique string (token) for that single user to gather data across domains (CORS).
Assuming verifyAPIKey() is robust
This isn't a pre-defined function in PHP. To include a 'robust' infastructure to API tokens, you would need a set of instructions.
- Client requesting data uses a unique token and return URI ->
- Client redirects user to your sandbox with this information ->
- User allows client access and Client recieves a seperate unique token ->
- Client creates a request with that token to an endpoint to then get the data
Really, the hack here would be an Injection when you're querying your database for them tokens. However, not to worry because SO has plenty of resoucres on securing queries.
A simple example of this context would look something like this:
class Endpoint {
private $Token;
public function setToken($token) {
$this->Token = $token;
return $this;
}
public function isToken() {
$smpt = (new Database)->Prepare('SQL HERE');
$smpt->execute([$token]);
return empty($smpt->fetch()[0]) ? false : true;
}
}
$e = new Endpoint();
$e->setToken($_POST['token']); // change to an MVC design rather than post methods
if($e->isToken()) {
// API logic (ie, return action related to that token)
}
Or see a working example of this.