-
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.
Merge pull request #5 from sjspereira/develop
Branch Merger
- Loading branch information
Showing
70 changed files
with
2,487 additions
and
239 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sjpereira\AzureStoragePhpSdk\Authentication; | ||
|
||
use DateTime; | ||
use GuzzleHttp\Client; | ||
use Psr\Http\Client\RequestExceptionInterface; | ||
use Sjpereira\AzureStoragePhpSdk\BlobStorage\Enums\HttpVerb; | ||
use Sjpereira\AzureStoragePhpSdk\Contracts\Authentication\Auth; | ||
use Sjpereira\AzureStoragePhpSdk\Exceptions\RequestException; | ||
use Sjpereira\AzureStoragePhpSdk\Http\{Headers}; | ||
|
||
final class MicrosoftEntraId implements Auth | ||
{ | ||
protected string $token = ''; | ||
|
||
protected ?DateTime $tokenExpiresAt = null; | ||
|
||
public function __construct( | ||
protected string $account, | ||
protected string $directoryId, | ||
protected string $applicationId, | ||
protected string $applicationSecret, | ||
) { | ||
// | ||
} | ||
|
||
public function getDate(): string | ||
{ | ||
return gmdate('D, d M Y H:i:s T'); | ||
} | ||
|
||
public function getAccount(): string | ||
{ | ||
return $this->account; | ||
} | ||
|
||
public function getAuthentication( | ||
HttpVerb $verb, | ||
Headers $headers, | ||
string $resource, | ||
): string { | ||
if (!empty($this->token) && $this->tokenExpiresAt > new DateTime()) { | ||
return $this->token; | ||
} | ||
|
||
$this->authenticate(); | ||
|
||
return $this->token; | ||
} | ||
|
||
protected function authenticate(): void | ||
{ | ||
try { | ||
$response = (new Client())->post("https://login.microsoftonline.com/{$this->directoryId}/oauth2/v2.0/token", [ | ||
'form_params' => [ | ||
'grant_type' => 'client_credentials', | ||
'client_id' => $this->applicationId, | ||
'client_secret' => $this->applicationSecret, | ||
'scope' => 'https://storage.azure.com/.default', | ||
], | ||
]); | ||
} catch (RequestExceptionInterface $e) { | ||
throw RequestException::createFromRequestException($e); | ||
} | ||
|
||
/** @var array{token_type: string, expires_in: int, access_token: string} */ | ||
$body = json_decode((string) $response->getBody(), true); | ||
|
||
$this->token = "{$body['token_type']} {$body['access_token']}"; | ||
|
||
$this->tokenExpiresAt = (new DateTime())->modify("+{$body['expires_in']} seconds"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sjpereira\AzureStoragePhpSdk\BlobStorage\Concerns; | ||
|
||
use Sjpereira\AzureStoragePhpSdk\Exceptions\InvalidArgumentException; | ||
|
||
trait ValidateContainerName | ||
{ | ||
/** @throws InvalidArgumentException */ | ||
protected function validateContainerName(string $name): void | ||
{ | ||
$replaced = preg_replace('/[^a-z0-9-]/', '', $name); | ||
|
||
if ($replaced !== $name) { | ||
throw InvalidArgumentException::create("Invalid container name: {$name}"); | ||
} | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sjpereira\AzureStoragePhpSdk\BlobStorage\Concerns; | ||
|
||
use Sjpereira\AzureStoragePhpSdk\Exceptions\InvalidArgumentException; | ||
|
||
trait ValidateMetadataKey | ||
{ | ||
/** @throws InvalidArgumentException */ | ||
protected function validateMetadataKey(string $key): void | ||
{ | ||
$message = "Invalid metadata key: {$key}."; | ||
|
||
if (is_numeric($key[0])) { | ||
throw InvalidArgumentException::create("{$message} Metadata keys cannot start with a number."); | ||
} | ||
|
||
$name = preg_replace('/[^a-z0-9_]/i', '', $key); | ||
|
||
if ($key !== $name) { | ||
throw InvalidArgumentException::create("{$message} Only alphanumeric characters and underscores are allowed."); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.