Generate share passwords and offer them again beside the new link
Uploaders no longer have to make up a share password. With "Password protect" on, the upload page has Generate and Copy under the field, and the page the upload leads to offers the password once more beside the link: masked, with the same copy button at the end of the field as the link's. The password also derives the share's encryption key and only its hash is stored, so a lost one means files nobody can open. - PasswordGeneratorService draws from Random\Randomizer's secure engine. Characters are drawn uniformly and redrawn until every chosen set appears; passphrases come from EFF's large word list (CC BY 3.0 US, credited in the README), without its four hyphenated words. - Admin settings gain a "Share Passwords" card: mode (off, on request, prefilled as protection is switched on), kind (characters: length 12–64, the sets, look-alikes left out; passphrase: 4–10 words and a separator), and an example with its estimated entropy that follows the form before saving. Fields the chosen mode or kind hides are excluded from validation and keep their saved value. The default is on request, 20 letters and numbers without look-alikes. - FileUploader flashes the password encrypted with the share's token; ShareCreated shows it only when the token matches, so a reload or any other visitor sees nothing. Crypt covers installs without SESSION_ENCRYPT, which the Docker setup does not set. - The symbol set leaves out what chat apps turn into formatting and what breaks inside quotes, so a pasted password arrives unchanged. - app.css imports group.css for <x-group>; .ai/rules/views.md records that <x-group> drops data-test and other attributes. - Tests cover the generator, the admin card's saving, validation and example, prefill and generate on the upload page, the flash, and in Chromium Generate and Copy on the upload page and the masked copy on the share page. The admin settings page now has six headed sections. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
a88a052d9a
commit
504971ad7f
@@ -3,8 +3,10 @@
|
||||
namespace App\Livewire\Admin;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Services\PasswordGeneratorService;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Component;
|
||||
@@ -35,6 +37,23 @@ class AdminSettings extends Component
|
||||
|
||||
public bool $allowNeverExpire = false;
|
||||
|
||||
/** How the upload page offers generated share passwords: `off`, `button` or `prefill`. */
|
||||
public string $passwordGeneratorMode = 'button';
|
||||
|
||||
/** `characters` or `passphrase`. */
|
||||
public string $passwordGeneratorType = 'characters';
|
||||
|
||||
public int $passwordLength = 20;
|
||||
|
||||
/** @var list<string> */
|
||||
public array $passwordCharacterSets = [];
|
||||
|
||||
public bool $passwordAvoidAmbiguous = true;
|
||||
|
||||
public int $passphraseWords = 6;
|
||||
|
||||
public string $passphraseSeparator = 'hyphen';
|
||||
|
||||
public string $siteTitle = '';
|
||||
|
||||
public string $siteDescription = '';
|
||||
@@ -61,6 +80,15 @@ class AdminSettings extends Component
|
||||
$this->allowNeverExpire = (bool) Setting::get('allow_never_expire', false);
|
||||
$this->siteTitle = Setting::get('site_title', '') ?? '';
|
||||
$this->siteDescription = Setting::get('site_description', '') ?? '';
|
||||
|
||||
$passwordOptions = app(PasswordGeneratorService::class)->options();
|
||||
$this->passwordGeneratorMode = $passwordOptions['mode'];
|
||||
$this->passwordGeneratorType = $passwordOptions['type'];
|
||||
$this->passwordLength = $passwordOptions['length'];
|
||||
$this->passwordCharacterSets = $passwordOptions['characterSets'];
|
||||
$this->passwordAvoidAmbiguous = $passwordOptions['avoidAmbiguous'];
|
||||
$this->passphraseWords = $passwordOptions['words'];
|
||||
$this->passphraseSeparator = $passwordOptions['separator'];
|
||||
}
|
||||
|
||||
public static function phpMaxUploadMb(): int
|
||||
@@ -88,7 +116,7 @@ class AdminSettings extends Component
|
||||
{
|
||||
$phpMaxMb = self::phpMaxUploadMb();
|
||||
|
||||
$this->validate([
|
||||
$validated = $this->validate([
|
||||
'colorProfile' => ['required', 'string', Rule::in(array_keys(Scheme::profiles()))],
|
||||
'maxFileSize' => ['required', 'integer', 'min:1', 'max:'.$phpMaxMb],
|
||||
'maxStorageQuota' => ['required', 'integer', 'min:1'],
|
||||
@@ -97,8 +125,10 @@ class AdminSettings extends Component
|
||||
'siteTitle' => ['nullable', 'string', 'max:255'],
|
||||
'siteDescription' => ['nullable', 'string', 'max:1000'],
|
||||
'siteLogo' => ['nullable', 'file', 'mimes:png,jpg,jpeg,gif,webp', 'max:2048'],
|
||||
...$this->passwordGeneratorRules(),
|
||||
], [
|
||||
'maxFileSize.max' => __('Cannot exceed the PHP limit of :max MB. Increase upload_max_filesize and post_max_size in your PHP configuration.', ['max' => $phpMaxMb]),
|
||||
...$this->passwordGeneratorMessages(),
|
||||
]);
|
||||
|
||||
if ($this->systemPassword) {
|
||||
@@ -116,6 +146,8 @@ class AdminSettings extends Component
|
||||
Setting::set('site_title', $this->siteTitle ?: null);
|
||||
Setting::set('site_description', $this->siteDescription ?: null);
|
||||
|
||||
$this->savePasswordGeneratorSettings($validated);
|
||||
|
||||
if ($this->siteLogo && is_object($this->siteLogo)) {
|
||||
$existingLogo = Setting::get('site_logo');
|
||||
if ($existingLogo) {
|
||||
@@ -132,6 +164,87 @@ class AdminSettings extends Component
|
||||
$this->success(__('Settings saved successfully.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* The generator's rules. A field the chosen mode or type hides is excluded, so it never blocks
|
||||
* saving and keeps the value saved before.
|
||||
*
|
||||
* @return array<string, array<int, mixed>>
|
||||
*/
|
||||
protected function passwordGeneratorRules(): array
|
||||
{
|
||||
$characters = ['exclude_if:passwordGeneratorMode,off', 'exclude_unless:passwordGeneratorType,characters'];
|
||||
$passphrase = ['exclude_if:passwordGeneratorMode,off', 'exclude_unless:passwordGeneratorType,passphrase'];
|
||||
|
||||
return [
|
||||
'passwordGeneratorMode' => ['required', 'string', Rule::in(PasswordGeneratorService::MODES)],
|
||||
'passwordGeneratorType' => ['exclude_if:passwordGeneratorMode,off', 'required', 'string', Rule::in(PasswordGeneratorService::TYPES)],
|
||||
'passwordLength' => [...$characters, 'required', 'integer', 'min:'.PasswordGeneratorService::MIN_LENGTH, 'max:'.PasswordGeneratorService::MAX_LENGTH],
|
||||
'passwordCharacterSets' => [...$characters, 'required', 'array'],
|
||||
'passwordCharacterSets.*' => [...$characters, 'string', Rule::in(array_keys(PasswordGeneratorService::CHARACTER_SETS))],
|
||||
'passwordAvoidAmbiguous' => [...$characters, 'boolean'],
|
||||
'passphraseWords' => [...$passphrase, 'required', 'integer', 'min:'.PasswordGeneratorService::MIN_WORDS, 'max:'.PasswordGeneratorService::MAX_WORDS],
|
||||
'passphraseSeparator' => [...$passphrase, 'required', 'string', Rule::in(array_keys(PasswordGeneratorService::SEPARATORS))],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function passwordGeneratorMessages(): array
|
||||
{
|
||||
return [
|
||||
'passwordCharacterSets.required' => __('Choose at least one kind of character.'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the generator settings that passed validation; excluded ones keep their saved value.
|
||||
*
|
||||
* @param array<string, mixed> $validated
|
||||
*/
|
||||
protected function savePasswordGeneratorSettings(array $validated): void
|
||||
{
|
||||
Setting::set('password_generator_mode', $validated['passwordGeneratorMode']);
|
||||
|
||||
if (array_key_exists('passwordGeneratorType', $validated)) {
|
||||
Setting::set('password_generator_type', $validated['passwordGeneratorType']);
|
||||
}
|
||||
|
||||
if (array_key_exists('passwordLength', $validated)) {
|
||||
Setting::set('password_generator_length', $validated['passwordLength']);
|
||||
Setting::set('password_generator_character_sets', implode(',', $validated['passwordCharacterSets']));
|
||||
Setting::set('password_generator_avoid_ambiguous', $validated['passwordAvoidAmbiguous'] ? '1' : '0');
|
||||
}
|
||||
|
||||
if (array_key_exists('passphraseWords', $validated)) {
|
||||
Setting::set('password_generator_words', $validated['passphraseWords']);
|
||||
Setting::set('password_generator_separator', $validated['passphraseSeparator']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The form's generator options while they are valid, for the example; `null` otherwise.
|
||||
*
|
||||
* @return array{type: string, length: int, characterSets: list<string>, avoidAmbiguous: bool, words: int, separator: string}|null
|
||||
*/
|
||||
protected function passwordPreviewOptions(): ?array
|
||||
{
|
||||
$values = $this->only(['passwordGeneratorMode', 'passwordGeneratorType', 'passwordLength', 'passwordCharacterSets', 'passwordAvoidAmbiguous', 'passphraseWords', 'passphraseSeparator']);
|
||||
|
||||
if ($this->passwordGeneratorMode === 'off' || Validator::make($values, $this->passwordGeneratorRules())->fails()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'type' => $this->passwordGeneratorType,
|
||||
'length' => $this->passwordLength,
|
||||
'characterSets' => array_values($this->passwordCharacterSets),
|
||||
'avoidAmbiguous' => $this->passwordAvoidAmbiguous,
|
||||
'words' => $this->passphraseWords,
|
||||
'separator' => $this->passphraseSeparator,
|
||||
];
|
||||
}
|
||||
|
||||
public function removeLogo(): void
|
||||
{
|
||||
$existingLogo = Setting::get('site_logo');
|
||||
@@ -157,10 +270,15 @@ class AdminSettings extends Component
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
$passwordGenerator = app(PasswordGeneratorService::class);
|
||||
$passwordPreviewOptions = $this->passwordPreviewOptions();
|
||||
|
||||
return view('livewire.admin.admin-settings', [
|
||||
'hasSystemPassword' => (bool) Setting::get('system_password'),
|
||||
'currentLogo' => Setting::get('site_logo'),
|
||||
'phpMaxUploadMb' => self::phpMaxUploadMb(),
|
||||
'passwordExample' => $passwordPreviewOptions ? $passwordGenerator->generate($passwordPreviewOptions) : null,
|
||||
'passwordEntropy' => $passwordPreviewOptions ? $passwordGenerator->entropyBits($passwordPreviewOptions) : null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user