Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Skip to content
base_ecloud_account_creator.php 7.86 KiB
Newer Older
<?php
require 'vendor/autoload.php';
require_once('language.php');
Akhil's avatar
Akhil committed
require_once('helpers.php');
Akhil's avatar
Akhil committed
class BaseEcloudAccountCreator implements AccountCreator
    private string $ecloudUrl;
    private string $ecloudAccountsApiUrl;
    private string $commonApiUrl;
    protected int $quota = 1024; # Quota in MB
Akhil's avatar
Akhil committed
    protected bool $usernameIsEmail = true;
    public function __construct(string $ecloudUrl)
        $this->ecloudUrl = endsWith($ecloudUrl, "/") ? $ecloudUrl : $ecloudUrl . "/";
        $this->ecloudAccountsApiUrl = $this->ecloudUrl . 'apps/ecloud-accounts/api/';

        $this->commonApiUrl = getenv('COMMON_SERVICES_URL');
        $this->commonApiUrl = endsWith($this->commonApiUrl, '/') ? $this->commonApiUrl : $this->commonApiUrl . '/';

        $quota = getenv('CLOUD_QUOTA_IN_MB');
        if ($quota !== false) {
Akhil's avatar
Akhil committed
            $this->quota = intval($quota);
        }
    }

    public function tryToCreate(object $userData)
    {
        global $strings;
Nivesh Krishna's avatar
Nivesh Krishna committed
        $userData->quota = $this->quota;
        $answer = $this->createAccount($userData);
Akhil's avatar
Akhil committed
        if ($answer->success === false) {
            sendAPIResponse(400, createAPIResponse("general", $strings[$answer->type]));
    public function validateData(object $userData): ValidatedData
    {
        $id = "e_cloud_account_data";
        try {
Akhil's avatar
Akhil committed
            // We check if account with uid set to email or username exists
            if ($this->isUsernameTaken($userData->username) || $this->isAliasTaken($userData->username)) {
                return new \ValidatedData($id, "error_account_taken");
Akhil's avatar
Akhil committed
            }
Akhil's avatar
Akhil committed
        } catch (\Error $_) {
            return new \ValidatedData($id, "error_server_side");
        }
        return new \ValidatedData($id, null);
    }
Nivesh Krishna's avatar
Nivesh Krishna committed
    protected function postCreationActions(object $userData, string $commonApiVersion = '')
Akhil's avatar
Akhil committed
        try {
            $hmeAlias = '';
Akhil's avatar
Akhil committed
            $aliasDomain =  getenv('ALIAS_DOMAIN');
            // Create HME Alias
Nivesh Krishna's avatar
Nivesh Krishna committed
            $hmeAlias = $this->createHMEAlias($userData->email, $this->commonApiUrl, $commonApiVersion, $aliasDomain);
Akhil's avatar
Akhil committed
            // Create Alias to new domain
            // $this->createNewDomainAlias($username, $email, $commonApiUrl, $commonApiVersion, $aliasDomain);
Akhil's avatar
Akhil committed

            // Create alias with same name as email pointing to email to block this alias
            $domain = getMailDomain();
Nivesh Krishna's avatar
Nivesh Krishna committed
            $this->createNewDomainAlias($userData->username, $userData->email, $this->commonApiUrl, $commonApiVersion, $domain);
            $userData->hmeAlias = $hmeAlias;
Akhil's avatar
Akhil committed
        } catch (Error $e) {
Nivesh Krishna's avatar
Nivesh Krishna committed
            error_log('Error during alias creation for user: ' . $userData->username . ' with email: ' . $userData->email . ' : ' . $e->getMessage());
Nivesh Krishna's avatar
Nivesh Krishna committed
        $userData->quota = strval($userData->quota) . ' MB';
        $answer = $this->setAccountDataAtNextcloud($userData);
Akhil's avatar
Akhil committed
        return $answer;
Akhil's avatar
Akhil committed

Akhil's avatar
Akhil committed
    private function createHMEAlias(string $resultmail, string $commonApiUrl, string $commonApiVersion, string $domain) : string
Akhil's avatar
Akhil committed
    {
        $token = getenv('COMMON_SERVICES_TOKEN');
Akhil's avatar
Akhil committed
        $endpoint = $commonApiVersion . '/aliases/hide-my-email/';
        $url = $commonApiUrl . $endpoint . $resultmail;
Akhil's avatar
Akhil committed
        $data = array(
            "domain" => $domain
        );
        $headers = [
            "Authorization: Bearer $token"
        ];
Akhil's avatar
Akhil committed

        $result = curlRequest('POST', $url, $headers, $data);
Akhil's avatar
Akhil committed
        $output = $result->output;
        if ($result->statusCode != 200) {
            $err = $output->message;
            throw new Error($err);
        }
        $alias = isset($output->emailAlias) ? $output->emailAlias : '';
        return $alias;
    }

Akhil's avatar
Akhil committed
    private function createNewDomainAlias(string $alias, string $resultmail, string $commonApiUrl, string $commonApiVersion, string $domain)
Akhil's avatar
Akhil committed
    {
        $token = getenv('COMMON_SERVICES_TOKEN');

Akhil's avatar
Akhil committed
        $endpoint = $commonApiVersion . '/aliases/';
        $url = $commonApiUrl . $endpoint . $resultmail;
Akhil's avatar
Akhil committed

        $data = array(
Akhil's avatar
Akhil committed
            "alias" => $alias,
Akhil's avatar
Akhil committed
            "domain" => $domain
        );
        $headers = [
            "Authorization: Bearer $token"
        ];

        $result = curlRequest('POST', $url, $headers, $data);
Akhil's avatar
Akhil committed
        $output = $result->output;
        if ($result->statusCode != 200) {
            $err = $output->message;
            throw new Error($err);
        }
    }

Nivesh Krishna's avatar
Nivesh Krishna committed
    private function setAccountDataAtNextcloud(object $userData)
Akhil's avatar
Akhil committed
    {
Akhil's avatar
Akhil committed
        $token = getenv('ECLOUD_ACCOUNTS_SECRET');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

Akhil's avatar
Akhil committed
        $data = [
Akhil's avatar
Akhil committed
            "token" => $token,
Nivesh Krishna's avatar
Nivesh Krishna committed
            "email" => $userData->email,
            "quota" => $userData->quota,
            "recoveryEmail" => $userData->authmail,
            "hmeAlias" => $userData->hmeAlias,
            "tosAccepted" => $userData->tosAccepted
Akhil's avatar
Akhil committed
        ];
Nivesh Krishna's avatar
Nivesh Krishna committed
        $data['uid'] = $this->usernameIsEmail ? $userData->email : $userData->username;
        $data['userLanguage'] = strtolower(getCurrentRequestLanguage());
        curl_setopt($ch, CURLOPT_URL, $this->ecloudAccountsApiUrl . 'set_account_data');
Akhil's avatar
Akhil committed
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $output = curl_exec($ch);
        $output = json_decode($output, false);
Akhil's avatar
Akhil committed
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        $answer = new \stdClass;
        $answer->success = true;

Akhil's avatar
Akhil committed
        $errorNotEmpty = !empty($output->error);
        $isRecoveryEmailError = $errorNotEmpty && $output->error === 'error_setting_recovery';
        $isHmeError = $errorNotEmpty && $output->error === 'error_adding_hme_alias';

        if ($isRecoveryEmailError) {
Nivesh Krishna's avatar
Nivesh Krishna committed
            $message = 'Setting recovery email of user ' . $userData->email . ' failed with status code: ' . $statusCode . '(recovery email: ' . $userData->authmail . ')' . PHP_EOL;
Akhil's avatar
Akhil committed
            error_log($message, 0);
        }
        if ($isHmeError) {
Nivesh Krishna's avatar
Nivesh Krishna committed
            $message = 'Setting HME alias of user ' . $userData->email . ' failed with status code: ' . $statusCode . '(HME alias: ' . $userData->hmeAlias . ')' . PHP_EOL;
Akhil's avatar
Akhil committed
            error_log($message, 0);
        }

Akhil's avatar
Akhil committed
        if ($statusCode !== 200) {
Akhil's avatar
Akhil committed
            // Don't fail if recovery email or hide-my-email alias not set correctly
            $answer->success = $isRecoveryEmailError || $isHmeError;
Akhil's avatar
Akhil committed
            $answer->type = $errorNotEmpty ? $output->error : 'error_creating_account';
        }

        return $answer;
    }

Akhil's avatar
Akhil committed
    private function isUsernameTaken(string $uid): bool
Akhil's avatar
Akhil committed
        $token = getenv('ECLOUD_ACCOUNTS_SECRET');
Akhil's avatar
Akhil committed
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
Akhil's avatar
Akhil committed
        $data = array(
            "uid" => $uid,
            "token" => $token,
        );
        curl_setopt($ch, CURLOPT_URL, $this->ecloudAccountsApiUrl . 'user_exists');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

        $output = curl_exec($ch);
        $output = json_decode($output);
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        if ($statusCode !== 200) {
            $err = curl_error($ch);
            throw new Error($err);
Akhil's avatar
Akhil committed

        return $output;
    private function isAliasTaken(string $alias): bool
    {
        $token = getenv('COMMON_SERVICES_TOKEN');

        $endpoint = "v2/aliases/hide-my-email?alias=$alias";
        $url = $this->commonApiUrl . $endpoint;
      
        $headers = [
            "Authorization: Bearer $token"
        ];

        $result = curlRequest('GET', $url, $headers);
        $statusCode = $result->statusCode;
        if ($statusCode !== 200 && $statusCode !== 404) {
            throw new Error("Error with request to check if alias exists, status code : " . $statusCode);
        }
        return $statusCode === 200;
    }

Nivesh Krishna's avatar
Nivesh Krishna committed
    protected function createAccount(object $userData)
Akhil's avatar
Akhil committed
}