the-djmaze / snappymail

Simple, modern & fast web-based email client
https://snappymail.eu
GNU Affero General Public License v3.0
924 stars 109 forks source link

ExternalLogin issue in v2.36.4 #1647

Closed dfangys closed 6 days ago

dfangys commented 6 days ago

Hello,

there's an issue with updates from 2.36.0 for external login, please check it.

domain.com/?ExternalLogin

thanks

Debug/logging information Read here how to log

the-djmaze commented 6 days ago

Checked, i don't see error.

dfangys commented 6 days ago

the response is empty while it working in the old version

Screenshot 2024-07-01 at 6 26 39 PM
dfangys commented 6 days ago

working after change it like this in /plugins/login-external


<?php

use SnappyMail\SensitiveString;

class LoginExternalPlugin extends \RainLoop\Plugins\AbstractPlugin
{
    const
        NAME     = 'Login External',
        AUTHOR   = 'SnappyMail',
        URL      = 'https://snappymail.eu/',
        VERSION  = '1.3',
        RELEASE  = '2024-03-27',
        REQUIRED = '2.36.1',
        CATEGORY = 'Login',
        LICENSE  = 'MIT',
        DESCRIPTION = 'Login with $_POST["Email"] and $_POST["Password"] from anywhere';

    public function Init() : void
    {
        $this->addPartHook('ExternalLogin', 'ServiceExternalLogin');
    }

    public function ServiceExternalLogin() : bool
    {
        $oActions = \RainLoop\Api::Actions();
        $oActions->Http()->ServerNoCache();

        $oAccount = null;
        $oException = null;

        $sEmail = isset($_POST['Email']) ? $_POST['Email'] : '';
        $sPassword = isset($_POST['Password']) ? $_POST['Password'] : '';

        // Log received email and password
        error_log("Received Email: $sEmail");
        error_log("Received Password: $sPassword");

        try
        {
            // Convert password to SensitiveString type
            $oPassword = new SensitiveString($sPassword);
            $oAccount = $oActions->LoginProcess($sEmail, $oPassword);
            error_log("LoginProcess executed.");
            if (!$oAccount instanceof \RainLoop\Model\MainAccount) {
                $oAccount = null;
                error_log("LoginProcess did not return a valid MainAccount.");
            } else {
                error_log("LoginProcess returned a valid MainAccount.");
            }
        }
        catch (\Throwable $oException)
        {
            $oLogger = $oActions->Logger();
            $oLogger && $oLogger->WriteException($oException);

            // Log exception
            error_log("Exception: " . $oException->getMessage());
        }

        if (isset($_POST['Output']) && 'json' === \strtolower($_POST['Output'])) {
            \header('Content-Type: application/json; charset=utf-8');
            $aResult = array(
                'Action' => 'ExternalLogin',
                'Result' => $oAccount ? true : false,
                'ErrorCode' => 0
            );
            if (!$oAccount) {
                if ($oException instanceof \RainLoop\Exceptions\ClientException) {
                    $aResult['ErrorCode'] = $oException->getCode();
                } else {
                    // Set a default error code
                    $aResult['ErrorCode'] = 1;  // Example error code
                }
            }
            // Log JSON response
            error_log("JSON Response: " . json_encode($aResult));
            echo \json_encode($aResult);
        } else {
            \MailSo\Base\Http::Location('./');
        }
        return true;
    }
}

?>
the-djmaze commented 6 days ago

Thanks for finding the bug. Extension is updated.