1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 18:04:08 +02:00

add option to limit uploads to a single key

This commit is contained in:
Robin Appelman 2018-04-24 23:24:47 +02:00
commit 96a9803e30
4 changed files with 68 additions and 12 deletions

View file

@ -34,6 +34,7 @@ class Container {
private $editKey; private $editKey;
private $request; private $request;
private $response; private $response;
private $uploadKey;
public function __construct( public function __construct(
Request $request, Request $request,
@ -45,7 +46,8 @@ class Container {
string $storeRoot, string $storeRoot,
string $storeUrl, string $storeUrl,
string $apiRoot, string $apiRoot,
string $editKey string $editKey,
string $uploadKey
) { ) {
$this->request = $request; $this->request = $request;
$this->response = $response; $this->response = $response;
@ -57,6 +59,7 @@ class Container {
$this->storeUrl = $storeUrl; $this->storeUrl = $storeUrl;
$this->apiRoot = $apiRoot; $this->apiRoot = $apiRoot;
$this->editKey = $editKey; $this->editKey = $editKey;
$this->uploadKey = $uploadKey;
} }
public function getAuthProvider(): AuthProvider { public function getAuthProvider(): AuthProvider {
@ -102,7 +105,8 @@ class Container {
$this->getChatProvider(), $this->getChatProvider(),
$this->getUserProvider(), $this->getUserProvider(),
$this->getDemoProvider() $this->getDemoProvider()
) ),
$this->getUploadKey()
); );
} }
@ -149,4 +153,8 @@ class Container {
public function getResponse(): Response { public function getResponse(): Response {
return $this->response; return $this->response;
} }
public function getUploadKey(): string {
return $this->uploadKey;
}
} }

View file

@ -30,16 +30,18 @@ class UploadProvider extends BaseProvider {
/** @var DemoSaver */ /** @var DemoSaver */
private $demoSaver; private $demoSaver;
private $baseUrl; private $baseUrl;
private $uploadKey;
public function __construct( public function __construct(
Connection $db, Connection $db,
string $baseUrl, string $baseUrl,
HeaderParser $headerParser, HeaderParser $headerParser,
Parser $parser, Parser $parser,
DemoStore $store, DemoStore $store,
UserProvider $userProvider, UserProvider $userProvider,
DemoProvider $demoProvider, DemoProvider $demoProvider,
DemoSaver $demoSaver DemoSaver $demoSaver,
string $uploadKey
) { ) {
parent::__construct($db); parent::__construct($db);
$this->baseUrl = $baseUrl; $this->baseUrl = $baseUrl;
@ -49,11 +51,12 @@ class UploadProvider extends BaseProvider {
$this->userProvider = $userProvider; $this->userProvider = $userProvider;
$this->demoProvider = $demoProvider; $this->demoProvider = $demoProvider;
$this->demoSaver = $demoSaver; $this->demoSaver = $demoSaver;
$this->uploadKey = $uploadKey;
} }
public function upload(string $key, string $red, string $blu, string $name, string $demoFile): string { public function upload(string $key, string $red, string $blu, string $name, string $demoFile): string {
$user = $this->userProvider->byKey($key); $user = $this->userProvider->byKey($key);
if (!$user) { if (!$user || ($this->uploadKey !== '' && $this->uploadKey !== $key)) {
return 'Invalid key'; return 'Invalid key';
} }

View file

@ -28,6 +28,7 @@ $storeHost = getenv('DEMO_HOST') ?: '';
$parserUrl = getenv('PARSER_URL') ?: ''; $parserUrl = getenv('PARSER_URL') ?: '';
$appRoot = getenv('APP_ROOT') ?: ''; $appRoot = getenv('APP_ROOT') ?: '';
$editKey = getenv('EDIT_SECRET') ?: ''; $editKey = getenv('EDIT_SECRET') ?: '';
$uploadKey = getenv('UPLOAD_KEY') ?: '';
$factory = new \RandomLib\Factory(); $factory = new \RandomLib\Factory();
$generator = $factory->getMediumStrengthGenerator(); $generator = $factory->getMediumStrengthGenerator();
@ -42,7 +43,8 @@ $container = new Container(
$storeRoot, $storeRoot,
$storeHost, $storeHost,
$appRoot, $appRoot,
$editKey $editKey,
$uploadKey
); );
return $container; return $container;

View file

@ -81,7 +81,8 @@ class UploadProviderTest extends TestCase {
$this->demoStore, $this->demoStore,
$this->userProvider, $this->userProvider,
$this->demoProvider, $this->demoProvider,
$this->demoSaver $this->demoSaver,
''
); );
} }
@ -306,4 +307,46 @@ class UploadProviderTest extends TestCase {
$this->assertEquals(0, $demo->getBlueScore()); $this->assertEquals(0, $demo->getBlueScore());
$this->assertEquals(3, $demo->getRedScore()); $this->assertEquals(3, $demo->getRedScore());
} }
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Not an HL2 demo
*/
public function testUploadKey() {
$uploadProvider = new UploadProvider(
$this->getDatabaseConnection(),
'http://example.com',
$this->headerParser,
$this->parser,
$this->demoStore,
$this->userProvider,
$this->demoProvider,
$this->demoSaver,
'foo'
);
$steamId = $this->getSteamId('123', 'a');
$token = $this->userProvider->store($steamId);
$this->assertEquals(
'Invalid key',
$uploadProvider->upload($token, 'RED', 'BLU', 'asdasd', 'asdas')
);
$uploadProvider = new UploadProvider(
$this->getDatabaseConnection(),
'http://example.com',
$this->headerParser,
$this->parser,
$this->demoStore,
$this->userProvider,
$this->demoProvider,
$this->demoSaver,
$token
);
file_put_contents($this->tmpDir . '/foo.dem', 'asd');
$uploadProvider->upload($token, 'RED', 'BLU', 'asdasd', $this->tmpDir . '/foo.dem');
}
} }