-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jan Galek
committed
Aug 3, 2023
1 parent
f60cd01
commit c80c071
Showing
11 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,39 @@ | ||
name: "Codesniffer" | ||
|
||
on: | ||
pull_request: | ||
|
||
push: | ||
branches: [ "*" ] | ||
|
||
schedule: | ||
- cron: "0 8 * * 1" | ||
|
||
jobs: | ||
phpstan: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: "Setup PHP" | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.2' | ||
|
||
- name: Validate composer.json and composer.lock | ||
run: composer validate | ||
|
||
- name: Cache Composer packages | ||
id: composer-cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: vendor | ||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-php- | ||
- name: Install dependencies | ||
run: composer install --prefer-dist --no-progress | ||
- name: Run coding standards | ||
run: make cs |
This file contains 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,39 @@ | ||
name: PHPstan | ||
|
||
on: | ||
pull_request: | ||
|
||
push: | ||
branches: [ "*" ] | ||
|
||
schedule: | ||
- cron: "0 8 * * 1" | ||
|
||
jobs: | ||
phpstan: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: "Setup PHP" | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.2' | ||
|
||
- name: Validate composer.json and composer.lock | ||
run: composer validate | ||
|
||
- name: Cache Composer packages | ||
id: composer-cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: vendor | ||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-php- | ||
- name: Install dependencies | ||
run: composer install --prefer-dist --no-progress | ||
- name: Run phpstan | ||
run: make phpstan |
This file contains 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,39 @@ | ||
name: PHP Unit tests | ||
|
||
on: | ||
pull_request: | ||
|
||
push: | ||
branches: [ "*" ] | ||
|
||
schedule: | ||
- cron: "0 8 * * 1" | ||
|
||
jobs: | ||
test82: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: "Setup PHP" | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.2' | ||
|
||
- name: Validate composer.json and composer.lock | ||
run: composer validate | ||
|
||
- name: Cache Composer packages | ||
id: composer-cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: vendor | ||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-php- | ||
- name: Install dependencies | ||
run: composer install --prefer-dist --no-progress | ||
- name: Run tests | ||
run: make tests |
This file contains 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,9 @@ | ||
# IDE | ||
/.idea | ||
|
||
# Composer | ||
/vendor | ||
composer.lock | ||
|
||
# Tests | ||
/coverage.xml |
This file contains 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,22 @@ | ||
.PHONY: install tests qa cs csf phpstan | ||
|
||
install: | ||
composer install | ||
|
||
qa: phpstan cs | ||
|
||
cs: | ||
ifdef GITHUB_ACTION | ||
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --colors -nsp -q --report=checkstyle src tests | ||
else | ||
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --colors -nsp src tests | ||
endif | ||
|
||
csf: | ||
vendor/bin/phpcbf --standard=ruleset.xml --encoding=utf-8 --colors -nsp src tests | ||
|
||
phpstan: | ||
vendor/bin/phpstan analyse -c phpstan.neon | ||
|
||
tests: | ||
vendor/bin/phpunit --bootstrap vendor/autoload.php tests |
This file contains 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,39 @@ | ||
{ | ||
"name": "troia-studio/ssh-key-validator-nette", | ||
"description": "Implementation troia-studio/ssh-key-validator to Nette", | ||
"type": "library", | ||
"autoload": { | ||
"psr-4": { | ||
"TroiaStudio\\SshKeyValidator\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Tests\\": "tests" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=8.2", | ||
"troia-studio/ssh-key-validator": "^1.0", | ||
"symfony/validator": "^6.3" | ||
}, | ||
"require-dev": { | ||
"ext-intl": "*", | ||
"contributte/qa": "^0.3.1", | ||
"contributte/phpstan": "^0.1.0", | ||
"phpunit/phpunit": "^10" | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jan Galek", | ||
"email": "me@jan-galek.cz" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"config": { | ||
"allow-plugins": { | ||
"dealerdirect/phpcodesniffer-composer-installer": true | ||
} | ||
} | ||
} |
This file contains 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,15 @@ | ||
includes: | ||
- vendor/contributte/phpstan/phpstan.neon | ||
|
||
parameters: | ||
level: 0 | ||
phpVersion: 80200 | ||
|
||
scanDirectories: | ||
- src | ||
|
||
fileExtensions: | ||
- php | ||
|
||
paths: | ||
- src |
This file contains 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,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ruleset name="TroiaStudio" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> | ||
<!-- Rulesets --> | ||
<rule ref="./vendor/troia-studio/ssh-key-validator/ruleset.xml" /> | ||
|
||
<config name="php_version" value="80200"/> | ||
|
||
<!-- Excludes --> | ||
<exclude-pattern>/tests/tmp</exclude-pattern> | ||
</ruleset> |
This file contains 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,33 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace TroiaStudio\SshKeyValidator; | ||
|
||
use Attribute; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD"}) | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)] | ||
class IsSshKey extends Constraint | ||
{ | ||
|
||
public const NOT_SSH_KEY_ERROR = '99a2ab41-3180-4c25-821b-ef403acd1c2d'; | ||
|
||
protected const ERROR_NAMES = [ | ||
self::NOT_SSH_KEY_ERROR => 'NOT_SSH_KEY_ERROR', | ||
]; | ||
|
||
public string $message = 'This value should be valid ssh key.'; | ||
|
||
public function __construct(mixed $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) | ||
{ | ||
parent::__construct($options ?? [], $groups, $payload); | ||
|
||
$this->message = $message ?? $this->message; | ||
} | ||
|
||
} |
This file contains 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,41 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace TroiaStudio\SshKeyValidator; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Validator\Exception\UnexpectedValueException; | ||
|
||
class IsSshKeyValidator extends ConstraintValidator | ||
{ | ||
|
||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof IsSshKey) { | ||
throw new UnexpectedTypeException($constraint, IsSshKey::class); | ||
} | ||
|
||
if ($value === null || $value === '') { | ||
return; | ||
} | ||
|
||
if (!is_string($value)) { | ||
throw new UnexpectedValueException($value, 'string'); | ||
} | ||
|
||
$validator = KeyValidator::createAll(); | ||
|
||
if ($validator->validate($value)) { | ||
return; | ||
} | ||
|
||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ string }}', $value) | ||
->setCode(IsSshKey::NOT_SSH_KEY_ERROR) | ||
->addViolation(); | ||
} | ||
|
||
} |
This file contains 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,52 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Tests; | ||
|
||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
use TroiaStudio\SshKeyValidator\IsSshKey; | ||
use TroiaStudio\SshKeyValidator\IsSshKeyValidator; | ||
|
||
class IsSshKeyValidatorTest extends ConstraintValidatorTestCase | ||
{ | ||
|
||
public function testNullIsValid(): void | ||
{ | ||
$this->validator->validate(null, new IsSshKey()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testBlankIsValid(): void | ||
{ | ||
$this->validator->validate('', new IsSshKey()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testKeyIsValid(): void | ||
{ | ||
$this->validator->validate('ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINCqSM/QRXfw4xbJo+DIW4k9Ehks9hpxMyN/SoFvJHNZ test ed25519', new IsSshKey()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testKeyIsInValid(): void | ||
{ | ||
$key = 'sshed25519 AAAAC3NzaC1lZDI1NTE5AAAAINCqSM/QRXfw4xbJo+DIW4k9Ehks9hpxMyN/SoFvJHNZ test ed25519'; | ||
$isSshKey = new IsSshKey(); | ||
$this->validator->validate($key, $isSshKey); | ||
|
||
$this->buildViolation($isSshKey->message) | ||
->setParameter('{{ string }}', $key) | ||
->setCode(IsSshKey::NOT_SSH_KEY_ERROR) | ||
->assertRaised(); | ||
} | ||
|
||
protected function createValidator(): IsSshKeyValidator | ||
{ | ||
return new IsSshKeyValidator(); | ||
} | ||
|
||
} |