Added create endpoint to provisioning controller, fixed RestAuth helper

This commit is contained in:
Duncan Sommerville 2015-02-09 17:41:03 -05:00
parent a1436bfebb
commit dd095e8933
2 changed files with 134 additions and 83 deletions

View File

@ -6,9 +6,16 @@ use Aws\S3\S3Client;
class ProvisioningController extends Zend_Controller_Action class ProvisioningController extends Zend_Controller_Action
{ {
static $dbh;
// Parameter values
private $dbuser, $dbpass, $dbname, $dbhost;
public function init() public function init()
{ {
} }
/** /**
* Delete the Airtime Pro station's files from Amazon S3 * Delete the Airtime Pro station's files from Amazon S3
*/ */
@ -17,7 +24,7 @@ class ProvisioningController extends Zend_Controller_Action
$this->view->layout()->disableLayout(); $this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true); $this->_helper->viewRenderer->setNoRender(true);
if (!$this->verifyAPIKey()) { if (!RestAuth::verifyAuth(true, true, $this)) {
return; return;
} }
@ -33,27 +40,75 @@ class ProvisioningController extends Zend_Controller_Action
->appendBody("OK"); ->appendBody("OK");
} }
private function verifyAPIKey() /**
{ * RESTful endpoint for setting up and installing the Airtime database
// The API key is passed in via HTTP "basic authentication": */
// http://en.wikipedia.org/wiki/Basic_access_authentication public function createDatabaseAction() {
Logging::info("Create Database action received");
$CC_CONFIG = Config::getConfig(); if (!RestAuth::verifyAuth(true, true, $this)) {
return;
}
// Decode the API key that was passed to us in the HTTP request. try {
$authHeader = $this->getRequest()->getHeader("Authorization"); $this->getParams();
$encodedRequestApiKey = substr($authHeader, strlen("Basic ")); $this->setNewDatabaseConnection();
$encodedStoredApiKey = base64_encode($CC_CONFIG["apiKey"][0] . ":"); $this->createDatabaseTables();
} catch(Exception $e) {
if ($encodedRequestApiKey === $encodedStoredApiKey) $this->getResponse()
{ ->setHttpResponseCode(400)
return true; ->appendBody($e->getMessage());
return;
} }
$this->getResponse() $this->getResponse()
->setHttpResponseCode(401) ->setHttpResponseCode(201);
->appendBody("ERROR: Incorrect API key."); }
return false; private function getParams() {
$this->dbuser = $this->_getParam('dbuser', '');
$this->dbpass = $this->_getParam('dbpass', '');
$this->dbname = $this->_getParam('dbname', '');
$this->dbhost = $this->_getParam('dbhost', '');
}
/**
* Set up a new database connection based on the parameters in the request
* @throws PDOException upon failure to connect
*/
private function setNewDatabaseConnection() {
self::$dbh = new PDO("pgsql:host=" . $this->dbhost
. ";dbname=" . $this->dbname
. ";port=5432" . ";user=" . $this->dbuser
. ";password=" . $this->dbpass);
$err = self::$dbh->errorInfo();
if ($err[1] != null) {
throw new PDOException("ERROR: Could not connect to database");
} }
} }
/**
* Install the Airtime database
* @throws Exception
*/
private function createDatabaseTables() {
$sqlDir = dirname(APPLICATION_PATH) . "/build/sql/";
$files = array("schema.sql", "sequences.sql", "views.sql", "triggers.sql", "defaultdata.sql");
foreach ($files as $f) {
try {
/*
* Unfortunately, we need to use exec here due to PDO's lack of support for importing
* multi-line .sql files. PDO->exec() almost works, but any SQL errors stop the import,
* so the necessary DROPs on non-existent tables make it unusable. Prepared statements
* have multiple issues; they similarly die on any SQL errors, fail to read in multi-line
* commands, and fail on any unescaped ? or $ characters.
*/
exec("export PGPASSWORD=" . $this->dbpass . " && psql -U " . $this->dbuser . " --dbname "
. $this->dbname . " -h " . $this->dbhost . " -f $sqlDir$f 2>/dev/null", $out, $status);
} catch (Exception $e) {
throw new Exception("ERROR: Failed to create database tables");
}
}
}
}

View File

@ -1,32 +1,30 @@
<?php <?php
class RestAuth class RestAuth {
{
public static function verifyAuth($checkApiKey, $checkSession) public static function verifyAuth($checkApiKey, $checkSession, $action) {
{
//Session takes precedence over API key for now: //Session takes precedence over API key for now:
if ($checkSession && RestAuth::verifySession() if ($checkSession && self::verifySession()
|| $checkApiKey && RestAuth::verifyAPIKey()) || $checkApiKey && self::verifyAPIKey($action)
{ ) {
return true; return true;
} }
$resp = $this->getResponse(); $action->getResponse()
$resp->setHttpResponseCode(401); ->setHttpResponseCode(401)
$resp->appendBody("ERROR: Incorrect API key."); ->appendBody(json_encode(array("message" => "ERROR: Incorrect API key.")));
return false; return false;
} }
public static function getOwnerId() public static function getOwnerId() {
{
try { try {
if (RestAuth::verifySession()) { if (self::verifySession()) {
$service_user = new Application_Service_UserService(); $service_user = new Application_Service_UserService();
return $service_user->getCurrentUser()->getDbId(); return $service_user->getCurrentUser()->getDbId();
} else { } else {
$defaultOwner = CcSubjsQuery::create() $defaultOwner = CcSubjsQuery::create()
->filterByDbType('A') ->filterByDbType(array('A', 'S'), Criteria::IN)
->orderByDbId() ->orderByDbId()
->findOne(); ->findOne();
if (!$defaultOwner) { if (!$defaultOwner) {
@ -41,20 +39,18 @@ class RestAuth
} }
} }
private static function verifySession() private static function verifySession() {
{
$auth = Zend_Auth::getInstance(); $auth = Zend_Auth::getInstance();
return $auth->hasIdentity(); return $auth->hasIdentity();
} }
private static function verifyAPIKey() private static function verifyAPIKey($action) {
{
//The API key is passed in via HTTP "basic authentication": //The API key is passed in via HTTP "basic authentication":
// http://en.wikipedia.org/wiki/Basic_access_authentication // http://en.wikipedia.org/wiki/Basic_access_authentication
$CC_CONFIG = Config::getConfig(); $CC_CONFIG = Config::getConfig();
//Decode the API key that was passed to us in the HTTP request. //Decode the API key that was passed to us in the HTTP request.
$authHeader = $this->getRequest()->getHeader("Authorization"); $authHeader = $action->getRequest()->getHeader("Authorization");
$encodedRequestApiKey = substr($authHeader, strlen("Basic ")); $encodedRequestApiKey = substr($authHeader, strlen("Basic "));
$encodedStoredApiKey = base64_encode($CC_CONFIG["apiKey"][0] . ":"); $encodedStoredApiKey = base64_encode($CC_CONFIG["apiKey"][0] . ":");