Page 1 of 1

2FA Setup Problem

Posted: Wed Apr 07, 2021 12:24 pm
by stoicprogrammer
Hi,
I am experiencing difficulty during 2FA setup.

First of all my email setup works as expected. I have tested it before using 2FA.
2FA also works when this option is activated:

Code: Select all

"nuOutput2FATokenToConsole($token);     // For testing purposes, output the token to the developer console".
I can get a code and it works when I submit.


But when I activate this option:

Code: Select all

nuSendCodeByEmail($token);   // Send the token by email.
It says: "An email has been sent to the registered account. Subject = '".subject."' But I do not receive any code. I tested it on the globel admin account, and on user accounts,
I did not receive any code.


Am I missing something?

Note: when I looked in to system codes, both in nubilder4.sql file, and core system folder , I saw that there are several email sending methods such as built-in php mailing or using phpmailer. and as far as I remember general email set up uses different method than 2FA.

Re: 2FA Setup Problem

Posted: Wed Apr 07, 2021 1:17 pm
by kev1n
Hi,

The function nuGetEmail() retrieves the email address from the database. For now you'd have to enter the email for the globeadmin directly in the zzzzsys_user table or
modify the two functions nuGetEmail() and nuSendCodeByEmail() as follows in the nuAuthentication2FA procedure:

Code: Select all

function nuGetEmail($adminEmail = '') {
    $u = nuUser();
    $email = $u->sus_email;
    if ($email == null) $email = $adminEmail;
    return $email;
} 

function nuSendCodeByEmail($code) {
    $content = 'Your Code: '.$code;
    $subject = 'nuBuilder Authentication Code';
    $fromName = 'nubuilder';
    $sendTo = nuGetEmail('your_globeadmin_email@something.com'); // <-------------------- Replace with your "globeadmin" email address.
    nuEmailPHP($sendTo, $fromAddress, $fromName, $content, $subject);
    nuDisplayError("An email has been sent to the registered account. Subject = '".subject."'");
}

Re: 2FA Setup Problem

Posted: Wed Apr 07, 2021 8:14 pm
by stoicprogrammer
Kev1n,
I have finally figured it out.

I could not make this function work:

Code: Select all

nuEmailPHP()

Instead I used this function:

Code: Select all

nuSendEmail()
The final working code is:

Code: Select all

function nuGetEmail() {
    $u = nuUser();
    if ($u == "" ) return  "your_own_admin@email.com";       // your globe admin email here if you want 2FA for admin.
    return $u->sus_email;   // user email
} 

function nuSendCodeByEmail($code) {
    $content = 'Your Code: '.$code;
    $subject = 'nuBuilder Authentication Code';
    $fromName = 'nubuilder';
    $sendTo = nuGetEmail();

    $fromAddress = "setup@email_settings.com";        // the mail used in general -- email settings here. (this variable is referred but missing in the original code, added here.)

    $cc = "some@email.com";               // this parameter is required by the function. if you dont want to use, you can replace it with globe admin email account.
    $bcc = "another@email.com";  // this parameter is required by the function. if you dont want to use, you can replace it with globe admin email account.

// this is the original function used in email setup testing:
//nuSendEmail($to, $fromAddress, $fromName, $body, $subject, array(), true, $cc, $bcc);
//it is adapted here like this:

 nuSendEmail($sendTo, $fromAddress, $fromName, $content, $subject, array(), true, $cc, $bcc);


// nuEmailPHP($sendTo, $fromAddress, $fromName, $content, $subject);    ------ old code: it did not work.

    nuDisplayError("An email has been sent to the registered account. Subject = '".$subject."'");
}
//...the following of the code is the same.

Re: 2FA Setup Problem

Posted: Wed Apr 07, 2021 8:17 pm
by kev1n
Good job :D