aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public/AppFramework/Http
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2023-04-21 15:08:55 +0200
committerJoas Schilling <coding@schilljs.com>2023-04-24 12:24:48 +0200
commit89c3c3140210c854fd1bee7507288ba216495220 (patch)
tree3f517b7714661a64dd986ae074f59077d2471194 /lib/public/AppFramework/Http
parent95c098170defa9362d09915a22a11e67cbead1b8 (diff)
downloadnextcloud-server-89c3c3140210c854fd1bee7507288ba216495220.tar.gz
nextcloud-server-89c3c3140210c854fd1bee7507288ba216495220.zip
feat(ratelimit): Add Attributes support to rate limit middleware
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/public/AppFramework/Http')
-rw-r--r--lib/public/AppFramework/Http/Attribute/ARateLimit.php57
-rw-r--r--lib/public/AppFramework/Http/Attribute/AnonRateLimit.php38
-rw-r--r--lib/public/AppFramework/Http/Attribute/UserRateLimit.php38
3 files changed, 133 insertions, 0 deletions
diff --git a/lib/public/AppFramework/Http/Attribute/ARateLimit.php b/lib/public/AppFramework/Http/Attribute/ARateLimit.php
new file mode 100644
index 00000000000..1a779d07518
--- /dev/null
+++ b/lib/public/AppFramework/Http/Attribute/ARateLimit.php
@@ -0,0 +1,57 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCP\AppFramework\Http\Attribute;
+
+/**
+ * Attribute for controller methods that want to limit the times a logged-in
+ * user can call the endpoint in a given time period.
+ *
+ * @since 27.0.0
+ */
+abstract class ARateLimit {
+ /**
+ * @since 27.0.0
+ */
+ public function __construct(
+ protected int $limit,
+ protected int $period,
+ ) {
+ }
+
+ /**
+ * @since 27.0.0
+ */
+ public function getLimit(): int {
+ return $this->limit;
+ }
+
+ /**
+ * @since 27.0.0
+ */
+ public function getPeriod(): int {
+ return $this->period;
+ }
+}
diff --git a/lib/public/AppFramework/Http/Attribute/AnonRateLimit.php b/lib/public/AppFramework/Http/Attribute/AnonRateLimit.php
new file mode 100644
index 00000000000..039a2022ebb
--- /dev/null
+++ b/lib/public/AppFramework/Http/Attribute/AnonRateLimit.php
@@ -0,0 +1,38 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCP\AppFramework\Http\Attribute;
+
+use Attribute;
+
+/**
+ * Attribute for controller methods that want to limit the times a not logged-in
+ * guest can call the endpoint in a given time period.
+ *
+ * @since 27.0.0
+ */
+#[Attribute(Attribute::TARGET_METHOD)]
+class AnonRateLimit extends ARateLimit {
+}
diff --git a/lib/public/AppFramework/Http/Attribute/UserRateLimit.php b/lib/public/AppFramework/Http/Attribute/UserRateLimit.php
new file mode 100644
index 00000000000..e34551ea256
--- /dev/null
+++ b/lib/public/AppFramework/Http/Attribute/UserRateLimit.php
@@ -0,0 +1,38 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCP\AppFramework\Http\Attribute;
+
+use Attribute;
+
+/**
+ * Attribute for controller methods that want to limit the times a logged-in
+ * user can call the endpoint in a given time period.
+ *
+ * @since 27.0.0
+ */
+#[Attribute(Attribute::TARGET_METHOD)]
+class UserRateLimit extends ARateLimit {
+}