Skip to content

Commit

Permalink
Required adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
RMartinOscar committed Oct 24, 2024
1 parent 211a091 commit f55d8e5
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function form(Form $form): Form
return $form
->schema([
Hidden::make('identifier')->default(ApiKey::generateTokenIdentifier(ApiKey::TYPE_APPLICATION)),
Hidden::make('token')->default(str_random(ApiKey::KEY_LENGTH)),
Hidden::make('token')->default(str_random(config('api.key.secret_length', 32))),

Hidden::make('user_id')
->default(auth()->user()->id)
Expand Down
156 changes: 111 additions & 45 deletions app/Filament/Resources/NodeResource/Pages/EditNode.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/Client/ApiKeyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function index(ClientApiRequest $request): array
*/
public function store(StoreApiKeyRequest $request): array
{
if ($request->user()->apiKeys->count() >= ApiKey::API_KEYS_LIMIT) {
if ($request->user()->apiKeys->count() >= config('api.key.limit', 25)) {
throw new DisplayException('You have reached the account limit for number of API keys.');
}

Expand Down
17 changes: 12 additions & 5 deletions app/Models/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@ class ApiKey extends Model

/**
* Maximum number of Api keys that a user can have.
*
* @deprecated
*/
public const API_KEYS_LIMIT = 24;
public const API_KEYS_LIMIT = 25;

/**
* Different API keys that can exist on the system.
*/
public const TYPE_NONE = 0;

public const TYPE_ACCOUNT = 1;

public const TYPE_APPLICATION = 2;

/* @deprecated */
Expand All @@ -85,12 +88,16 @@ class ApiKey extends Model

/**
* The length of API key identifiers.
*
* @deprecated
*/
public const IDENTIFIER_LENGTH = 16;

/**
* The length of the actual API key that is encrypted and stored
* in the database.
*
* @deprecated
*/
public const KEY_LENGTH = 32;

Expand Down Expand Up @@ -141,7 +148,7 @@ class ApiKey extends Model
*/
public static array $validationRules = [
'user_id' => 'required|exists:users,id',
'key_type' => 'present|integer|min:0|max:2',
'key_type' => 'present|integer|min:0|max:4',
'identifier' => 'required|string|size:16|unique:api_keys,identifier',
'token' => 'required|string',
'memo' => 'required|nullable|string|max:500',
Expand Down Expand Up @@ -203,7 +210,7 @@ public function tokenable(): BelongsTo
*/
public static function findToken(string $token): ?self
{
$identifier = substr($token, 0, self::IDENTIFIER_LENGTH);
$identifier = substr($token, 0, config('api.key.identifier_length', 16));

$model = static::where('identifier', $identifier)->first();
if (!is_null($model) && $model->token === substr($token, strlen($identifier))) {
Expand All @@ -230,6 +237,6 @@ public static function generateTokenIdentifier(int $type): string
{
$prefix = self::getPrefixForType($type);

return $prefix . Str::random(self::IDENTIFIER_LENGTH - strlen($prefix));
return $prefix . Str::random(config('api.key.identifier_length', 16) - strlen($prefix));
}
}
2 changes: 1 addition & 1 deletion app/Models/Traits/HasAccessTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function createToken(?string $memo, ?array $ips): NewAccessToken
'user_id' => $this->id,
'key_type' => ApiKey::TYPE_ACCOUNT,
'identifier' => ApiKey::generateTokenIdentifier(ApiKey::TYPE_ACCOUNT),
'token' => $plain = Str::random(ApiKey::KEY_LENGTH),
'token' => $plain = Str::random(config('api.key.secret_length', 32)),
'memo' => $memo ?? '',
'allowed_ips' => $ips ?? [],
]);
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Api/KeyCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function handle(array $data, array $permissions = []): ApiKey
$data = array_merge($data, [
'key_type' => $this->keyType,
'identifier' => ApiKey::generateTokenIdentifier($this->keyType),
'token' => str_random(ApiKey::KEY_LENGTH),
'token' => str_random(config('api.key.secret_length', 32)),
]);

if ($this->keyType === ApiKey::TYPE_APPLICATION) {
Expand Down
3 changes: 1 addition & 2 deletions app/Services/Nodes/NodeAutoDeployService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function handle(Request $request, Node $node, ?bool $docker = false): ?st
{
/** @var ApiKey|null $key */
$key = ApiKey::query()
->where('user_id', $request->user()->id)
->where('key_type', ApiKey::TYPE_APPLICATION)
->where('r_nodes', 1)
->first();
Expand All @@ -47,7 +46,7 @@ public function handle(Request $request, Node $node, ?bool $docker = false): ?st
sprintf(
'%s wings configure --panel-url %s --token %s --node %d%s',
$docker ? 'docker compose exec -it' : 'sudo',
config('app.url'),
route('index'),
$token,
$node->id,
$request->isSecure() ? '' : ' --allow-insecure'
Expand Down
9 changes: 9 additions & 0 deletions config/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
'key' => [
'limit' => env('API_KEYS_LIMIT', 25),
'identifier_length' => env('API_KEYS_IDENTIFIER_LENGTH', 16),
'secret_length' => env('API_KEYS_SECRET_LENGTH', 32),
],
];
2 changes: 1 addition & 1 deletion database/Factories/ApiKeyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function definition(): array
return [
'key_type' => ApiKey::TYPE_APPLICATION,
'identifier' => ApiKey::generateTokenIdentifier(ApiKey::TYPE_APPLICATION),
'token' => $token ?: $token = Str::random(ApiKey::KEY_LENGTH),
'token' => $token ?: $token = Str::random(config('api.key.secret_length', 32)),
'allowed_ips' => [],
'memo' => 'Test Function Key',
'created_at' => Carbon::now(),
Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/Api/Client/ApiKeyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Models\ApiKey;
use Illuminate\Support\Facades\Event;
use App\Events\ActivityLogged;
use Spatie\FlareClient\Api;

class ApiKeyControllerTest extends ClientApiIntegrationTestCase
{
Expand Down Expand Up @@ -104,7 +103,7 @@ public function testApiKeyLimitIsApplied(): void
{
/** @var \App\Models\User $user */
$user = User::factory()->create();
ApiKey::factory()->times(ApiKey::API_KEYS_LIMIT + 1)->for($user)->create([
ApiKey::factory()->times(config('api.key.limit', 25))->for($user)->create([
'key_type' => ApiKey::TYPE_ACCOUNT,
]);

Expand Down

0 comments on commit f55d8e5

Please sign in to comment.