Compare commits

...
Author SHA1 Message Date
Matej Bačo 9beb9bf072 Add tests 2024-01-23 13:50:14 +00:00
Matej Bačo 3815aa90be Remember feature for email&pass auth 2024-01-23 10:20:02 +00:00
2 changed files with 58 additions and 4 deletions
+5 -4
View File
@@ -205,6 +205,7 @@ App::post('/v1/account/sessions/email')
->label('abuse-key', 'url:{url},email:{param-email}')
->param('email', '', new Email(), 'User email.')
->param('password', '', new Password(), 'User password. Must be at least 8 chars.')
->param('remember', true, new Boolean(), 'Toggle persistency of session secrets on the client device. Enabled by default, and if disabled, session cookie will expire when user leaves the website, closes browser, or quits the application.', true)
->inject('request')
->inject('response')
->inject('user')
@@ -213,7 +214,7 @@ App::post('/v1/account/sessions/email')
->inject('locale')
->inject('geodb')
->inject('queueForEvents')
->action(function (string $email, string $password, Request $request, Response $response, Document $user, Database $dbForProject, Document $project, Locale $locale, Reader $geodb, Event $queueForEvents) {
->action(function (string $email, string $password, bool $remember, Request $request, Response $response, Document $user, Database $dbForProject, Document $project, Locale $locale, Reader $geodb, Event $queueForEvents) {
$email = \strtolower($email);
$protocol = $request->getProtocol();
@@ -283,11 +284,11 @@ App::post('/v1/account/sessions/email')
;
}
$expire = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), $duration));
$expire = !$remember ? null : (new \DateTime(DateTime::formatTz(DateTime::addSeconds(new \DateTime(), $duration))))->getTimestamp();
$response
->addCookie(Auth::$cookieName . '_legacy', Auth::encodeSession($user->getId(), $secret), (new \DateTime($expire))->getTimestamp(), '/', Config::getParam('cookieDomain'), ('https' == $protocol), true, null)
->addCookie(Auth::$cookieName, Auth::encodeSession($user->getId(), $secret), (new \DateTime($expire))->getTimestamp(), '/', Config::getParam('cookieDomain'), ('https' == $protocol), true, Config::getParam('cookieSamesite'))
->addCookie(Auth::$cookieName . '_legacy', Auth::encodeSession($user->getId(), $secret), $expire, '/', Config::getParam('cookieDomain'), ('https' == $protocol), true, null)
->addCookie(Auth::$cookieName, Auth::encodeSession($user->getId(), $secret), $expire, '/', Config::getParam('cookieDomain'), ('https' == $protocol), true, Config::getParam('cookieSamesite'))
->setStatusCode(Response::STATUS_CODE_CREATED)
;
@@ -300,4 +300,57 @@ trait AccountBase
$this->assertEquals(400, $response['headers']['status-code']);
$this->assertEquals('general_argument_invalid', $response['body']['type']);
}
public function testEmailPasswordRememberSession(): void
{
$response = $this->client->call(Client::METHOD_POST, '/account', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'userId' => ID::unique(),
'email' => 'emailpass@appwrite.io',
'password' => 'password123'
]);
$this->assertEquals(201, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'email' => 'emailpass@appwrite.io',
'password' => 'password123',
'remember' => true
]);
$this->assertEquals(201, $response['headers']['status-code']);
$this->assertStringContainsStringIgnoringCase('expires=', $response['headers']['set-cookie']);
$response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'email' => 'emailpass@appwrite.io',
'password' => 'password123',
'remember' => false
]);
$this->assertEquals(201, $response['headers']['status-code']);
$this->assertStringNotContainsStringIgnoringCase('expires=', $response['headers']['set-cookie']);
$session = $response['cookies']['a_session_' . $this->getProject()['$id']];
$response = $this->client->call(Client::METHOD_DELETE, '/account', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session,
]));
$this->assertEquals($response['headers']['status-code'], 204);
}
}