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

View file

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

View file

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

View file

@ -81,7 +81,8 @@ class UploadProviderTest extends TestCase {
$this->demoStore,
$this->userProvider,
$this->demoProvider,
$this->demoSaver
$this->demoSaver,
''
);
}
@ -306,4 +307,46 @@ class UploadProviderTest extends TestCase {
$this->assertEquals(0, $demo->getBlueScore());
$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');
}
}