1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 09:54:17 +02:00

allow access key to access private demos

This commit is contained in:
Robin Appelman 2025-05-03 15:45:22 +02:00
commit 52b9bc09fd
10 changed files with 37 additions and 52 deletions

View file

@ -34,6 +34,7 @@ class Container {
private Request $request; private Request $request;
private Response $response; private Response $response;
private string $uploadKey; private string $uploadKey;
private string $accessKey;
public function __construct( public function __construct(
Request $request, Request $request,
@ -46,7 +47,8 @@ class Container {
string $storeUrl, string $storeUrl,
string $apiRoot, string $apiRoot,
string $editKey, string $editKey,
string $uploadKey string $uploadKey,
string $accessKey,
) { ) {
$this->request = $request; $this->request = $request;
$this->response = $response; $this->response = $response;
@ -59,6 +61,7 @@ class Container {
$this->apiRoot = $apiRoot; $this->apiRoot = $apiRoot;
$this->editKey = $editKey; $this->editKey = $editKey;
$this->uploadKey = $uploadKey; $this->uploadKey = $uploadKey;
$this->accessKey = $accessKey;
} }
public function getAuthProvider(): AuthProvider { public function getAuthProvider(): AuthProvider {
@ -156,4 +159,8 @@ class Container {
public function getUploadKey(): string { public function getUploadKey(): string {
return $this->uploadKey; return $this->uploadKey;
} }
public function getAccessKey(): string {
return $this->accessKey;
}
} }

View file

@ -34,8 +34,12 @@ class BaseController {
return $this->request->data[$name] ?? $default; return $this->request->data[$name] ?? $default;
} }
protected function getAccessKey(): string {
return Request::getHeader('ACCESS-KEY');
}
protected function getEditKey(): string { protected function getEditKey(): string {
$key = Request::getHeader('EDIT_KEY'); $key = Request::getHeader('EDIT-KEY');
if ($key) { if ($key) {
return $key; return $key;
} }

View file

@ -28,7 +28,8 @@ class DemoController extends BaseController {
ChatProvider $chatProvider, ChatProvider $chatProvider,
DemoListProvider $demoListProvider, DemoListProvider $demoListProvider,
DemoStore $store, DemoStore $store,
string $editKey string $editKey,
string $accessKey,
) { ) {
parent::__construct($request, $response); parent::__construct($request, $response);
$this->demoProvider = $demoProvider; $this->demoProvider = $demoProvider;
@ -36,6 +37,11 @@ class DemoController extends BaseController {
$this->demoListProvider = $demoListProvider; $this->demoListProvider = $demoListProvider;
$this->store = $store; $this->store = $store;
$this->editKey = $editKey; $this->editKey = $editKey;
if ($this->getAccessKey() === $accessKey) {
$this->demoProvider->showPrivateData(true);
$this->demoListProvider->showPrivateData(true);
}
} }
public function get(string $id): void { public function get(string $id): void {

View file

@ -8,6 +8,7 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Query\QueryBuilder;
class BaseProvider { class BaseProvider {
protected bool $showPrivateData = false;
protected Connection $connection; protected Connection $connection;
/** /**
@ -30,4 +31,8 @@ class BaseProvider {
protected function getQueryBuilder() { protected function getQueryBuilder() {
return new QueryBuilder($this->connection); return new QueryBuilder($this->connection);
} }
function showPrivateData(bool $show): void {
$this->showPrivateData = $show;
}
} }

View file

@ -189,7 +189,9 @@ class DemoListProvider extends BaseProvider {
*/ */
protected function formatList(array $rows): array { protected function formatList(array $rows): array {
return array_map(function ($row) { return array_map(function ($row) {
return Demo::fromRow($row); $demo = Demo::fromRow($row);
$demo->showPrivateData($this->showPrivateData);
return $demo;
}, $rows); }, $rows);
} }
} }

View file

@ -43,6 +43,7 @@ class DemoProvider extends BaseProvider {
if (null === $demo) { if (null === $demo) {
return null; return null;
} }
$demo->showPrivateData($this->showPrivateData);
if ($fetchDetails) { if ($fetchDetails) {
$uploader = $this->userProvider->getById($demo->getUploader()); $uploader = $this->userProvider->getById($demo->getUploader());

View file

@ -17,7 +17,8 @@ $demoController = new Controllers\DemoController(
$container->getChatProvider(), $container->getChatProvider(),
$container->getDemoListProvider(), $container->getDemoListProvider(),
$container->getDemoStore(), $container->getDemoStore(),
$container->getEditKey() $container->getEditKey(),
$container->getAccessKey()
); );
$authController = new Controllers\AuthController( $authController = new Controllers\AuthController(
$container->getRequest(), $container->getRequest(),

View file

@ -60,6 +60,7 @@ $parserPath = getEnvVar('PARSER_PATH');
$appRoot = getEnvVar('APP_ROOT'); $appRoot = getEnvVar('APP_ROOT');
$editKey = getEnvVar('EDIT_SECRET'); $editKey = getEnvVar('EDIT_SECRET');
$uploadKey = getEnvVar('UPLOAD_KEY'); $uploadKey = getEnvVar('UPLOAD_KEY');
$accessKey = getEnvVar('ACCESS_KEY');
$factory = new \RandomLib\Factory(); $factory = new \RandomLib\Factory();
$generator = $factory->getMediumStrengthGenerator(); $generator = $factory->getMediumStrengthGenerator();
@ -75,7 +76,8 @@ $container = new Container(
$storeHost, $storeHost,
$appRoot, $appRoot,
$editKey, $editKey,
$uploadKey $uploadKey,
$accessKey,
); );
return $container; return $container;

View file

@ -43,7 +43,8 @@ class DemoControllerTest extends ControllerTest {
$this->chatProvider, $this->chatProvider,
$this->demoListProvider, $this->demoListProvider,
$this->demoStore, $this->demoStore,
'supersecretkey' 'supersecretkey',
'accesskey'
); );
} }

View file

@ -2,58 +2,14 @@
* parser server * parser server
*/ */
var DemoParser = require('tf2-demo'); var DemoParser = require('tf2-demo');
var express = require('express');
var app = express();
var url = require('url');
var https = require('https');
var http = require('http');
app.set('port', (process.env.PORT || 80));
app.use(express.static(__dirname + '/public'));
app.get('/', function (request, response) {
response.send('Hello World!');
});
function handleDataStream(stream, cb) {
var buffers = [];
stream.on('data', function (buffer) {
buffers.push(buffer);
});
stream.on('end', function () {
try {
var buffer = Buffer.concat(buffers);
var demo = DemoParser.Demo.fromNodeBuffer(buffer);
var parser = demo.getParser(true);
var header = parser.readHeader();
var match = parser.parseBody();
var body = match.getState();
body.header = header;
cb(body);
} catch (e) {
cb(e);
}
});
}
app.post('/parse', function (req, res) {
handleDataStream(req, function (body) {
res.set('Content-Type', 'application/json');
res.write(JSON.stringify(body));
res.end();
})
});
app.listen(9123);
const chakram = require('chakram'); const chakram = require('chakram');
const expect = chakram.expect; const expect = chakram.expect;
const root = 'http://localhost:8000/'; const root = 'http://localhost:8000/';
const fs = require('fs'); const fs = require('fs');
process.env.PARSER_URL = `http://localhost:9123/parse`;
process.env.EDIT_SECRET = 'edit_key'; process.env.EDIT_SECRET = 'edit_key';
process.env.ACCESS_KEY = 'access';
chakram.setRequestDefaults({baseUrl: root}); chakram.setRequestDefaults({baseUrl: root});