-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace authToken with authProvider (#14)
Co-authored-by: Pete Gautier <pete@momentohq.com>
- Loading branch information
1 parent
9e40217
commit 6852fbb
Showing
7 changed files
with
158 additions
and
61 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,27 @@ | ||
<?php | ||
|
||
namespace Momento\Auth; | ||
|
||
use Momento\Cache\Errors\InvalidArgumentError; | ||
|
||
class AuthUtils | ||
{ | ||
|
||
private static function throwBadAuthToken() { | ||
throw new InvalidArgumentError('Invalid Momento auth token.'); | ||
} | ||
|
||
public static function parseAuthToken(string $authToken) : array { | ||
$exploded = explode (".", $authToken); | ||
if (count($exploded) != 3) { | ||
self::throwBadAuthToken(); | ||
} | ||
list($header, $payload, $signature) = $exploded; | ||
$token = json_decode(base64_decode($payload), true); | ||
if ($token === null) { | ||
self::throwBadAuthToken(); | ||
} | ||
return $token; | ||
} | ||
|
||
} |
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,41 @@ | ||
<?php | ||
namespace Momento\Auth; | ||
|
||
use Momento\Auth\AuthUtils; | ||
use Momento\Cache\Errors\InvalidArgumentError; | ||
use function \Momento\Utilities\isNullOrEmpty; | ||
|
||
|
||
class EnvMomentoTokenProvider implements ICredentialProvider | ||
{ | ||
private string $authToken; | ||
private string $controlEndpoint; | ||
private string $cacheEndpoint; | ||
|
||
public function __construct(string $envVariableName) | ||
{ | ||
$authToken = getenv($envVariableName); | ||
if (isNullOrEmpty($authToken)) { | ||
throw new InvalidArgumentError("Environment variable $envVariableName is empty or null."); | ||
} | ||
$payload = AuthUtils::parseAuthToken($authToken); | ||
$this->authToken = $authToken; | ||
$this->controlEndpoint = $payload["cp"]; | ||
$this->cacheEndpoint = $payload["c"]; | ||
} | ||
|
||
public function getAuthToken() : string | ||
{ | ||
return $this->authToken; | ||
} | ||
|
||
public function getCacheEndpoint() : string | ||
{ | ||
return $this->cacheEndpoint; | ||
} | ||
|
||
public function getControlEndpoint() : string | ||
{ | ||
return $this->controlEndpoint; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
namespace Momento\Auth; | ||
|
||
interface ICredentialProvider | ||
{ | ||
public function getAuthToken() : string; | ||
public function getControlEndpoint() : string; | ||
public function getCacheEndpoint() : 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
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
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
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