From 8ef55ec8e8072478fe21fa62a3cc615ca83065f9 Mon Sep 17 00:00:00 2001 From: smiley Date: Wed, 25 Oct 2017 05:49:32 +0200 Subject: [PATCH] :octocat: added authenticator example --- README.md | 28 ++++++++++++++++++++-------- composer.json | 1 + examples/example.php | 14 ++++++++------ tests/QRCodeTest.php | 15 ++++++++++++--- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 9b879d620..977bf46be 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ ## Info This library is based on the [QR code implementation](https://github.com/kazuhikoarase/qrcode-generator) by [Kazuhiko Arase](https://github.com/kazuhikoarase), -namespaced, cleaned up, made extensible and PHP7 ready (among other stuff). The main intend is to use it along with a [Google authenticator implementation](https://github.com/codemasher/php-googleauth). +namespaced, cleaned up, made extensible and PHP7 ready (among other stuff). ## Requirements - PHP 7+ @@ -148,6 +148,18 @@ foreach($matrix as $row){ ``` +### Authenticator example + +[codemasher/php-googleauth](https://github.com/codemasher/php-googleauth) features creation of `otpauth://` URIs for use with most mobile authenticators: +```php +use chillerlan\GoogleAuth\Authenticator; + +$authenticator = new Authenticator; + +$data = $authenticator->getUri($authenticator->createSecret(), 'test', 'chillerlan.net'); +$qrcode = new QRCode($data, new QRImage); +``` + ### Custom output modules But then again, instead of bloating your own code, you can simply create your own output module by extending `QROutputAbstract`. ```php @@ -163,8 +175,8 @@ class MyCustomOutput extends QROutputAbstract{ protected $options; // MyCustomOutputOptions (if present) // optional constructor - public function __construct(QROutputOptionsAbstract $outputOptions = null){ - $this->options = $outputOptions; + public function __construct(MyCustomOutputOptions $options = null){ + $this->options = $options; if(!$this->options){ // MyCustomOutputOptions should supply default values @@ -193,9 +205,9 @@ class MyCustomOutput extends QROutputAbstract{ method | return ------ | ------ `__construct($data, QROutputInterface $output, QROptions $options = null)` | - -`setData($data, QROptions $options = null)` | `$this` +`setData($data, QROptions $options = null)` | `QRCode` `output()` | mixed `QROutputInterface::dump()` -`getRawData()` | array `QRCode::$matrix` +`getRawData()` | array `QRDataGenerator::$matrix` ### Properties of `QROptions` @@ -211,8 +223,8 @@ property | type | default | allowed | description property | type | default | allowed | description -------- | ---- | ------- | ------- | ----------- `$type` | int | JSON | QRCode::OUTPUT_STRING_XXXX | XXXX = TEXT, JSON -`$textDark` | string | '#' | * | string substitute for dark -`$textLight` | string | ' ' | * | string substitute for light +`$textDark` | string | '🔴' | * | string substitute for dark +`$textLight` | string | '⭕' | * | string substitute for light `$eol` | string | `PHP_EOL` | * | newline string ### Properties of `QRMarkupOptions` @@ -224,7 +236,7 @@ property | type | default | allowed | description `$htmlOmitEndTag` | bool | true | - | the closing tag may be omitted (moar bloat!) `$fgColor` | string | '#000' | * | foreground color `$bgColor` | string | '#fff' | * | background color -`$cssClass` | string | '' | * | a common css class +`$cssClass` | string | null | * | a common css class `$pixelSize` | int | 5 | * | size of a QR code pixel `$marginSize` | int | 5 | * | margin around the QR code `$eol` | string | `PHP_EOL` | * | newline string diff --git a/composer.json b/composer.json index 862dff494..cc3bbc586 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "php": ">=7.0.3" }, "require-dev": { + "chillerlan/php-googleauth": "1.1.*", "phpunit/phpunit": "6.4.*" }, "suggest": { diff --git a/examples/example.php b/examples/example.php index c0c71bf7f..cd971df9b 100644 --- a/examples/example.php +++ b/examples/example.php @@ -9,16 +9,16 @@ require_once '../vendor/autoload.php'; -use chillerlan\QRCode\Output\QRMarkup; -use chillerlan\QRCode\Output\QRMarkupOptions; +use chillerlan\GoogleAuth\Authenticator; +use chillerlan\QRCode\Output\{QRMarkup, QRMarkupOptions, QRImage, QRString,QRStringOptions}; use chillerlan\QRCode\QRCode; -use chillerlan\QRCode\Output\QRImage; -use chillerlan\QRCode\Output\QRString; -use chillerlan\QRCode\Output\QRStringOptions; -$data = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net'; #$data = 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s'; #$data = 'skype://echo123'; +$authenticator = new Authenticator; + +# otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net +$data = $authenticator->getUri($authenticator->createSecret(), 'test', 'chillerlan.net'); ?> @@ -82,6 +82,8 @@ echo '
'.(new QRCode($data, new QRString))->output().'
';
 // string - text
 $outputOptions = new QRStringOptions;
 $outputOptions->type = QRCode::OUTPUT_STRING_TEXT;
+$outputOptions->textDark = '🔴';
+$outputOptions->textLight = 'â­•';
 
 echo '
'.(new QRCode($data, new QRString($outputOptions)))->output().'
'; diff --git a/tests/QRCodeTest.php b/tests/QRCodeTest.php index fe2f8f1c1..aceb91d12 100644 --- a/tests/QRCodeTest.php +++ b/tests/QRCodeTest.php @@ -11,9 +11,9 @@ namespace chillerlan\QRCodeTest; -use chillerlan\QRCode\Output\QRString; -use chillerlan\QRCode\QRCode; -use chillerlan\QRCode\QROptions; +use chillerlan\GoogleAuth\Authenticator; +use chillerlan\QRCode\Output\{QRImage, QRImageOptions, QRString}; +use chillerlan\QRCode\{QRCode, QROptions}; use ReflectionClass; use PHPUnit\Framework\TestCase; @@ -106,4 +106,13 @@ class QRCodeTest extends TestCase{ $method->invoke($this->reflectionClass->newInstanceArgs(['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', $this->output, $this->options])); } + public function testAuthenticatorExample(){ + $authenticator = new Authenticator; + + $data = $authenticator->getUri($authenticator->createSecret(), 'test', 'chillerlan.net'); + $qrcode = $this->reflectionClass->newInstanceArgs([$data, new QRImage(new QRImageOptions(['type' => QRCode::OUTPUT_IMAGE_GIF]))]); + + $this->markTestSkipped(print_r($qrcode->output(), true)); + } + }