Example #1
0
 private function requestWithAuthorizationOAuth()
 {
     //print_r($_POST);
     $authorization = new GalaxyAuthorizationOAuth($this->headers['Authorization']);
     if ($authorization->isAuthorized()) {
         // load the application command context:
         $api = null;
         $response = null;
         // GalaxyResponse
         // At this point we know the user has a valid application
         // if they are attempting to access a channel, we need to confirm the channel
         // permissions, if they are accessing the root of their application, they are good
         // to go at this point.
         $context = $this->context_for_realm($authorization->realm);
         $context->origin = $authorization->application;
         $context->origin_description = $authorization->description;
         $context->origin_domain = $authorization->domain;
         if ($context) {
             $api = $this->commandLibraryForType($authorization->instance);
             // format: command_method e.g., channels_get, topics_post, topics_delete
             $method = GalaxyAPI::methodForEndpoint(GalaxyAPI::endpoint());
             if (!$api) {
                 GalaxyResponse::unauthorized();
             }
             // accessing the application
             if (!$context->channel) {
                 if ($context->application == $authorization->application) {
                     if (method_exists($api, $method)) {
                         $response = $api->{$method}($context);
                     } else {
                         GalaxyResponse::unauthorized();
                     }
                     echo $response;
                 } else {
                     GalaxyResponse::unauthorized();
                 }
             } else {
                 $has_permission = false;
                 $db_certificates = GalaxyAPI::database(GalaxyAPIConstants::kDatabaseRedis, GalaxyAPIConstants::kDatabaseCertificates);
                 $permissions = json_decode($db_certificates->get(GalaxyAPIConstants::kTypeCertificate . ':' . $authorization->oauth_consumer_key . ':' . $context->channel));
                 $verb = strtolower($_SERVER['REQUEST_METHOD']);
                 switch ($verb) {
                     case 'get':
                         $has_permission = $permissions & GalaxyAPIConstants::kPermissionRead ? true : false;
                         break;
                     case 'post':
                     case 'put':
                         $has_permission = $permissions & GalaxyAPIConstants::kPermissionWrite ? true : false;
                         break;
                     case 'delete':
                         $has_permission = $permissions & GalaxyAPIConstants::kPermissionDelete ? true : false;
                         break;
                 }
                 if ($has_permission && method_exists($api, $method)) {
                     $log = new GalaxyLog();
                     $log->setEndpoint(GalaxyAPI::endpoint());
                     $log->setContext($context);
                     $log->setMethod($verb);
                     $log->write();
                     $response = $api->{$method}($context);
                 } else {
                     echo GalaxyResponse::unauthorized();
                 }
                 echo $response;
             }
         } else {
             echo GalaxyResponse::unauthorized();
         }
     } else {
         echo "*****";
         echo GalaxyResponse::unauthorized();
     }
 }