1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00

Pass session to non-web clients

This commit is contained in:
Eldad Fux 2020-04-08 20:04:46 +03:00
parent 0e134d2c71
commit 73d4012934
4 changed files with 63 additions and 9 deletions

View file

@ -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',

View file

@ -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);
}
);

View file

@ -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);
}
}

View file

@ -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&param2=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&param2=value2', $result);
}
}