Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions src/services/ImapInboundService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,40 +54,33 @@ class ImapMailboxSession {
try {
const useOAuth2 = this.oauth2AccessToken != null && this.oauth2AccessToken.length > 0;

const imap = new ImapConnection({
user: useOAuth2 ? undefined : this.imapUser,
password: useOAuth2 ? undefined : this.imapPass,
const imapOpts: ImapConnection.Config = {
user: this.imapUser,
password: this.imapPass,
host: this.imapHost,
port: this.imapPort,
tls: this.imapSecure,
tlsOptions: {
servername: this.imapHost,
},
keepalive: true,
});
};

if (useOAuth2) {
await new Promise<void>((resolve, reject) => {
const xoauth2Token = Buffer.from(
`user=${this.imapUser}\x01auth=Bearer ${this.oauth2AccessToken}\x01\x01`,
'utf-8',
).toString('base64');

imap.once('connect', () => {
(imap as any).C('AUTHENTICATE XOAUTH2 ' + xoauth2Token);
});
imap.once('ready', () => resolve());
imap.once('error', reject);
imap.connect();
});
} else {
await new Promise<void>((resolve, reject) => {
imap.once('ready', () => resolve());
imap.once('error', reject);
imap.connect();
});
imapOpts.xoauth2 = Buffer.from(
`user=${this.imapUser}\x01auth=Bearer ${this.oauth2AccessToken}\x01\x01`,
'utf-8',
).toString('base64');
}

const imap = new ImapConnection(imapOpts);

await new Promise<void>((resolve, reject) => {
imap.once('ready', () => resolve());
imap.once('error', reject);
imap.connect();
});

this.imap = imap;
this.consecutiveErrors = 0;
logger.info({ providerId: this.providerId, host: this.imapHost, authMethod: useOAuth2 ? 'XOAUTH2' : 'password' }, 'IMAP connected');
Expand Down
Loading