Exemplo n.º 1
0
 public function testUnwrappingGet()
 {
     $unencryptedValue = 'foobar';
     $encryptedValue = $this->crypto->encrypt($unencryptedValue);
     $this->wrappedSession->expects($this->once())->method('get')->with('encrypted_session_data')->willReturnCallback(function () use($encryptedValue) {
         return $encryptedValue;
     });
     $this->assertSame($unencryptedValue, $this->wrappedSession->get('encrypted_session_data'));
 }
Exemplo n.º 2
0
 public function set($key, $value)
 {
     $value = $this->crypto->encrypt($value);
     if ($this->hasKey($key, $value)) {
         $sql = "UPDATE `*PREFIX*ocsms_config` SET `value` = ? WHERE `user` = ? AND `key` = ?";
         $this->execute($sql, array($value, $this->user, $key));
     } else {
         $sql = "INSERT INTO `*PREFIX*ocsms_config` (`user`,`key`,`value`) VALUES (?,?,?);";
         $this->execute($sql, array($this->user, $key, $value));
     }
 }
Exemplo n.º 3
0
 /**
  * @NoAdminRequired
  *
  * @param string $accountName
  * @param string $emailAddress
  * @param string $password
  * @param string $imapHost
  * @param int $imapPort
  * @param string $imapSslMode
  * @param string $imapUser
  * @param string $imapPassword
  * @param string $smtpHost
  * @param int $smtpPort
  * @param string $smtpSslMode
  * @param string $smtpUser
  * @param string $smtpPassword
  * @param bool $autoDetect
  * @return JSONResponse
  */
 public function create($accountName, $emailAddress, $password, $imapHost, $imapPort, $imapSslMode, $imapUser, $imapPassword, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $smtpPassword, $autoDetect)
 {
     try {
         if ($autoDetect) {
             $this->logger->info('setting up auto detected account');
             $newAccount = $this->autoConfig->createAutoDetected($emailAddress, $password, $accountName);
         } else {
             $this->logger->info('Setting up manually configured account');
             $newAccount = new MailAccount(['accountName' => $accountName, 'emailAddress' => $emailAddress, 'imapHost' => $imapHost, 'imapPort' => $imapPort, 'imapSslMode' => $imapSslMode, 'imapUser' => $imapUser, 'imapPassword' => $imapPassword, 'smtpHost' => $smtpHost, 'smtpPort' => $smtpPort, 'smtpSslMode' => $smtpSslMode, 'smtpUser' => $smtpUser, 'smtpPassword' => $smtpPassword]);
             $newAccount->setUserId($this->currentUserId);
             $newAccount->setInboundPassword($this->crypto->encrypt($newAccount->getInboundPassword()));
             $newAccount->setOutboundPassword($this->crypto->encrypt($newAccount->getOutboundPassword()));
             $a = new Account($newAccount);
             $this->logger->debug('Connecting to account {account}', ['account' => $newAccount->getEmail()]);
             $a->testConnectivity();
         }
         if ($newAccount) {
             $this->accountService->save($newAccount);
             $this->logger->debug("account created " . $newAccount->getId());
             return new JSONResponse(['data' => ['id' => $newAccount->getId()]], Http::STATUS_CREATED);
         }
     } catch (\Exception $ex) {
         $this->logger->error('Creating account failed: ' . $ex->getMessage());
         return new JSONResponse(array('message' => $this->l10n->t('Creating account failed: ') . $ex->getMessage()), HTTP::STATUS_BAD_REQUEST);
     }
     $this->logger->info('Auto detect failed');
     return new JSONResponse(array('message' => $this->l10n->t('Auto detect failed. Please use manual mode.')), HTTP::STATUS_BAD_REQUEST);
 }
 /**
  * @param MailAccount $account
  * @param $host
  * @param $users
  * @param $password
  * @param bool $withHostPrefix
  * @return bool
  */
 public function test(MailAccount $account, $host, $users, $password, $withHostPrefix = false)
 {
     if (!is_array($users)) {
         $users = [$users];
     }
     // port 25 should be the last one to test
     $ports = [587, 465, 25];
     $protocols = ['ssl', 'tls', null];
     $hostPrefixes = [''];
     if ($withHostPrefix) {
         $hostPrefixes = ['', 'imap.'];
     }
     foreach ($hostPrefixes as $hostPrefix) {
         $url = $hostPrefix . $host;
         if (gethostbyname($url) === $url) {
             continue;
         }
         foreach ($ports as $port) {
             if (!$this->canConnect($url, $port)) {
                 continue;
             }
             foreach ($protocols as $protocol) {
                 foreach ($users as $user) {
                     try {
                         $account->setOutboundHost($url);
                         $account->setOutboundPort($port);
                         $account->setOutboundUser($user);
                         $password = $this->crypto->encrypt($password);
                         $account->setOutboundPassword($password);
                         $account->setOutboundSslMode($protocol);
                         $a = new Account($account);
                         $smtp = $a->createTransport();
                         $smtp->getSMTPObject();
                         $this->logger->info("Test-Account-Successful: {$this->userId}, {$url}, {$port}, {$user}, {$protocol}");
                         return true;
                     } catch (\Exception $e) {
                         $error = $e->getMessage();
                         $this->logger->info("Test-Account-Failed: {$this->userId}, {$url}, {$port}, {$user}, {$protocol} -> {$error}");
                     }
                 }
             }
         }
     }
     return false;
 }
Exemplo n.º 5
0
 /**
  * Close the session and release the lock, also writes all changed data in batch
  */
 public function close()
 {
     if ($this->isModified) {
         $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
         $this->session->set(self::encryptedSessionName, $encryptedValue);
         $this->isModified = false;
     }
     $this->session->close();
 }
Exemplo n.º 6
0
    /**
     * @param $backend id of the backend
     * @param $key key of the config value
     * @param $value the config value
     */
    public function set($backend, $key, $value)
    {
        $value = $this->crypto->encrypt($value);
        if ($this->hasKey($backend, $key, $value)) {
            $sql = <<<SQL
\t\t\t\tUPDATE
\t\t\t\t\t`*PREFIX*chat_config`
\t\t\t\tSET
\t\t\t\t\t`value` = ?
\t\t\t\tWHERE
\t\t\t\t\t`user` = ?
\t\t\t\tAND
\t\t\t\t\t`backend` = ?
\t\t\t\tAND
\t\t\t\t\t`key` = ?
SQL;
            $this->execute($sql, array($value, $this->user, $backend, $key));
        } else {
            $sql = <<<SQL
\t\t\t\tINSERT
\t\t\t\tINTO
\t\t\t\t\t`*PREFIX*chat_config`
\t\t\t\t(
\t\t\t\t\t`user`,
\t\t\t\t\t`key`,
\t\t\t\t\t`value`,
\t\t\t\t\t`backend`
\t\t\t\t) VALUES (
\t\t\t\t\t?,
\t\t\t\t\t?,
\t\t\t\t\t?,
\t\t\t\t\t?
\t\t\t\t)
SQL;
            $this->execute($sql, array($this->user, $key, $value, $backend));
        }
    }
Exemplo n.º 7
0
 /**
  * @param $email
  * @param $password
  * @param $name
  * @param $host
  * @param $port
  * @param string|null $encryptionProtocol
  * @param $user
  * @return MailAccount
  */
 private function connectImap($email, $password, $name, $host, $port, $encryptionProtocol, $user)
 {
     $account = new MailAccount();
     $account->setUserId($this->userId);
     $account->setName($name);
     $account->setEmail($email);
     $account->setInboundHost($host);
     $account->setInboundPort($port);
     $account->setInboundSslMode($encryptionProtocol);
     $account->setInboundUser($user);
     $password = $this->crypto->encrypt($password);
     $account->setInboundPassword($password);
     $a = new Account($account);
     $a->getImapConnection();
     $this->logger->info("Test-Account-Successful: {$this->userId}, {$host}, {$port}, {$user}, {$encryptionProtocol}");
     return $account;
 }
Exemplo n.º 8
0
 private function encryptValue($value)
 {
     return $this->crypto->encrypt($value);
 }
Exemplo n.º 9
0
 /**
  * Hook listener on post login
  *
  * @param array $params
  */
 public function authenticate(array $params)
 {
     $this->session->set('password::sessioncredentials/credentials', $this->crypto->encrypt(json_encode($params)));
 }
Exemplo n.º 10
0
 /**
  * Store a set of credentials
  *
  * @param string|null $userId Null for system-wide credentials
  * @param string $identifier
  * @param mixed $credentials
  */
 public function store($userId, $identifier, $credentials)
 {
     $value = $this->crypto->encrypt(json_encode($credentials));
     $this->dbConnection->setValues(self::DB_TABLE, ['user' => $userId, 'identifier' => $identifier], ['credentials' => $value]);
 }
Exemplo n.º 11
0
 /**
  * Encrypt the given password
  *
  * The token is used as key
  *
  * @param string $password
  * @param string $token
  * @return string encrypted password
  */
 private function encryptPassword($password, $token)
 {
     $secret = $this->config->getSystemValue('secret');
     return $this->crypto->encrypt($password, $token . $secret);
 }
Exemplo n.º 12
0
 /**
  * @param $email
  * @param $password
  * @param $name
  * @return MailAccount|null
  */
 public function createAutoDetected($email, $password, $name)
 {
     // splitting the email address into user and host part
     // TODO: use horde libs for email address parsing
     list(, $host) = explode("@", $email);
     $ispdb = $this->mozillaIspDb->query($host);
     if (!empty($ispdb)) {
         $account = null;
         if (isset($ispdb['imap'])) {
             foreach ($ispdb['imap'] as $imap) {
                 $host = $imap['hostname'];
                 $port = $imap['port'];
                 $encryptionProtocol = null;
                 if ($imap['socketType'] === 'SSL') {
                     $encryptionProtocol = 'ssl';
                 }
                 if ($imap['socketType'] === 'STARTTLS') {
                     $encryptionProtocol = 'tls';
                 }
                 if ($imap['username'] === '%EMAILADDRESS%') {
                     $user = $email;
                 } elseif ($imap['username'] === '%EMAILLOCALPART%') {
                     list($user, ) = explode("@", $email);
                 } else {
                     $this->logger->info("Unknown username variable: " . $imap['username']);
                     return null;
                 }
                 try {
                     $account = $this->imapConnector->connect($email, $password, $name, $host, $port, $encryptionProtocol, $user);
                     break;
                 } catch (\Horde_Imap_Client_Exception $e) {
                     $error = $e->getMessage();
                     $this->logger->info("Test-Account-Failed: {$this->userId}, {$host}, {$port}, {$user}, {$encryptionProtocol} -> {$error}");
                 }
             }
         }
         if (!is_null($account)) {
             foreach ($ispdb['smtp'] as $smtp) {
                 try {
                     if ($smtp['username'] === '%EMAILADDRESS%') {
                         $user = $email;
                     } elseif ($smtp['username'] === '%EMAILLOCALPART%') {
                         list($user, ) = explode("@", $email);
                     } else {
                         $this->logger->info("Unknown username variable: " . $smtp['username']);
                         return null;
                     }
                     $account->setOutboundHost($smtp['hostname']);
                     $account->setOutboundPort($smtp['port']);
                     $password = $this->crypto->encrypt($password);
                     $account->setOutboundPassword($password);
                     $account->setOutboundUser($user);
                     $account->setOutboundSslMode(strtolower($smtp['socketType']));
                     $a = new Account($account);
                     $smtp = $a->createTransport();
                     if ($smtp instanceof Horde_Mail_Transport_Smtphorde) {
                         $smtp->getSMTPObject();
                     }
                     break;
                 } catch (\PEAR_Exception $ex) {
                     $error = $ex->getMessage();
                     $this->logger->info("Test-Account-Failed(smtp): {$error}");
                 }
             }
             return $account;
         }
     }
     $account = $this->detectImapAndSmtp($email, $password, $name);
     if (!is_null($account)) {
         return $account;
     }
     return null;
 }
Exemplo n.º 13
0
 /**
  * Set a value in the session
  *
  * @param string $key
  * @param mixed $value
  */
 public function set($key, $value)
 {
     $encryptedValue = $this->crypto->encrypt(json_encode($value), $this->passphrase);
     $this->session->set($key, $encryptedValue);
 }