Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix configuration attlasian + fix cross headers #7

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 56 additions & 20 deletions application/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

$app = AppFactory::create();


// Add Error Handling Middleware
//$app->addErrorMiddleware(true, true, false);

function procesJsonRequest($request): Request
{
$contentType = $request->getHeaderLine('Content-Type');
Expand All @@ -26,21 +30,43 @@ function procesJsonRequest($request): Request
return $request;
}

function callAtlassian(HandlerInterface $handler, Request $request, Response $response): Response
{
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Access-Control-Allow-Origin' => '*',
];
function callAtlassian(
HandlerInterface $handler,
Request $request,
Response $response,
bool $isOption = false,
string $accept = 'application/json',
string $contentType = 'application/json'
): Response {
$headers = [];
$headers["Access-Control-Allow-Origin"] = "*";
$headers['Access-Control-Allow-Methods'] = "*";
$headers['Access-Control-Allow-Headers'] = 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,Recaptcha-Token,Recaptcha-Action';
$headers['Access-Control-Allow-Credentials'] = "true";
$headers['Access-Control-Max-Age'] = 86400;
$headers['Content-Type'] = $contentType;
$headers['Accept'] = $accept;

if ($isOption) {
return new \GuzzleHttp\Psr7\Response(200, $headers, json_encode([]));
}

try {
$request = procesJsonRequest($request);
return $handler->handle($request);
$response = $handler->handle($request);
return new \GuzzleHttp\Psr7\Response(
$response->getStatusCode(),
$headers,
$response->getBody()
);
} catch (\Exception $e) {
$response = $response->withStatus(500);
$response->getBody()->write($e->getMessage());
return new \GuzzleHttp\Psr7\Response(
500,
$headers,
json_encode([
'error' => $e->getMessage()
])
);
}
return $response;
}

$app->get('/test/ping', function (Request $request, Response $response, $args) {
Expand All @@ -49,30 +75,40 @@ function callAtlassian(HandlerInterface $handler, Request $request, Response $re
return $response;
});

$app->get('/test/auth', function (Request $request, Response $response, $args) {
$app->post('/test/auth', function (Request $request, Response $response, $args) {
$handler = new TestAuthHandler();
return callAtlassian($handler, $request, $response);
return $handler->handle($request);
});

$app->options('/test/auth', function (Request $request, Response $response, $args) {
$handler = new TestAuthHandler();
return $handler->handle($request);
});

$app->get('/issue/search', function (Request $request, Response $response, $args) {
$handler = new GetIssueSearchHandler();
$request = procesJsonRequest($request);
return callAtlassian($handler, $request, $response);
});

$app->post('/issue/idea', function (Request $request, Response $response, $args) {
$handler = new SendIdeaHandler();
return callAtlassian($handler, $request, $response);
return callAtlassian($handler, $request, $response, false, 'multipart/form-data');
});

$app->options('/issue/idea', function (Request $request, Response $response, $args) {
$handler = new SendIdeaHandler();
return callAtlassian($handler, $request, $response, true, 'multipart/form-data');
});

$app->post('/issue/problem', function (Request $request, Response $response, $args) {
$handler = new SendProblemHandler();
return callAtlassian($handler, $request, $response);
return callAtlassian($handler, $request, $response, false, 'multipart/form-data');
});

$app->get('/issue', function (Request $request, Response $response, $args) {
$request = procesJsonRequest($request);
$response->getBody()->write("Hello world!");
return $response;
$app->options('/issue/problem', function (Request $request, Response $response, $args) {
$handler = new SendProblemHandler();
return callAtlassian($handler, $request, $response, true, 'multipart/form-data');
});

$app->run();
72 changes: 45 additions & 27 deletions application/src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,61 @@

namespace App\Handler;

use App\Service\AtlassianApiService;
use App\Traits\RecaptchaTrait;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\UploadedFile;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class AbstractHandler implements HandlerInterface
{
protected function validRecaptcha(?string $token, ?string $action) {
$secretKey = getenv('recaptcha_secret');

if (isset($_POST['email']) && $_POST['email']) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
} else {
// set error message and redirect back to form...
header('location: subscribe_newsletter_form.php');
exit;
}
use RecaptchaTrait;
protected AtlassianApiService $atlassianApi;

// call curl to POST request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('secret' => $secretKey, 'response' => $token)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$arrResponse = json_decode($response, true);

// verify the response
if($arrResponse["success"] == '1' && $arrResponse["action"] == $action && $arrResponse["score"] >= 0.5) {
return true;
} else {
return false;
}
public function __construct()
{
$this->atlassianApi = new AtlassianApiService();
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
return new Response();
}
}

protected function getApiResponseHeaders(): array
{
$headers = [];

$headers["Access-Control-Allow-Origin"] = "*";
$headers['Access-Control-Allow-Methods'] = "*";
$headers['Access-Control-Allow-Headers'] = 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,Recaptcha-Token,Recaptcha-Action';
$headers['Access-Control-Allow-Credentials'] = "true";
$headers['Access-Control-Max-Age'] = 86400;
$headers['Content-Type'] = 'application/json';
$headers['Accept'] = 'application/json';
return $headers;
}

public function getFilesFromRequest(ServerRequestInterface $request): array
{
$uploadedFiles = $request->getUploadedFiles();
$files = [];

/**
* @var UploadedFile $image
*/
foreach ($uploadedFiles as $image) {

if ($image->getError() === UPLOAD_ERR_OK) {
$file = [
"name" => 'file',
"contents" => (string)$image->getStream(),
'filename' => $image->getClientFilename(),
];
$files[] = $file;
}
}
return $files;
}
}
8 changes: 0 additions & 8 deletions application/src/Handler/GetIssueSearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@

namespace App\Handler;

use App\Service\AtlassianApiService;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class GetIssueSearchHandler extends AbstractHandler
{
private AtlassianApiService $atlassianApi;

public function __construct()
{
$this->atlassianApi = new AtlassianApiService();
}

/**
* @throws GuzzleException
*/
Expand Down
82 changes: 82 additions & 0 deletions application/src/Handler/IssueHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace App\Handler;

use App\Model\AtlassianIdeaModel;
use App\Service\AtlassianApiService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\UploadedFile;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class IssueHandler extends AbstractHandler
{
private string $issueClassName = AtlassianIdeaModel::class;

public function getFilesFromRequest(ServerRequestInterface $request): array
{
$uploadedFiles = $request->getUploadedFiles();
$files = [];

/**
* @var UploadedFile $image
*/
foreach ($uploadedFiles as $image) {

if ($image->getError() === UPLOAD_ERR_OK) {
$file = [
"name" => 'file',
"contents" => (string)$image->getStream(),
'filename' => $image->getClientFilename(),
];
$files[] = $file;
}
}
return $files;
}

public function addIssueAttachments(array $files, string $issueId): ?ResponseInterface
{
if (empty($files)) return null;
/* @var AtlassianApiService $attlasianApi */
$tempResponse = $this->atlassianApi->CreateAttachment($files);
$tempResponseData = json_decode($tempResponse->getBody()->getContents(), true);
$attachmentIds = [];
foreach ($tempResponseData['temporaryAttachments'] ?? [] as $attachment) {
$attachmentIds[] = $attachment['temporaryAttachmentId'];
}
return $this->atlassianApi->addIssueAttachments($issueId, $attachmentIds);
}


public function handle(ServerRequestInterface $request): ResponseInterface
{
$data = $request->getParsedBody();
$token = $request->getHeader('Recaptcha-Token')[0] ?? null;
$action = $request->getHeader('Recaptcha-Action')[0] ?? null;

try {
if ($this->validRecaptcha($token, $action)) {
$issueData = new $this->issueClassName();
$issueData->fromArray($data);
$issueResponse = $this->atlassianApi->sendIssue($issueData);

$files = $this->getFilesFromRequest($request);
if ($issueResponse->getStatusCode() === 201 && !empty($files)) {
$issue = json_decode($issueResponse->getBody()->getContents(), true);
$this->addIssueAttachments($files, $issue['issueId']);
}

return $issueResponse;
}
} catch (Exception | GuzzleException $e) {
return new Response(401, [], json_encode([$e->getMessage()]));
}

return new Response(401, [], json_encode(['error' => 'DATA_INVALID']));
}
}
30 changes: 2 additions & 28 deletions application/src/Handler/SendIdeaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,8 @@
namespace App\Handler;

use App\Model\AtlassianIdeaModel;
use App\Service\AtlassianApiService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class SendIdeaHandler extends AbstractHandler
final class SendIdeaHandler extends IssueHandler
{
private AtlassianApiService $atlassianApi;

public function __construct()
{
$this->atlassianApi = new AtlassianApiService();
}

/**
* @throws GuzzleException
* @throws Exception
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$data = $request->getParsedBody();
if ($this->validRecaptcha($data['token'] ?? null, $data['action'] ?? null)) {
$issueData = new AtlassianIdeaModel();
$issueData->fromArray($data);
return $this->atlassianApi->sendIssue($issueData);
}
return new Response(401, [], 'DATA_INVALID');
}
private string $issueClassName = AtlassianIdeaModel::class;
}
32 changes: 3 additions & 29 deletions application/src/Handler/SendProblemHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,8 @@
namespace App\Handler;

use App\Model\AtlassianProblemModel;
use App\Service\AtlassianApiService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class SendProblemHandler extends AbstractHandler
final class SendProblemHandler extends IssueHandler
{
private AtlassianApiService $atlassianApi;

public function __construct()
{
$this->atlassianApi = new AtlassianApiService();
}

/**
* @throws GuzzleException
* @throws Exception
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$data = $request->getParsedBody();
if ($this->validRecaptcha($data['token'] ?? null, $data['action'] ?? null)) {
$problemData = new AtlassianProblemModel();
$problemData->fromArray($data);
return $this->atlassianApi->sendIssue($problemData);
}
return new Response(401, [], 'DATA_INVALID');
}
}
private string $issueClassName = AtlassianProblemModel::class;
}
Loading