Commit 023ff1b6 authored by Alasdair Keyes's avatar Alasdair Keyes

Add code and tests

parent 123a4d0e
<?php
namespace RedirectToken;
use RedirectToken\Exception\InvalidHashAlgorithmException;
use RedirectToken\Exception\InvalidSecretKeyException;
abstract class AbstractBase
{
/**
* @const int
*/
const SECRET_KEY_MINIMUM_LENGTH = 10;
/**
* @const string
*/
const DEFAULT_HASH_ALGORITHM = 'sha256';
/**
* @var string Secret Key for hashing
*/
protected $secretKey;
/**
* @var string Has Algorithm
*/
protected $hashAlgorithm;
/**
* Generator constructor.
* @param string $secretKey
* @param string $hashAlgorithm
* @throws InvalidSecretKeyException
* @throws InvalidHashAlgorithmException
*/
public function __construct($secretKey = '', $hashAlgorithm = self::DEFAULT_HASH_ALGORITHM)
{
if (!$secretKey || strlen($secretKey) < self::SECRET_KEY_MINIMUM_LENGTH) {
throw new InvalidSecretKeyException(
'Secret key must be at least ' . self::SECRET_KEY_MINIMUM_LENGTH . ' characters'
);
}
if (! in_array($hashAlgorithm, hash_algos())) {
throw new InvalidHashAlgorithmException("Invalid Hash Algorithm");
}
$this->secretKey = $secretKey;
$this->hashAlgorithm = $hashAlgorithm;
}
/**
* @return string
*/
public function getHashAlgorithm()
{
return $this->hashAlgorithm;
}
}
\ No newline at end of file
<?php
namespace RedirectToken\Exception;
use Exception;
class InvalidHashAlgorithmException extends Exception
{
}
\ No newline at end of file
<?php
namespace RedirectToken\Exception;
use Exception;
class InvalidSecretKeyException extends Exception
{
}
\ No newline at end of file
<?php
namespace RedirectToken;
use PSR\Http\Message\UriInterface;
class Generator extends AbstractBase
{
/**
* @param UriInterface $uri
* @return string
*/
public function generateToken(UriInterface $uri)
{
return hash($this->hashAlgorithm, $this->secretKey.$uri);
}
}
<?php
namespace RedirectToken;
use PSR\Http\Message\UriInterface;
class Validator extends AbstractBase
{
/**
* @param UriInterface $uri
* @param string $token
* @return bool
*/
public function validateUriToken(UriInterface $uri, $token = '') {
if (hash($this->hashAlgorithm, $this->secretKey.$uri) == $token) {
return true;
}
return false;
}
}
\ No newline at end of file
<?php
namespace Test\RedirectToken;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface;
use RedirectToken\Generator;
class GeneratorTest extends TestCase
{
/**
* @var Generator Generator object
*/
private $generator;
public function setUp() {
$this->generator = new Generator(TestEnum::VALID_SECRET_KEY);
}
/**
* Test Instantiation
*/
public function testInstantiationValid() {
$generator = new Generator(TestEnum::VALID_SECRET_KEY);
$this->assertInstanceOf(Generator::class, $generator);
}
/**
* @expectedException RedirectToken\Exception\InvalidSecretKeyException
*/
public function testInstantiationInvalidKeyTooShort() {
new Generator(TestEnum::SHORT_SECRET_KEY);
}
/**
* @expectedException RedirectToken\Exception\InvalidSecretKeyException
*/
public function testInstantiationInvalidKeyNoKey() {
new Generator();
}
/**
* @expectedException RedirectToken\Exception\InvalidHashAlgorithmException
*/
public function testInstantiationInvalidHashAlgorithm() {
new Generator(TestEnum::VALID_SECRET_KEY, 'asdasd');
}
/**
* Test functions
*/
public function testGetHashAlgorithm() {
$generator = new Generator(TestEnum::VALID_SECRET_KEY, 'md5');
$this->assertEquals('md5', $generator->getHashAlgorithm());
}
public function testGetHashAlgorithmDefault() {
$generator = new Generator(TestEnum::VALID_SECRET_KEY);
$this->assertEquals('sha256', $generator->getHashAlgorithm());
}
public function testGenerateFunctionValid() {
$uriStub = $this->createMock(UriInterface::class);
$uriStub->method('__toString')->willReturn(TestEnum::MATCHING_URL);
$this->assertEquals(
TestEnum::MATCHING_URL_TOKEN,
$this->generator->generateToken($uriStub)
);
}
}
<?php
namespace Test\RedirectToken;
class TestEnum
{
const VALID_SECRET_KEY = 'abcdefghij';
const SHORT_SECRET_KEY = 'a';
const MATCHING_URL = 'https://www.google.co.uk/';
const MATCHING_URL_TOKEN = '86d4609fa2460a420ae5ea4a31db8f606dda48805ebe767fc42dec2adc1b9bfa';
const UNMATCHING_URL = 'https://www.invalid.com/';
const UNMATCHING_URL_TOKEN = 'asdasdasfasfd';
}
\ No newline at end of file
<?php
namespace Test\RedirectToken;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface;
use RedirectToken\Validator;
class ValidatorTest extends TestCase
{
/**
* @var Validator Validator object
*/
private $Validator;
public function setUp() {
$this->Validator = new Validator(TestEnum::VALID_SECRET_KEY);
}
/*
* Test Instantiation
*/
public function testInstantiationValid() {
$Validator = new Validator(TestEnum::VALID_SECRET_KEY);
$this->assertInstanceOf(Validator::class, $Validator);
}
/**
* @expectedException RedirectToken\Exception\InvalidSecretKeyException
*/
public function testInstantiationInvalidKeyTooShort() {
new Validator(TestEnum::SHORT_SECRET_KEY);
}
/**
* @expectedException RedirectToken\Exception\InvalidSecretKeyException
*/
public function testInstantiationInvalidKeyNoKey() {
new Validator();
}
/**
* @expectedException RedirectToken\Exception\InvalidHashAlgorithmException
*/
public function testInstantiationInvalidHashAlgorithm() {
new Validator(TestEnum::VALID_SECRET_KEY, 'asdasd');
}
/**
* Test functions
*/
public function testGetHashAlgorithm() {
$validator = new Validator(TestEnum::VALID_SECRET_KEY, 'md5');
$this->assertEquals('md5', $validator->getHashAlgorithm());
}
public function testGetHashAlgorithmDefault() {
$generator = new Validator(TestEnum::VALID_SECRET_KEY);
$this->assertEquals('sha256', $generator->getHashAlgorithm());
}
public function testValidateUriTokenValid() {
$uriStub = $this->createMock(UriInterface::class);
$uriStub->method('__toString')->willReturn(TestEnum::MATCHING_URL);
$this->assertTrue(
$this->Validator->validateUriToken($uriStub, TestEnum::MATCHING_URL_TOKEN)
);
}
public function testValidateUriTokenInvalid() {
$uriStub = $this->createMock(UriInterface::class);
$uriStub->method('__toString')->willReturn(TestEnum::MATCHING_URL);
$this->assertFalse(
$this->Validator->validateUriToken($uriStub, TestEnum::UNMATCHING_URL_TOKEN)
);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment