forked from php-amqplib/php-amqplib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement updatePassword method to allow long-running connections to …
…reauthenticate
- Loading branch information
1 parent
804dd61
commit bb7d914
Showing
4 changed files
with
96 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
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,44 @@ | ||
<?php | ||
|
||
include(__DIR__ . '/config.php'); | ||
|
||
use PhpAmqpLib\Connection\AMQPStreamConnection; | ||
|
||
|
||
require_once __DIR__ . '/../tests/generate_jwt_keys.php'; | ||
|
||
// you should implement your own logic to provide tokens to authorize against RabbitMQ broker from your OAuth server | ||
function getNextOauth2Token() { | ||
static $tokens = [ | ||
JWT_TOKEN_1, // connect using this token | ||
JWT_TOKEN_2, // upgrade on first refresh | ||
]; | ||
|
||
if (empty($tokens)) { | ||
return "invalidToken"; // fail on 2nd refresh | ||
} | ||
|
||
return array_shift($tokens); | ||
} | ||
|
||
$connection = new AMQPStreamConnection(HOST, PORT, posix_getpid(), getNextOauth2Token(), VHOST); | ||
|
||
// for workers running longer than the token expiration time, YOU HAVE TO REFRESH TOKEN proactively | ||
// we will use pcntl alarm for this purpose - we are refreshing every 5 seconds | ||
|
||
pcntl_async_signals(true); | ||
pcntl_signal(SIGALRM, function () use ($connection) { | ||
echo "Refreshing token...\n"; | ||
$connection->updatePassword(getNextOauth2Token()); // this will fail on 2nd attempt - see getNextOauth2Token | ||
pcntl_alarm(5); | ||
}, true); | ||
pcntl_alarm(5); | ||
|
||
register_shutdown_function(function () use ($connection) { | ||
$connection->close(); | ||
}); | ||
|
||
while (true) { | ||
echo "Connection is ", ($connection->isConnected() ? "connected" : "not connected"), "\n"; | ||
sleep(1); | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace Functional\Connection; | ||
|
||
use PhpAmqpLib\Connection\AMQPStreamConnection; | ||
use PhpAmqpLib\Exception\AMQPConnectionClosedException; | ||
use PhpAmqpLib\Tests\Functional\AbstractConnectionTest; | ||
|
||
/** | ||
* @group connection | ||
*/ | ||
class OAuth2ConnectionTest extends AbstractConnectionTest | ||
{ | ||
/** | ||
* @test | ||
* @covers \PhpAmqpLib\Connection\AbstractConnection::connection_update_secret_ok() | ||
* @covers \PhpAmqpLib\Connection\AbstractConnection::x_update_secret() | ||
* @covers \PhpAmqpLib\Connection\AbstractConnection::updatePassword() | ||
*/ | ||
public function update_password() | ||
{ | ||
$conn = new AMQPStreamConnection(HOST, PORT, 'oauth', JWT_TOKEN_1, '/', false, 'PLAIN', null, 'en_US', 1); | ||
$conn->updatePassword(JWT_TOKEN_2); | ||
self::assertTrue($conn->isConnected()); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers \PhpAmqpLib\Connection\AbstractConnection::x_update_secret() | ||
* @covers \PhpAmqpLib\Connection\AbstractConnection::updatePassword() | ||
*/ | ||
public function update_password_invalid() | ||
{ | ||
$this->expectException(AMQPConnectionClosedException::class); | ||
$this->expectExceptionMessage('New secret was refused'); | ||
|
||
$conn = new AMQPStreamConnection(HOST, PORT, 'oauth', JWT_TOKEN_1, '/', false, 'PLAIN', null, 'en_US', 1); | ||
$conn->updatePassword('invalidJwt'); | ||
} | ||
} |