Skip to content

Commit 7478bda

Browse files
author
Greg Bowler
committed
feature: fake login
for #14
1 parent 1f60dfe commit 7478bda

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

Diff for: src/Authenticator.php

+44-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use Authwave\ProviderUri\LogoutUri;
88
use Authwave\ProviderUri\ProfileUri;
99
use Authwave\ResponseData\UserResponseData;
10+
use Gt\Cipher\InitVector;
1011
use Gt\Cipher\Key;
1112
use Gt\Cipher\Message\EncryptedMessage;
13+
use Gt\Cipher\Message\PlainTextMessage;
1214
use Gt\Http\Uri;
1315
use Gt\Logger\Log;
1416
use Gt\Session\SessionContainer;
@@ -17,6 +19,7 @@
1719
class Authenticator {
1820
const SESSION_STORE_KEY = "AUTHWAVE_CONSUMER_SESSION";
1921
const RESPONSE_QUERY_PARAMETER = "AUTHWAVE_RESPONSE_DATA";
22+
const FAKE_EMAIL = "[email protected]";
2023

2124
private SessionData $sessionData;
2225
private User $user;
@@ -84,9 +87,47 @@ public function logout(Token $token = null):void {
8487
$token = new Token($this->secret);
8588
}
8689

87-
$this->sessionData = new SessionData($token);
88-
$this->session->set(SessionData::class, $this->sessionData);
89-
$this->redirectHandler->redirect($this->getLogoutUri($token));
90+
if($this->user->email === self::FAKE_EMAIL) {
91+
$this->session->remove(SessionData::class);
92+
unset($this->user);
93+
}
94+
else {
95+
$this->redirectHandler->redirect($this->getLogoutUri($token));
96+
$this->sessionData = new SessionData($token);
97+
$this->session->set(SessionData::class, $this->sessionData);
98+
}
99+
}
100+
101+
public function fakeLogin(string $userId, string $redirectTo = "/"):void {
102+
$secretIv = new InitVector();
103+
$token = new Token($this->secret, $secretIv);
104+
$sessionData = new SessionData($token);
105+
$this->session->set(SessionData::class, $sessionData);
106+
107+
$userData = new UserResponseData(
108+
$userId,
109+
self::FAKE_EMAIL,
110+
);
111+
112+
$this->session->set(
113+
SessionData::class,
114+
new SessionData($token, $userData)
115+
);
116+
117+
$message = new PlainTextMessage(
118+
json_encode([
119+
"id" => $userData->getId(),
120+
"email" => $userData->getEmail(),
121+
]),
122+
$secretIv,
123+
);
124+
125+
$cipherText = $message->encrypt(new Key($this->secret));
126+
$queryString = http_build_query([
127+
"AUTHWAVE_RESPONSE_DATA" => (string)$cipherText,
128+
]);
129+
$uri = new Uri("$redirectTo?$queryString");
130+
$this->redirectHandler->redirect($uri);
90131
}
91132

92133
public function getUser():User {
@@ -140,7 +181,6 @@ private function completeAuth():void {
140181
}
141182

142183
if(!isset($this->sessionData)) {
143-
die("No session data");
144184
$this->tidyResponseData();
145185
return;
146186
}

0 commit comments

Comments
 (0)