Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkregel committed Jun 11, 2024
1 parent ce03aed commit 0fefeab
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 129 deletions.
25 changes: 25 additions & 0 deletions app/Services/SshService.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,30 @@ public static function factory(string $host, User $user): Credential
if (isset($credential)) {
return $credential;
}

[$privateKey, $publicKey] = app(SshKeyGeneratorService::class)->generate('');

$randomName = Str::random(16);
$publicKeyFile = storage_path('app/keys/'.$randomName.'.pub');
$privateKeyFile = storage_path('app/keys/'.$randomName);

file_put_contents($publicKeyFile, $publicKey);
chmod($publicKeyFile, 0600);
file_put_contents($privateKeyFile, $privateKey);
chmod($privateKeyFile, 0600);

return $user->credentials()->create([
'service' => Credential::TYPE_SSH,
'type' => Credential::TYPE_SSH,
'name' => 'SSH '.$host,
'api_key' => Str::random(32),
'settings' => [
'pub_key' => $publicKey,
'pub_key_file' => $publicKeyFile,
'private_key' => encrypt($privateKey),
'private_key_file' => $privateKeyFile,
'pass_key' => ! empty($passKey) ? encrypt($passKey) : '',
],
]);
}
}
3 changes: 2 additions & 1 deletion bin/sail
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
set -ex
set -e

export WWWUSER=${WWWUSER:-$UID}
export WWWGROUP=${WWWGROUP:-$(id -g)}
Expand All @@ -14,3 +14,4 @@ if [ ! -f vendor/bin/sail ]; then
fi

vendor/bin/sail "$@"

5 changes: 1 addition & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ parameters:
- docker/
paths:
- app/
- database/
- tests/
- routes/

# Level 9 is the highest level
level: 5
level: 0
9 changes: 3 additions & 6 deletions routes/pages/deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
use App\Models\Credential;

Route::middleware(['web', 'auth:sanctum'])->get('/register-device', function () {
$ssh = \App\Services\SshService::factory(
request()->ip(),
request()->user(),
);
$ssh = request()->user()->credentials()->where('type', 'ssh')->firstOrFail();

return response()->view('basement-scripts.link-server', [
'credential' => $ssh,
Expand All @@ -27,7 +24,7 @@
], 200, [
'Content-type' => 'text/text',
]);
})->middleware('throttle:api')->name('register-device');
})->middleware('throttle:api')->name('link-device');

Route::middleware([
'throttle:api',
Expand Down Expand Up @@ -75,7 +72,7 @@
->where('is_enabled', true)
->firstOrFail();

$ssh = \App\Services\SshService::factory(request()->ip(), $code->user);
$ssh = \App\Services\SshService::factory(request()->ip(), request()->user());

$server = \App\Models\Server::create(array_merge($data, [
'credential_id' => $ssh->id,
Expand Down
21 changes: 0 additions & 21 deletions routes/pages/spork.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,6 @@
}
}

Route::get('/api/device-code', function () {
$code = request()->user()->codes()->firstWhere('is_enabled', true);

if (empty($code)) {
$code = request()->user()->codes()->create([
'short_code' => $shortCode = Str::random(),
'long_url' => route('create-device', [
'short_code' => $shortCode,
]),
'is_enabled' => true,
'status' => 301,
]);
}

return [
'route' => str_replace('https://', 'http://', route('redirect', [
'code' => $code->short_code,
])),
];
})->name('setup-device');

Route::post('/api/mail/mark-as-read', Controllers\Api\Mail\MarkAsReadController::class);
Route::post('/api/mail/mark-as-unread', Controllers\Api\Mail\MarkAsUnreadController::class);
Route::post('/api/mail/mark-as-spam', Controllers\Api\Mail\MarkAsSpamAndMoveController::class);
Expand Down
97 changes: 0 additions & 97 deletions tests/Feature/Http/Controllers/LinkServerTest.php

This file was deleted.

0 comments on commit 0fefeab

Please sign in to comment.