Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
"test:unit": "phpunit -c tests/phpunit.xml tests/Unit",
"test:unit:dev": "phpunit -c tests/phpunit.xml tests/Unit --no-coverage",
"post-install-cmd": ["@composer bin all install --ansi"],
"psalm": "psalm --threads=1",
"psalm:update-baseline": "psalm --threads=1 --update-baseline",
"psalm:update-baseline:force": "psalm --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml",
"psalm:clear": "psalm --clear-cache && psalm --clear-global-cache"
"psalm": "psalm.phar --threads=1",
"psalm:fix": "psalm.phar --no-cache --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType,MissingOverrideAttribute",
"psalm:update-baseline": "psalm.phar --threads=1 --update-baseline",
"psalm:update-baseline:force": "psalm.phar --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml",
"psalm:clear": "psalm.phar --clear-cache && psalm --clear-global-cache"
},
"config": {
"sort-packages": true,
Expand Down
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
}

#[\Override]
public function register(IRegistrationContext $context): void {
include_once __DIR__ . '/../../vendor/autoload.php';

Expand All @@ -39,6 +40,7 @@ public function register(IRegistrationContext $context): void {
$context->registerNotifierService(Notifier::class);
}

#[\Override]
public function boot(IBootContext $context): void {
$context->injectFn(function (IEventDispatcher $dispatcher) {
$loginHookAdapter = new class($dispatcher) {
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJob/ETLJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(ETLService $etlService,
*
* @return void
*/
#[\Override]
protected function run($argument) {
$this->etlService->extractAndTransform();
}
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJob/TrainIpV4OnceJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(
*
* @return void
*/
#[\Override]
protected function run($argument) {
try {
$strategy = new Ipv4Strategy();
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJob/TrainIpV6OnceJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(
*
* @return void
*/
#[\Override]
protected function run($argument) {
try {
$strategy = new IpV6Strategy();
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJob/TrainJobIpV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct(
*
* @return void
*/
#[\Override]
protected function run($argument) {
try {
$strategy = new Ipv4Strategy();
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJob/TrainJobIpV6.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct(
*
* @return void
*/
#[\Override]
protected function run($argument) {
try {
$strategy = new IpV6Strategy();
Expand Down
1 change: 1 addition & 0 deletions lib/Db/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Model extends Entity implements JsonSerializable {
protected $createdAt;
protected $addressType;

#[\Override]
#[ReturnTypeWillChange]
public function jsonSerialize(): array {
return [
Expand Down
1 change: 1 addition & 0 deletions lib/Listener/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(IRequest $request,
$this->loginClassifier = $loginClassifier;
}

#[\Override]
public function handle(Event $event): void {
if (!($event instanceof PostLoginEvent)) {
// Unrelated
Expand Down
1 change: 1 addition & 0 deletions lib/Listener/LoginMailListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __construct(
) {
}

#[\Override]
public function handle(Event $event): void {
if (!($event instanceof SuspiciousLoginEvent)) {
return;
Expand Down
1 change: 1 addition & 0 deletions lib/Listener/LoginNotificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(
) {
}

#[\Override]
public function handle(Event $event): void {
if (!($event instanceof SuspiciousLoginEvent)) {
return;
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version0Date20181127130604.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Version0Date20181127130604 extends SimpleMigrationStep {
* @param array $options
* @return ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version0Date20181213090142.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Version0Date20181213090142 extends SimpleMigrationStep {
*
* @return ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version0Date20181214080456.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Version0Date20181214080456 extends SimpleMigrationStep {
*
* @return ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version0Date20181214083108.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Version0Date20181214083108 extends SimpleMigrationStep {
*
* @return ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version0Date20181214103754.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Version0Date20181214103754 extends SimpleMigrationStep {
*
* @return ISchemaWrapper|null
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version0Date20190115134303.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Version0Date20190115134303 extends SimpleMigrationStep {
*
* @return ISchemaWrapper|null
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version0Date20190121184304.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Version0Date20190121184304 extends SimpleMigrationStep {
*
* @return ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version0Date20190123142603.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Version0Date20190123142603 extends SimpleMigrationStep {
*
* @return ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version2Date20190906100917.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Version2Date20190906100917 extends SimpleMigrationStep {
*
* @return ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version2Date20190913144908.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Version2Date20190913144908 extends SimpleMigrationStep {
*
* @return ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
2 changes: 2 additions & 0 deletions lib/Migration/Version4002Date20220922094803.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(IDBConnection $connection) {
*
* @return ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down Expand Up @@ -69,6 +70,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
return $schema;
}

#[\Override]
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version4002Date20220922094804.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Version4002Date20220922094804 extends SimpleMigrationStep {
*
* @return null|ISchemaWrapper
*/
#[\Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
Expand Down
1 change: 1 addition & 0 deletions lib/Migration/Version9000Date20250114095826.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
) {
}

#[\Override]
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
$this->jobList->add(TrainIpV4OnceJob::class);
$this->jobList->add(TrainIpV6OnceJob::class);
Expand Down
3 changes: 3 additions & 0 deletions lib/Notifications/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ public function __construct(IFactory $factory, IRequest $request, IConfig $confi
$this->url = $urlGenerator;
}

#[\Override]
public function getID(): string {
return Application::APP_ID;
}

#[\Override]
public function getName(): string {
return $this->factory->get(Application::APP_ID)->t('Suspicious Login');
}

#[\Override]
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== Application::APP_ID) {
// Not my app => throw
Expand Down
6 changes: 5 additions & 1 deletion lib/Service/AClassificationStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ protected function uidAsFeatureVector(string $uid): array {
return $this->numStringsToBitArray($splitHash, 16, 4);
}

protected function numStringsToBitArray(array $strings, $base, $padding): array {
/**
* @psalm-param 10|16 $base
* @psalm-param 4|8 $padding
*/
protected function numStringsToBitArray(array $strings, int $base, int $padding): array {
$converted = array_reduce(array_map(function (string $s) use ($base, $padding) {
return $this->numStringToBitArray($s, $base, $padding);
}, $strings), 'array_merge', []);
Expand Down
7 changes: 7 additions & 0 deletions lib/Service/IpV6Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@
use function substr;

class IpV6Strategy extends AClassificationStrategy {
#[\Override]
public static function getTypeName(): string {
return 'ipv6';
}

#[\Override]
public function hasSufficientData(LoginAddressAggregatedMapper $loginAddressMapper, int $validationDays): bool {
return $loginAddressMapper->hasSufficientIpV6Data($validationDays);
}

#[\Override]
public function findHistoricAndRecent(LoginAddressAggregatedMapper $loginAddressMapper, int $validationThreshold, int $maxAge): array {
return $loginAddressMapper->findHistoricAndRecentIpv6($validationThreshold, $maxAge);
}

#[\Override]
protected function ipToVec(string $ip): array {
$addr = inet_pton($ip);
if ($addr === false) {
Expand All @@ -54,16 +58,19 @@ function (string $bit) {
);
}

#[\Override]
public function generateRandomIp(): string {
return implode(':', array_map(function (int $index) {
return base_convert((string)random_int(0, 2 ** 16 - 1), 10, 16);
}, range(0, 7)));
}

#[\Override]
public function getSize(): int {
return 16 + 64;
}

#[\Override]
public function getDefaultMlpConfig(): Config {
return Config::default()->setEpochs(20);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/Service/Ipv4Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,46 @@
use function explode;

class Ipv4Strategy extends AClassificationStrategy {
#[\Override]
public static function getTypeName(): string {
return 'ipv4';
}

#[\Override]
public function hasSufficientData(LoginAddressAggregatedMapper $loginAddressMapper, int $validationDays): bool {
return $loginAddressMapper->hasSufficientIpV4Data($validationDays);
}

#[\Override]
public function findHistoricAndRecent(LoginAddressAggregatedMapper $loginAddressMapper, int $validationThreshold, int $maxAge): array {
return $loginAddressMapper->findHistoricAndRecentIpv4($validationThreshold, $maxAge);
}

#[\Override]
public function generateRandomIpVector(): array {
$ip = $this->generateRandomIp();
$splitIp = explode('.', $ip);
return $this->numStringsToBitArray($splitIp, 10, 8);
}

#[\Override]
protected function ipToVec(string $ip): array {
return $this->numStringsToBitArray(explode('.', $ip), 10, 8);
}

#[\Override]
public function generateRandomIp(): string {
return implode('.', array_map(function (int $index) {
return random_int(0, 255);
}, range(0, 3)));
}

#[\Override]
public function getSize(): int {
return 16 + 32;
}

#[\Override]
public function getDefaultMlpConfig(): Config {
return Config::default();
}
Expand Down
1 change: 1 addition & 0 deletions lib/Service/Statistics/AppStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function getTrainingDataStatistics(): TrainingDataStatistics {
return $this->trainingDataStatistics;
}

#[\Override]
#[ReturnTypeWillChange]
public function jsonSerialize(): array {
return [
Expand Down
1 change: 1 addition & 0 deletions lib/Service/Statistics/TrainingDataStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function getLoginsAggregated(): int {
return $this->loginsAggregated;
}

#[\Override]
#[ReturnTypeWillChange]
public function jsonSerialize(): array {
return [
Expand Down
1 change: 1 addition & 0 deletions lib/Service/TrainingDataConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function setNow(int $now): TrainingDataConfig {
return $clone;
}

#[\Override]
#[ReturnTypeWillChange]
public function jsonSerialize(): array {
return [
Expand Down
1 change: 1 addition & 0 deletions lib/Task/TrainTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(Config $config,
$this->strategy = $strategy;
}

#[\Override]
public function run(Environment $environment) {
// TODO: only works if the app is placed into a sub-sub directory of Nextcloud
require_once __DIR__ . '/../../../../lib/base.php';
Expand Down
9 changes: 9 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
errorLevel="4"
findUnusedBaselineEntry="true"
findUnusedCode="false"
strictBinaryOperands="false"
phpVersion="8.2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -24,11 +25,19 @@
<extraFiles>
<directory name="vendor" />
<directory name="vendor-bin/psalm/vendor/nextcloud/ocp" />
<directory name="vendor-bin/psalm/vendor/psr" />
</extraFiles>
<stubs>
<file name="tests/stubs/doctrine_dbal_types_type.php" />
</stubs>
<issueHandlers>
<UndefinedClass>
<errorLevel type="suppress">
<referencedClass name="Symfony\Component\Console\Command\Command" />
<referencedClass name="Symfony\Component\Console\Output\OutputInterface" />
<referencedClass name="Symfony\Component\Console\Helper\ProgressBar" />
</errorLevel>
</UndefinedClass>
<UndefinedDocblockClass>
<errorLevel type="suppress">
<referencedClass name="Doctrine\DBAL\Driver\Statement" />
Expand Down
Loading
Loading