From 8f49c8815d4f71d00ff5686327eddd371fae7d93 Mon Sep 17 00:00:00 2001 From: Brady McDonough Date: Wed, 7 Feb 2024 12:35:40 -0700 Subject: [PATCH] Added basic orchestration script, apply defaults before checking for missing fields --- .gitignore | 5 ++++- devel.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++ src/URI/Otpauth.php | 9 ++++----- tests/URITest.php | 7 ++++--- 4 files changed, 59 insertions(+), 9 deletions(-) create mode 100755 devel.sh diff --git a/.gitignore b/.gitignore index 14c4c40..0fe6c72 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ \#*\# .\#* .projectile -TAGS \ No newline at end of file +TAGS + +composer.lock +vendor/ diff --git a/devel.sh b/devel.sh new file mode 100755 index 0000000..39a8c4f --- /dev/null +++ b/devel.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +function tidy { + rm -vf {./,tests/,src/{./,AntiCSRF/,Clock/,Hash/,Request/,Required/,RFC/,Session/,URI/,Workflow/}}*~ + rm -vf {./,tests/,src/{./,AntiCSRF/,Clock/,Hash/,Request/,Required/,RFC/,Session/,URI/,Workflow/}}#*# +} + +function clean { + if [[ ! -d "vendor/" ]]; then + echo "No vendor/ folder to remove." + else + echo "Removing ./vendor/ ..." + rm -rf "vendor/" + echo "Done" + fi + rm -fv "composer.lock" +} + +function update { + composer update + composer dumpautoload -v +} + +if [[ $# -ne 1 ]]; then + echo "This script requires a verb." + echo "VERBS:" + echo " update - Update dependencies with composer" + echo " test - If no vendor/ folder Update, then run phpunit tests." + echo " clean - Remove all buildfiles and editor artifacts." + echo " tidy - Remove all editor artifacts." +fi + +cmd=$1 +if [[ $cmd == "update" ]]; then + update +elif [[ $cmd == "test" ]]; then + if [[ ! -d "vendor/" ]]; then + update + fi + ./vendor/bin/phpunit --testdox tests +elif [[ $cmd == "tidy" ]]; then + tidy +elif [[ $cmd == "clean" ]]; then + tidy && clean +else + echo "Unrecognized verb: ${cmd}" +fi diff --git a/src/URI/Otpauth.php b/src/URI/Otpauth.php index 6e65012..9633434 100644 --- a/src/URI/Otpauth.php +++ b/src/URI/Otpauth.php @@ -55,11 +55,6 @@ class Otpauth { return \array_key_exists('secret', $que);}, $parsedUri['query'] . "has no secret key."], ]; - - $parsedQuery = []; - \parse_str($parsedUri['query'], $parsedQuery); - $runCheck($queryChecks, $parsedQuery); - $labelChecks = [ [ function($lab) { return \count($lab) === 2;}, @@ -69,6 +64,9 @@ class Otpauth $label = \explode(":", $parsedUri['path']); $runCheck($labelChecks, $label); + $parsedQuery = []; + \parse_str($parsedUri['query'], $parsedQuery); + $queryDefaults = [ "algorithm" => "SHA1", "period" => "30", @@ -76,6 +74,7 @@ class Otpauth "issuer" => \rawurldecode(\ltrim($label[0], "/")), ]; $parsedQuery = \array_merge($queryDefaults, $parsedQuery); + $runCheck($queryChecks, $parsedQuery); $convertFields = function(array $conversion, array &$target) { foreach ($conversion as $k => $v) diff --git a/tests/URITest.php b/tests/URITest.php index 3dd38da..c9a726c 100644 --- a/tests/URITest.php +++ b/tests/URITest.php @@ -7,7 +7,7 @@ use BradyMcD\TAATP\URI\Otpauth; /** @SuppressWarnings(PHPMD.StaticAccess)*/ final class URITest extends TestCase { - public function testExampleURI(): void + public function testPreservesAllUriFields(): void { // Sourced from Google's otpauth URI specification // https://github.com/google/google-authenticator/wiki/Key-Uri-Format @@ -48,9 +48,9 @@ final class URITest extends TestCase $this->assertEqualsCanonicalizing($parsedOtpQuery, $parsedTestQuery); } - public function testIncompleteURI(): void + public function testCanFallbackToDefaults(): void { - $string = 'otpauth://totp/Example:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=Example'; + $string = 'otpauth://totp/Example:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ'; $queryComponents = [ "secret" => "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", "issuer" => "Example", "algorithm" => "SHA1", @@ -65,6 +65,7 @@ final class URITest extends TestCase $this->assertSame($provisioningUri->algorithm, $queryComponents['algorithm']); $this->assertSame($provisioningUri->digits, $queryComponents['digits']); $this->assertSame($provisioningUri->period, $queryComponents['period']); + $this->assertSame($provisioningUri->issuer, $queryComponents['issuer']); } }