mscdex / node-imap

An IMAP client module for node.js.
MIT License
2.14k stars 379 forks source link

Authentication Failing on Sever, working on localhost #874

Closed heitorflorido closed 2 years ago

heitorflorido commented 2 years ago

I'm trying to connect to Office 365 Exchange Server. Everything is working fine at localhost, but I'm receiving a Login error when executing at the server environment (using Heroku).

Both environments use the same node version (14.17.6).

Here's my configuration:

const imap = new Imap({
    user: 'user@domain.com.br',
    password: 'xxx',
    host: 'outlook.office365.com',
    port: '993',
    tls: true,
    authTimeout: 60000,
    debug: console.log,
  });

Here's the debug info with the error:

[connection] Connected to host
<= '* OK The Microsoft Exchange IMAP4 service is ready. [TQBOADIAUABSADAANwBDAEEAMAAwADEAMgAuAG4AYQBtAHAAcgBkADAANwAuAHAAcgBvAGQALgBvAHUAdABsAG8AbwBrAC4AYwBvAG0A]'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+'
<= 'A0 OK CAPABILITY completed.'
=> 'A1 LOGIN "user@domain.com.br" "xxx"'
<= 'A1 NO LOGIN failed.'
Error: LOGIN failed.
        at Connection._resTagged (/app/node_modules/imap/lib/Connection.js:1502:11)
        at Parser.<anonymous> (/app/node_modules/imap/lib/Connection.js:194:10)
        at Parser.emit (events.js:400:28)
        at Parser.emit (domain.js:470:12)
        at Parser._resTagged (/app/node_modules/imap/lib/Parser.js:175:10)
        at Parser._parse (/app/node_modules/imap/lib/Parser.js:139:16)
        at Parser._tryread (/app/node_modules/imap/lib/Parser.js:82:15)
        at TLSSocket.Parser._cbReadable (/app/node_modules/imap/lib/Parser.js:53:12)
        at TLSSocket.emit (events.js:400:28)
        at TLSSocket.emit (domain.js:470:12)
        at emitReadable_ (internal/streams/readable.js:550:12)
        at processTicksAndRejections (internal/process/task_queues.js:81:21) {
    type: 'no',
    textCode: undefined,
    source: 'authentication'
}
[connection] Ended
[connection] Closed

Here's the debug info at localhost (working):

[connection] Connected to host
<= '* OK The Microsoft Exchange IMAP4 service is ready. [QwBQADUAUAAyADgANABDAEEAMAAwADYAOQAuAEIAUgBBAFAAMgA4ADQALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+'
<= 'A0 OK CAPABILITY completed.'
=> 'A1 LOGIN "user@domain.com.br" "xxx"'
<= 'A1 OK LOGIN completed.'
=> 'A2 CAPABILITY'
<= '* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+'
<= 'A2 OK CAPABILITY completed.'
=> 'A3 NAMESPACE'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A3 OK NAMESPACE completed.'
=> 'A4 LIST "" ""'
<= '* LIST (\\Noselect \\HasChildren) "/" ""'
<= 'A4 OK LIST completed.'
=> 'A5 EXAMINE "INBOX"'
<= '* 9654 EXISTS'
<= '* 9654 RECENT'
<= '* FLAGS (\\Seen \\Answered \\Flagged \\Deleted \\Draft $MDNSent)'
<= '* OK [PERMANENTFLAGS ()] Permanent flags'
<= '* OK [UNSEEN 3] Is the first unseen message'
<= '* OK [UIDVALIDITY 14] UIDVALIDITY value'
<= '* OK [UIDNEXT 19855] The next unique identifier value'
<= 'A5 OK [READ-ONLY] EXAMINE completed.'
=> 'A6 UID SEARCH UID 19835:*'
...
mscdex commented 2 years ago

If I had to guess, the server might be silently enforcing oauth2 for authentication. Try that instead.

heitorflorido commented 2 years ago

I tried to authenticate using oauth2 and still got the error. Turns out the server was blocking my auth attempts due to a geographic policy (my backend server is located on another country). After a disabled that security policy, everything started to work.

Thank you for your support.

VitorBrangioni commented 1 year ago

@heitorflorido Is there any configuration in Outlook to allow this auth? I'm trying at localhost, but I'm receiving the same error (login failed).