-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add authstrategy classes and tests
- Loading branch information
1 parent
6fd8a3e
commit 18ef524
Showing
7 changed files
with
204 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
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; | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
|
||
|
||
} |
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,11 @@ | ||
<?php | ||
namespace Twilio\Http\BearerToken; | ||
|
||
/** | ||
* @property string $token | ||
* @property string $tokenManager | ||
*/ | ||
|
||
abstract class TokenManager { | ||
abstract public function fetchToken(): string; | ||
} |
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,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()); | ||
} | ||
} |
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,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()); | ||
} | ||
} |