From e4e5dea6b8858219fd7cfef084423b8027d69d57 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 28 Mar 2020 16:06:36 -0700 Subject: [PATCH] Fix API key creation logic --- app/Models/ApiKey.php | 5 +- app/Models/Validable.php | 7 +- .../dashboard/AccountApiContainer.tsx | 113 +++++++++--------- 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/app/Models/ApiKey.php b/app/Models/ApiKey.php index 6fb8a0e8..53a3fa81 100644 --- a/app/Models/ApiKey.php +++ b/app/Models/ApiKey.php @@ -53,7 +53,7 @@ class ApiKey extends Validable * @var array */ protected $casts = [ - 'allowed_ips' => 'json', + 'allowed_ips' => 'array', 'user_id' => 'int', 'r_' . AdminAcl::RESOURCE_USERS => 'int', 'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int', @@ -99,7 +99,8 @@ class ApiKey extends Validable 'identifier' => 'required|string|size:16|unique:api_keys,identifier', 'token' => 'required|string', 'memo' => 'required|nullable|string|max:500', - 'allowed_ips' => 'nullable|json', + 'allowed_ips' => 'nullable|array', + 'allowed_ips.*' => 'string', 'last_used_at' => 'nullable|date', 'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3', 'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3', diff --git a/app/Models/Validable.php b/app/Models/Validable.php index 43545c3a..f11c8ad0 100644 --- a/app/Models/Validable.php +++ b/app/Models/Validable.php @@ -140,7 +140,12 @@ abstract class Validable extends Model } return $this->getValidator()->setData( - $this->toArray() + // Trying to do self::toArray() here will leave out keys based on the whitelist/blacklist + // for that model. Doing this will return all of the attributes in a format that can + // properly be validated. + $this->addCastAttributesToArray( + $this->getAttributes(), $this->getMutatedAttributes() + ) )->passes(); } } diff --git a/resources/scripts/components/dashboard/AccountApiContainer.tsx b/resources/scripts/components/dashboard/AccountApiContainer.tsx index 38d7f757..51f1f780 100644 --- a/resources/scripts/components/dashboard/AccountApiContainer.tsx +++ b/resources/scripts/components/dashboard/AccountApiContainer.tsx @@ -46,62 +46,67 @@ export default () => { }; return ( -
+
- - setKeys(s => ([...s!, key]))}/> - - - - {deleteIdentifier && - { - doDeletion(deleteIdentifier); - setDeleteIdentifier(''); - }} - onDismissed={() => setDeleteIdentifier('')} - > - Are you sure you wish to delete this API key? All requests using it will immediately be - invalidated and will fail. - - } - { - keys.length === 0 ? -

- {loading ? 'Loading...' : 'No API keys exist for this account.'} -

- : - keys.map(key => ( -
- -
-

{key.description}

-

- Last - used: {key.lastUsedAt ? format(key.lastUsedAt, 'MMM Do, YYYY HH:mm') : 'Never'} -

-
-

- - {key.identifier} - -

- -
- )) - } -
+ +
+

{key.description}

+

+ Last + used: {key.lastUsedAt ? format(key.lastUsedAt, 'MMM Do, YYYY HH:mm') : 'Never'} +

+
+

+ + {key.identifier} + +

+ +
+ )) + } + +
); };