-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Bit-Wasp/bump2
Refactoring, examples, tests
- Loading branch information
Showing
20 changed files
with
556 additions
and
174 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,14 @@ | ||
<?php | ||
$required = [1=>0, 2=>0]; | ||
|
||
$tests = [ | ||
[0=>0, 1=>0, 2=>0], | ||
[1=>0, 2=>0, 3=>0], | ||
]; | ||
|
||
foreach ($tests as $test) { | ||
$missingKeys = array_diff_key($required, $test); | ||
if (count($missingKeys) > 0) { | ||
echo "Missing keys " . implode(" ", $missingKeys) . PHP_EOL; | ||
} | ||
} |
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
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 @@ | ||
<?php | ||
require __DIR__ . "/../vendor/autoload.php"; | ||
use BitWasp\PinEntry\PinEntry; | ||
use BitWasp\PinEntry\Process\DebugDecorator; | ||
use BitWasp\PinEntry\Process\Process; | ||
|
||
$pinEntry = new PinEntry(new DebugDecorator(new Process("/usr/bin/pinentry"))); | ||
$pin = $pinEntry->getInfo("dummy"); | ||
echo "got pin {$pin}\n"; |
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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
<?php | ||
require "vendor/autoload.php"; | ||
require __DIR__ . "/../vendor/autoload.php"; | ||
use BitWasp\PinEntry\PinEntry; | ||
use BitWasp\PinEntry\Process\Process; | ||
use BitWasp\PinEntry\Process\DebugDecorator; | ||
|
||
$pinEntry = new PinEntry("/usr/bin/pinentry"); | ||
$pin = $pinEntry->getInfo("pid"); | ||
echo "got pin {$pin}\n"; | ||
$pinEntry = new PinEntry(new DebugDecorator(new Process("/usr/bin/pinentry"))); | ||
$pin = $pinEntry->getPID(); | ||
echo "got PID {$pin}\n"; | ||
|
||
$pin = $pinEntry->getVersion(); | ||
echo "got version {$pin}\n"; |
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,37 @@ | ||
<?php | ||
declare(strict_types=1); | ||
require __DIR__ . "/../vendor/autoload.php"; | ||
use BitWasp\PinEntry\PinEntry; | ||
use BitWasp\PinEntry\PinValidation\PinValidatorInterface; | ||
use BitWasp\PinEntry\Process\DebugDecorator; | ||
use BitWasp\PinEntry\Process\Process; | ||
use BitWasp\PinEntry\PinRequest; | ||
|
||
|
||
$request = new PinRequest(); | ||
$request->withTitle("Enter that secure pin of yours"); | ||
$request->withDesc("Your pin is required in order to proceed"); | ||
$request->withPrompt("promptpromptprompt"); | ||
|
||
$pinEntry = new PinEntry(new DebugDecorator(new Process("/usr/bin/pinentry"))); | ||
|
||
class SixDigitPinValidator implements PinValidatorInterface | ||
{ | ||
public function validate(string $input, string &$error = null): bool | ||
{ | ||
if ($input !== (string)(int)$input) { | ||
$error = "Pin should be an integer, with no special characters"; | ||
return false; | ||
} | ||
if (strlen($input) != 6) { | ||
$error = "PIN must be 6 digits"; | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
$validator = new SixDigitPinValidator(); | ||
echo "call getpin\n"; | ||
$pin = $pinEntry->getPin($request, $validator); | ||
echo "got pin {$pin}\n"; |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
<?php | ||
require "vendor/autoload.php"; | ||
require __DIR__ . "/../vendor/autoload.php"; | ||
use BitWasp\PinEntry\PinEntry; | ||
use BitWasp\PinEntry\PinRequest; | ||
use BitWasp\PinEntry\Process\Process; | ||
|
||
$pinEntry = new PinEntry("/usr/bin/pinentry"); | ||
$pinEntry = new PinEntry(new Process("/usr/bin/pinentry")); | ||
$pin = $pinEntry->getInfo("version"); | ||
echo "got pin {$pin}\n"; |
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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitWasp\PinEntry\Assuan; | ||
|
||
use BitWasp\PinEntry\Exception\RemotePinEntryException; | ||
use BitWasp\PinEntry\Process\ProcessInterface; | ||
use BitWasp\PinEntry\Response; | ||
|
||
class Assuan | ||
{ | ||
/** | ||
* @param ProcessInterface $process | ||
* @return Response | ||
* @throws RemotePinEntryException | ||
*/ | ||
public function parseResponse(ProcessInterface $process): Response | ||
{ | ||
$data = []; | ||
$statuses = []; | ||
$comments = []; | ||
$okMsg = null; | ||
|
||
for (;;) { | ||
$buffer = $process->recv(); | ||
foreach (explode("\n", $buffer) as $piece) { | ||
if (substr($piece, 0, 4) === "ERR ") { | ||
$c = explode(" ", $piece, 3); | ||
// don't change state, it's only a single line | ||
throw new RemotePinEntryException($c[2], (int)$c[1]); | ||
} | ||
|
||
$prefix = substr($piece, 0, 2); | ||
if ($prefix === "D ") { | ||
$data[] = substr($piece, 2); | ||
} else if ($prefix === "S ") { | ||
$statuses[] = substr($piece, 2); | ||
} else if ($prefix === "# ") { | ||
// don't change state, it's only a single line | ||
$comments[] = substr($piece, 2); | ||
} else if ($prefix === "OK") { | ||
if (strlen($piece) > 2) { | ||
$okMsg = substr($piece, 3); | ||
} | ||
break 2; | ||
} | ||
} | ||
} | ||
|
||
return new Response($data, $statuses, $comments, $okMsg); | ||
} | ||
|
||
/** | ||
* @param ProcessInterface $process | ||
* @param string $command | ||
* @param string|null $params | ||
*/ | ||
public function send(ProcessInterface $process, string $command, string $params = null) | ||
{ | ||
$process->send(sprintf("%s%s\n", $command, $params ? " {$params}" : "")); | ||
} | ||
} |
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
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
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
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 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitWasp\PinEntry\PinValidation; | ||
|
||
/** | ||
* This interface must be implemented for the desired type of PIN. | ||
* It uses the return value of validate to indicate a positive/negative | ||
* outcome, and upon failure, error message will be written to $errorMsg | ||
*/ | ||
interface PinValidatorInterface | ||
{ | ||
public function validate(string $input, string &$errorMsg = null): bool; | ||
} |
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
Oops, something went wrong.