Skip to content

Commit beff0a0

Browse files
chore: add authStrategy classes and tests (#843)
* chore: add authstrategy classes and tests * chore: add inline documentation for authStrategies
1 parent 6fd8a3e commit beff0a0

File tree

7 files changed

+272
-0
lines changed

7 files changed

+272
-0
lines changed

Diff for: src/Twilio/AuthStrategy/AuthStrategy.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace Twilio\AuthStrategy;
3+
4+
/**
5+
* Class AuthStrategy
6+
* Abstract parent class for all authentication strategies - Basic, Bearer Token, NoAuth etc.
7+
* @property string $authType The type of authentication strategy
8+
*/
9+
10+
abstract class AuthStrategy {
11+
private $authType;
12+
13+
public function __construct(string $authType) {
14+
$this->authType = $authType;
15+
}
16+
17+
public function getAuthType(): string {
18+
return $this->authType;
19+
}
20+
21+
/**
22+
* Returns the value to be set in the authentication header
23+
*
24+
* @return string the authentication string
25+
*/
26+
abstract public function getAuthString(): string;
27+
28+
/**
29+
* Returns true if the authentication strategy requires authentication and false otherwise
30+
*
31+
* @return bool whether the authentication strategy requires authentication
32+
*/
33+
abstract public function requiresAuthentication(): bool;
34+
}

Diff for: src/Twilio/AuthStrategy/BasicAuthStrategy.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Twilio\AuthStrategy;
3+
4+
/**
5+
* Class BasicAuthStrategy
6+
* Implementation of the AuthStrategy for Basic authentication
7+
* @property string $username
8+
* @property string $password
9+
*/
10+
11+
class BasicAuthStrategy extends AuthStrategy {
12+
private $username;
13+
private $password;
14+
15+
public function __construct(string $username, string $password) {
16+
parent::__construct("basic");
17+
$this->username = $username;
18+
$this->password = $password;
19+
}
20+
21+
/**
22+
* Returns the base64 encoded string concatenating the username and password
23+
*
24+
* @return string the base64 encoded string
25+
*/
26+
public function getAuthString(): string {
27+
return base64_encode($this->username . ':' . $this->password);
28+
}
29+
30+
/**
31+
* Returns true since the basic authentication strategy requires authentication
32+
*
33+
* @return bool true
34+
*/
35+
public function requiresAuthentication(): bool {
36+
return true;
37+
}
38+
}

Diff for: src/Twilio/AuthStrategy/NoAuthStrategy.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Twilio\AuthStrategy;
3+
4+
/**
5+
* Class NoAuthStrategy
6+
* Implementation of the AuthStrategy for No Authentication
7+
*/
8+
9+
class NoAuthStrategy extends AuthStrategy {
10+
11+
public function __construct() {
12+
parent::__construct("noauth");
13+
}
14+
15+
/**
16+
* Returns an empty string since no authentication is required
17+
*
18+
* @return string an empty string
19+
*/
20+
public function getAuthString(): string {
21+
return "";
22+
}
23+
24+
/**
25+
* Returns false since the NoAuthStrategy does not require authentication
26+
*
27+
* @return bool false
28+
*/
29+
public function requiresAuthentication(): bool {
30+
return false;
31+
}
32+
}

Diff for: src/Twilio/AuthStrategy/TokenAuthStrategy.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
namespace Twilio\AuthStrategy;
3+
4+
use Twilio\Http\BearerToken\TokenManager;
5+
use Twilio\Jwt\JWT;
6+
7+
/**
8+
* Class TokenAuthStrategy
9+
* Implementation of the AuthStrategy for Bearer Token Authentication
10+
* @property string $token The bearer token
11+
* @property TokenManager $tokenManager The manager for the bearer token
12+
*/
13+
14+
class TokenAuthStrategy extends AuthStrategy {
15+
private $token;
16+
private $tokenManager;
17+
18+
public function __construct(TokenManager $tokenManager) {
19+
parent::__construct("token");
20+
$this->tokenManager = $tokenManager;
21+
}
22+
23+
/**
24+
* Checks if the token is expired or not
25+
*
26+
* @param string $token the token to be checked
27+
*
28+
* @return bool whether the token is expired or not
29+
*/
30+
public function isTokenExpired(string $token): bool {
31+
// Decode the JWT token
32+
$decodedToken = JWT::decode($token);
33+
34+
// If the token doesn't have an expiration, consider it expired
35+
if($decodedToken === null || $decodedToken->exp === null) {
36+
return false;
37+
}
38+
39+
// Calculate the expiration time with a buffer of 30 seconds
40+
$expiresAt = $decodedToken->exp * 1000;
41+
$bufferMilliseconds = 30 * 1000;
42+
$bufferExpiresAt = $expiresAt - $bufferMilliseconds;
43+
44+
// Return true if the current time is after the expiration time with buffer
45+
return time() > $bufferExpiresAt;
46+
}
47+
48+
/**
49+
* Fetches the bearer token
50+
*
51+
* @return string the bearer token
52+
*/
53+
public function fetchToken(): string {
54+
if(empty($this->token) || $this->isTokenExpired($this->token)) {
55+
$this->token = $this->tokenManager->fetchToken();
56+
}
57+
return $this->token;
58+
}
59+
60+
/**
61+
* Returns the bearer token authentication string
62+
*
63+
* @return string the bearer token authentication string
64+
*/
65+
public function getAuthString(): string {
66+
return "Bearer " . $this->fetchToken();
67+
}
68+
69+
/**
70+
* Returns true since the bearer token authentication strategy requires authentication
71+
*
72+
* @return bool true
73+
*/
74+
public function requiresAuthentication(): bool {
75+
return true;
76+
}
77+
}

Diff for: src/Twilio/Http/BearerToken/TokenManager.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
namespace Twilio\Http\BearerToken;
3+
4+
/**
5+
* Class TokenManager
6+
* Abstract parent class for all token managers
7+
* @property string $token The bearer token
8+
* @property string $tokenManager The manager for the bearer token
9+
*/
10+
11+
abstract class TokenManager {
12+
/**
13+
* Fetches the bearer token
14+
*
15+
* @return string the bearer token
16+
*/
17+
abstract public function fetchToken(): string;
18+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
4+
namespace Twilio\Tests\Unit\AuthStrategy;
5+
6+
use Twilio\Tests\Unit\UnitTest;
7+
use Twilio\AuthStrategy\BasicAuthStrategy;
8+
9+
class BasicAuthStrategyTest extends UnitTest {
10+
/**
11+
* @var string
12+
*/
13+
private $username;
14+
/**
15+
* @var string
16+
*/
17+
private $password;
18+
/**
19+
* @var BasicAuthStrategy
20+
*/
21+
private $basicAuthStrategy;
22+
23+
public function setUp(): void {
24+
parent::setUp();
25+
$this->username = "username";
26+
$this->password = "password";
27+
$this->basicAuthStrategy = new BasicAuthStrategy($this->username, $this->password);
28+
}
29+
30+
public function testAuthType(): void {
31+
$this->assertEquals('basic', $this->basicAuthStrategy->getAuthType());
32+
}
33+
34+
public function testAuthString(): void {
35+
$auth = base64_encode($this->username . ':' . $this->password);
36+
$this->assertEquals($auth, $this->basicAuthStrategy->getAuthString());
37+
}
38+
39+
public function testRequiresAuthentication(): void {
40+
$this->assertTrue($this->basicAuthStrategy->requiresAuthentication());
41+
}
42+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
4+
namespace Twilio\Tests\Unit\AuthStrategy;
5+
6+
use Twilio\Tests\Unit\UnitTest;
7+
use Twilio\AuthStrategy\NoAuthStrategy;
8+
9+
class NoAuthStrategyTest extends UnitTest {
10+
/**
11+
* @var NoAuthStrategy
12+
*/
13+
private $noAuthStrategy;
14+
15+
public function setUp(): void {
16+
parent::setUp();
17+
$this->noAuthStrategy = new NoAuthStrategy();
18+
}
19+
20+
public function testAuthType(): void {
21+
$this->assertEquals('noauth', $this->noAuthStrategy->getAuthType());
22+
}
23+
24+
public function testEmptyAuthString(): void {
25+
$this->assertEquals('', $this->noAuthStrategy->getAuthString());
26+
}
27+
28+
public function testRequiresAuthentication(): void {
29+
$this->assertFalse($this->noAuthStrategy->requiresAuthentication());
30+
}
31+
}

0 commit comments

Comments
 (0)