From 8403f6aaf65fbc302ed17fdfca800c38ed235ecc Mon Sep 17 00:00:00 2001 From: Jakob Heuser Date: Fri, 4 Feb 2022 15:28:21 -0800 Subject: [PATCH 1/2] feat: Adds PHP-CS-Fixer and PSR-12 on commit This change enables PSR-12 syntax formatting on commit for developers. It ensures that code is consistent on commit. In order for this to work in CI, the versions for phpunit and php-cs-fixer needed to be widened by enough to accomodate 7.2. Resolves #14 --- .gitattributes | 1 + .github/workflows/php.yml | 32 ++++++++++++++----------------- .php-cs-fixer.php | 14 ++++++++++++++ CHANGELOG.md | 21 ++++++++++++++++++++ composer.json | 28 +++++++++++++++++++++++---- example.php | 40 +++++++++++++++++++-------------------- tests/HOTPTest.php | 26 ++++++++++++------------- 7 files changed, 106 insertions(+), 56 deletions(-) create mode 100644 .php-cs-fixer.php create mode 100644 CHANGELOG.md diff --git a/.gitattributes b/.gitattributes index b55542b..6ef4f66 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,6 +2,7 @@ .gitattributes export-ignore .gitignore export-ignore .github/ export-ignore +.php-cs-fixer.php export-ignore composer.json export-ignore example.php export-ignore phpunit.xml export-ignore diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index ae9590f..3e4074e 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -5,7 +5,8 @@ on: pull_request: jobs: - run: + run-ci: + name: PHP ${{ matrix.php-versions }} test runs-on: ubuntu-latest strategy: matrix: @@ -15,21 +16,16 @@ jobs: - "7.4" - "8.0" - "8.1" - - name: PHP ${{ matrix.php-versions }} test steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP with XDebug - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - coverage: xdebug - tools: composer - - - name: Install dependencies - run: composer install --prefer-dist --no-progress - - - name: Run tests - run: composer test + - name: Checkout + uses: actions/checkout@v2 + - name: Setup PHP with XDebug + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + coverage: xdebug + tools: composer + - name: Install dependencies + run: composer install --prefer-dist --no-progress + - name: Run tests + run: composer test diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..b803245 --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,14 @@ +exclude("tests") + ->exclude("vendor") + ->in(__DIR__); + +$config = new PhpCsFixer\Config(); + +return $config->setRules([ + '@PSR12' => true, + 'trailing_comma_in_multiline' => true, +])->setUsingCache(false) + ->setFinder($finder); diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..c5ab3ad --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,21 @@ +# Changelog + +# next (planned @ 2.0.1) +- Adopts PSR-12 syntax everywhere (:heart: [jrzepa](https://github.com/jrzepa)) +- Expands version range for phpunit to ensure tests run against 7.2 [#16](https://github.com/jakobo/hotp-php/pull/16) + +# 2.0.0 (Dec 20, 2020) +- Code coverage reporting added (:heart: [reedy](https://github.com/reedy)) +- Documentation updates and return type hints (:heart: [reedy](https://github.com/reedy)) +- Github CI Support [ac0d8d](https://github.com/jakobo/hotp-php/commit/ac0d8d0d64adc5f7ef83952bde25425bf74184cf) (:heart: [legoktm](https://github.com/legoktm)) + +## Breaking Changes +- **Bump minimum version** - To stay current, the minimum version was bumped from 5.3.3 to 7.2. We encourage you to update your PHP version to stay on top of potential security vulnerabilities and receive the latest performance and bug fixes. + +# 1.0.1 (Apr 11, 2019) +- Token replay mitigation [d24e0d](https://github.com/jakobo/hotp-php/commit/d24e0d021710718cb9104ffb5c6ffb447fce65ab) (:heart: [reedy](https://github.com/reedy)) +- Created `composer.json` and made available through composer (:heart: [reedy](https://github.com/reedy)) + +# 1.0.0 +- [reedy](https://github.com/reedy) joined the maintainer team +- Initial Release with a tag \ No newline at end of file diff --git a/composer.json b/composer.json index 4046751..b47fcb2 100644 --- a/composer.json +++ b/composer.json @@ -22,17 +22,37 @@ "php": ">=7.2" }, "require-dev": { - "ockcyp/covers-validator": "1.3.3", - "phpunit/phpunit": "^8.5.13||^9.5.0", + "ockcyp/covers-validator": "^1.3.3", + "phpunit/phpunit": "^6.5.14||^7.0.15||^8.5.13||^9.5.0", "php-parallel-lint/php-console-highlighter": "0.5", - "php-parallel-lint/php-parallel-lint": "1.3.1" + "php-parallel-lint/php-parallel-lint": "1.3.1", + "friendsofphp/php-cs-fixer": "^3.4||^3.5||^3.6", + "brainmaestro/composer-git-hooks": "^2.8" }, "scripts": { + "post-install-cmd": "cghooks add --ignore-lock", + "post-update-cmd": "cghooks update", "test": [ "parallel-lint . --exclude vendor", "covers-validator", "phpunit --coverage-text" ], - "cover": "phpunit --coverage-html coverage" + "cover": "phpunit --coverage-html coverage", + "cghooks": "vendor/bin/cghooks", + "fix": "php-cs-fixer fix --config=.php-cs-fixer.php" + }, + "extra": { + "hooks": { + "config": { + "stop-on-failure": ["pre-commit"] + }, + "pre-commit": [ + "composer run-script fix", + "git update-index --again :/:" + ], + "post-merge": [ + "composer install" + ] + } } } diff --git a/example.php b/example.php index 86c8100..02f23c2 100644 --- a/example.php +++ b/example.php @@ -106,24 +106,23 @@ echo "----------------------------------------------------------------------\n"; // loop over all HOTP table results, and calculate the matching value -foreach ( $table['HOTP'] as $seed => $results ) { - $hotp = HOTP::generateByCounter( $key, $seed ); +foreach ($table['HOTP'] as $seed => $results) { + $hotp = HOTP::generateByCounter($key, $seed); $first = true; - foreach ( $results as $type => $calc ) { - if ( $first ) { - echo str_pad( $seed, 4, ' ', STR_PAD_LEFT ); + foreach ($results as $type => $calc) { + if ($first) { + echo str_pad($seed, 4, ' ', STR_PAD_LEFT); $first = false; - } - else { + } else { echo ' '; } echo ' '; - echo str_pad( $type, 5, ' ', STR_PAD_RIGHT); + echo str_pad($type, 5, ' ', STR_PAD_RIGHT); echo ' '; - echo str_pad( $calc, 47, ' ', STR_PAD_RIGHT); + echo str_pad($calc, 47, ' ', STR_PAD_RIGHT); echo ' '; - $method = 'to' . ( ucfirst( str_replace( 'HMAC', 'string', $type ) ) ); - echo str_pad( ( $calc == $hotp->$method( 6 ) ) ? '[OK]' : '[FAIL]', 9, ' ', STR_PAD_LEFT ); + $method = 'to' . (ucfirst(str_replace('HMAC', 'string', $type))); + echo str_pad(($calc == $hotp->$method(6)) ? '[OK]' : '[FAIL]', 9, ' ', STR_PAD_LEFT); echo "\n"; } } @@ -138,22 +137,21 @@ echo "----------------------------------------------------------------------\n"; // now echo over the TOTP table -foreach ( $table['TOTP'] as $seed => $results ) { - $totp = HOTP::generateByTime( $key, 30, $seed ); +foreach ($table['TOTP'] as $seed => $results) { + $totp = HOTP::generateByTime($key, 30, $seed); $first = true; - foreach ( $results as $type => $calc ) { - if ( $first ) { - echo str_pad( $seed, 10, ' ', STR_PAD_LEFT ); + foreach ($results as $type => $calc) { + if ($first) { + echo str_pad($seed, 10, ' ', STR_PAD_LEFT); $first = false; - } - else { + } else { echo ' '; } echo ' '; - echo str_pad( $calc, 47, ' ', STR_PAD_RIGHT ); + echo str_pad($calc, 47, ' ', STR_PAD_RIGHT); echo ' '; - $method = 'to' . ( ucfirst( str_replace( 'totp', 'hotp', $type ) ) ); - echo str_pad( ( $calc == $totp->$method( 8 ) ) ? '[OK]' : '[FAIL]', 9, ' ', STR_PAD_LEFT ); + $method = 'to' . (ucfirst(str_replace('totp', 'hotp', $type))); + echo str_pad(($calc == $totp->$method(8)) ? '[OK]' : '[FAIL]', 9, ' ', STR_PAD_LEFT); echo "\n"; } } diff --git a/tests/HOTPTest.php b/tests/HOTPTest.php index 6b19e86..2b3af50 100644 --- a/tests/HOTPTest.php +++ b/tests/HOTPTest.php @@ -23,7 +23,7 @@ public function provideHOTP(): array 'hex' => '4c93cf18', 'dec' => '1284755224', 'hotp' => '755224', - ] + ], ], [ 1, [ @@ -31,7 +31,7 @@ public function provideHOTP(): array 'hex' => '41397eea', 'dec' => '1094287082', 'hotp' => '287082', - ] + ], ], [ 2, [ @@ -39,7 +39,7 @@ public function provideHOTP(): array 'hex' => '82fef30', 'dec' => '137359152', 'hotp' => '359152', - ] + ], ], [ 3, [ @@ -47,7 +47,7 @@ public function provideHOTP(): array 'hex' => '66ef7655', 'dec' => '1726969429', 'hotp' => '969429', - ] + ], ], [ 4, [ @@ -55,7 +55,7 @@ public function provideHOTP(): array 'hex' => '61c5938a', 'dec' => '1640338314', 'hotp' => '338314', - ] + ], ], [ 5, [ @@ -63,7 +63,7 @@ public function provideHOTP(): array 'hex' => '33c083d4', 'dec' => '868254676', 'hotp' => '254676', - ] + ], ], [ 6, [ @@ -71,7 +71,7 @@ public function provideHOTP(): array 'hex' => '7256c032', 'dec' => '1918287922', 'hotp' => '287922', - ] + ], ], [ 7, [ @@ -79,7 +79,7 @@ public function provideHOTP(): array 'hex' => '4e5b397', 'dec' => '82162583', 'hotp' => '162583', - ] + ], ], [ 8, [ @@ -87,7 +87,7 @@ public function provideHOTP(): array 'hex' => '2823443f', 'dec' => '673399871', 'hotp' => '399871', - ] + ], ], [ 9, [ @@ -95,7 +95,7 @@ public function provideHOTP(): array 'hex' => '2679dc69', 'dec' => '645520489', 'hotp' => '520489', - ] + ], ], ]; } @@ -177,9 +177,9 @@ public function provideGenerateByTimeWindow(): array "266759", "306183", "466594", - "754889" - ] - ] + "754889", + ], + ], ]; } From f26dcf02d1dd94d587a4b83d00a2d02dd511014d Mon Sep 17 00:00:00 2001 From: Jakob Heuser Date: Thu, 24 Feb 2022 11:21:01 -0800 Subject: [PATCH 2/2] chore: Adds strict types to files --- example.php | 2 ++ src/HOTP.php | 2 ++ src/HOTPResult.php | 4 +++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/example.php b/example.php index 02f23c2..9d81037 100644 --- a/example.php +++ b/example.php @@ -1,5 +1,7 @@ toDec(), $length, "0", STR_PAD_LEFT); + $str = str_pad((string)$this->toDec(), $length, "0", STR_PAD_LEFT); return substr($str, (-1 * $length)); } }