Skip to content

Commit

Permalink
Send actions as encrypted messages
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed May 18, 2020
1 parent be5d06a commit 77b7d2c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/ProviderUri/AbstractProviderUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ protected function normaliseBaseUri(string $baseUri):Uri {
protected function buildQuery(
Token $token,
string $currentPath,
string $data = null
string $message = null
):string {
return http_build_query([
self::QUERY_STRING_CIPHER => (string)$token->generateRequestCipher($data),
self::QUERY_STRING_CIPHER => (string)$token->generateRequestCipher($message),
self::QUERY_STRING_INIT_VECTOR => (string)$token->getIv(),
self::QUERY_STRING_CURRENT_PATH => bin2hex($currentPath),
]);
Expand Down
7 changes: 5 additions & 2 deletions src/ProviderUri/LogoutUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ public function __construct(
string $baseRemoteUri = self::DEFAULT_BASE_REMOTE_URI
) {
$baseRemoteUri = $this->normaliseBaseUri($baseRemoteUri);
$baseRemoteUri = $baseRemoteUri->withPath("/logout");

parent::__construct($baseRemoteUri);
$this->query = $this->buildQuery($token, $currentPath);
$this->query = $this->buildQuery(
$token,
$currentPath,
"action=logout"
);
}
}
49 changes: 45 additions & 4 deletions test/phpunit/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ public function testIsLoggedInTrueWhenSessionDataSet() {
self::assertTrue($sut->isLoggedIn());
}

// TODO: Session shouldn't be cleared on call to logout - instead it should
// redirect to the provider, and a new test should asset the response data
// contains a logout confirmation.
public function TODO_UPDATE_testLogoutClearsSession() {
public function testLogoutCallsLogoutUri() {
$sessionData = self::createMock(SessionData::class);
$_SESSION = [
Authenticator::SESSION_KEY => $sessionData
];

$redirectHandler = self::createMock(RedirectHandler::class);
$redirectHandler->expects(self::once())
->method("redirect")
->with(self::callback(fn(UriInterface $uri) =>
$uri->getHost() === "login.authwave.com"
&& $uri->getPath() === "/logout"
));

$sut = new Authenticator(
"test-key",
Expand All @@ -83,6 +86,44 @@ public function TODO_UPDATE_testLogoutClearsSession() {
$redirectHandler
);
$sut->logout();
self::assertNotEmpty($_SESSION);
}

public function testCompleteAuthFromLogoutClearsSession() {
$token = self::createMock(Token::class);

$sessionData = self::createMock(SessionData::class);
$sessionData->method("getToken")
->willReturn($token);

$_SESSION = [
Authenticator::SESSION_KEY => $sessionData,
];

$responseCipher = "abcdef";

$currentUri = "/example-page-" . uniqid();
$currentUri .= "?";
$currentUri .= http_build_query([
Authenticator::RESPONSE_QUERY_PARAMETER => $responseCipher,
]);

$redirectHandler = self::createMock(RedirectHandler::class);
$redirectHandler->expects(self::once())
->method("redirect")
->with(self::callback(fn(UriInterface $uri) =>
$uri->getHost() == ""
&& $uri->getPath() == $currentUri
));

new Authenticator(
"test-key",
"/",
LoginUri::DEFAULT_BASE_REMOTE_URI,
null,
$redirectHandler
);

self::assertEmpty($_SESSION);
}

Expand Down

0 comments on commit 77b7d2c

Please sign in to comment.