-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved authentication logic to individual handlers (#283)
- Loading branch information
1 parent
6d85bd2
commit 5360821
Showing
12 changed files
with
324 additions
and
78 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,28 @@ | ||
<?php | ||
|
||
namespace Vonage\Client\Credentials\Handler; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Vonage\Client\Credentials\Container; | ||
use Vonage\Client\Credentials\CredentialsInterface; | ||
|
||
abstract class AbstractHandler | ||
{ | ||
abstract function __invoke(RequestInterface $request, CredentialsInterface $credentials); | ||
|
||
protected function extract(string $class, CredentialsInterface $credentials): CredentialsInterface | ||
{ | ||
if ($credentials instanceof $class) { | ||
return $credentials; | ||
} | ||
|
||
if ($credentials instanceof Container) { | ||
$creds = $credentials->get($class); | ||
if (!is_null($creds)) { | ||
return $creds; | ||
} | ||
} | ||
|
||
throw new \RuntimeException('Requested auth type not found'); | ||
} | ||
} |
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 Vonage\Client\Credentials\Handler; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Vonage\Client\Credentials\Basic; | ||
use Vonage\Client\Credentials\CredentialsInterface; | ||
|
||
class BasicHandler extends AbstractHandler | ||
{ | ||
public function __invoke(RequestInterface $request, CredentialsInterface $credentials) | ||
{ | ||
$credentials = $this->extract(Basic::class, $credentials); | ||
|
||
$c = $credentials->asArray(); | ||
$cx = base64_encode($c['api_key'] . ':' . $c['api_secret']); | ||
|
||
$request = $request->withHeader('Authorization', 'Basic ' . $cx); | ||
|
||
return $request; | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace Vonage\Client\Credentials\Handler; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Vonage\Client\Credentials\CredentialsInterface; | ||
|
||
interface HandlerInterface | ||
{ | ||
/** | ||
* Add authentication to a request | ||
*/ | ||
function __invoke(RequestInterface $request, CredentialsInterface $credentials): RequestInterface; | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Vonage\Client\Credentials\Handler; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Vonage\Client\Credentials\CredentialsInterface; | ||
use Vonage\Client\Credentials\Keypair; | ||
|
||
class KeypairHandler extends AbstractHandler | ||
{ | ||
public function __invoke(RequestInterface $request, CredentialsInterface $credentials) | ||
{ | ||
/** @var Keypair $credentials */ | ||
$credentials = $this->extract(Keypair::class, $credentials); | ||
$token = $credentials->generateJwt(); | ||
|
||
return $request->withHeader('Authorization', 'Bearer ' . $token->toString()); | ||
} | ||
} |
Oops, something went wrong.