Skip to content

Commit

Permalink
Merge pull request #1998 from Shruti3004/master
Browse files Browse the repository at this point in the history
feat(restAPI): Added options request and verification function

Reviewed-by: mishra.gaurav@siemens.com
Tested-by: mishra.gaurav@siemens.com
  • Loading branch information
GMishx committed May 25, 2021
2 parents 84a1897 + 1d7f5f9 commit e779fb0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/lib/php/common-sysconfig.php
Expand Up @@ -211,6 +211,13 @@ function Populate_sysconfig()
"group_order", "description", "validation_function", "option_value");
$valueArray = array();

/* CorsOrigin */
$variable = "CorsOrigins";
$prompt = _('Allowed origins for REST API');
$desc = _('[scheme]://[hostname]:[port], "*" for anywhere');
$valueArray[$variable] = array("'$variable'", "'localhost:3000'", "'$prompt'",
strval(CONFIG_TYPE_TEXT), "'PAT'", "3", "'$desc'", "null", "null");

/* Email */
$variable = "SupportEmailLabel";
$supportEmailLabelPrompt = _('Support Email Label');
Expand Down
10 changes: 9 additions & 1 deletion src/www/ui/api/Controllers/AuthController.php
Expand Up @@ -55,7 +55,15 @@ public function getAuthHeaders($request, $response, $args)
return $response->withHeader('Warning', $warningMessage)->withJson(
$returnVal->getArray(), $returnVal->getCode());
}

public function optionsVerification($request, $response, $args)
{
global $SysConf;
return $response->withStatus(204)
->withHeader('Access-Control-Allow-Origin', $SysConf['SYSCONFIG']['CorsOrigins'])
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS')
->withHeader('Access-Control-Allow-Credentials', 'true');
}
/**
* Get the JWT authentication headers for the user
*
Expand Down
4 changes: 3 additions & 1 deletion src/www/ui/api/Middlewares/RestAuthMiddleware.php
Expand Up @@ -49,7 +49,9 @@ class RestAuthMiddleware
public function __invoke($request, $response, $next)
{
$requestUri = $request->getUri();
if (stristr($requestUri->getPath(), "/auth") !== false) {
if (stristr($request->getMethod(), "options") !== false) {
$response = $next($request, $response);
} elseif (stristr($requestUri->getPath(), "/auth") !== false) {
$response = $next($request, $response);
} elseif (stristr($requestUri->getPath(), "/version") !== false) {
$response = $next($request, $response);
Expand Down
3 changes: 3 additions & 0 deletions src/www/ui/api/index.php
Expand Up @@ -97,6 +97,9 @@
// Middleware for authentication
$app->add(new RestAuthMiddleware());

//////////////////////////OPTIONS/////////////////////
$app->options(VERSION_1 . '{routes:.+}', AuthController::class . ':optionsVerification');

//////////////////////////AUTH/////////////////////
$app->get(VERSION_1 . 'auth', AuthController::class . ':getAuthHeaders');
$app->post(VERSION_1 . 'tokens', AuthController::class . ':createNewJwtToken');
Expand Down

0 comments on commit e779fb0

Please sign in to comment.