2020-04-14 17:44:15 +12:00
|
|
|
<?php
|
|
|
|
|
2020-06-12 07:36:10 +12:00
|
|
|
namespace Appwrite\Network\Validator;
|
2020-04-14 17:44:15 +12:00
|
|
|
|
|
|
|
use Utopia\Validator;
|
|
|
|
|
|
|
|
class Origin extends Validator
|
|
|
|
{
|
|
|
|
const CLIENT_TYPE_UNKNOWN = 'unknown';
|
|
|
|
const CLIENT_TYPE_WEB = 'web';
|
|
|
|
const CLIENT_TYPE_FLUTTER_IOS = 'flutter-ios';
|
|
|
|
const CLIENT_TYPE_FLUTTER_ANDROID = 'flutter-android';
|
|
|
|
const CLIENT_TYPE_FLUTTER_MACOS = 'flutter-macos';
|
|
|
|
const CLIENT_TYPE_FLUTTER_WINDOWS = 'flutter-windows';
|
|
|
|
const CLIENT_TYPE_FLUTTER_LINUX = 'flutter-linux';
|
|
|
|
|
|
|
|
const SCHEME_TYPE_HTTP = 'http';
|
|
|
|
const SCHEME_TYPE_HTTPS = 'https';
|
|
|
|
const SCHEME_TYPE_IOS = 'appwrite-ios';
|
|
|
|
const SCHEME_TYPE_ANDROID = 'appwrite-android';
|
|
|
|
const SCHEME_TYPE_MACOS = 'appwrite-macos';
|
|
|
|
const SCHEME_TYPE_WINDOWS = 'appwrite-windows';
|
|
|
|
const SCHEME_TYPE_LINUX = 'appwrite-linux';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $platforms = [
|
|
|
|
self::SCHEME_TYPE_HTTP => 'Web',
|
|
|
|
self::SCHEME_TYPE_HTTPS => 'Web',
|
|
|
|
self::SCHEME_TYPE_IOS => 'iOS',
|
|
|
|
self::SCHEME_TYPE_ANDROID => 'Android',
|
|
|
|
self::SCHEME_TYPE_MACOS => 'macOS',
|
|
|
|
self::SCHEME_TYPE_WINDOWS => 'Windows',
|
|
|
|
self::SCHEME_TYPE_LINUX => 'Linux',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $clients = [
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $client = self::CLIENT_TYPE_UNKNOWN;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $host = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $target
|
|
|
|
*/
|
|
|
|
public function __construct($platforms)
|
|
|
|
{
|
2020-06-25 09:02:27 +12:00
|
|
|
foreach ($platforms as $platform) {
|
2020-04-14 17:44:15 +12:00
|
|
|
$type = (isset($platform['type'])) ? $platform['type'] : '';
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case self::CLIENT_TYPE_WEB:
|
|
|
|
$this->clients[] = (isset($platform['hostname'])) ? $platform['hostname'] : '';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case self::CLIENT_TYPE_FLUTTER_IOS:
|
|
|
|
case self::CLIENT_TYPE_FLUTTER_ANDROID:
|
|
|
|
case self::CLIENT_TYPE_FLUTTER_MACOS:
|
|
|
|
case self::CLIENT_TYPE_FLUTTER_WINDOWS:
|
|
|
|
case self::CLIENT_TYPE_FLUTTER_LINUX:
|
|
|
|
$this->clients[] = (isset($platform['key'])) ? $platform['key'] : '';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
# code...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription()
|
|
|
|
{
|
2020-06-25 09:02:27 +12:00
|
|
|
if (!\array_key_exists($this->client, $this->platforms)) {
|
2020-04-14 17:44:15 +12:00
|
|
|
return 'Unsupported platform';
|
|
|
|
}
|
|
|
|
|
2020-06-16 08:05:49 +12:00
|
|
|
return 'Invalid Origin. Register your new client ('.$this->host.') as a new '
|
|
|
|
.$this->platforms[$this->client].' platform on your project console dashboard';
|
2020-04-14 17:44:15 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if Origin has been whiltlisted
|
|
|
|
* for access to the API
|
|
|
|
*
|
|
|
|
* @param string $origin
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValid($origin)
|
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
$scheme = \parse_url($origin, PHP_URL_SCHEME);
|
|
|
|
$host = \parse_url($origin, PHP_URL_HOST);
|
2020-04-14 17:44:15 +12:00
|
|
|
|
|
|
|
$this->host = $host;
|
|
|
|
$this->client = $scheme;
|
|
|
|
|
2020-06-25 09:02:27 +12:00
|
|
|
if (empty($host)) {
|
2020-04-14 17:44:15 +12:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-25 09:02:27 +12:00
|
|
|
if (\in_array($host, $this->clients)) {
|
2020-04-14 17:44:15 +12:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|