From 3d0993deab57415c24033fdcc65ae876f67654a1 Mon Sep 17 00:00:00 2001 From: Ralph Soika Date: Tue, 25 Oct 2022 11:47:36 +0200 Subject: [PATCH] update outlook test --- office-alexander-logistics-app/pom.xml | 23 ++ .../TestIMAPAuhtenticator.java | 71 ++++++ .../alexanderlogistics/TestIMAPGetToken.java | 230 +++++++++++++++++- 3 files changed, 318 insertions(+), 6 deletions(-) create mode 100644 office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPAuhtenticator.java diff --git a/office-alexander-logistics-app/pom.xml b/office-alexander-logistics-app/pom.xml index 65664fd..749db7d 100644 --- a/office-alexander-logistics-app/pom.xml +++ b/office-alexander-logistics-app/pom.xml @@ -320,6 +320,12 @@ + + com.sun.mail + javax.mail + 1.6.2 + test + org.apache.httpcomponents httpclient @@ -332,5 +338,22 @@ 2.13.3 test + + + + + javax.json + javax.json-api + 1.1 + test + + + + org.glassfish + javax.json + 1.1 + test + + \ No newline at end of file diff --git a/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPAuhtenticator.java b/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPAuhtenticator.java new file mode 100644 index 0000000..7725f6e --- /dev/null +++ b/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPAuhtenticator.java @@ -0,0 +1,71 @@ +package com.alexanderlogistics; + +import java.util.Properties; +import java.util.logging.Logger; + +import javax.mail.MessagingException; +import javax.mail.Store; + +import org.imixs.archive.importer.DocumentImportService; +import org.imixs.archive.importer.mail.IMAPOutlookAuthenticator; +import org.imixs.workflow.ItemCollection; +import org.junit.Before; +import org.junit.Test; + +import junit.framework.Assert; + +/** + * This Imixs IMAPAuthenticator + * @author rsoika + * + */ +public class TestIMAPAuhtenticator { + + + private final static Logger logger = Logger.getLogger(TestIMAPAuhtenticator.class.getName()); + + @Before + public void setUp() throws Exception { + + } + + /** + * Test imap connect via oauth + * + * https://techcommunity.microsoft.com/t5/exchange/javamail-connecting-to-office-365-xoauth2-for-imap/m-p/1505026 + */ + @Test + public void testOutlookToken() { + + IMAPOutlookAuthenticator auth=new IMAPOutlookAuthenticator(); + + + ItemCollection source=new ItemCollection(); + source.setItemValue(DocumentImportService.SOURCE_ITEM_SERVER, "outlook.office365.com"); + source.setItemValue(DocumentImportService.SOURCE_ITEM_PORT, "993"); + source.setItemValue(DocumentImportService.SOURCE_ITEM_USER, "imixs@alexander-logistics.com"); + source.setItemValue(DocumentImportService.SOURCE_ITEM_PASSWORD, "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL"); + + + + + + Properties sourceOptions = new Properties(); + sourceOptions.put("microsoft.tenantid", "51e2f038-0a96-41be-801d-bb4118aa018e"); + sourceOptions.put("microsoft.clientid", "39e9eec5-6939-47dc-9729-3438a9898e63"); + + try { + Store store = auth.openMessageStore(source, sourceOptions); + + Assert.assertNotNull(store); + } catch (MessagingException e) { + e.printStackTrace(); + Assert.fail(); + + } + + } + + + +} diff --git a/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPGetToken.java b/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPGetToken.java index 88e29b5..37c9bb5 100644 --- a/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPGetToken.java +++ b/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPGetToken.java @@ -3,9 +3,13 @@ package com.alexanderlogistics; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.Map; import java.util.Properties; +import javax.mail.Folder; +import javax.mail.Message; import javax.mail.Session; import javax.mail.Store; @@ -21,6 +25,7 @@ import org.junit.Test; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; +import com.sun.mail.imap.IMAPFolder; import junit.framework.Assert; @@ -40,7 +45,8 @@ public class TestIMAPGetToken { @Test public void imapAccess() { String mailServerAdress = "outlook.office365.com"; - String mailboxUserName = "###MyUserName###"; + // String mailboxUserName = "###MyUserName###"; + String mailboxUserName = "imixs@alexander-logistics.com"; String mailboxPassword = "###MyPassword###"; String protocol = "imaps"; @@ -74,7 +80,116 @@ public class TestIMAPGetToken { Store store = session.getStore(protocol); store.connect(mailServerAdress, mailboxUserName, mailboxPassword); } catch (Exception e) { - + + e.printStackTrace(); + Assert.fail(); + } + } + + @Test + public void imapAccess2() { + + String userEmailId = "imixs@alexander-logistics.com"; + try { + String oauth2AccessToken = getAuthToken(); + + String host = "outlook.office365.com"; + String port = "993"; + Store store = null; + + String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; + Properties props = new Properties(); + + props.put("mail.imaps.ssl.enable", "true"); + props.put("mail.imaps.sasl.enable", "true"); + props.put("mail.imaps.port", port); + + props.put("mail.imaps.auth.mechanisms", "XOAUTH2"); + props.put("mail.imaps.sasl.mechanisms", "XOAUTH2"); + + props.put("mail.imaps.auth.login.disable", "true"); + props.put("mail.imaps.auth.plain.disable", "true"); + + props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY); + props.setProperty("mail.imaps.socketFactory.fallback", "false"); + props.setProperty("mail.imaps.socketFactory.port", port); + props.setProperty("mail.imaps.starttls.enable", "true"); + props.put("mail.imaps.auth.plain.disable", "true"); + props.put("mail.debug", "true"); + props.put("mail.debug.auth", "true"); + + Session session = Session.getInstance(props); + session.setDebug(true); + + store = session.getStore("imaps"); + + System.out.println("OAUTH2 IMAP trying to connect with system properties to Host:" + host + ", Port: " + + port + ", userEmailId: " + userEmailId + ", AccessToken: " + oauth2AccessToken); + + String anderesPasswort = "i^i$oe-!+o1o0io("; + store.connect(host, userEmailId, oauth2AccessToken); + System.out.println("IMAP connected with system properties to Host:" + host + ", Port: " + port + + ", userEmailId: " + userEmailId + ", AccessToken: " + oauth2AccessToken); + if (store.isConnected()) { + System.out.println("Connection Established using imap protocol successfully !"); + + IMAPFolder inbox = (IMAPFolder) store.getFolder("INBOX"); + inbox.open(Folder.READ_WRITE); + + // fetches all messages from the INBOX... + Message[] messages = inbox.getMessages(); + System.out.println("..." + messages.length + " new messages found"); + } + } catch (Exception e) { + System.out.println("Store.Connect failed with the errror: " + e.getMessage()); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + String exceptionAsString = sw.toString(); + System.out.println(exceptionAsString); + Assert.fail(); + } + + } + + /** + * Tests the Imap access + * + * https://stackoverflow.com/questions/73463357/cannot-authenticate-to-imap-on-office365-using-javamail + * + * mail.store.protocol="imap" mail.imap.host="outlook.office365.com" + * mail.imap.port="993" mail.imap.ssl.enable="true" + * mail.imap.starttls.enable="true" mail.imap.auth="true" + * mail.imap.auth.mechanisms="XOAUTH2" mail.imap.user="" + */ + @Test + public void imapAccess3() { + Properties props = new Properties(); + + props.put("mail.store.protocol", "imap"); + props.put("mail.imap.host", "outlook.office365.com"); + props.put("mail.imap.port", "993"); + props.put("mail.imap.ssl.enable", "true"); + + props.put("mail.imap.starttls.enable", "true"); + props.put("mail.imap.auth", "true"); + props.put("mail.imap.auth.mechanisms", "XOAUTH2"); + props.put("mail.imap.user", "imixs@alexander-logistics.com"); + + // props.put("mail.imaps.auth.plain.disable","true"); + + props.put("mail.debug", "true"); + props.put("mail.debug.auth", "true"); + try { + + String token = getAuthToken(); + + Session session = Session.getInstance(props); + session.setDebug(true); + System.out.println("info: properties: " + session.getProperties().toString()); + Store store = session.getStore("imap"); + store.connect("outlook.office365.com", "imixs@alexander-logistics.com", token); + } catch (Exception e) { + e.printStackTrace(); Assert.fail(); } @@ -100,17 +215,120 @@ public class TestIMAPGetToken { /** * Helper method to receive a Microsoft access Token * + * https://jwt.ms/ + * + * "OAuth2": { + * + * "Authority": + * https://login.microsoftonline.com/51e2f038-0a96-41be-801d-bb4118aa018e/v2.0, + * + * "ClientId": "39e9eec5-6939-47dc-9729-3438a9898e63", + * + * "ClientSecret": "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL" + * + * } + * + * + * + * + * + * Hallo Herr Soika, + * + * + * + * ich habe eine neue App angelegt und hier die Berechtigungen neu gesetzt. + * + * + * + * Hier die Daten dazu: + * + * + * + * "OAuth2": { + * + * "Authority": + * https://login.microsoftonline.com/51e2f038-0a96-41be-801d-bb4118aa018e/v2.0, + * + * "ClientId": "39e9eec5-6939-47dc-9729-3438a9898e63", + * + * "ClientSecret": "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL" + * + * } + * + * + * + * Hier noch der Link zu einem Video, welches ganz gut erklärt wie das ganze + * eingebunden werden muss. + * + * Anhand dieser Anleitung bin ich ebefalls vorgegangen. + * + * + * + * https://www.youtube.com/watch?v=bMYA-146dmM&t=356s + * + * + * * @return * @throws IOException * @throws ClientProtocolException */ public String getAuthToken() throws ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); - HttpPost loginPost = new HttpPost("https://login.microsoftonline.com/###MyTenantID###/oauth2/v2.0/token"); - String clientId = "###MyClientId###"; - String scopes = "https://outlook.office365.com/.default"; + String tanantId = "51e2f038-0a96-41be-801d-bb4118aa018e"; + String clientId = "39e9eec5-6939-47dc-9729-3438a9898e63"; + String client_secret = "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL"; - String client_secret = "###MyClientSecret###"; + HttpPost loginPost = new HttpPost("https://login.microsoftonline.com/" + tanantId + "/oauth2/v2.0/token"); + String scopes = "https://outlook.office365.com/.default"; + // scopes="https://outlook.office365.com/IMAP.AccessAsUser.All"; + // scopes="https://graph.microsoft.com/.default"; + // scopes="openid profile offline_access https://outlook.office365.com/SMTP.Send + // https://outlook.office365.com/POP.AccessAsUser.All + // https://outlook.office365.com/IMAP.AccessAsUser.All"; + + String encodedBody = "client_id=" + clientId + "&scope=" + scopes + "&client_secret=" + client_secret + + "&grant_type=client_credentials"; + + loginPost.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_FORM_URLENCODED)); + + loginPost.addHeader(new BasicHeader("cache-control", "no-cache")); + CloseableHttpResponse loginResponse = client.execute(loginPost); + + InputStream inputStream = loginResponse.getEntity().getContent(); + byte[] response = readAllBytes(inputStream); + ObjectMapper objectMapper = new ObjectMapper(); + JavaType type = objectMapper.constructType( + objectMapper.getTypeFactory().constructParametricType(Map.class, String.class, String.class)); + Map parsed = new ObjectMapper().readValue(response, type); + String result = parsed.get("access_token"); + + System.out.println("....access token = " + result); + return result; + } + + /** + * Helper method to receive a Microsoft access Token + * + * @return + * @throws IOException + * @throws ClientProtocolException + */ + public String getAuthTokenOld() throws ClientProtocolException, IOException { + CloseableHttpClient client = HttpClients.createDefault(); + String tanantId = "51e2f038-0a96-41be-801d-bb4118aa018e"; + String clientId = "75657ae4-465d-4245-9c1f-ebd6fdfc4814"; + // String clientId = "6ed3fb66-a16a-47da-a4ae-fb3d8690b117"; + String objectId = "229d0e4f-eef1-476c-9b8c-97c437a93314"; + // String client_secret= "2a41bf2d-ffd8-444d-a2c5-6a4a418650ba"; + String client_secret = "EM_8Q~6HRhhKhwJKAIXQcRdlJzn.iOj2Fq3Yhdo_"; + + HttpPost loginPost = new HttpPost("https://login.microsoftonline.com/" + tanantId + "/oauth2/v2.0/token"); + String scopes = "https://outlook.office365.com/.default"; + // scopes="https://outlook.office365.com/IMAP.AccessAsUser.All"; + // scopes="https://graph.microsoft.com/.default"; + // scopes="openid profile offline_access https://outlook.office365.com/SMTP.Send + // https://outlook.office365.com/POP.AccessAsUser.All + // https://outlook.office365.com/IMAP.AccessAsUser.All"; String encodedBody = "client_id=" + clientId + "&scope=" + scopes + "&client_secret=" + client_secret + "&grant_type=client_credentials";