Skip to content

Commit

Permalink
chore: add authstrategy classes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 committed Jan 22, 2025
1 parent 6fd8a3e commit 18ef524
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Twilio/AuthStrategy/AuthStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Twilio\AuthStrategy;

/**
* @property string $authType
*/

abstract class AuthStrategy {
private $authType;

public function __construct(string $authType) {
$this->authType = $authType;
}

public function getAuthType(): string {
return $this->authType;
}

abstract public function getAuthString(): string;
abstract public function requiresAuthentication(): bool;
}
26 changes: 26 additions & 0 deletions src/Twilio/AuthStrategy/BasicAuthStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Twilio\AuthStrategy;

/**
* @property string $username
* @property string $password
*/

class BasicAuthStrategy extends AuthStrategy {
private $username;
private $password;

public function __construct(string $username, string $password) {
parent::__construct("basic");
$this->username = $username;
$this->password = $password;
}

public function getAuthString(): string {
return base64_encode($this->username . ':' . $this->password);
}

public function requiresAuthentication(): bool {
return true;
}
}
22 changes: 22 additions & 0 deletions src/Twilio/AuthStrategy/NoAuthStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Twilio\AuthStrategy;

/**
* @property string $username
* @property string $password
*/

class NoAuthStrategy extends AuthStrategy {

public function __construct() {
parent::__construct("noauth");
}

public function getAuthString(): string {
return "";
}

public function requiresAuthentication(): bool {
return false;
}
}
51 changes: 51 additions & 0 deletions src/Twilio/AuthStrategy/TokenAuthStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace Twilio\AuthStrategy;

use Twilio\Http\BearerToken\TokenManager;
use Twilio\Jwt\JWT;

/**
* @property string $token
* @property TokenManager $tokenManager
*/

class TokenAuthStrategy extends AuthStrategy {
private $token;
private $tokenManager;

public function __construct(TokenManager $tokenManager) {
parent::__construct("token");
$this->tokenManager = $tokenManager;
}

public function getAuthString(): string {
return $this->fetchToken();
}

public function requiresAuthentication(): bool {
return true;
}

public function fetchToken(): string {
if(empty($this->token) || $this->isTokenExpired($this->token)) {
$this->token = $this->tokenManager->fetchToken();
}
return $this->token;
}

public function isTokenExpired(string $token): bool {
$decodedToken = JWT::decode($token);
if($decodedToken === null || $decodedToken->exp === null) {
// If the token doesn't have an expiration, consider it expired
return false;
}
$expiresAt = $decodedToken->exp * 1000;
$bufferMilliseconds = 30 * 1000;
$bufferExpiresAt = $expiresAt - $bufferMilliseconds;

// Return true if the current time is after the expiration time with buffer
return time() > $bufferExpiresAt;
}


}
11 changes: 11 additions & 0 deletions src/Twilio/Http/BearerToken/TokenManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Twilio\Http\BearerToken;

/**
* @property string $token
* @property string $tokenManager
*/

abstract class TokenManager {
abstract public function fetchToken(): string;
}
42 changes: 42 additions & 0 deletions tests/Twilio/Unit/AuthStrategy/BasicAuthStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php


namespace Twilio\Tests\Unit\AuthStrategy;

use Twilio\Tests\Unit\UnitTest;
use Twilio\AuthStrategy\BasicAuthStrategy;

class BasicAuthStrategyTest extends UnitTest {
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $password;
/**
* @var BasicAuthStrategy
*/
private $basicAuthStrategy;

public function setUp(): void {
parent::setUp();
$this->username = "username";
$this->password = "password";
$this->basicAuthStrategy = new BasicAuthStrategy($this->username, $this->password);
}

public function testAuthType(): void {
$this->assertEquals('basic', $this->basicAuthStrategy->getAuthType());
}

public function testAuthString(): void {
$auth = base64_encode($this->username . ':' . $this->password);
$this->assertEquals($auth, $this->basicAuthStrategy->getAuthString());
}

public function testRequiresAuthentication(): void {
$this->assertTrue($this->basicAuthStrategy->requiresAuthentication());
}
}
31 changes: 31 additions & 0 deletions tests/Twilio/Unit/AuthStrategy/NoAuthStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace Twilio\Tests\Unit\AuthStrategy;

use Twilio\Tests\Unit\UnitTest;
use Twilio\AuthStrategy\NoAuthStrategy;

class NoAuthStrategyTest extends UnitTest {
/**
* @var NoAuthStrategy
*/
private $noAuthStrategy;

public function setUp(): void {
parent::setUp();
$this->noAuthStrategy = new NoAuthStrategy();
}

public function testAuthType(): void {
$this->assertEquals('noauth', $this->noAuthStrategy->getAuthType());
}

public function testEmptyAuthString(): void {
$this->assertEquals('', $this->noAuthStrategy->getAuthString());
}

public function testRequiresAuthentication(): void {
$this->assertFalse($this->noAuthStrategy->requiresAuthentication());
}
}

0 comments on commit 18ef524

Please sign in to comment.