Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kerin committed May 3, 2018
0 parents commit 3b681e5
Show file tree
Hide file tree
Showing 16 changed files with 885 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/test export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore
/README.md export-ignore
/Makefile export-ignore
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License

Copyright 2018 The BitWasp developers

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

_GENERATE_DIR=./src/

ifdef GENERATE_DIR
_GENERATE_DIR=$(GENERATE_DIR)
endif

phpcs: pretest
vendor/bin/phpcs --standard=PSR2 -n src test/unit/

phpcbf: pretest
vendor/bin/phpcbf --standard=PSR2 -n src test/unit/

pretest:
if [ ! -d vendor ] || [ ! -f composer.lock ]; then composer install; else echo "Already have dependencies"; fi
if [ ! -d build ]; then mkdir build; fi

phpunit-ci-unit: pretest
php vendor/bin/phpunit -c phpunit.xml --coverage-text --coverage-clover=build/coverage.clover

phpunit-ci-integration: pretest
php vendor/bin/phpunit -c phpunit.device.xml --coverage-text --coverage-clover=build/coverage.clover

ocular:
if [ ! -f ocular.phar ]; then wget https://scrutinizer-ci.com/ocular.phar; fi

scrutinizer: ocular
php ocular.phar code-coverage:upload --format=php-clover build/coverage.clover

clean: clean-env clean-deps

clean-env:
rm -rf ocular.phar
rm -rf build

clean-deps:
rm -rf vendor/
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "bitwasp/pinentry",
"type": "library",
"description": "Interface to gpgtools pinentry for PHP applications",
"keywords": ["pinentry", "gpgtools", "password"],
"license": "MIT",
"authors": [
{
"name": "Thomas Kerin"
}
],
"autoload": {
"psr-4": {
"BitWasp\\PinEntry\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BitWasp\\Test\\PinEntry\\": "test/unit"
}
},
"require": {
"guzzlehttp/streams": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0.0",
"squizlabs/php_codesniffer": "^3.2",
"phpstan/phpstan": "^0.9.2"
}
}
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="All tests">
<directory>test/unit</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
41 changes: 41 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace BitWasp\PinEntry;

class Command
{
const SETDESC = 'SETDESC';
const SETPROMPT = 'SETPROMPT';
const SETKEYINFO = 'SETKEYINFO';
const SETREPEAT = 'SETREPEAT';
const SETTITLE = 'SETTITLE';
const SETERROR = 'SETERROR';
const SETOK = 'SETOK';
const SETNOTOK = 'SETNOTOK';
const SETCANCEL = 'SETCANCEL';
const SETREPEATERROR = 'SETREPEATERROR';
const SETTIMEOUT = 'SETTIMEOUT';
const SETQUALITYBAR = 'SETQUALITYBAR';
const SETQUALITYBAR_TT = 'SETQUALITYBAR_TT';

private $cmd;
private $param;

public function __construct(string $command, $param)
{
$this->cmd = $command;
$this->param = $param;
}

public function getCommand(): string
{
return $this->cmd;
}

public function getParam()
{
return $this->param;
}
}
10 changes: 10 additions & 0 deletions src/Exception/PinEntryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace BitWasp\PinEntry\Exception;

class PinEntryException extends \Exception
{

}
10 changes: 10 additions & 0 deletions src/Exception/RemotePinEntryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace BitWasp\PinEntry\Exception;

class RemotePinEntryException extends PinEntryException
{

}
11 changes: 11 additions & 0 deletions src/Exception/UnexpectedResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace Exception;

use BitWasp\PinEntry\Exception\RemotePinEntryException;

class UnexpectedResponseException extends RemotePinEntryException
{

}
51 changes: 51 additions & 0 deletions src/PinEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace BitWasp\PinEntry;

use BitWasp\PinEntry\Exception\PinEntryException;
use BitWasp\PinEntry\Process\ProcessInterface;

class PinEntry
{
/**
* @var ProcessInterface
*/
private $process;

public function __construct(
ProcessInterface $process
) {
$msg = $process->waitFor("OK");

if ($msg !== "OK Pleased to meet you\n") {
throw new PinEntryException("Unexpected start of pinnetry protocol");
}
$this->process = $process;
}

public function getInfo(string $type): string
{
$this->process->send("GETINFO {$type}\n");
$msg = $this->process->waitFor("D");
return $msg;
}

public function getPin(Request $request): string
{
foreach ($request->getCommands() as $command => $param) {
$this->process->send("{$command} {$param}\n");
$this->process->waitFor("OK");
}

foreach ($request->getOptions() as $option => $value) {
$this->process->send("OPTION {$option} {$value}\n");
$this->process->waitFor("OK");
}

$this->process->send("GETPIN\n");
$msg = $this->process->waitFor("D");
return $msg;
}
}
45 changes: 45 additions & 0 deletions src/Process/DebugDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace BitWasp\PinEntry\Process;

class DebugDecorator implements ProcessInterface
{
/**
* @var ProcessInterface
*/
private $process;

public function __construct(ProcessInterface $process)
{
$this->process = $process;
}

public function close(): bool
{
echo sprintf("%s(%s)\n", __METHOD__);
return $this->process->close();
}

public function recv(): string
{
$recv = $this->process->recv();
return $recv;
}

public function waitFor(string $text)
{
echo sprintf("%s(%s)\n", __METHOD__, trim($text));
$recv = $this->process->waitFor($text);
echo sprintf("%s(%s) received\n%s\n", __METHOD__, trim($text), $recv);
return $recv;
}

public function send(string $data)
{
echo sprintf("%s(%s)\n", __METHOD__, trim($data));
$send = $this->process->send($data);
return $send;
}
}
Loading

0 comments on commit 3b681e5

Please sign in to comment.