Skip to content

Commit

Permalink
Merge pull request #32 from axonivy-market/MARP-1189-mail-store-conne…
Browse files Browse the repository at this point in the history
…ctor-support-o-auth-2-authentication

Marp 1189 mail store connector support o auth 2 authentication
  • Loading branch information
nvvi authored Nov 5, 2024
2 parents fd6d27a + d137676 commit 4bd253c
Show file tree
Hide file tree
Showing 12 changed files with 744 additions and 28 deletions.
88 changes: 88 additions & 0 deletions mailstore-connector-demo/config/variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,91 @@ Variables:
properties:
mail.imaps.ssl.checkserveridentity: false
mail.imaps.ssl.trust: '*'
# only set below credential when you go with oauth2
# mail.imaps.auth.mechanisms: 'XOAUTH2'
# mail.imaps.sasl.enable: 'true'
# mail.imaps.sasl.mechanisms: 'XOAUTH2'

# com.axonivy.connector.oauth.BasicUserPasswordProvider for Basic Authentication
# com.axonivy.connector.oauth.AzureOauth2UserPasswordProvider for AzureOauth2UserPasswordProvider
userPasswordProvider: ''

# only set below credential when you go with oauth2
# tenant to use for OAUTH2 request.
# set the Azure Directory (tenant) ID, for application requests.
tenantId: ''
# Your Azure Application (client) ID, used for OAuth2 authentication
appId: ''
# Secret key from your applications "certificates & secrets" (client secret)
secretKey: ''
# for client_credentials: https://outlook.office365.com/.default
scope: ''
#[client_credentials]
grantType: ''


localhost-imap-basic-authentication:
# [enum: pop3, pop3s, imap, imaps]
protocol: 'imap'
# Host for store connection
host: 'localhost'
# Port for store connection (only needed if not default)
port: -1
# User name for store connection
user: '[email protected]'
# Password for store connection
# [password]
password: 'pass'
# show debug output for connection
debug: true
# Additional properties for store connection,
# see https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/package-summary.html
properties:
mail.imaps.ssl.checkserveridentity: false
mail.imaps.ssl.trust: '*'

userPasswordProvider: 'com.axonivy.connector.oauth.BasicUserPasswordProvider'


localhost-imap-azure-oauth2-authentication:
# [enum: pop3, pop3s, imap, imaps]
protocol: 'imap'
# Host for store connection
host: 'localhost'
# Port for store connection (only needed if not default)
port: -1
# User name for store connection
user: '[email protected]'
# Password for store connection
# [password]
password: ''
# show debug output for connection
debug: true
# Additional properties for store connection,
# see https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/package-summary.html
properties:
mail.imaps.ssl.checkserveridentity: false
mail.imaps.ssl.trust: '*'
# only set below credential when you go with oauth2
mail.imaps.auth.mechanisms: 'XOAUTH2'
mail.imaps.sasl.enable: 'true'
mail.imaps.sasl.mechanisms: 'XOAUTH2'

userPasswordProvider: 'com.axonivy.connector.oauth.AzureOauth2UserPasswordProvider'

# only set below credential when you go with oauth2
# tenant to use for OAUTH2 request.
# set the Azure Directory (tenant) ID, for application requests.
tenantId: ''
# Your Azure Application (client) ID, used for OAuth2 authentication
appId: ''
# Secret key from your applications "certificates & secrets" (client secret)
secretKey: ''
# for client_credentials/password: https://outlook.office365.com/.default
scope: ''
#[client_credentials, password]
grantType: ''
# login microsoft azure
azureOAuth:
loginUrl: 'login.microsoftonline.com'

90 changes: 83 additions & 7 deletions mailstore-connector-demo/processes/MailStoreDemo.p.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"id" : "f1",
"type" : "TaskEnd",
"visual" : {
"at" : { "x" : 640, "y" : 48 }
"at" : { "x" : 888, "y" : 48 }
}
}, {
"id" : "f3",
Expand Down Expand Up @@ -49,7 +49,7 @@
}
},
"visual" : {
"at" : { "x" : 512, "y" : 48 }
"at" : { "x" : 624, "y" : 48 }
},
"connect" : { "id" : "f2", "to" : "f1" }
}, {
Expand Down Expand Up @@ -92,7 +92,7 @@
"id" : "f6",
"type" : "TaskEnd",
"visual" : {
"at" : { "x" : 640, "y" : 184 }
"at" : { "x" : 888, "y" : 184 }
}
}, {
"id" : "f11",
Expand All @@ -108,7 +108,7 @@
}
},
"visual" : {
"at" : { "x" : 512, "y" : 184 }
"at" : { "x" : 624, "y" : 184 }
},
"connect" : { "id" : "f9", "to" : "f6" }
}, {
Expand All @@ -121,14 +121,14 @@
"case" : { }
},
"visual" : {
"at" : { "x" : 376, "y" : 328 }
"at" : { "x" : 384, "y" : 328 }
},
"connect" : { "id" : "f14", "to" : "f13" }
}, {
"id" : "f10",
"type" : "TaskEnd",
"visual" : {
"at" : { "x" : 800, "y" : 328 }
"at" : { "x" : 888, "y" : 328 }
}
}, {
"id" : "f13",
Expand All @@ -144,8 +144,84 @@
}
},
"visual" : {
"at" : { "x" : 584, "y" : 328 }
"at" : { "x" : 624, "y" : 328 }
},
"connect" : { "id" : "f15", "to" : "f10" }
}, {
"id" : "f16",
"type" : "RequestStart",
"name" : "connectMailStoreWithBasicAuth.ivp",
"config" : {
"callSignature" : "connectMailStoreWithBasicAuth",
"outLink" : "connectMailStoreWithBasicAuth.ivp",
"case" : { }
},
"visual" : {
"at" : { "x" : 384, "y" : 560 }
},
"connect" : { "id" : "f19", "to" : "f18" }
}, {
"id" : "f17",
"type" : "TaskEnd",
"visual" : {
"at" : { "x" : 888, "y" : 560 }
}
}, {
"id" : "f18",
"type" : "Script",
"name" : "connectMailStoreWithBasicAuth",
"config" : {
"security" : "system",
"output" : {
"code" : [
"import com.axonivy.connector.mailstore.demo.DemoService;",
"",
"DemoService.connectMailStoreWithBasicAuth();"
]
}
},
"visual" : {
"at" : { "x" : 632, "y" : 560 },
"size" : { "width" : 232, "height" : 66 }
},
"connect" : { "id" : "f20", "to" : "f17" }
}, {
"id" : "f21",
"type" : "RequestStart",
"name" : "connectMailStoreWithAzureOauth2.ivp",
"config" : {
"callSignature" : "connectMailStoreWithAzureOauth2",
"outLink" : "connectMailStoreWithAzureOauth2.ivp",
"case" : { }
},
"visual" : {
"at" : { "x" : 384, "y" : 768 }
},
"connect" : { "id" : "f24", "to" : "f22" }
}, {
"id" : "f22",
"type" : "Script",
"name" : "connectMailStoreWithAzureOauth2",
"config" : {
"security" : "system",
"output" : {
"code" : [
"import com.axonivy.connector.mailstore.demo.DemoService;",
"",
"DemoService.connectMailStoreWithAzureOauth2();"
]
}
},
"visual" : {
"at" : { "x" : 632, "y" : 768 },
"size" : { "width" : 232, "height" : 60 }
},
"connect" : { "id" : "f25", "to" : "f23" }
}, {
"id" : "f23",
"type" : "TaskEnd",
"visual" : {
"at" : { "x" : 888, "y" : 768 }
}
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.axonivy.connector.mailstore.MailStoreService;
import com.axonivy.connector.mailstore.MailStoreService.MessageIterator;
import com.axonivy.connector.oauth.UserPasswordProvider;
import com.axonivy.connector.mailstore.MessageService;

import ch.ivyteam.ivy.environment.Ivy;
Expand All @@ -21,9 +22,14 @@

public class DemoService {
private static final Logger LOG = Ivy.log();
private static final String INBOX = "INBOX";
private static final String USER_PASSWORD_PROVIDER = "userPasswordProvider";
private static final String LOCALHOST_IMAP = "localhost-imap";
private static final String LOCALHOST_IMAP_BASIC_AUTHENTICATION = "localhost-imap-basic-authentication";
private static final String LOCALHOST_IMAP_AZURE_OAUTH2_AUTHENTICATION = "localhost-imap-azure-oauth2-authentication";

public static void handleMessages() throws MessagingException, IOException {
MessageIterator iterator = MailStoreService.messageIterator("localhost-imap", "INBOX", null, false, MailStoreService.subjectMatches(".*test [0-9]+.*"), new MessageComparator());
MessageIterator iterator = MailStoreService.messageIterator(LOCALHOST_IMAP, INBOX, null, false, MailStoreService.subjectMatches(".*test [0-9]+.*"), new MessageComparator());

while (iterator.hasNext()) {
Message message = iterator.next();
Expand Down Expand Up @@ -51,7 +57,7 @@ public static boolean handleMessage(Message message) throws MessagingException,
}

public static void handleMessagesMultiDestinationFolder() throws MessagingException, IOException {
MessageIterator iterator = MailStoreService.messageIterator("localhost-imap", "INBOX", true, MailStoreService.subjectMatches(".*test [0-9]+.*"), new MessageComparator(), Arrays.asList("Processed", "ErrorFolder"));
MessageIterator iterator = MailStoreService.messageIterator(LOCALHOST_IMAP, INBOX, true, MailStoreService.subjectMatches(".*test [0-9]+.*"), new MessageComparator(), Arrays.asList("Processed", "ErrorFolder"));
int runner = 0;

while (iterator.hasNext()) {
Expand All @@ -63,15 +69,41 @@ public static void handleMessagesMultiDestinationFolder() throws MessagingExcept
}
}

public static void connectMailStoreWithBasicAuth() throws MessagingException, IOException {
// get from variable mailstore-connector.localhost-imap.userPasswordProvider
String authProviderPath = MailStoreService.getVar(LOCALHOST_IMAP_BASIC_AUTHENTICATION, USER_PASSWORD_PROVIDER);
initAuthProvider(LOCALHOST_IMAP_BASIC_AUTHENTICATION, authProviderPath);

MessageIterator iterator = MailStoreService.messageIterator(LOCALHOST_IMAP_BASIC_AUTHENTICATION, INBOX, null, false, MailStoreService.subjectMatches(".*"), new MessageComparator());

while (iterator.hasNext()) {
Message message = iterator.next();
boolean handled = handleMessage(message);
iterator.handledMessage(handled);
}
}

public static void connectMailStoreWithAzureOauth2() throws MessagingException, IOException {
// get from variable mailstore-connector.localhost-imap.userPasswordProvider
String authProviderPath = MailStoreService.getVar(LOCALHOST_IMAP_AZURE_OAUTH2_AUTHENTICATION, USER_PASSWORD_PROVIDER);
initAuthProvider(LOCALHOST_IMAP_AZURE_OAUTH2_AUTHENTICATION, authProviderPath);

MessageIterator iterator = MailStoreService.messageIterator(LOCALHOST_IMAP_AZURE_OAUTH2_AUTHENTICATION, INBOX, null, false, MailStoreService.subjectMatches(".*"), new MessageComparator());

while (iterator.hasNext()) {
Message message = iterator.next();
boolean handled = handleMessage(message);
iterator.handledMessage(handled);
}
}

public static void handleAttachmentMessages() throws MessagingException, IOException {
MessageIterator iterator = MailStoreService.messageIterator(
"localhost-imap",
"INBOX",
LOCALHOST_IMAP,
INBOX,
null,
false,
null);
// MailStoreService.hasAttachment(true));

while (iterator.hasNext()) {
Message message = iterator.next();
Expand All @@ -86,6 +118,16 @@ public static void handleAttachmentMessages() throws MessagingException, IOExcep
iterator.handledMessage(handled);
}
}

private static void initAuthProvider(String storeName, String authProviderPath) {
try {
Class<?> clazz = Class.forName(authProviderPath);
UserPasswordProvider userPasswordProvider = (UserPasswordProvider) clazz.getDeclaredConstructor().newInstance();
MailStoreService.registerUserPasswordProvider(storeName, userPasswordProvider);
} catch(Exception ex) {
LOG.error("Exception during instatiation of UserPasswordProvider ''{0}''.",ex, authProviderPath);
}
}

private static boolean logMessage(Message message) throws MessagingException, IOException {
LOG.info("Working on message {0} received at {1} type {2}", message.getSubject(), message.getReceivedDate(), message.getContent().getClass());
Expand Down
Loading

0 comments on commit 4bd253c

Please sign in to comment.