-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: jesmandzimba <simplyjesman@gmail.com>
- Loading branch information
1 parent
814ab5a
commit cc976dc
Showing
5 changed files
with
116 additions
and
99 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
<?php | ||
|
||
namespace Clickatell\Exceptions; | ||
|
||
class ValidationException extends ClickatellException | ||
{ | ||
|
||
} |
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,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; | ||
} | ||
} |
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,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.'); | ||
} | ||
} | ||
} | ||
} |
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