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:
parent
1593435254
commit
52b9bc09fd
10 changed files with 37 additions and 52 deletions
|
|
@ -34,6 +34,7 @@ class Container {
|
|||
private Request $request;
|
||||
private Response $response;
|
||||
private string $uploadKey;
|
||||
private string $accessKey;
|
||||
|
||||
public function __construct(
|
||||
Request $request,
|
||||
|
|
@ -46,7 +47,8 @@ class Container {
|
|||
string $storeUrl,
|
||||
string $apiRoot,
|
||||
string $editKey,
|
||||
string $uploadKey
|
||||
string $uploadKey,
|
||||
string $accessKey,
|
||||
) {
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
|
|
@ -59,6 +61,7 @@ class Container {
|
|||
$this->apiRoot = $apiRoot;
|
||||
$this->editKey = $editKey;
|
||||
$this->uploadKey = $uploadKey;
|
||||
$this->accessKey = $accessKey;
|
||||
}
|
||||
|
||||
public function getAuthProvider(): AuthProvider {
|
||||
|
|
@ -156,4 +159,8 @@ class Container {
|
|||
public function getUploadKey(): string {
|
||||
return $this->uploadKey;
|
||||
}
|
||||
|
||||
public function getAccessKey(): string {
|
||||
return $this->accessKey;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,8 +34,12 @@ class BaseController {
|
|||
return $this->request->data[$name] ?? $default;
|
||||
}
|
||||
|
||||
protected function getAccessKey(): string {
|
||||
return Request::getHeader('ACCESS-KEY');
|
||||
}
|
||||
|
||||
protected function getEditKey(): string {
|
||||
$key = Request::getHeader('EDIT_KEY');
|
||||
$key = Request::getHeader('EDIT-KEY');
|
||||
if ($key) {
|
||||
return $key;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,8 @@ class DemoController extends BaseController {
|
|||
ChatProvider $chatProvider,
|
||||
DemoListProvider $demoListProvider,
|
||||
DemoStore $store,
|
||||
string $editKey
|
||||
string $editKey,
|
||||
string $accessKey,
|
||||
) {
|
||||
parent::__construct($request, $response);
|
||||
$this->demoProvider = $demoProvider;
|
||||
|
|
@ -36,6 +37,11 @@ class DemoController extends BaseController {
|
|||
$this->demoListProvider = $demoListProvider;
|
||||
$this->store = $store;
|
||||
$this->editKey = $editKey;
|
||||
|
||||
if ($this->getAccessKey() === $accessKey) {
|
||||
$this->demoProvider->showPrivateData(true);
|
||||
$this->demoListProvider->showPrivateData(true);
|
||||
}
|
||||
}
|
||||
|
||||
public function get(string $id): void {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use Doctrine\DBAL\Connection;
|
|||
use Doctrine\DBAL\Query\QueryBuilder;
|
||||
|
||||
class BaseProvider {
|
||||
protected bool $showPrivateData = false;
|
||||
protected Connection $connection;
|
||||
|
||||
/**
|
||||
|
|
@ -30,4 +31,8 @@ class BaseProvider {
|
|||
protected function getQueryBuilder() {
|
||||
return new QueryBuilder($this->connection);
|
||||
}
|
||||
|
||||
function showPrivateData(bool $show): void {
|
||||
$this->showPrivateData = $show;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -189,7 +189,9 @@ class DemoListProvider extends BaseProvider {
|
|||
*/
|
||||
protected function formatList(array $rows): array {
|
||||
return array_map(function ($row) {
|
||||
return Demo::fromRow($row);
|
||||
$demo = Demo::fromRow($row);
|
||||
$demo->showPrivateData($this->showPrivateData);
|
||||
return $demo;
|
||||
}, $rows);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ class DemoProvider extends BaseProvider {
|
|||
if (null === $demo) {
|
||||
return null;
|
||||
}
|
||||
$demo->showPrivateData($this->showPrivateData);
|
||||
|
||||
if ($fetchDetails) {
|
||||
$uploader = $this->userProvider->getById($demo->getUploader());
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ $demoController = new Controllers\DemoController(
|
|||
$container->getChatProvider(),
|
||||
$container->getDemoListProvider(),
|
||||
$container->getDemoStore(),
|
||||
$container->getEditKey()
|
||||
$container->getEditKey(),
|
||||
$container->getAccessKey()
|
||||
);
|
||||
$authController = new Controllers\AuthController(
|
||||
$container->getRequest(),
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ $parserPath = getEnvVar('PARSER_PATH');
|
|||
$appRoot = getEnvVar('APP_ROOT');
|
||||
$editKey = getEnvVar('EDIT_SECRET');
|
||||
$uploadKey = getEnvVar('UPLOAD_KEY');
|
||||
$accessKey = getEnvVar('ACCESS_KEY');
|
||||
|
||||
$factory = new \RandomLib\Factory();
|
||||
$generator = $factory->getMediumStrengthGenerator();
|
||||
|
|
@ -75,7 +76,8 @@ $container = new Container(
|
|||
$storeHost,
|
||||
$appRoot,
|
||||
$editKey,
|
||||
$uploadKey
|
||||
$uploadKey,
|
||||
$accessKey,
|
||||
);
|
||||
|
||||
return $container;
|
||||
|
|
|
|||
|
|
@ -43,7 +43,8 @@ class DemoControllerTest extends ControllerTest {
|
|||
$this->chatProvider,
|
||||
$this->demoListProvider,
|
||||
$this->demoStore,
|
||||
'supersecretkey'
|
||||
'supersecretkey',
|
||||
'accesskey'
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,58 +2,14 @@
|
|||
* parser server
|
||||
*/
|
||||
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 expect = chakram.expect;
|
||||
const root = 'http://localhost:8000/';
|
||||
const fs = require('fs');
|
||||
|
||||
process.env.PARSER_URL = `http://localhost:9123/parse`;
|
||||
process.env.EDIT_SECRET = 'edit_key';
|
||||
process.env.ACCESS_KEY = 'access';
|
||||
|
||||
chakram.setRequestDefaults({baseUrl: root});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue