Forráskód Böngészése

add option `hostname` to create alias API endpoint

Lawrence Thorpe 3 éve
szülő
commit
28678e407a

+ 20 - 4
app/Http/Controllers/Api/AliasController.php

@@ -11,6 +11,8 @@ use App\Models\Domain;
 use App\Models\Username;
 use Illuminate\Support\Str;
 use Ramsey\Uuid\Uuid;
+use Pdp\Rules;
+use Pdp\Domain\fromIDNA2008;
 
 class AliasController extends Controller
 {
@@ -65,6 +67,10 @@ class AliasController extends Controller
             return response('You have reached your hourly limit for creating new aliases', 429);
         }
 
+        $formatEmail = function(string $localPart, string $domain, string $prefix = '') {
+            return $prefix . '.' . $localPart . '@' . $domain;
+        };
+
         if (isset($request->validated()['local_part'])) {
             $localPart = $request->validated()['local_part'];
 
@@ -80,18 +86,29 @@ class AliasController extends Controller
                 'extension' => $extension ?? null
             ];
         } else {
+            $prefix = null;
+            if (isset($request->validated()['hostname'])) {
+                // TODO this should be cached, perhaps on boot, from https://publicsuffix.org/
+                // see https://github.com/jeremykendall/php-domain-parser
+                $publicSuffixList = Rules::fromPath(sys_get_temp_dir() . '/public_suffix_list.dat');
+
+                $hostname = $request->validated()['hostname'];
+                $domain = Domain::fromIDNA2008($hostname);
+                $prefix = $publicSuffixList->resolve($domain)->secondLevelDomain()->toString();
+            }
+
             if ($request->input('format', 'random_characters') === 'random_words') {
                 $localPart = user()->generateRandomWordLocalPart();
 
                 $data = [
-                    'email' => $localPart . '@' . $request->domain,
+                    'email' => formatEmail($localPart, $request->domain, $prefix),
                     'local_part' => $localPart,
                 ];
             } elseif ($request->input('format', 'random_characters') === 'random_characters') {
                 $localPart = user()->generateRandomCharacterLocalPart(8);
 
                 $data = [
-                    'email' => $localPart . '@' . $request->domain,
+                    'email' => formatEmail($localPart, $request->domain, $prefix),
                     'local_part' => $localPart,
                 ];
             } else {
@@ -99,13 +116,12 @@ class AliasController extends Controller
 
                 $data = [
                     'id' => $uuid,
-                    'email' => $uuid . '@' . $request->domain,
+                    'emai' => formatEmail($uuid, $request->domain, $prefix),
                     'local_part' => $uuid,
                 ];
             }
         }
 
-
         // Check if domain is for username or custom domain
         $parentDomain = collect(config('anonaddy.all_domains'))
                     ->filter(function ($name) use ($request) {

+ 2 - 1
app/Http/Requests/StoreAliasRequest.php

@@ -39,7 +39,8 @@ class StoreAliasRequest extends FormRequest
                 'array',
                 'max:10',
                 new VerifiedRecipientId
-            ]
+            ],
+            'hostname' => 'nullable|string|max:100'
         ];
     }
 

+ 1 - 0
composer.json

@@ -14,6 +14,7 @@
         "fideloper/proxy": "^4.2",
         "fruitcake/laravel-cors": "^2.0",
         "guzzlehttp/guzzle": "^7.0.1",
+        "jeremykendall/php-domain-parser": "^6.1",
         "laravel/framework": "^8.0",
         "laravel/passport": "^10.0",
         "laravel/tinker": "^2.0",

+ 97 - 2
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "f7a28b5a628118a9e98cc2c118e93415",
+    "content-hash": "1335a8511503e6e1ac2df759dbeb3321",
     "packages": [
         {
             "name": "asbiin/laravel-webauthn",
@@ -1951,6 +1951,101 @@
             ],
             "time": "2022-05-21T17:30:32+00:00"
         },
+        {
+            "name": "jeremykendall/php-domain-parser",
+            "version": "6.1.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/jeremykendall/php-domain-parser.git",
+                "reference": "e195ab7bf0dd0ca231e066790fb0771f1acba5f1"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/jeremykendall/php-domain-parser/zipball/e195ab7bf0dd0ca231e066790fb0771f1acba5f1",
+                "reference": "e195ab7bf0dd0ca231e066790fb0771f1acba5f1",
+                "shasum": ""
+            },
+            "require": {
+                "ext-filter": "*",
+                "ext-intl": "*",
+                "ext-json": "*",
+                "php": "^7.4 || ^8.0"
+            },
+            "require-dev": {
+                "friendsofphp/php-cs-fixer": "^v3.5.0",
+                "guzzlehttp/guzzle": "^7.0",
+                "guzzlehttp/psr7": "^1.6||^2.0",
+                "phpstan/phpstan": "^1.4.6.",
+                "phpstan/phpstan-phpunit": "^1.0.0",
+                "phpstan/phpstan-strict-rules": "^1.1.0",
+                "phpunit/phpunit": "^9.5.13",
+                "psalm/plugin-phpunit": "^0.15.2",
+                "psr/http-factory": "^1.0",
+                "psr/simple-cache": "^1.0",
+                "symfony/cache": "^v5.0",
+                "vimeo/psalm": "^4.20"
+            },
+            "suggest": {
+                "league/uri": "To parse URL and validate host",
+                "psr/http-client-implementation": "To use the storage functionnality which depends on PSR-18",
+                "psr/http-factory-implementation": "To use the storage functionnality which depends on PSR-17",
+                "psr/simple-cache-implementation": "To use the storage functionnality which depends on PSR-16"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "6.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Pdp\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Jeremy Kendall",
+                    "homepage": "http://about.me/jeremykendall",
+                    "role": "Developer"
+                },
+                {
+                    "name": "Ignace Nyamagana Butera",
+                    "homepage": "http://nyamsprod.com",
+                    "role": "Developer"
+                },
+                {
+                    "name": "Contributors",
+                    "homepage": "https://github.com/jeremykendall/php-domain-parser/graphs/contributors"
+                }
+            ],
+            "description": "Public Suffix List and IANA Root Zone Database based Domain parsing implemented in PHP.",
+            "homepage": "https://github.com/jeremykendall/php-domain-parser",
+            "keywords": [
+                "PSL",
+                "Public Suffix List",
+                "Top Level Domains",
+                "domain parsing",
+                "iana",
+                "icann",
+                "idn",
+                "tld"
+            ],
+            "support": {
+                "issues": "https://github.com/jeremykendall/php-domain-parser/issues",
+                "source": "https://github.com/jeremykendall/php-domain-parser"
+            },
+            "funding": [
+                {
+                    "url": "https://github.com/sponsors/nyamsprod",
+                    "type": "github"
+                }
+            ],
+            "time": "2022-02-18T22:00:34+00:00"
+        },
         {
             "name": "laravel/framework",
             "version": "v8.83.16",
@@ -12223,5 +12318,5 @@
         "php": "^7.3|^8.0"
     },
     "platform-dev": [],
-    "plugin-api-version": "2.2.0"
+    "plugin-api-version": "2.3.0"
 }

+ 2 - 2
config/version.yml

@@ -5,9 +5,9 @@ current:
   major: 0
   minor: 11
   patch: 2
-  prerelease: 3-ga4fd2df
+  prerelease: 4-g59522f7
   buildmetadata: ''
-  commit: a4fd2d
+  commit: 59522f
   timestamp:
     year: 2020
     month: 10