CC-5709: Airtime Analyzer

* Fixed error in media API authentication
* Improved documentation
This commit is contained in:
Albert Santoni 2014-03-17 14:43:50 -04:00
parent 13a664207f
commit 16c56e6aff
4 changed files with 55 additions and 12 deletions

View File

@ -1,5 +1,6 @@
<?php <?php
class Rest_MediaController extends Zend_Rest_Controller class Rest_MediaController extends Zend_Rest_Controller
{ {
//fields that are not modifiable via our RESTful API //fields that are not modifiable via our RESTful API
@ -26,10 +27,11 @@ class Rest_MediaController extends Zend_Rest_Controller
{ {
$this->view->layout()->disableLayout(); $this->view->layout()->disableLayout();
} }
public function indexAction() public function indexAction()
{ {
if (!$this->verifyApiKey() && !$this->verifySession()) { if (!$this->verifyAuth(true, true))
{
return; return;
} }
@ -52,9 +54,11 @@ class Rest_MediaController extends Zend_Rest_Controller
public function getAction() public function getAction()
{ {
if (!$this->verifyApiKey() && !$this->verifySession()) { if (!$this->verifyAuth(true, true))
{
return; return;
} }
$id = $this->getId(); $id = $this->getId();
if (!$id) { if (!$id) {
return; return;
@ -73,9 +77,11 @@ class Rest_MediaController extends Zend_Rest_Controller
public function postAction() public function postAction()
{ {
if (!$this->verifyApiKey() && !$this->verifySession()) { if (!$this->verifyAuth(true, true))
{
return; return;
} }
//If we do get an ID on a POST, then that doesn't make any sense //If we do get an ID on a POST, then that doesn't make any sense
//since POST is only for creating. //since POST is only for creating.
if ($id = $this->_getParam('id', false)) { if ($id = $this->_getParam('id', false)) {
@ -104,9 +110,11 @@ class Rest_MediaController extends Zend_Rest_Controller
public function putAction() public function putAction()
{ {
if (!$this->verifyApiKey() && !$this->verifySession()) { if (!$this->verifyAuth(true, true))
{
return; return;
} }
$id = $this->getId(); $id = $this->getId();
if (!$id) { if (!$id) {
return; return;
@ -150,9 +158,11 @@ class Rest_MediaController extends Zend_Rest_Controller
public function deleteAction() public function deleteAction()
{ {
if (!$this->verifyApiKey() && !$this->verifySession()) { if (!$this->verifyAuth(true, true))
{
return; return;
} }
$id = $this->getId(); $id = $this->getId();
if (!$id) { if (!$id) {
return; return;
@ -179,6 +189,27 @@ class Rest_MediaController extends Zend_Rest_Controller
} }
return $id; return $id;
} }
private function verifyAuth($checkApiKey, $checkSession)
{
//Session takes precedence over API key for now:
if ($checkSession && $this->verifySession())
{
return true;
}
if ($checkApiKey && $this->verifyAPIKey())
{
return true;
}
$resp = $this->getResponse();
$resp->setHttpResponseCode(401);
$resp->appendBody("ERROR: Incorrect API key.");
return false;
}
private function verifyAPIKey() private function verifyAPIKey()
{ {
@ -196,11 +227,10 @@ class Rest_MediaController extends Zend_Rest_Controller
{ {
return true; return true;
} else { } else {
$resp = $this->getResponse();
$resp->setHttpResponseCode(401);
$resp->appendBody("ERROR: Incorrect API key.");
return false; return false;
} }
return false;
} }
private function verifySession() private function verifySession()
@ -210,6 +240,7 @@ class Rest_MediaController extends Zend_Rest_Controller
{ {
return true; return true;
} }
return false;
//Token checking stub code. We'd need to change LoginController.php to generate a token too, but //Token checking stub code. We'd need to change LoginController.php to generate a token too, but
//but luckily all the token code already exists and works. //but luckily all the token code already exists and works.

View File

@ -12,6 +12,15 @@ You will need to allow the "airtime" RabbitMQ user to access all exchanges and q
Usage Usage
========== ==========
This program must run as a user with permissions to write to your Airtime music library
directory. For standard Airtime installations, run it as the www-data user:
$ sudo -u www-data airtime_analyzer --debug
Or during development, add the --debug flag for more verbose output:
$ sudo -u www-data airtime_analyzer --debug
To print usage instructions, run: To print usage instructions, run:
$ airtime_analyzer --help $ airtime_analyzer --help
@ -35,6 +44,8 @@ For example, run:
$ php tools/message_sender.php '{ "tmp_file_path" : "foo.mp3", "final_directory" : ".", "callback_url" : "http://airtime.localhost/rest/media/1", "api_key" : "YOUR_API_KEY" }' $ php tools/message_sender.php '{ "tmp_file_path" : "foo.mp3", "final_directory" : ".", "callback_url" : "http://airtime.localhost/rest/media/1", "api_key" : "YOUR_API_KEY" }'
$ php tools/message_sender.php '{"tmp_file_path":"foo.mp3", "import_directory":"/srv/airtime/stor/imported/1","original_filename":"foo.mp3","callback_url": "http://airtime.localhost/rest/media/1", "api_key":"YOUR_API_KEY"}'
Logging Logging
========= =========

View File

@ -112,12 +112,13 @@ class MessageListener:
# TODO: Report this as a failed upload to the File Upload REST API. # TODO: Report this as a failed upload to the File Upload REST API.
# #
# TODO: If the JSON was invalid, then don't report to the REST API # TODO: If the JSON was invalid or the web server is down,
# then don't report that failure to the REST API
StatusReporter.report_failure_to_callback_url(callback_url, api_key, error_status=1, StatusReporter.report_failure_to_callback_url(callback_url, api_key, error_status=1,
reason=u'An error occurred while importing this file') reason=u'An error occurred while importing this file')
logging.error(e) logging.exception(e)
else: else:
# ACK at the very end, after the message has been successfully processed. # ACK at the very end, after the message has been successfully processed.

View File

@ -19,9 +19,9 @@ class StatusReporter():
timeout=StatusReporter._HTTP_REQUEST_TIMEOUT) timeout=StatusReporter._HTTP_REQUEST_TIMEOUT)
logging.debug("HTTP request returned status: " + str(r.status_code)) logging.debug("HTTP request returned status: " + str(r.status_code))
logging.debug(r.text) # Log the response body logging.debug(r.text) # Log the response body
r.raise_for_status() # Raise an exception if there was an HTTP error code returned
#TODO: Queue up failed requests and try them again later. #TODO: Queue up failed requests and try them again later.
r.raise_for_status() # Raise an exception if there was an HTTP error code returned
@classmethod @classmethod
def report_failure_to_callback_url(self, callback_url, api_key, error_status, reason): def report_failure_to_callback_url(self, callback_url, api_key, error_status, reason):