Skip to content

Instantly share code, notes, and snippets.

@angelxmoreno
Last active October 28, 2019 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelxmoreno/09713d6b515c371da605bdf4ed501c54 to your computer and use it in GitHub Desktop.
Save angelxmoreno/09713d6b515c371da605bdf4ed501c54 to your computer and use it in GitHub Desktop.
<?php
namespace App\Authentication\Authenticator;
use Authentication\Authenticator\FormAuthenticator;
use Psr\Http\Message\ServerRequestInterface;
/**
* Class ApiAuthenticator
* @package App\Authentication\Authenticator
*/
class JsonAuthenticator extends FormAuthenticator
{
protected function _getData(ServerRequestInterface $request)
{
$fields = $this->_config['fields'];
$body = json_decode($request->getBody()->getContents(), true);
$data = [];
foreach ($fields as $key => $field) {
if (!isset($body[$field])) {
return null;
}
$value = $body[$field];
if (!is_string($value) || !strlen($value)) {
return null;
}
$data[$key] = $value;
}
return $data;
}
}
@angelxmoreno
Copy link
Author

This bit is no longer needed since CakePHP itself has a BodyParserMiddleware that addresses this.
cakephp/authentication#185
cakephp/cakephp#11875

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment