From d4c39b7b738f19c97c9e069728414612b78ad960 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Wed, 16 Jun 2021 11:14:08 +0100 Subject: [PATCH 1/9] Add session by ID API Endpoint --- app/controllers/api/account.php | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 13639c912..691adf6f2 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -892,6 +892,7 @@ App::get('/v1/account/sessions') ]), Response::MODEL_SESSION_LIST); }); + App::get('/v1/account/logs') ->desc('Get Account Logs') ->groups(['api', 'account']) @@ -968,6 +969,46 @@ App::get('/v1/account/logs') $response->dynamic(new Document(['logs' => $output]), Response::MODEL_LOG_LIST); }); +App::get('/v1/account/sessions/:sessionId') + ->desc('Get Session By ID') + ->groups(['api', 'account']) + ->label('scope', 'account') + ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT]) + ->label('sdk.namespace', 'account') + ->label('sdk.method', 'getSessions') + ->label('sdk.description', '/docs/references/account/get-sessions.md') + ->label('sdk.response.code', Response::STATUS_CODE_OK) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_SESSION_LIST) + ->param('sessionId', null, new UID(), 'Session unique ID. Use the string \'current\' to get the current device session.') + ->inject('response') + ->inject('user') + ->inject('locale') + ->inject('projectDB') + ->action(function ($sessionId, $response, $user, $locale, $projectDB) { + /** @var Appwrite\Utopia\Response $response */ + /** @var Appwrite\Database\Document $user */ + /** @var Utopia\Locale\Locale $locale */ + + $sessionId = ($sessionId === 'current') + ? Auth::sessionVerify($user->getAttribute('sessions'), Auth::$secret) + : $sessionId; + + $session = $projectDB->getCollectionFirst([ // Get user by email address + 'limit' => 1, + 'filters' => [ + '$collection='.Database::SYSTEM_COLLECTION_SESSIONS, + '$id='.$sessionId, + ], + ]); + + if ($session == false) { + throw new Exception('Session not found', 404); + }; + + $response->dynamic($session, Response::MODEL_SESSION); + }); + App::patch('/v1/account/name') ->desc('Update Account Name') ->groups(['api', 'account']) From e3273bc49fa4574d22e5724a5cff040d8aa8588d Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Wed, 16 Jun 2021 11:41:15 +0100 Subject: [PATCH 2/9] Add Tests --- .../Account/AccountCustomClientTest.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 77a4f9368..e3d5587d5 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -424,4 +424,39 @@ class AccountCustomClientTest extends Scope return []; } + + public function testGetSessionByID() { + $session = $this->testCreateAnonymousAccount(); + + $response = $this->client->call(Client::METHOD_GET, '/account/sessions/current', 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'], 200); + $this->assertEquals($response['body']['provider'], 'anonymous'); + + $sessionID = $response['body']['$id']; + + $response = $this->client->call(Client::METHOD_GET, '/account/sessions/'.$sessionID, 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'], 200); + $this->assertEquals($response['body']['provider'], 'anonymous'); + + $response = $this->client->call(Client::METHOD_GET, '/account/sessions/97823askjdkasd80921371980', 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'], 404); + } } \ No newline at end of file From cdb8fb82868d37b4efa26176b2b6640918352c41 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Wed, 16 Jun 2021 11:48:12 +0100 Subject: [PATCH 3/9] Implement suggestions --- app/controllers/api/account.php | 13 ++++++++----- docs/references/account/get-session.md | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 docs/references/account/get-session.md diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 691adf6f2..ffb6c2cf0 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -892,7 +892,6 @@ App::get('/v1/account/sessions') ]), Response::MODEL_SESSION_LIST); }); - App::get('/v1/account/logs') ->desc('Get Account Logs') ->groups(['api', 'account']) @@ -975,11 +974,11 @@ App::get('/v1/account/sessions/:sessionId') ->label('scope', 'account') ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT]) ->label('sdk.namespace', 'account') - ->label('sdk.method', 'getSessions') - ->label('sdk.description', '/docs/references/account/get-sessions.md') + ->label('sdk.method', 'getSession') + ->label('sdk.description', '/docs/references/account/get-session.md') ->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_SESSION_LIST) + ->label('sdk.response.model', Response::MODEL_SESSION) ->param('sessionId', null, new UID(), 'Session unique ID. Use the string \'current\' to get the current device session.') ->inject('response') ->inject('user') @@ -994,7 +993,7 @@ App::get('/v1/account/sessions/:sessionId') ? Auth::sessionVerify($user->getAttribute('sessions'), Auth::$secret) : $sessionId; - $session = $projectDB->getCollectionFirst([ // Get user by email address + $session = $projectDB->getCollectionFirst([ // Get user by sessionId 'limit' => 1, 'filters' => [ '$collection='.Database::SYSTEM_COLLECTION_SESSIONS, @@ -1006,6 +1005,10 @@ App::get('/v1/account/sessions/:sessionId') throw new Exception('Session not found', 404); }; + $session->setAttribute('countryName', (isset($countries[strtoupper($session->getAttribute('countryCode'))])) + ? $countries[strtoupper($session->getAttribute('countryCode'))] + : $locale->getText('locale.country.unknown')); + $response->dynamic($session, Response::MODEL_SESSION); }); diff --git a/docs/references/account/get-session.md b/docs/references/account/get-session.md new file mode 100644 index 000000000..91b58fa48 --- /dev/null +++ b/docs/references/account/get-session.md @@ -0,0 +1 @@ +Use this endpoint to get a logged in user's session using a Session ID. Inputting 'current' will return the current session being used. \ No newline at end of file From 320cc97ad8ca0ca7c472724e2d283b77f984ecb2 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Wed, 16 Jun 2021 12:48:33 +0100 Subject: [PATCH 4/9] Replace getCollectionFirst with getDocument Should be faster due to redis caching --- app/controllers/api/account.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index ffb6c2cf0..6580e415e 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -993,15 +993,9 @@ App::get('/v1/account/sessions/:sessionId') ? Auth::sessionVerify($user->getAttribute('sessions'), Auth::$secret) : $sessionId; - $session = $projectDB->getCollectionFirst([ // Get user by sessionId - 'limit' => 1, - 'filters' => [ - '$collection='.Database::SYSTEM_COLLECTION_SESSIONS, - '$id='.$sessionId, - ], - ]); + $session = $projectDB->getDocument($sessionId); // get user by session ID - if ($session == false) { + if (empty($session->getId()) || Database::SYSTEM_COLLECTION_SESSIONS != $session->getCollection()) { throw new Exception('Session not found', 404); }; From fa918f952300002fc7c2bfb26364ed1238fc73c2 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Wed, 16 Jun 2021 14:48:30 +0100 Subject: [PATCH 5/9] Add Eldad's Suggestions --- app/controllers/api/account.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 6580e415e..2e8fb1a5c 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -988,6 +988,7 @@ App::get('/v1/account/sessions/:sessionId') /** @var Appwrite\Utopia\Response $response */ /** @var Appwrite\Database\Document $user */ /** @var Utopia\Locale\Locale $locale */ + /** @var Appwrite\Database\Database $projectDB */ $sessionId = ($sessionId === 'current') ? Auth::sessionVerify($user->getAttribute('sessions'), Auth::$secret) @@ -995,7 +996,7 @@ App::get('/v1/account/sessions/:sessionId') $session = $projectDB->getDocument($sessionId); // get user by session ID - if (empty($session->getId()) || Database::SYSTEM_COLLECTION_SESSIONS != $session->getCollection()) { + if ($session->isEmpty() || Database::SYSTEM_COLLECTION_SESSIONS != $session->getCollection()) { throw new Exception('Session not found', 404); }; From 40e9fa9b368242aeed188a91e640c5c23e3af3c0 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 17 Jun 2021 10:08:01 +0100 Subject: [PATCH 6/9] Add Christy's Suggestions --- app/controllers/api/account.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 2e8fb1a5c..1f6f99199 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -878,9 +878,11 @@ App::get('/v1/account/sessions') foreach ($sessions as $key => $session) { /** @var Document $session */ - $session->setAttribute('countryName', (isset($countries[strtoupper($session->getAttribute('countryCode'))])) - ? $countries[strtoupper($session->getAttribute('countryCode'))] - : $locale->getText('locale.country.unknown')); + $countryName = (isset($countries[strtoupper($session->getAttribute('countryCode'))])) + ? $countries[strtoupper($session->getAttribute('countryCode'))] + : $locale->getText('locale.country.unknown'); + + $session->setAttribute('countryName', $countryName); $session->setAttribute('current', ($current == $session->getId()) ? true : false); $sessions[$key] = $session; From 978668cbb92ef96da71f43103ab32d597edc6670 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 17 Jun 2021 10:33:57 +0100 Subject: [PATCH 7/9] Implement changes on other endpoints --- app/controllers/api/account.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 1f6f99199..123cd9fc4 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -254,9 +254,13 @@ App::post('/v1/account/sessions') $countries = $locale->getText('countries'); + $countryName = isset($countries[strtoupper($session->getAttribute('countryCode'))]) + ? $countries[strtoupper($session->getAttribute('countryCode'))] + : $locale->getText('locale.country.unknown'); + $session ->setAttribute('current', true) - ->setAttribute('countryName', (isset($countries[strtoupper($session->getAttribute('countryCode'))])) ? $countries[strtoupper($session->getAttribute('countryCode'))] : $locale->getText('locale.country.unknown')) + ->setAttribute('countryName', $countryName) ; $response->dynamic($session, Response::MODEL_SESSION); @@ -753,9 +757,13 @@ App::post('/v1/account/sessions/anonymous') ->setStatusCode(Response::STATUS_CODE_CREATED) ; + $countryName = isset($countries[strtoupper($session->getAttribute('countryCode'))]) + ? $countries[strtoupper($session->getAttribute('countryCode'))] + : $locale->getText('locale.country.unknown'); + $session ->setAttribute('current', true) - ->setAttribute('countryName', (isset($countries[$session->getAttribute('countryCode')])) ? $countries[$session->getAttribute('countryCode')] : $locale->getText('locale.country.unknown')) + ->setAttribute('countryName', $countryName) ; $response->dynamic($session, Response::MODEL_SESSION); @@ -1001,10 +1009,12 @@ App::get('/v1/account/sessions/:sessionId') if ($session->isEmpty() || Database::SYSTEM_COLLECTION_SESSIONS != $session->getCollection()) { throw new Exception('Session not found', 404); }; - - $session->setAttribute('countryName', (isset($countries[strtoupper($session->getAttribute('countryCode'))])) + + $countryName = (isset($countries[strtoupper($session->getAttribute('countryCode'))])) ? $countries[strtoupper($session->getAttribute('countryCode'))] - : $locale->getText('locale.country.unknown')); + : $locale->getText('locale.country.unknown'); + + $session->setAttribute('countryName', $countryName); $response->dynamic($session, Response::MODEL_SESSION); }); From 88bb46ec95ce7cc5451eb9874a56e6467886a794 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 17 Jun 2021 10:41:04 +0100 Subject: [PATCH 8/9] Update account.php --- app/controllers/api/account.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 123cd9fc4..f85938045 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -254,7 +254,7 @@ App::post('/v1/account/sessions') $countries = $locale->getText('countries'); - $countryName = isset($countries[strtoupper($session->getAttribute('countryCode'))]) + $countryName = (isset($countries[strtoupper($session->getAttribute('countryCode'))])) ? $countries[strtoupper($session->getAttribute('countryCode'))] : $locale->getText('locale.country.unknown'); @@ -757,7 +757,7 @@ App::post('/v1/account/sessions/anonymous') ->setStatusCode(Response::STATUS_CODE_CREATED) ; - $countryName = isset($countries[strtoupper($session->getAttribute('countryCode'))]) + $countryName = (isset($countries[strtoupper($session->getAttribute('countryCode'))])) ? $countries[strtoupper($session->getAttribute('countryCode'))] : $locale->getText('locale.country.unknown'); From aa41382b265da11d9a1fc653f0601ee041e47c42 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 17 Jun 2021 11:05:57 +0100 Subject: [PATCH 9/9] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 0dd92eeff..37cbff360 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,7 @@ - Add Provider Icon to each Session - Add Anonymous Account Placeholder - Upgraded telegraf docker image version to v1.1.0 +- Added new endpoint to get a session based on it's ID (#1294) ## Bugs