Skip to content

Commit

Permalink
Added basic support for Scanova.
Browse files Browse the repository at this point in the history
  • Loading branch information
jocic committed Feb 12, 2020
1 parent d049613 commit 26d18ec
Show file tree
Hide file tree
Showing 3 changed files with 389 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Expand Up @@ -7,6 +7,7 @@
<testsuite name="qr-generators">
<file>tests/qr-generators/test-google-qr.php</file>
<file>tests/qr-generators/test-go-qr.php</file>
<file>tests/qr-generators/test-scanova.php</file>
</testsuite>
<testsuite name="elements">
<file>tests/elements/test-secret.php</file>
Expand Down
194 changes: 194 additions & 0 deletions source/Jocic/GoogleAuthenticator/Qr/Remote/Scanova.php
@@ -0,0 +1,194 @@
<?php

/*******************************************************************\
|* Author: Djordje Jocic *|
|* Year: 2018 *|
|* License: MIT License (MIT) *|
|* =============================================================== *|
|* Personal Website: http://www.djordjejocic.com/ *|
|* =============================================================== *|
|* 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, RISING *|
|* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *|
|* OTHER DEALINGS IN THE SOFTWARE. *|
\*******************************************************************/

namespace Jocic\GoogleAuthenticator\Qr\Remote;

use Jocic\GoogleAuthenticator\Qr\QrInterface;
use Jocic\GoogleAuthenticator\Qr\QrCore;
use Jocic\GoogleAuthenticator\Account;
use Jocic\GoogleAuthenticator\Helper;

/**
* <i>Scanova</i> class is used for generating QR codes using
* pubilcly available GoQr's API.
*
* @author Djordje Jocic <office@djordjejocic.com>
* @copyright 2018 All Rights Reserved
* @version 1.0.0
*/

class Scanova extends RemoteQrCore implements QrInterface,
RemoteQrInterface
{
/******************\
|* CORE CONSTANTS *|
\******************/

// CORE CONSTANTS GO HERE

/******************\
|* CORE VARIABLES *|
\******************/

// CORE VARIABLES GO HERE

/*******************\
|* MAGIC FUNCTIONS *|
\*******************/

/**
* Constructor for the class <i>Scanova</i>. It's used for
* setting core class parameters upon object instantiation.
*
* @author Djordje Jocic <office@djordjejocic.com>
* @copyright 2018 All Rights Reserved
* @version 1.0.0
*
* @param integer $qrCodeSize
* Size of the QR codes that should be generated.
* @param string $storageDirectory
* Storage directory that should be set.
* @return void
*/

public function __construct($apiKey = null, $qrCodeSize = null,
$storageDirectory = null)
{
// Step 1 - Handle QR Code Size

if ($apiKey != null)
{
$this->setApiKey($apiKey);
}

// Step 2 - Handle QR Code Size

if ($qrCodeSize != null)
{
$this->setQrCodeSize($qrCodeSize);
}

// Step 3 - Handle Storage Directory

if ($storageDirectory != null)
{
$this->setStorageDirectory($storageDirectory);
}
}

/***************\
|* GET METHODS *|
\***************/

/**
* Forms and returns an appropriate URL for that can be used for
* generating QR codes remotely by sending a GET request.
*
* @author Djordje Jocic <office@djordjejocic.com>
* @copyright 2018 All Rights Reserved
* @version 1.0.0
*
* @param object $account
* Account that should be used for generating the QR code.
* @return string
* Formed url that can be used for generating QR codes.
*/

public function getUrl($account)
{
// Core Variables

$urlFormat = "https://api.scanova.io/v2/qrcode/url?" .
"size=%s&url=%s&apikey=%s";

// Logic

return vsprintf($urlFormat, [
"code-size" => $this->getQrCodeSize(),
"request" => $this->compileRequest($account),
"api-key" => $this->getApiKey()
]);
}

/***************\
|* SET METHODS *|
\***************/

/**
* Sets QR code size - s, m, l, xl, xxl, xxxl.
*
* @author Djordje Jocic <office@djordjejocic.com>
* @copyright 2018 All Rights Reserved
* @version 1.0.0
*
* @param integer $qrCodeSize
* Size of the QR codes that should be generated.
* @return void
*/

public function setQrCodeSize($qrCodeSize)
{
// Core Variables

$validSizes = [
"s", "m", "l", "xl", "xxl", "xxxl"
];

// Logic

if (!in_array($qrCodeSize, $validSizes))
{
throw new \Exception("Invalid value provided.");
}

$this->qrCodeSize = $qrCodeSize;
}

/****************\
|* CORE METHODS *|
\****************/

// CORE METHODS GO HERE

/*****************\
|* CHECK METHODS *|
\*****************/

// CHECK METHODS GO HERE

/*****************\
|* OTHER METHODS *|
\*****************/

// OTHER METHODS GO HERE
}

?>
194 changes: 194 additions & 0 deletions tests/qr-generators/test-scanova.php
@@ -0,0 +1,194 @@
<?php

/*******************************************************************\
|* Author: Djordje Jocic *|
|* Year: 2018 *|
|* License: MIT License (MIT) *|
|* =============================================================== *|
|* Personal Website: http://www.djordjejocic.com/ *|
|* =============================================================== *|
|* 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, RISING *|
|* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *|
|* OTHER DEALINGS IN THE SOFTWARE. *|
\*******************************************************************/

use PHPUnit\Framework\TestCase;

use Jocic\GoogleAuthenticator\Account;
use Jocic\GoogleAuthenticator\Secret;
use Jocic\GoogleAuthenticator\Qr\Remote\Scanova;

/**
* <i>TestGoQr</i> class is used for testing method implementation of the
* class <i>GoQr</i>.
*
* @author Djordje Jocic <office@djordjejocic.com>
* @copyright 2018 All Rights Reserved
* @version 1.0.0
*/

class TestScanova extends TestCase
{
/*********************\
|* GET & SET METHODS *|
\*********************/

/**
* Tests <i>setApiKey</i> & <i>getApiKey</i> methods.
*
* @author Djordje Jocic <office@djordjejocic.com>
* @copyright 2018 All Rights Reserved
* @version 1.0.0
*
* @return void
*/

public function testApiKeyMethods()
{
// Core Variables

$scanova = new Scanova();
$test = "1234567890";

// Logic

$scanova->setApiKey($test);

$this->assertEquals($test, $scanova->getApiKey());
}

/**
* Tests <i>getUrl</i> method.
*
* @author Djordje Jocic <office@djordjejocic.com>
* @copyright 2018 All Rights Reserved
* @version 1.0.0
*
* @return void
*/

public function testUrlMethtod()
{
// Core Variables

$scanova = new Scanova();

// Other Variables

$tempDirectory = sys_get_temp_dir();

// Other Variables

$testCombinations = [[
"serviceName" => "A",
"accountName" => "B",
"secretValue" => "RMB4AMUMDHODBYNR",
"qrCodeSize" => "s",
"qrUrl" => "https://api.scanova.io/v2/qrcode/url?size=s&url=otpauth%3A%2F%2Ftotp%2FA%2520-%2520B%3Fsecret%3DRMB4AMUMDHODBYNR&apikey=1234567890",
"apiKey" => "1234567890"
], [
"serviceName" => "C",
"accountName" => "D",
"secretValue" => "4JT4TVALIJOHCRZX",
"qrCodeSize" => "l",
"qrUrl" => "https://api.scanova.io/v2/qrcode/url?size=l&url=otpauth%3A%2F%2Ftotp%2FC%2520-%2520D%3Fsecret%3D4JT4TVALIJOHCRZX&apikey=0987654321",
"apiKey" => "0987654321"
], [
"serviceName" => "E",
"accountName" => "F",
"secretValue" => "YPPMQXR6UGWBP3UI",
"qrCodeSize" => "xxl",
"qrUrl" => "https://api.scanova.io/v2/qrcode/url?size=xxl&url=otpauth%3A%2F%2Ftotp%2FE%2520-%2520F%3Fsecret%3DYPPMQXR6UGWBP3UI&apikey=XXXXXXXXXXX",
"apiKey" => "XXXXXXXXXXX"
]];

// Step 1 - Test Combinations

foreach ($testCombinations as $testCombination)
{
$account = new Account($testCombination["serviceName"],
$testCombination["accountName"],
$testCombination["secretValue"]);

$scanova->setQrCodeSize($testCombination["qrCodeSize"]);
$scanova->setApiKey($testCombination["apiKey"]);
$scanova->setStorageDirectory($tempDirectory);

$this->assertSame($testCombination["qrUrl"],
$scanova->getUrl($account));
}

// Step 2 - Teset Method With Invalid Secret

try
{
$scanova->getUrl(new Secret());

$this->fail("Exception should've been thrown!");
}
catch (\Exception $e)
{
$this->assertEquals("Invalid object provided.",
$e->getMessage());
}

// Step 3 - Teset Method Without Secret

try
{
$account = new Account();

$scanova->getUrl($account);

$this->fail("Exception should've been thrown!");
}
catch (\Exception $e)
{
$this->assertEquals("Account is without a secret.",
$e->getMessage());
}
}

/*****************\
|* CHECK METHODS *|
\*****************/

// CHECK METHODS GO HERE

/*******************\
|* PRIMARY METHODS *|
\*******************/

// PRIMARY METHODS GO HERE

/*********************\
|* SECONDARY METHODS *|
\*********************/

// SECODARY METHODS GO HERE

/*****************\
|* OTHER METHODS *|
\*****************/

// OTHER METHODS GO HERE
}

?>

0 comments on commit 26d18ec

Please sign in to comment.