Skip to content

Commit

Permalink
Added basic auth verification (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: jesmandzimba <simplyjesman@gmail.com>
  • Loading branch information
bondeveloper and jesmandzimba authored Mar 12, 2023
1 parent 814ab5a commit cc976dc
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 99 deletions.
99 changes: 0 additions & 99 deletions src/Clickatell/Common/Client.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/Clickatell/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Clickatell\Exceptions;

class ValidationException extends ClickatellException
{

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

namespace Clickatell;

class RequestValidator
{
/**
* Username and password the request should be authenticated with
*
* @var string
*/
public string $signingUsername;
public string $signingPassword;

public function __construct(string $signingUsername, string $signingPassword)
{
$this->signingUsername = $signingUsername;
$this->signingPassword = $signingPassword;
}

/**
* Verify the request is from someone authenticated
*/
public function verify(SignedRequest $signedRequest): bool
{
if( $this->signingUsername !== $signedRequest->username ||
$this->signingPassword !== $signedRequest->password
) {
return false;
}
return true;
}
}
57 changes: 57 additions & 0 deletions src/Clickatell/SignedRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Clickatell;

use Clickatell\Exceptions\ValidationException;
use stdClass;

class SignedRequest
{
public string $username;
public string $password;
/**
* @throws ValidationException
*/
public static function createBasic(string $username, string $password): SignedRequest
{
try {
$signedRequest = new self();
$requestData = compact('username', 'password');
$signedRequest->validateRequest($requestData);
$signedRequest->setPropertyValues((object) $requestData);
} catch(ValidationException $ex) {
throw new ValidationException($ex->getMessage());
}
return $signedRequest;
}

public function setPropertyValues(stdClass $object): self
{
foreach($object as $key => $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
}
}
return $this;
}

/**
* @throws ValidationException
*/
private function validateRequest(array $request): void
{
$required = [
'username' => 'is_string',
'password'=> 'is_string'
];
foreach($required as $name => $type) {
if (!isset($request[$name])) {
throw new ValidationException($name.' is required.');
}

if (! $type($request[$name])) {
throw new ValidationException($name.' is invalid.');
}
}
}
}
18 changes: 18 additions & 0 deletions tests/MessageClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PHPUnit\Framework\TestCase;
use Clickatell\Client;
use Clickatell\Models\Messages\Sms;
use Clickatell\SignedRequest;
use Clickatell\RequestValidator;


class MessageClientTest extends TestCase
Expand All @@ -18,6 +20,22 @@ public function testSendingMessage(): void
$this->assertTrue($res->success());
}

public function testSignedRequest(): void
{
$username = 'signedSignature';
$password = '234234';
$signedRequest = SignedRequest::create($username, $password);
$this->assertEquals($username, $signedRequest->username);
$this->assertEquals($password, $signedRequest->password);
}

public function testRequestValidation()
{
$validator = new RequestValidator('jane', 'janedoe');
$signedRequest = SignedRequest::create('jane', 'janedoe');
$this->assertTrue($validator->verify($signedRequest));
}

private function createSms(): Sms
{
return new Sms('your_test_mobile', 'Base sent from unit tests @ '.time(), 'your_test_from');
Expand Down

0 comments on commit cc976dc

Please sign in to comment.