Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
389 additions
and
0 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
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,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 | ||
} | ||
|
||
?> |
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,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 | ||
} | ||
|
||
?> |