Skip to content

Commit b378c5b

Browse files
authored
Merge pull request #105 from utopia-php/feat-appwrite-adapter
Feat: Implement AppwriteTablesDB adapter
2 parents 611fa66 + 6520245 commit b378c5b

10 files changed

Lines changed: 535 additions & 90 deletions

File tree

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
APPWRITE_ENDPOINT=
2+
APPWRITE_PROJECT_ID=
3+
APPWRITE_API_KEY=

.github/workflows/tests.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ jobs:
1717

1818
- run: git checkout HEAD^2
1919

20+
- name: Generate .env file
21+
run: |
22+
echo "APPWRITE_ENDPOINT=${{ secrets.APPWRITE_ENDPOINT }}" > .env
23+
echo "APPWRITE_PROJECT_ID=${{ secrets.APPWRITE_PROJECT_ID }}" >> .env
24+
echo "APPWRITE_API_KEY=${{ secrets.APPWRITE_API_KEY }}" >> .env
25+
2026
- name: Build
2127
run: |
2228
export PHP_VERSION=${{ matrix.php-versions }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
2-
/.idea/
2+
/.idea/
3+
.env

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ composer require utopia-php/abuse
2121
The time limit abuse allow each key (action) to be performed [X] times in given time frame.
2222
This adapter uses a MySQL / MariaDB to store usage attempts. Before using it create the table schema as documented in this repository (./data/schema.sql)
2323

24+
### Database adapter
25+
2426
```php
2527
<?php
2628

@@ -66,6 +68,38 @@ if($abuse->check()) {
6668
}
6769
```
6870

71+
### Appwrite TablesDB adapter
72+
73+
```php
74+
<?php
75+
76+
require_once __DIR__ . '/../../vendor/autoload.php';
77+
78+
use Utopia\Abuse\Abuse;
79+
use Utopia\Abuse\Adapters\TimeLimit\Appwrite\TablesDB as TablesDBAdapter;
80+
use Appwrite\Client;
81+
82+
$client = (new Client())
83+
->setEndpoint('[YOUR_ENDPOINT]')
84+
->setProject('[YOUR_PROJECT_ID]')
85+
->setKey('[YOUR_API_KEY]');
86+
$databaseId = 'abuse';
87+
88+
// Limit login attempts to 10 time in 5 minutes time frame
89+
$adapter = new TablesDBAdapter('login-attempt-from-{{ip}}', 10, (60 * 5), $client, $databaseId);
90+
91+
$adapter->setup(); //setup database as required
92+
$adapter->setParam('{{ip}}', '127.0.0.1');
93+
94+
$abuse = new Abuse($adapter);
95+
96+
// Use vars to resolve adapter key
97+
98+
if($abuse->check()) {
99+
throw new Exception('Service was abused!'); // throw error and return X-Rate limit headers here
100+
}
101+
```
102+
69103
**ReCaptcha Abuse**
70104

71105
The ReCaptcha abuse controller is using Google ReCaptcha service to detect when service is being abused by bots.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"ext-pdo": "*",
2323
"ext-curl": "*",
2424
"ext-redis": "*",
25-
"utopia-php/database": "*"
25+
"utopia-php/database": "3.*.*",
26+
"appwrite/appwrite": "18.*.*"
2627
},
2728
"require-dev": {
2829
"phpunit/phpunit": "9.*",

0 commit comments

Comments
 (0)