MailCore / mailcore2

MailCore 2 provide a simple and asynchronous API to work with e-mail protocols IMAP, POP and SMTP. The API has been redesigned from ground up.
Other
2.6k stars 625 forks source link

[Problem] - OAUTH IMAP Outlook Authenticate Error #2007

Open Be-Maps opened 2 weeks ago

Be-Maps commented 2 weeks ago

Summary I get token but not able to do IMAP, getting error: 2024-08-29 09:32:23 : imapConnect - IMAP Connect Error: Error Domain=MCOErrorDomain Code=5 "Unable to authenticate with the current session's credentials." UserInfo={NSLocalizedDescription=Unable to authenticate with the current session's credentials.}

Related to (as at the end solution was not to use OAuth for outlook): https://github.com/MailCore/mailcore2/issues/1652

NOTE: Standard Outlook access will DISCONTINUE in a month, switch to OAuth is a must: https://support.microsoft.com/en-gb/office/modern-authentication-methods-now-needed-to-continue-syncing-outlook-email-in-non-microsoft-email-apps-c5d65390-9676-4763-b41f-d7986499a90d

Please advise what I might be doing wrong.

Platform(s)

<iOS>

Happens on Mail Server

<Outlook.com>

Piece of code

    public static func imapOAuthOutlook() async {
        let session =  MCOIMAPSession()
        session.hostname = "imap-mail.outlook.com"
        //session.username = "<****example>@<outlook.com>"
        session.username = "****example@outlook.com"
        //session.password = nil
        session.oAuth2Token  = OAuthDetails.getToken()
        session.port = 143//993//143
        session.allowsFolderConcurrentAccessEnabled = true
        session.connectionType              = MCOConnectionType.startTLS
        session.authType                    = MCOAuthType.xoAuth2Outlook//xoAuth2//xoAuth2Outlook
        session.isCheckCertificateEnabled   = false
        session.isVoIPEnabled               = false
        session.maximumConnections          = 2
        session.timeout                     = App.connectionTimeout

        if let loginOperation = session.checkAccountOperation() {
            do { try await loginOperation.start(); logd("imapConnect - Successful IMAP connection") }
            catch { logd("imapConnect - IMAP Connect Error: \(error)"); return }
        }

        // list folders
        var folderList:[Any]? = nil
        if let folderListOperation = session.fetchAllFoldersOperation() {
            do { folderList = try await folderListOperation.start(); logd("All IMAP Folders loaded") }
            catch { logd("Error listing folders: \(error)") }
        }

        for value in folderList! { // MARK: folderlist was null
            switch value {
            case is MCOIMAPFolder:
                let folder:MCOIMAPFolder = value as! MCOIMAPFolder
                //folder.path
                logd("\(folder.path.description) is a folder")
                //folder.
            default:
                logd("Skip ... possibly null value!")
            }
        }
    }

my scopes for the token are:
    let kScopes:[String] = [//"ExternalItem.Read.All",
                            "IMAP.AccessAsUser.All",
                            "Mail.Read",
                            "Mail.ReadBasic",
                            //"Mail.ReadBasic.All",
                            "Mail.ReadWrite",
                            "Mail.Send",
                            //"MailboxFolder.Read.All",
                            //"MailboxFolder.ReadWrite.All",
                            //"MailboxItem.ImportExport.All",
                            //"MailboxItem.Read.All",
                            //"MailboxSettings.Read",
                            //"MailboxSettings.ReadWrite",
                            "SMTP.Send",
                            //"User-Mail.ReadWrite.All",
                            "User.Read"
    ]

Actual outcome MailCore did or did not get folders data via IMAP

Connection Logs

2024-08-29 09:32:22 SettingsDialog :  : acquireTokenSilently: Access token expiery is: 2024-08-29 10:32:22
2024-08-29 09:32:22 SettingsDialog :  : acquireTokenSilently: Refreshed Access token is: EwCYA8l6BAAUbDba3x2OMJElkF7gJ4z/VbCPEz0AARR7frPnZGsM8SaVqr6yJ2Cp/t/ZppviKuQFzopp2OCTA+k4CTgbBc5gjbXjYrpAGSMZhibBmnIKu0zgW+kgsPOMZZ2451HpGOlYMhjIXNYP1HB7prux6o53bNu9j4MdXFww9rwXKxja0q1rPxkZCak1ZUyLwd1rNVU7Dzt1uGbnAR5qYqP08KV18n3MmyfuXKhpV8T7BPC2HEm7LzTd2zMh6YesGWV8jA1IPR8i+frDwwv1FtAR95wMtmMyi8+6BjirFYLNTgPZh7/k8rMX33t/rUwaxt0N4F9AenD7gICrTzdwWgXenNCdjeQO0T2Obu8tnE5rm0LzGmBsBlEYcS0QZgAAECNeD+H8O1h//7/Omj3eOM5gAj96OFnuKPkhZw7pCHzqzR0wLNLkNDv7/NFMaUXD4f3RwlE1i43SXOIzHYJ2/D0iJAiGPEWTrD5ArohJ3bHCsTVpCE1NYC8Wo0hOKWeq7OHfy/b0ou8o757mnCfMigJsPOQzDsuF+H7HgHQx/E4/6swfdWRrPqviRjeUpo0b7834CRe8h4eZhPuonPOBakg8NMTQkA+AHGYqUNAEJuJjdttOtUhrik65xEpOpdKl8Q5Tz9MQrZmN26sF1XG6CsBL1GCB/2qpLMa4VVvWKHJWk5mQZBjlDbTNxtPfm2Pnx4tZ2bleQwHlBf2sMnsjCecpDAPAHJLp8Bl05dGj5WL3TaKZGiQjuHPtGYYHs1LMn9VPyb+dDcfuKuwjMw5jPEuDNtLhTet73N07HVT0ebHrU7jg9GJwYkR1d7th0LuGv0h1NHZ8fKbSdnqvQhPMnGmGBD+O5Lil+tdwitpPL7VoR//W9exG/CMrDKYd3WS5QxRlMBz2zwUaPyRidLFEtJLQPbHG4wJYwOd7biIrESq09Cdq7uKLwokq9a7ItAIq0sKbfainBL+QCos60kHu+QGEpIR/lfnyNunewtAAIc/+YFALymXcUGE8uiDj+97X/URnXGvphDD5hrsSdg86Cuj+Kk4vf/tpTnR5KYk1j/DpBYSMKspCND0rTT5iNkm3L8C/9nBW5ix7Qmm5y+vlnhdU/Tzt9yXsN3d9uUjezMV6vR5flDVfJuIsitqOdvAPGOi523vgtQtIZoUgP/xDRb58Vmd3rrjBkg+BqwjGKAvdBnT/4op2hkdKLc3CTMU/T2ZWpAI=
2024-08-29 09:32:22 SettingsDialog :  :  ... tasks started ------------>>   EXECUTE OUTLOOK TEST 
2024-08-29 09:32:23 : imapConnect - IMAP Connect Error: Error Domain=MCOErrorDomain Code=5 "Unable to authenticate with the current session's credentials." UserInfo={NSLocalizedDescription=Unable to authenticate with the current session's credentials.}

Expected outcome MailCore should list folders via IMAP

Link to sample code on GitHub reproducing the issue (a full Xcode project):

https://
Be-Maps commented 2 weeks ago

getting 501 at the end of TLS negotiation.