Skip to content

Commit

Permalink
Replace with helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lancepioch committed Mar 19, 2024
1 parent d9cfb62 commit d58496a
Show file tree
Hide file tree
Showing 20 changed files with 28 additions and 125 deletions.
11 changes: 1 addition & 10 deletions app/Extensions/DynamicDatabaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
namespace App\Extensions;

use App\Models\DatabaseHost;
use Illuminate\Contracts\Encryption\Encrypter;

class DynamicDatabaseConnection
{
public const DB_CHARSET = 'utf8';
public const DB_COLLATION = 'utf8_unicode_ci';
public const DB_DRIVER = 'mysql';

/**
* DynamicDatabaseConnection constructor.
*/
public function __construct(
protected Encrypter $encrypter,
) {
}

/**
* Adds a dynamic database connection entry to the runtime config.
*/
Expand All @@ -34,7 +25,7 @@ public function set(string $connection, DatabaseHost|int $host, string $database
'port' => $host->port,
'database' => $database,
'username' => $host->username,
'password' => $this->encrypter->decrypt($host->password),
'password' => decrypt($host->password),
'charset' => self::DB_CHARSET,
'collation' => self::DB_COLLATION,
]);
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/Admin/NodeAutoDeployController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Models\ApiKey;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\Encrypter;
use App\Services\Api\KeyCreationService;

class NodeAutoDeployController extends Controller
Expand All @@ -16,7 +15,6 @@ class NodeAutoDeployController extends Controller
* NodeAutoDeployController constructor.
*/
public function __construct(
private Encrypter $encrypter,
private KeyCreationService $keyCreationService
) {
}
Expand Down Expand Up @@ -58,7 +56,7 @@ public function __invoke(Request $request, Node $node): JsonResponse

return new JsonResponse([
'node' => $node->id,
'token' => $key->identifier . $this->encrypter->decrypt($key->token),
'token' => $key->identifier . decrypt($key->token),
]);
}
}
4 changes: 1 addition & 3 deletions app/Http/Controllers/Admin/Settings/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Illuminate\Support\Facades\Notification;
use App\Exceptions\DisplayException;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\Encrypter;
use App\Providers\SettingsServiceProvider;
use App\Http\Requests\Admin\Settings\MailSettingsFormRequest;

Expand All @@ -21,7 +20,6 @@ class MailController extends Controller
* MailController constructor.
*/
public function __construct(
private Encrypter $encrypter,
private Kernel $kernel,
) {
}
Expand Down Expand Up @@ -56,7 +54,7 @@ public function update(MailSettingsFormRequest $request): Response

foreach ($values as $key => $value) {
if (in_array($key, SettingsServiceProvider::getEncryptedKeys()) && !empty($value)) {
$value = $this->encrypter->encrypt($value);
$value = encrypt($value);
}

Setting::set('settings::' . $key, $value);
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/Auth/LoginCheckpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Http\JsonResponse;
use PragmaRX\Google2FA\Google2FA;
use Illuminate\Support\Facades\Event;
use Illuminate\Contracts\Encryption\Encrypter;
use App\Events\Auth\ProvidedAuthenticationToken;
use App\Http\Requests\Auth\LoginCheckpointRequest;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
Expand All @@ -21,7 +20,6 @@ class LoginCheckpointController extends AbstractLoginController
* LoginCheckpointController constructor.
*/
public function __construct(
private Encrypter $encrypter,
private Google2FA $google2FA,
private ValidationFactory $validation
) {
Expand Down Expand Up @@ -67,7 +65,7 @@ public function __invoke(LoginCheckpointRequest $request): JsonResponse
return $this->sendLoginResponse($user, $request);
}
} else {
$decrypted = $this->encrypter->decrypt($user->totp_secret);
$decrypted = decrypt($user->totp_secret);

if ($this->google2FA->verifyKey($decrypted, (string) $request->input('authentication_code'), config('panel.auth.2fa.window'))) {
Event::dispatch(new ProvidedAuthenticationToken($user));
Expand Down
10 changes: 1 addition & 9 deletions app/Http/Middleware/Api/Daemon/DaemonAuthenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Node;
use Illuminate\Http\Request;
use Illuminate\Contracts\Encryption\Encrypter;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
Expand All @@ -18,13 +17,6 @@ class DaemonAuthenticate
'daemon.configuration',
];

/**
* DaemonAuthenticate constructor.
*/
public function __construct(private Encrypter $encrypter)
{
}

/**
* Check if a request from the daemon can be properly attributed back to a single node instance.
*
Expand All @@ -49,7 +41,7 @@ public function handle(Request $request, \Closure $next): mixed
/** @var Node $node */
$node = Node::query()->where('daemon_token_id', $parts[0])->firstOrFail();

if (hash_equals((string) $this->encrypter->decrypt($node->daemon_token), $parts[1])) {
if (hash_equals((string) decrypt($node->daemon_token), $parts[1])) {
$request->attributes->set('node', $node);

return $next($request);
Expand Down
6 changes: 2 additions & 4 deletions app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Container\Container;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

Expand Down Expand Up @@ -136,7 +134,7 @@ public function getConfiguration(): array
'debug' => false,
'uuid' => $this->uuid,
'token_id' => $this->daemon_token_id,
'token' => Container::getInstance()->make(Encrypter::class)->decrypt($this->daemon_token),
'token' => decrypt($this->daemon_token),
'api' => [
'host' => '0.0.0.0',
'port' => $this->daemonListen,
Expand Down Expand Up @@ -179,7 +177,7 @@ public function getJsonConfiguration(bool $pretty = false): string
*/
public function getDecryptedKey(): string
{
return (string) Container::getInstance()->make(Encrypter::class)->decrypt(
return (string) decrypt(
$this->daemon_token
);
}
Expand Down
10 changes: 5 additions & 5 deletions app/Providers/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace App\Providers;

use App\Models\Setting;
use Exception;
use Psr\Log\LoggerInterface as Log;
use Illuminate\Database\QueryException;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Contracts\Encryption\DecryptException;

class SettingsServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -56,7 +55,7 @@ class SettingsServiceProvider extends ServiceProvider
/**
* Boot the service provider.
*/
public function boot(Encrypter $encrypter, Log $log): void
public function boot(Log $log): void
{
// Only set the email driver settings from the database if we
// are configured using SMTP as the driver.
Expand All @@ -78,8 +77,9 @@ public function boot(Encrypter $encrypter, Log $log): void
$value = array_get($values, 'settings::' . $key, config(str_replace(':', '.', $key)));
if (in_array($key, self::$encrypted)) {
try {
$value = $encrypter->decrypt($value);
} catch (DecryptException $exception) {
$value = decrypt($value);
} catch (Exception) {
// ignore
}
}

Expand Down
10 changes: 1 addition & 9 deletions app/Services/Api/KeyCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@
namespace App\Services\Api;

use App\Models\ApiKey;
use Illuminate\Contracts\Encryption\Encrypter;

class KeyCreationService
{
private int $keyType = ApiKey::TYPE_NONE;

/**
* ApiKeyService constructor.
*/
public function __construct(private Encrypter $encrypter)
{
}

/**
* Set the type of key that should be created. By default, an orphaned key will be
* created. These keys cannot be used for anything, and will not render in the UI.
Expand All @@ -39,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' => $this->encrypter->encrypt(str_random(ApiKey::KEY_LENGTH)),
'token' => encrypt(str_random(ApiKey::KEY_LENGTH)),
]);

if ($this->keyType === ApiKey::TYPE_APPLICATION) {
Expand Down
7 changes: 2 additions & 5 deletions app/Services/Databases/DatabaseManagementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace App\Services\Databases;

use Exception;
use App\Models\Server;
use App\Models\Database;
use App\Helpers\Utilities;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use App\Extensions\DynamicDatabaseConnection;
use App\Exceptions\Repository\DuplicateDatabaseNameException;
use App\Exceptions\Service\Database\TooManyDatabasesException;
Expand All @@ -34,7 +32,6 @@ class DatabaseManagementService
public function __construct(
protected ConnectionInterface $connection,
protected DynamicDatabaseConnection $dynamic,
protected Encrypter $encrypter,
) {
}

Expand Down Expand Up @@ -89,7 +86,7 @@ public function create(Server $server, array $data): Database
$data = array_merge($data, [
'server_id' => $server->id,
'username' => sprintf('u%d_%s', $server->id, str_random(10)),
'password' => $this->encrypter->encrypt(
'password' => encrypt(
Utilities::randomStringWithSpecialCharacters(24)
),
]);
Expand All @@ -103,7 +100,7 @@ public function create(Server $server, array $data): Database
$database->createUser(
$database->username,
$database->remote,
$this->encrypter->decrypt($database->password),
decrypt($database->password),
$database->max_connections
);
$database->assignUserToDatabase($database->database, $database->username, $database->remote);
Expand Down
4 changes: 1 addition & 3 deletions app/Services/Databases/DatabasePasswordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\Database;
use App\Helpers\Utilities;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use App\Extensions\DynamicDatabaseConnection;

class DatabasePasswordService
Expand All @@ -16,7 +15,6 @@ class DatabasePasswordService
public function __construct(
private ConnectionInterface $connection,
private DynamicDatabaseConnection $dynamic,
private Encrypter $encrypter,
) {
}

Expand All @@ -35,7 +33,7 @@ public function handle(Database|int $database): string
$this->dynamic->set('dynamic', $database->database_host_id);

$database->update([
'password' => $this->encrypter->encrypt($password),
'password' => encrypt($password),
]);

$database->dropUser($database->username, $database->remote);
Expand Down
4 changes: 1 addition & 3 deletions app/Services/Databases/Hosts/HostCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\DatabaseHost;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use App\Extensions\DynamicDatabaseConnection;

class HostCreationService
Expand All @@ -17,7 +16,6 @@ public function __construct(
private ConnectionInterface $connection,
private DatabaseManager $databaseManager,
private DynamicDatabaseConnection $dynamic,
private Encrypter $encrypter,
) {
}

Expand All @@ -30,7 +28,7 @@ public function handle(array $data): DatabaseHost
{
return $this->connection->transaction(function () use ($data) {
$host = DatabaseHost::query()->create([
'password' => $this->encrypter->encrypt(array_get($data, 'password')),
'password' => encrypt(array_get($data, 'password')),
'name' => array_get($data, 'name'),
'host' => array_get($data, 'host'),
'port' => array_get($data, 'port'),
Expand Down
4 changes: 1 addition & 3 deletions app/Services/Databases/Hosts/HostUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\DatabaseHost;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use App\Extensions\DynamicDatabaseConnection;

class HostUpdateService
Expand All @@ -17,7 +16,6 @@ public function __construct(
private ConnectionInterface $connection,
private DatabaseManager $databaseManager,
private DynamicDatabaseConnection $dynamic,
private Encrypter $encrypter,
) {
}

Expand All @@ -29,7 +27,7 @@ public function __construct(
public function handle(int $hostId, array $data): DatabaseHost
{
if (!empty(array_get($data, 'password'))) {
$data['password'] = $this->encrypter->encrypt($data['password']);
$data['password'] = encrypt($data['password']);
} else {
unset($data['password']);
}
Expand Down
3 changes: 1 addition & 2 deletions app/Services/Nodes/NodeCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Str;
use App\Models\Node;
use Illuminate\Contracts\Encryption\Encrypter;

class NodeCreationService
{
Expand All @@ -17,7 +16,7 @@ class NodeCreationService
public function handle(array $data): Node
{
$data['uuid'] = Uuid::uuid4()->toString();
$data['daemon_token'] = app(Encrypter::class)->encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH));
$data['daemon_token'] = encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH));
$data['daemon_token_id'] = Str::random(Node::DAEMON_TOKEN_ID_LENGTH);

return Node::query()->create($data);
Expand Down
4 changes: 1 addition & 3 deletions app/Services/Nodes/NodeUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Support\Str;
use App\Models\Node;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use App\Repositories\Daemon\DaemonConfigurationRepository;
use App\Exceptions\Http\Connection\DaemonConnectionException;
use App\Exceptions\Service\Node\ConfigurationNotPersistedException;
Expand All @@ -18,7 +17,6 @@ class NodeUpdateService
public function __construct(
private ConnectionInterface $connection,
private DaemonConfigurationRepository $configurationRepository,
private Encrypter $encrypter,
) {
}

Expand All @@ -30,7 +28,7 @@ public function __construct(
public function handle(Node $node, array $data, bool $resetToken = false): Node
{
if ($resetToken) {
$data['daemon_token'] = $this->encrypter->encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH));
$data['daemon_token'] = encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH));
$data['daemon_token_id'] = Str::random(Node::DAEMON_TOKEN_ID_LENGTH);
}

Expand Down
Loading

0 comments on commit d58496a

Please sign in to comment.