diff --git a/office-alexander-logistics-app/pom.xml b/office-alexander-logistics-app/pom.xml index 091b2b2..65664fd 100644 --- a/office-alexander-logistics-app/pom.xml +++ b/office-alexander-logistics-app/pom.xml @@ -319,5 +319,18 @@ 3.6 + + + org.apache.httpcomponents + httpclient + 4.5.10 + test + + + com.fasterxml.jackson.core + jackson-databind + 2.13.3 + test + \ No newline at end of file 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 new file mode 100644 index 0000000..88e29b5 --- /dev/null +++ b/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPGetToken.java @@ -0,0 +1,162 @@ +package com.alexanderlogistics; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.Properties; + +import javax.mail.Session; +import javax.mail.Store; + +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicHeader; +import org.junit.Test; + +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; + +import junit.framework.Assert; + +/** + * This test generates + * + * See: https://www.java-forum.org/thema/javamail-mit-oauth2-an-o365.199133/ + * + * @author rsoika + * + */ +public class TestIMAPGetToken { + + /** + * Tests the Imap access + */ + @Test + public void imapAccess() { + String mailServerAdress = "outlook.office365.com"; + String mailboxUserName = "###MyUserName###"; + String mailboxPassword = "###MyPassword###"; + String protocol = "imaps"; + + String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; + Properties props = new Properties(); + + props.put("mail.imap.ssl.enable", "true"); + props.put("mail.imap.port", "993"); + + props.put("mail.imap.auth.mechanisms", "XOAUTH2"); + props.put("mail.imap.sasl.mechanisms", "XOAUTH2"); + + props.put("mail.imap.auth.login.disable", "true"); + props.put("mail.imap.auth.plain.disable", "true"); + + props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY); + props.setProperty("mail.imap.socketFactory.fallback", "false"); + props.setProperty("mail.imap.socketFactory.port", "993"); + props.setProperty("mail.imap.starttls.enable", "true"); + + props.put("mail.debug", "true"); + props.put("mail.debug.auth", "true"); + try { + + mailboxPassword = getAuthToken(); + System.out.println("info: AccessToken: " + mailboxPassword); + + Session session = Session.getInstance(props); + session.setDebug(true); + System.out.println("info: properties: " + session.getProperties().toString()); + Store store = session.getStore(protocol); + store.connect(mailServerAdress, mailboxUserName, mailboxPassword); + } catch (Exception e) { + + e.printStackTrace(); + Assert.fail(); + } + } + + /** + * Test the getToken method + */ + @Test + public void testGetToken() { + try { + + String token = getAuthToken(); + + System.out.println("....access token = " + token); + + } catch (Exception e) { + e.printStackTrace(); + Assert.fail(); + } + } + + /** + * Helper method to receive a Microsoft access Token + * + * @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 client_secret = "###MyClientSecret###"; + + 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; + } + + public static byte[] readAllBytes(InputStream inputStream) throws IOException { + final int bufLen = 4 * 0x400; // 4KB + byte[] buf = new byte[bufLen]; + int readLen; + IOException exception = null; + + try { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + while ((readLen = inputStream.read(buf, 0, bufLen)) != -1) + outputStream.write(buf, 0, readLen); + + return outputStream.toByteArray(); + } + } catch (IOException e) { + exception = e; + throw e; + } finally { + if (exception == null) + inputStream.close(); + else + try { + inputStream.close(); + } catch (IOException e) { + exception.addSuppressed(e); + } + } + } +} \ No newline at end of file diff --git a/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPOauth2.java b/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPOauth2.java index 7cdedcc..4ec6964 100644 --- a/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPOauth2.java +++ b/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/TestIMAPOauth2.java @@ -16,6 +16,8 @@ import junit.framework.Assert; /** * This test generates random test data * + * https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#get-an-access-token + * * @author rsoika * */ @@ -48,6 +50,8 @@ public class TestIMAPOauth2 { // are same properties.put("mail.imap.auth.login.disable", "true"); properties.put("mail.imap.auth.plain.disable", "true"); + + // Activate debug mode properties.put("mail.debug", "true"); properties.put("mail.debug.auth", "true"); @@ -79,4 +83,6 @@ public class TestIMAPOauth2 { } } + + }