Мужики подскажите пожалуйста, что я делаю не так...
Задача: Нужно что бы смог авторизоваться через cURL.
Проект написан на Silex.
Пытаюсь внедрить авторизацию через токен по вот этому мануальчику
Token based authentication with Silex ApplicationsGitHub Repository gonzalo123/token
Пытаюсь авторизоваться и пишет что запрашиваемая страница не найдена.
Вот как пытаюсь портировать к себе в проект
public\
index.phprequire_once __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
$app->after(function (Request $request, Response $response) {
$response->headers->set('Access-Control-Allow-Origin', '*');
});
require_once __DIR__ . '/../app/routes.php';
require_once __DIR__ . '/../app/providers.php';
$app['debug'] = true;
$app->run();
app\
routes.php/** @var $app \Silex\Application */
$app->get('/', 'App\\Controller\\BaseController::index');
$app->get('/login', 'App\\Controller\\AuthController::login');
$app->mount('/cabinet', new \App\Provider\Rater());
$app->mount('/api', new \App\Provider\API());
$app->mount('/auth', new \App\Provider\LoginControllerProvider());
$app->error(
function (\Exception $e, $code) use ($app) {
$jsonResponse = false;
switch ($code) {
case 404:
$message = '1 Запрашиваемая вами страница не найдена.';
break;
case 500:
$message = $e->getMessage();
$jsonResponse = 429 === $e->getCode();
break;
default:
$message = '2 Невозможно обработать запрос.';
}
return $jsonResponse
? $app->json(['message' => $message], $e->getCode())
: $app['twig']->render('error-page.html.twig', ['message' => $message]);
}
);
app\
providers.php/** @var $app Silex\Application */
require_once __DIR__ . '/config/config.php';
$app->register(new Silex\Provider\TwigServiceProvider(), $configTwig);
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Silex\Provider\SessionServiceProvider());
$app->register(new Silex\Provider\SecurityServiceProvider(), $configSecurity);
$app->register(new Silex\Provider\DoctrineServiceProvider(), $configDatabase);
$app->register(new Silex\Provider\ServiceControllerServiceProvider(), $configServiceControllerService);
$app->register(new Silex\Provider\SwiftmailerServiceProvider(), $configSwiftmailer);
$app->register(new App\Services\LoginServiceProvider());
$app['auth.validate.credentials'] = $app->protect(function ($user, $pass) {
return new App\Services\LoginServiceProvider;//->validateCredentials($user, $pass);
});
$app['auth.validate.token'] = $app->protect(function ($token) {
return new App\Services\LoginServiceProvider;//->validateToken($token);
});
$app['auth.new.token'] = $app->protect(function ($user) {
return new App\Services\LoginServiceProvider;//->getNewTokenForUser($user);
});
App\Provider\
LoginControllerProvider.phpnamespace App\Provider;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class LoginControllerProvider implements ControllerProviderInterface
{
const VALIDATE_CREDENTIALS = '/validateCredentials';
const TOKEN_HEADER_KEY = 'X-Token';
const TOKEN_REQUEST_KEY = '_token';
private $baseRoute;
public function setBaseRoute($baseRoute) {
$this->baseRoute = $baseRoute;
return $this;
}
public function connect(Application $app) {
$this->setUpMiddlewares($app);
return $this->extractControllers($app);
}
private function setUpMiddlewares(Application $app) {
$app->before(function (Request $request) use ($app) {
if (!$this->isAuthRequiredForPath($request->getPathInfo())) {
if (!$this->isValidTokenForApplication($app, $this->getTokenFromRequest($request))) {
throw new AccessDeniedHttpException('Access Denied');
}
}
}
);
}
private function isAuthRequiredForPath($path) {
return in_array($path, [$this->baseRoute . self::VALIDATE_CREDENTIALS]);
}
private function isValidTokenForApplication(Application $app, $token) {
return $app['auth.validate.credentials']($token);
}
private function getTokenFromRequest(Request $request) {
return $request->headers->get(self::TOKEN_HEADER_KEY, $request->get(self::TOKEN_REQUEST_KEY));
}
private function extractControllers(Application $app) {
$controllers = $app['controllers_factory'];
$controllers->get(self::VALIDATE_CREDENTIALS, function (Request $request) use ($app) {
$user = $request->get('user');
$pass = $request->get('pass');
$status = $app['auth.validate.token']($user, $pass);
return $app->json([
'status' => $status,
'info' => $status ? ['token' => $app['auth.new.token']($user)] : []
]
);
}
);
return $controllers;
}
}
App\Provider\
LoginServiceProvider.phpnamespace App\Services;
use Silex\Application;
use Silex\ServiceProviderInterface;
class LoginServiceProvider implements ServiceProviderInterface
{
public function register(Application $app) {
$app['auth.validate.credentials'] = $app->protect(function ($user, $pass) {
return $this->validateCredentials($user, $pass);
});
$app['auth.validate.token'] = $app->protect(function ($token) {
return $this->validateToken($token);
});
$app['auth.new.token'] = $app->protect(function ($user) {
return $this->getNewTokenForUser($user);
});
}
public function validateCredentials($user, $pass) {
return $user == $pass;
}
public function validateToken($token) {
return $token == 'a';
}
public function getNewTokenForUser($user) {
return 'a';
}
public function boot(Application $app) {
}
}