From 73d4012934c490769ba0ce548bcda4e8de64f87b Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Wed, 8 Apr 2020 20:04:46 +0300 Subject: [PATCH] Pass session to non-web clients --- app/controllers/api/account.php | 11 ++++++++-- app/controllers/api/locale.php | 9 +++++---- src/Appwrite/URL/URL.php | 36 ++++++++++++++++++++++++++++++--- tests/unit/URL/URLTest.php | 16 +++++++++++++++ 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index a87086de1a..2555c262f9 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -24,6 +24,7 @@ use Appwrite\Database\Validator\UID; use Appwrite\Database\Validator\Authorization; use Appwrite\Template\Template; use Appwrite\OpenSSL\OpenSSL; +use Appwrite\URL\URL as URLParser; use DeviceDetector\DeviceDetector; use GeoIp2\Database\Reader; @@ -480,7 +481,13 @@ $utopia->get('/v1/account/sessions/oauth2/:provider/redirect') } if($state['success'] === $oauthDefaultSuccess) { // Add keys for non-web platforms - $state['success'] = $state['success'].'/?end=true'; + $state['success'] = URLParser::parse($state['success']); + $query = URLParser::parseQuery($state['success']['query']); + $query['domain'] = COOKIE_DOMAIN; + $query['key'] = Auth::$cookieName; + $query['secret'] = Auth::encodeSession($user->getId(), $secret); + $state['success']['query'] = URLParser::unparseQuery($query); + $state['success'] = URLParser::unparse($state['success']); } $response @@ -502,7 +509,7 @@ $utopia->get('/v1/account') ->label('sdk.description', '/docs/references/account/get.md') ->label('sdk.response', ['200' => 'user']) ->action( - function () use ($response, &$user, $oauth2Keys) { + function () use ($response, &$user, $oauth2Keys) { $response->json(array_merge($user->getArrayCopy(array_merge( [ '$id', diff --git a/app/controllers/api/locale.php b/app/controllers/api/locale.php index eab8f5531a..2e48e45cb4 100644 --- a/app/controllers/api/locale.php +++ b/app/controllers/api/locale.php @@ -59,10 +59,11 @@ $utopia->get('/v1/locale') $output['currency'] = $currency; } - $response - ->addHeader('Cache-Control', 'public, max-age='.$time) - ->addHeader('Expires', date('D, d M Y H:i:s', time() + $time).' GMT') // 45 days cache - ->json($output); + $response->json($_COOKIE); + // $response + // ->addHeader('Cache-Control', 'public, max-age='.$time) + // ->addHeader('Expires', date('D, d M Y H:i:s', time() + $time).' GMT') // 45 days cache + // ->json($output); } ); diff --git a/src/Appwrite/URL/URL.php b/src/Appwrite/URL/URL.php index dc29a568a0..e49845c539 100644 --- a/src/Appwrite/URL/URL.php +++ b/src/Appwrite/URL/URL.php @@ -9,7 +9,7 @@ class URL * * Take a URL string and split it to array parts * - * @param $url string + * @param string $url * * @return array */ @@ -34,8 +34,8 @@ class URL * * Take URL parts and combine them to a valid string * - * @param $url array - * @param $ommit array + * @param array $url + * @param array $ommit * * @return string */ @@ -75,4 +75,34 @@ class URL return $parts['scheme'].$parts['user'].$parts['pass'].$parts['host'].$parts['port'].$parts['path'].$parts['query'].$parts['fragment']; } + + /** + * Parse Query String + * + * Convert query string to array + * + * @param string $query + * + * @return array + */ + static public function parseQuery(string $query):array + { + parse_str($query, $result); + + return $result; + } + + /** + * Un-Parse Query String + * + * Convert query string array to string + * + * @param string $query + * + * @return array + */ + static public function unparseQuery(array $query):string + { + return http_build_query($query); + } } \ No newline at end of file diff --git a/tests/unit/URL/URLTest.php b/tests/unit/URL/URLTest.php index c65bcfb438..469b77dd82 100644 --- a/tests/unit/URL/URLTest.php +++ b/tests/unit/URL/URLTest.php @@ -87,4 +87,20 @@ class URLTest extends TestCase $this->assertIsString($url); $this->assertEquals('https://eldad:fux@appwrite.io/#bottom', $url); } + + public function testParseQuery() + { + $result = URL::parseQuery('param1=value1¶m2=value2'); + + $this->assertIsArray($result); + $this->assertEquals(['param1' => 'value1', 'param2' => 'value2'], $result); + } + + public function testUnParseQuery() + { + $result = URL::unparseQuery(['param1' => 'value1', 'param2' => 'value2']); + + $this->assertIsString($result); + $this->assertEquals('param1=value1¶m2=value2', $result); + } } \ No newline at end of file