-
-
Notifications
You must be signed in to change notification settings - Fork 108
chore: Improvements at response from API when generate root certificate #5198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4754556
chore: scenery validate test
Any97Cris 4e81367
chore: more steps about tests
Any97Cris 8295004
fix: name of scenario
vitormattos 2d84d6a
fix: adjust table row size
vitormattos b363c58
chore: replace Name by Common Name
vitormattos 07de6ef
chore: restrict the return type to nullable and string only
vitormattos fb27977
chore: cover with more tests
vitormattos 6f6fc9b
chore: test get helper text with invalid field name
vitormattos a276363
chore: test get rule to valid field
vitormattos 4b2d15d
chore: test get rule to invalid field
vitormattos 1527625
chore: add type hint to return
vitormattos 3d5c742
fix: consider to return empty array when is invalid field name
vitormattos 9e2e88f
fix: run cs:fix
vitormattos 4da6a72
fix: run cs:fix
vitormattos 4b43967
chore: simplify test and compare actual and expected directly
vitormattos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Libresign\Service\Certificate; | ||
|
|
||
| use OCP\IL10N; | ||
|
|
||
| class RulesService { | ||
|
|
||
| private array $rules = [ | ||
| 'CN' => [ | ||
| 'required' => true, | ||
| 'min' => 1, | ||
| 'max' => 64, | ||
| ], | ||
| 'C' => [ | ||
| 'min' => 2, | ||
| 'max' => 2, | ||
| ], | ||
| 'ST' => [ | ||
| 'min' => 1, | ||
| 'max' => 128, | ||
| ], | ||
| 'L' => [ | ||
| 'min' => 1, | ||
| 'max' => 128, | ||
| ], | ||
| 'O' => [ | ||
| 'min' => 1, | ||
| 'max' => 64, | ||
| ], | ||
| 'OU' => [ | ||
| 'min' => 1, | ||
| 'max' => 64, | ||
| ], | ||
| ]; | ||
|
|
||
| public function __construct( | ||
| protected IL10N $l10n, | ||
| ) { | ||
|
|
||
vitormattos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public function getRule(string $fieldName): array { | ||
| if (!isset($this->rules[$fieldName]['helperText'])) { | ||
vitormattos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $this->rules[$fieldName]['helperText'] = $this->getHelperText($fieldName); | ||
| if (empty($this->rules[$fieldName]['helperText'])) { | ||
| unset($this->rules[$fieldName]['helperText']); | ||
| } | ||
| } | ||
| return $this->rules[$fieldName]; | ||
| } | ||
|
|
||
| public function getHelperText(string $fieldName): ?string { | ||
| return match ($fieldName) { | ||
| 'CN' => $this->l10n->t('Common Name (CN)'), | ||
| 'C' => $this->l10n->t('Two-letter ISO 3166 country code'), | ||
| 'ST' => $this->l10n->t('Full name of states or provinces'), | ||
| 'L' => $this->l10n->t('Name of a locality or place, such as a city, county, or other geographic region'), | ||
| 'O' => $this->l10n->t('Name of an organization'), | ||
| 'OU' => $this->l10n->t('Name of an organizational unit'), | ||
| default => null, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Libresign\Service\Certificate; | ||
|
|
||
| use InvalidArgumentException; | ||
| use OCP\IL10N; | ||
|
|
||
| class ValidateService { | ||
|
|
||
| public function __construct( | ||
| protected RulesService $rulesService, | ||
| protected IL10N $l10n, | ||
| ) { | ||
|
|
||
| } | ||
|
|
||
| public function validate(string $fieldName, string $value):void { | ||
| $rule = $this->rulesService->getRule($fieldName); | ||
vitormattos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $value = trim($value); | ||
| $length = strlen($value); | ||
| if (!$length && isset($rule['required']) && $rule['required']) { | ||
| throw new InvalidArgumentException( | ||
| $this->l10n->t("Parameter '%s' is required!", [$fieldName]) | ||
| ); | ||
| } | ||
| if ($length > $rule['max'] || $length < $rule['min']) { | ||
| throw new InvalidArgumentException( | ||
| $this->l10n->t("Parameter '%s' should be betweeen %s and %s.", [$fieldName, $rule['min'], $rule['max']]) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function validateNames(array $names) { | ||
| foreach ($names as $item) { | ||
| if (empty($item['id'])) { | ||
| throw new InvalidArgumentException('Parameter id is required!'); | ||
| } | ||
| $this->validate($item['id'], $item['value']); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be good to rewrite this line in multiple lines, to make it easier to read. Something like:
It's something silly, but may help people new to the application to understand it's code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this too much vars?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe would be best create a fallback issue to implement a
NormalizeServiceclass to add the trim rule into all values and also add an uppercase rule into Country and maintain the scope of this PR more amalgamated with the issue that motivated this PR.