test clients
This commit is contained in:
parent
a35633b7aa
commit
77e966f387
3 changed files with 181 additions and 0 deletions
|
|
@ -319,5 +319,18 @@
|
||||||
<version>3.6</version>
|
<version>3.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Microsoft oauth token -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.5.10</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.13.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -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<String, String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,6 +16,8 @@ import junit.framework.Assert;
|
||||||
/**
|
/**
|
||||||
* This test generates random test data
|
* 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
|
* @author rsoika
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
@ -48,6 +50,8 @@ public class TestIMAPOauth2 {
|
||||||
// are same
|
// are same
|
||||||
properties.put("mail.imap.auth.login.disable", "true");
|
properties.put("mail.imap.auth.login.disable", "true");
|
||||||
properties.put("mail.imap.auth.plain.disable", "true");
|
properties.put("mail.imap.auth.plain.disable", "true");
|
||||||
|
|
||||||
|
// Activate debug mode
|
||||||
properties.put("mail.debug", "true");
|
properties.put("mail.debug", "true");
|
||||||
properties.put("mail.debug.auth", "true");
|
properties.put("mail.debug.auth", "true");
|
||||||
|
|
||||||
|
|
@ -79,4 +83,6 @@ public class TestIMAPOauth2 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue