1
0
Fork 0
mirror of synced 2024-06-26 18:20:43 +12:00

Replaced web platform url with hostname

This commit is contained in:
Eldad Fux 2020-03-05 00:54:49 +02:00
parent 8f392ec228
commit 92babbd21a
5 changed files with 58 additions and 75 deletions

View file

@ -34,34 +34,34 @@ $usage = new Event('v1-usage', 'UsageV1');
* + Filter for duplicated entries
*/
$clientsConsole = array_map(function ($node) {
return $node['url'];
}, array_filter($console->getAttribute('platforms', []), function ($node) {
if (isset($node['type']) && $node['type'] === 'web' && isset($node['url']) && !empty($node['url'])) {
return true;
}
return $node['hostname'];
}, array_filter($console->getAttribute('platforms', []), function ($node) {
if (isset($node['type']) && $node['type'] === 'web' && isset($node['hostname']) && !empty($node['hostname'])) {
return true;
}
return false;
}));
return false;
}));
$clients = array_unique(array_merge($clientsConsole, array_map(function ($node) {
return $node['url'];
}, array_filter($project->getAttribute('platforms', []), function ($node) {
if (isset($node['type']) && $node['type'] === 'web' && isset($node['url']) && !empty($node['url'])) {
return true;
}
return $node['hostname'];
}, array_filter($project->getAttribute('platforms', []), function ($node) {
if (isset($node['type']) && $node['type'] === 'web' && isset($node['hostname']) && !empty($node['hostname'])) {
return true;
}
return false;
}))));
return false;
}))));
$utopia->init(function () use ($utopia, $request, $response, &$user, $project, $roles, $webhook, $audit, $usage, $domain, $clients) {
$utopia->init(function () use ($utopia, $request, $response, &$user, $project, $roles, $webhook, $audit, $usage, $domain, $clients, $protocol) {
$route = $utopia->match($request);
$referrer = $request->getServer('HTTP_REFERER', '');
$origin = $request->getServer('HTTP_ORIGIN', parse_url($referrer, PHP_URL_SCHEME).'://'.parse_url($referrer, PHP_URL_HOST));
$origin = parse_url($request->getServer('HTTP_ORIGIN', $referrer), PHP_URL_HOST);
$refDomain = (in_array($origin, $clients))
? $origin : 'http://localhost';
$refDomain = $protocol.'://'.((in_array($origin, $clients))
? $origin : 'localhost');
/*
* Security Headers
@ -86,13 +86,14 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $
* Adding Appwrite API domains to allow XDOMAIN communication
* Skip this check for non-web platforms which are not requiredto send an origin header
*/
$hostValidator = new Host($clients);
$origin = $request->getServer('HTTP_ORIGIN', $request->getServer('HTTP_REFERER', ''));
$origin = parse_url($request->getServer('HTTP_ORIGIN', $request->getServer('HTTP_REFERER', '')), PHP_URL_HOST);
if (!empty($origin) && !$hostValidator->isValid($origin)
if (!empty($origin)
&& !in_array($origin, $clients)
&& in_array($request->getMethod(), [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_PATCH, Request::METHOD_DELETE])
&& empty($request->getHeader('X-Appwrite-Key', ''))) {
throw new Exception('Access from this client host is forbidden. '.$hostValidator->getDescription(), 403);
&& empty($request->getHeader('X-Appwrite-Key', ''))
) {
throw new Exception('Access from this client host is forbidden', 403);
}
/*

View file

@ -20,43 +20,25 @@ $collections = [
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'name' => 'Production',
'type' => 'web',
'url' => 'https://appwrite.io',
'hostname' => 'appwrite.io',
],
[
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'name' => 'Development (SSL)',
'name' => 'Development',
'type' => 'web',
'url' => 'https://appwrite.test',
'hostname' => 'appwrite.test',
],
[
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'name' => 'Development (Non-SSL)',
'name' => 'Localhost',
'type' => 'web',
'url' => 'http://appwrite.test',
'hostname' => 'localhost',
],
[
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'name' => 'Localhost (SSL)',
'name' => 'Current Host',
'type' => 'web',
'url' => 'https://localhost',
],
[
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'name' => 'Localhost (Non-SSL)',
'type' => 'web',
'url' => 'http://localhost',
],
[
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'name' => 'Current Host (SSL)',
'type' => 'web',
'url' => 'https://'.$request->getServer('HTTP_HOST'),
],
[
'$collection' => Database::SYSTEM_COLLECTION_PLATFORMS,
'name' => 'Current Host (Non-SSL)',
'type' => 'web',
'url' => 'http://'.$request->getServer('HTTP_HOST'),
'hostname' => $request->getServer('HTTP_HOST'),
],
],
'legalName' => '',
@ -932,8 +914,8 @@ $collections = [
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'url',
'key' => 'url',
'label' => 'Hostname',
'key' => 'hostname',
'type' => 'text',
'default' => '',
'required' => false,

View file

@ -1033,9 +1033,9 @@ $utopia->post('/v1/projects/:projectId/platforms')
->param('name', null, function () { return new Text(256); }, 'Platform name.')
->param('key', '', function () { return new Text(256); }, 'Package name for android or bundle ID for iOS.', true)
->param('store', '', function () { return new Text(256); }, 'App store or Google Play store ID.', true)
->param('url', '', function () { return new URL(); }, 'Platform client URL.', true)
->param('hostname', '', function () { return new Text(256); }, 'Platform client hostname.', true)
->action(
function ($projectId, $type, $name, $key, $store, $url) use ($response, $consoleDB) {
function ($projectId, $type, $name, $key, $store, $hostname) use ($response, $consoleDB) {
$project = $consoleDB->getDocument($projectId);
if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) {
@ -1052,7 +1052,7 @@ $utopia->post('/v1/projects/:projectId/platforms')
'name' => $name,
'key' => $key,
'store' => $store,
'url' => $url,
'hostname' => $hostname,
'dateCreated' => time(),
'dateUpdated' => time(),
]);
@ -1083,7 +1083,7 @@ $utopia->get('/v1/projects/:projectId/platforms')
->label('sdk.method', 'listPlatforms')
->param('projectId', '', function () { return new UID(); }, 'Project unique ID.')
->action(
function ($projectId) use ($request, $response, $consoleDB) {
function ($projectId) use ($response, $consoleDB) {
$project = $consoleDB->getDocument($projectId);
if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) {
@ -1104,7 +1104,7 @@ $utopia->get('/v1/projects/:projectId/platforms/:platformId')
->param('projectId', null, function () { return new UID(); }, 'Project unique ID.')
->param('platformId', null, function () { return new UID(); }, 'Platform unique ID.')
->action(
function ($projectId, $platformId) use ($request, $response, $consoleDB) {
function ($projectId, $platformId) use ($response, $consoleDB) {
$project = $consoleDB->getDocument($projectId);
if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) {
@ -1131,9 +1131,9 @@ $utopia->put('/v1/projects/:projectId/platforms/:platformId')
->param('name', null, function () { return new Text(256); }, 'Platform name.')
->param('key', '', function () { return new Text(256); }, 'Package name for android or bundle ID for iOS.', true)
->param('store', '', function () { return new Text(256); }, 'App store or Google Play store ID.', true)
->param('url', '', function () { return new URL(); }, 'Platform client URL.', true)
->param('hostname', '', function () { return new Text(256); }, 'Platform client URL.', true)
->action(
function ($projectId, $platformId, $name, $key, $store, $url) use ($response, $consoleDB) {
function ($projectId, $platformId, $name, $key, $store, $hostname) use ($response, $consoleDB) {
$project = $consoleDB->getDocument($projectId);
if (empty($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS != $project->getCollection()) {
@ -1151,7 +1151,7 @@ $utopia->put('/v1/projects/:projectId/platforms/:platformId')
->setAttribute('dateUpdated', time())
->setAttribute('key', $key)
->setAttribute('store', $store)
->setAttribute('url', $url)
->setAttribute('hostname', $hostname)
;
if (false === $consoleDB->updateDocument($platform->getArrayCopy())) {

View file

@ -42,11 +42,6 @@ $callbacks = [
Console::success('Fetched '.$sum.' (offset: '.$offset.' / limit: '.$limit.') documents from a total of '.$projectDB->getSum());
foreach($all as $document) {
if(empty($document->getAttribute('$uid', null))) {
Console::info('Skipped document');
continue;
}
$document = fixDocument($document);
if(empty($document->getId())) {
@ -132,12 +127,17 @@ function fixDocument(Document $document) {
}
}
if(empty($document->getAttribute('$uid', null))) {
return $document;
if($document->getAttribute('$collection') === Database::SYSTEM_COLLECTION_PLATFORMS) {
if($document->getAttribute('url', null) !== null) {
$document
->setAttribute('hostname', parse_url($document->getAttribute('url', $document->getAttribute('hostname', '')), PHP_URL_HOST))
->removeAttribute('url')
;
}
}
$document
->setAttribute('$id', $document->getAttribute('$uid', null))
->setAttribute('$id', $document->getAttribute('$uid', $document->getAttribute('$id')))
->removeAttribute('$uid')
;

View file

@ -13,7 +13,7 @@ $graph = $this->getParam('graph', false);
<span class="title" data-ls-bind="{{console-project.name}}">&nbsp;</span>&nbsp;&nbsp;
</h1>
<ul class="margin-top margin-bottom clear">
<ul class="margin-top-negative-small margin-bottom clear">
<li class="pull-start margin-end margin-bottom-small"><a data-ls-attrs="href=/console/settings?project={{router.params.project}}"><i class="icon-cog"></i> &nbsp;Settings</a> &nbsp;&nbsp;</li>
<li class="pull-start margin-end margin-bottom-small"><a data-ls-attrs="href=/console/keys?project={{router.params.project}}"><i class="icon-key-inv"></i> &nbsp;API Keys</a> &nbsp;&nbsp;</li>
<li class="pull-start margin-end margin-bottom-small"><a data-ls-attrs="href=/console/webhooks?project={{router.params.project}}"><i class="icon-link"></i> &nbsp;Webhooks</a> &nbsp;&nbsp;</li>
@ -120,7 +120,7 @@ $graph = $this->getParam('graph', false);
<span data-ls-bind="{{platform.name}}"></span>
</div>
<p class="margin-bottom-no"><small data-ls-bind="{{platform.url}}"></small></p>
<p class="margin-bottom-no"><small data-ls-bind="{{platform.hostname}}"></small></p>
</li>
</ul>
</div>
@ -178,10 +178,10 @@ $graph = $this->getParam('graph', false);
<input type="hidden" name="type" data-ls-bind="web" />
<label for="name">Name <span class="tooltip large" data-tooltip="Choose any name that will help you distinguish between your different apps."><i class="icon-question"></i></span></label>
<input type="text" class="full-width" id="name" name="name" required autocomplete="off" />
<input type="text" class="full-width" id="name" name="name" required autocomplete="off" placeholder="My Web App" />
<label for="url">URL <span class="tooltip large" data-tooltip="The URL that your website will use to interact with the <?php echo APP_NAME; ?> APIs in production or development environments."><i class="icon-question"></i></span></label>
<input name="url" type="url" class="margin-bottom" autocomplete="off" placeholder="https://localhost:3000" required>
<label for="hostname">Hostname <span class="tooltip large" data-tooltip="The hostname that your website will use to interact with the <?php echo APP_NAME; ?> APIs in production or development environments."><i class="icon-question"></i></span></label>
<input name="hostname" type="text" class="margin-bottom" autocomplete="off" placeholder="localhost" required>
<div class="info margin-top margin-bottom">
<div class="text-bold margin-bottom-small">Next Steps</div>
@ -212,14 +212,14 @@ $graph = $this->getParam('graph', false);
data-failure-param-alert-text="Failed to update platform"
data-failure-param-alert-classname="error">
<input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}"/ >
<input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />
<input type="hidden" name="platformId" data-ls-bind="{{platform.$id}}" />
<label data-ls-attrs="for=name-{{platform.$id}}">Name <span class="tooltip large" data-tooltip="Choose any name that will help you distinguish between your different apps."><i class="icon-question"></i></span></label>
<input type="text" class="full-width" data-ls-attrs="id=name-{{platform.$id}}" name="name" required autocomplete="off" data-ls-bind="{{platform.name}}" />
<input type="text" class="full-width" data-ls-attrs="id=name-{{platform.$id}}" name="name" required autocomplete="off" data-ls-bind="{{platform.name}}" placeholder="My Web App" />
<label for="url">URL <span class="tooltip large" data-tooltip="The URL that your website will use to interact with the <?php echo APP_NAME; ?> APIs in production or development environments."><i class="icon-question"></i></span></label>
<input name="url" type="url" class="margin-bottom" autocomplete="off" placeholder="example.com" data-ls-bind="{{platform.url}}" required>
<label for="hostname">Hostname <span class="tooltip large" data-tooltip="The hostname that your website will use to interact with the <?php echo APP_NAME; ?> APIs in production or development environments."><i class="icon-question"></i></span></label>
<input name="hostname" type="text" class="margin-bottom" autocomplete="off" placeholder="localhost" data-ls-bind="{{platform.hostname}}" required />
<hr />