Beispiel #1
0
 function api_validate_form1($action, &$flowScope)
 {
     $registry = Zend_Registry::getInstance();
     $translate = $registry->get("Zend_Translate");
     /**
      * @var Api $api
      */
     $api = $flowScope["api"];
     $validationErrors = array();
     if (isset($_POST["apiName"]) && !empty($_POST["apiName"])) {
         $api->displayName = $_POST["apiName"];
     } else {
         $validationErrors["name"] = $translate->translate("Apis must have a name.");
     }
     if (isset($_POST["apiEndpoint"]) && !empty($_POST["apiEndpoint"])) {
         $api->endpoint = $_POST["apiEndpoint"];
     } else {
         $validationErrors["endpoint"] = $translate->translate("Apis must have an endpoint.");
     }
     $targetHosts = array();
     foreach ($_POST as $k => $v) {
         if (preg_match('/^targethost[0-9]+$/', $k) && !empty($v)) {
             $th = new TargetHost();
             $th->url = $v;
             $targetHosts[] = $th;
         }
     }
     if (empty($targetHosts)) {
         $validationErrors["targethost0"] = $translate->translate("Apis must have at least one targethost.");
     } else {
         $contexts = $api->getContexts();
         if (empty($contexts)) {
             $contexts = array(new ApiContext());
         }
         $context = $contexts[0];
         $context->setStatus(Status::$ACTIVE);
         $context->targetHosts = array();
         $i = 0;
         foreach ($targetHosts as $th) {
             $isbad = $this->target_host_is_bad($th);
             if ($isbad) {
                 $validationErrors["targethost" . $i] = $isbad;
             }
             $context->targetHosts[] = $th;
         }
         $api->setContexts($contexts);
     }
     if ($_POST["apienabled"]) {
         $api->setStatus(Status::$ACTIVE);
     } else {
         $api->setStatus(Status::$INACTIVE);
     }
     $authTypes = array();
     $authkeykey = null;
     foreach ($_POST as $k => $v) {
         $matches = array();
         if (preg_match('/^auth-(\\w*)$/', $k, $matches) && $v == 1) {
             $authType = isset($matches[1]) ? AuthType::fromString($matches[1]) : null;
             if (!empty($authType)) {
                 $authTypes[] = $authType;
                 if ($authType === AuthType::$AUTHKEY) {
                     $authkeykey = $_POST["authkey-key"];
                     $isbad = $this->auth_key_key_is_bad($authkeykey);
                     if ($isbad) {
                         $validationErrors["authkey-key"] = $isbad;
                     }
                 }
             }
         }
     }
     if (!empty($authTypes)) {
         $provAuth = new ProvisionAuthentication();
         $provAuth->setAuths($authTypes);
         $provAuth->setAuthKey($authkeykey);
         $api->setAuthentication($provAuth);
     } else {
         $validationErrors["auth"] = $translate->translate("Apis must have at least one auth type.");
     }
     if ($_POST["https"]) {
         $https = new HTTPSType();
         $https->setEnabled("true");
         $https_mode = $_POST["https-mode"];
         if (empty($https_mode) || TLSMode::fromString($https_mode) === null) {
             $validationErrors["https-mode"] = $translate->translate("With https on, TLS Mode must be 1way or 2way");
         } else {
             $https->setTlsMode(TLSMode::fromString($https_mode));
         }
         $api->setHttps($https);
     } else {
         $https = new HTTPSType();
         $https->setEnabled(false);
         $api->setHttps($https);
     }
     foreach (array("tps-warn", "tps-threshold", "tpm-warn", "tpm-threshold") as $tpx) {
         if (isset($_POST[$tpx])) {
             if (is_numeric($_POST[$tpx])) {
                 $contexts = $api->getContexts();
                 /**
                  * @var ApiContext $context
                  */
                 $context = $contexts[0];
                 switch ($tpx) {
                     case "tps-warn":
                         $context->setMaxRateLimitTPSWarning($_POST[$tpx]);
                         break;
                     case "tps-threshold":
                         $context->setMaxRateLimitTPSThreshold($_POST[$tpx]);
                         break;
                     case "tpm-warn":
                         $context->setMaxRateLimitTPMWarning($_POST[$tpx]);
                         break;
                     case "tpm-threshold":
                         $context->setMaxRateLimitTPMThreshold($_POST[$tpx]);
                         break;
                 }
             } else {
                 switch ($tpx) {
                     case "tps-warn":
                         $validationErrors[$tpx] = $translate->translate("Transactions-per-second warning trigger must be a number");
                         break;
                     case "tps-threshold":
                         $validationErrors[$tpx] = $translate->translate("Transactions-per-second cutoff threshold must be a number");
                         break;
                     case "tpm-warn":
                         $validationErrors[$tpx] = $translate->translate("Transactions-per-minute warning trigger must be a number");
                         break;
                     case "tpm-threshold":
                         $validationErrors[$tpx] = $translate->translate("Transactions-per-minute cutoff threshold must be a number");
                         break;
                 }
             }
         }
     }
     $methods = $api->getAllowedHttpMethods();
     $methods = array_diff($methods, array("GET"));
     if ($_POST["method-get"]) {
         $methods[] = "GET";
     }
     $methods = array_diff($methods, array("POST"));
     if ($_POST["method-post"]) {
         $methods[] = "POST";
     }
     $methods = array_diff($methods, array("PUT"));
     if ($_POST["method-put"]) {
         $methods[] = "PUT";
     }
     $methods = array_diff($methods, array("DELETE"));
     if ($_POST["method-delete"]) {
         $methods[] = "DELETE";
     }
     $api->setAllowedHttpMethods($methods);
     $headerTransformations = SharedViewUtility::deserializeHeaderTransformations($this->getRequest());
     $api->setHeaderTransformations($headerTransformations);
     $api->setHeaderTransformationEnabled(count($headerTransformations) > 0);
     SharedViewUtility::validateHeaderTransformations($api->getHeaderTransformations(), $validationErrors);
     $properties = SharedViewUtility::deserializeProperties($this->getRequest());
     $api->setProperties($properties);
     SharedViewUtility::validateProperties($properties, $validationErrors);
     $tdrsenabled = (bool) $_POST["tdrsenabled"];
     $api->setTdrEnabled($tdrsenabled);
     $tdrRules = SharedViewUtility::deserializeTdrRules($this->getRequest());
     $api->setTdrData($tdrRules);
     SharedViewUtility::validateTdrRules($tdrRules, $validationErrors);
     // If I don't have access to the view, set error messages in the flow scope
     $flowScope["validationErrors"] = $validationErrors;
     $flowScope['gotoAuthsubflow'] = $action === "submitAndAuth";
     return count($validationErrors) === 0 ? "valid" : "invalid";
 }
Beispiel #2
0
         if ($policy === null) {
             $policy = $this->policyManager->getPolicy($id);
             $flowScope['isNew'] = false;
             $flowScope["relatedProperties"] = JsonPropertyPrinter::getRelatedFromPolicy($policy);
         }
     }
     /**
      * Put the policy in the flow scope for reference by other states and the views
      */
     $flowScope['policy'] = $policy;
 }
 /**
  * On-exit callback for the "form" state
  * it should take the form submission and deserialize it into a Policy object and
  * stick it on the flowScope.
  *
  * @param $action
  * @param $flowScope
  */
 public function deserializeForm($action, &$flowScope)
 {
     /**
      * @var Policy $policy
      */
     $policy = $flowScope['policy'];
     // Only accept the id if we are creating a new one
     if ($flowScope['isNew']) {
         $policy->id = $this->_getParam('policy_id');
     }
     $policy->setApiIds($this->getSelectedApis());
     $policy->setAuthIds($this->getSelectedAuths());
     $context = $this->getContextAndRates();
     if ($context != null) {
         $contexts[] = $context;
         $policy->setContexts($contexts);
     }
     $policy->headerTransformations = SharedViewUtility::deserializeHeaderTransformations($this->getRequest());
Beispiel #3
0
 /**
  * on-enter callback for the "submit" state.
  * Should validate the data and then submit the data to AG
  *
  * @param $action
  * @param $flowScope
  */
 public function validateFormAndSubmit($action, &$flowScope)
 {
     $registry = Zend_Registry::getInstance();
     $translate = $registry->get("Zend_Translate");
     $validationErrors = array();
     $isNew = $flowScope['isNew'];
     /**
      * @var Auth $auth
      */
     $auth = $flowScope['auth'];
     $howMany = $flowScope['howMany'];
     if ($isNew && empty($howMany)) {
         $validationErrors['howMany'] = "Please choose how many Auths you would like";
     }
     $shouldValidateCreds = $isNew && $howMany === "1" || !$isNew;
     if ($shouldValidateCreds && $auth->type === AuthType::$AUTHKEY && empty($auth->authKeyAuth->keyValue)) {
         $validationErrors['authKey'] = $translate->translate("For authKey auth, you must specify a key");
     }
     if ($shouldValidateCreds && ($auth->type === AuthType::$BASIC || $auth->type === AuthType::$WSSE) && empty($auth->basicAuth->username) && empty($auth->wsseAuth->username)) {
         $validationErrors['username'] = $translate->translate("Username is required");
     }
     if ($shouldValidateCreds && ($auth->type === AuthType::$BASIC || $auth->type === AuthType::$WSSE) && empty($auth->basicAuth->password) && empty($auth->wsseAuth->password)) {
         $validationErrors['password'] = $translate->translate("Password is required");
     }
     if ($shouldValidateCreds && $auth->type === AuthType::$IPWHITELIST && empty($auth->ipWhiteListAuth->ips)) {
         $validationErrors['ipWhiteList'] = $translate->translate("IP list is required");
     }
     SharedViewUtility::validateHeaderTransformations($auth->headerTransformations, $validationErrors);
     SharedViewUtility::validateProperties($auth->properties, $validationErrors);
     SharedViewUtility::validateTdrRules($auth->tdrData, $validationErrors);
     if (count($validationErrors) > 0) {
         $flowScope['validationErrors'] = $validationErrors;
         return "invalid";
     }
     /**
      * Submit to AG
      */
     $authManager = new AuthManager();
     $creds = array();
     for ($i = 0; $i < $howMany; $i++) {
         // If we are doing a batch create, then generate the credentials
         if ($howMany > 1) {
             switch ($auth->type) {
                 case AuthType::$AUTHKEY:
                     $auth->authKeyAuth->keyValue = uniqid();
                     break;
                 case AuthType::$BASIC:
                     $auth->basicAuth->username = uniqid();
                     $auth->basicAuth->password = uniqid();
                     break;
                 case AuthType::$WSSE:
                     $auth->wsseAuth->username = uniqid();
                     $auth->wsseAuth->password = uniqid();
                     break;
             }
             $auth->id = "";
         }
         $result = $authManager->setAuth($auth, $isNew);
         if ($result->getHTTPCode() !== "200") {
             $xml = simplexml_load_string($result->getPayload());
             $validationErrors['default'] = (string) $xml->error->errorText;
             $flowScope['validationErrors'] = $validationErrors;
             return "invalid";
         }
         // Pull the credentials out for display
         switch ($auth->type) {
             case AuthType::$AUTHKEY:
                 $creds[$auth->id] = "key: " . $auth->authKeyAuth->keyValue;
                 break;
             case AuthType::$BASIC:
                 $creds[$auth->id] = "u/p: " . $auth->basicAuth->username . " / " . $auth->basicAuth->password;
                 break;
             case AuthType::$WSSE:
                 $creds[$auth->id] = "u/p: " . $auth->wsseAuth->username . " / " . $auth->wsseAuth->password;
                 break;
             case AuthType::$IPWHITELIST:
                 $creds[$auth->id] = implode("; ", $auth->ipWhiteListAuth->ips);
                 break;
         }
     }
     if ($isNew) {
         $this->_helper->FlashMessenger("Successfully Created Auth(s)");
         foreach ($creds as $key => $value) {
             $this->_helper->FlashMessenger("{$key} ({$value})");
         }
     } else {
         $this->_helper->FlashMessenger("Successfully Updated Auth");
     }
     $flowScope['authIds'] = array_keys($creds);
     return "valid";
 }