AzuraCast/src/Console/Command/Internal/SftpAuthCommand.php

58 lines
1.8 KiB
PHP
Raw Normal View History

<?php
2021-07-19 05:53:45 +00:00
declare(strict_types=1);
namespace App\Console\Command\Internal;
use App\Console\Command\CommandAbstract;
use App\Entity\SftpUser;
use Brick\Math\BigInteger;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
2020-07-08 07:03:50 +00:00
use const JSON_NUMERIC_CHECK;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
class SftpAuthCommand extends CommandAbstract
{
public function __invoke(
SymfonyStyle $io,
EntityManagerInterface $em
): int {
2021-07-19 05:53:45 +00:00
$username = getenv('SFTPGO_AUTHD_USERNAME') ?: null;
$password = getenv('SFTPGO_AUTHD_PASSWORD') ?: null;
$pubKey = getenv('SFTPGO_AUTHD_PUBLIC_KEY') ?: null;
2021-06-08 06:40:49 +00:00
$sftpUser = $em->getRepository(SftpUser::class)->findOneBy(['username' => $username]);
if ($sftpUser instanceof SftpUser && $sftpUser->authenticate($password, $pubKey)) {
2021-06-08 06:40:49 +00:00
$storageLocation = $sftpUser->getStation()->getMediaStorageLocation();
$quotaRaw = $storageLocation->getStorageQuotaBytes();
$quota = ($quotaRaw instanceof BigInteger)
? (string)$quotaRaw
: 0;
$row = [
'status' => 1,
'username' => $sftpUser->getUsername(),
'expiration_date' => 0,
'home_dir' => $storageLocation->getPath(),
'uid' => 0,
'gid' => 0,
'quota_size' => $quota,
'permissions' => [
'/' => ['*'],
],
];
2020-07-08 07:03:50 +00:00
$io->write(json_encode($row, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK));
return 0;
}
2020-07-08 07:03:50 +00:00
$io->write(json_encode(['username' => ''], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES));
return 1;
}
}