diff --git a/.gitignore b/.gitignore index 18451896..ad2fdb33 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ *.iws *.ipr .idea +.DS_Store + # Mobile Tools for Java (J2ME) .mtj.tmp/ diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.notification/pom.xml b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/pom.xml new file mode 100644 index 00000000..d8976a3b --- /dev/null +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/pom.xml @@ -0,0 +1,122 @@ + + + + + 4.0.0 + + + org.wso2.carbon.identity.conditional.auth.functions + identity-conditional-auth-functions + 0.1.1-SNAPSHOT + ../../pom.xml + + + org.wso2.carbon.identity.conditional.auth.functions.notification + Conditional Authentication - Notification Related Functions + bundle + + + + commons-lang.wso2 + commons-lang + + + org.wso2.carbon + org.wso2.carbon.logging + + + org.wso2.carbon + org.wso2.carbon.core + + + org.wso2.carbon + org.wso2.carbon.user.core + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.application.authentication.framework + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.event + + + + + org.powermock + powermock-module-testng + test + + + org.powermock + powermock-api-mockito + test + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.testutil + + + + + + + org.apache.felix + maven-scr-plugin + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.artifactId} + ${project.artifactId} + + org.wso2.carbon.identity.conditional.auth.functions.notification.internal + + + !org.wso2.carbon.identity.conditional.auth.functions.notification.internal, + org.wso2.carbon.identity.conditional.auth.functions.notification + + + org.apache.commons.lang, + org.apache.commons.logging, + org.osgi.service.component, + org.osgi.service.component.annotations, + org.wso2.carbon, + org.wso2.carbon.core.util, + org.wso2.carbon.identity.application.authentication.framework, + org.wso2.carbon.identity.application.authentication.framework.exception, + org.wso2.carbon.identity.application.authentication.framework.model, + org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js, + org.wso2.carbon.identity.event, + org.wso2.carbon.identity.event.event, + org.wso2.carbon.identity.event.services, + org.wso2.carbon.registry.core.service, + org.wso2.carbon.user.core.service + + + + + + + diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/SendEmailFunction.java b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/SendEmailFunction.java new file mode 100644 index 00000000..b79c4119 --- /dev/null +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/SendEmailFunction.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.conditional.auth.functions.notification; + +import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticatedUser; + +import java.util.Map; + +@FunctionalInterface +public interface SendEmailFunction { + + /** + * Send an email to the given user using a provided template. + * @param user The user object to whom the mail is send + * @param templateId The email template id, Which is configured under email templates. + * @param paramMap Placeholder value map + * @return true if the email is successfully queued to be sent. false If the mail + * couldn't be queued due to any error. + */ + boolean sendMail(JsAuthenticatedUser user, String templateId, Map paramMap); + +} diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/SendEmailFunctionImpl.java b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/SendEmailFunctionImpl.java new file mode 100644 index 00000000..204d5cfa --- /dev/null +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/SendEmailFunctionImpl.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.conditional.auth.functions.notification; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticatedUser; +import org.wso2.carbon.identity.conditional.auth.functions.notification.internal.NotificationFunctionServiceHolder; +import org.wso2.carbon.identity.event.IdentityEventConstants; +import org.wso2.carbon.identity.event.IdentityEventException; +import org.wso2.carbon.identity.event.event.Event; + +import java.util.HashMap; +import java.util.Map; + +/** + * Contains the sendEmail() function implementation. + */ +public class SendEmailFunctionImpl implements SendEmailFunction { + + private static final String TEMPLATE_TYPE = "TEMPLATE_TYPE"; + + private static final Log LOG = LogFactory.getLog(SendEmailFunctionImpl.class); + + @Override + public boolean sendMail(JsAuthenticatedUser user, String templateId, Map paramMap) { + + String eventName = IdentityEventConstants.Event.TRIGGER_NOTIFICATION; + + HashMap properties = new HashMap<>(paramMap); + properties.put(IdentityEventConstants.EventProperty.USER_NAME, user.getWrapped().getUserName()); + properties.put(IdentityEventConstants.EventProperty.TENANT_DOMAIN, user.getWrapped().getTenantDomain()); + properties.put(IdentityEventConstants.EventProperty.USER_STORE_DOMAIN, user.getWrapped().getUserStoreDomain()); + properties.put(TEMPLATE_TYPE, templateId); + + Event identityMgtEvent = new Event(eventName, properties); + try { + NotificationFunctionServiceHolder.getInstance().getIdentityEventService().handleEvent(identityMgtEvent); + } catch (IdentityEventException e) { + LOG.error(String.format("Error when sending notification of template %s to user %s", templateId, user + .getWrapped().toFullQualifiedUsername()), e); + } + return true; + } +} diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/internal/NotificationFunctionServiceComponent.java b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/internal/NotificationFunctionServiceComponent.java new file mode 100644 index 00000000..3ea90b99 --- /dev/null +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/internal/NotificationFunctionServiceComponent.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.conditional.auth.functions.notification.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; +import org.osgi.service.component.annotations.ReferencePolicy; +import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry; +import org.wso2.carbon.identity.conditional.auth.functions.notification.SendEmailFunction; +import org.wso2.carbon.identity.conditional.auth.functions.notification.SendEmailFunctionImpl; +import org.wso2.carbon.identity.event.services.IdentityEventService; +import org.wso2.carbon.registry.core.service.RegistryService; +import org.wso2.carbon.user.core.service.RealmService; + +/** + * OSGi declarative services component which handles registration and de-registration of notifications related + * conditional auth functions. + */ + +@Component( + name = "identity.conditional.auth.functions.notification.component", + immediate = true +) +public class NotificationFunctionServiceComponent { + + private static final Log LOG = LogFactory.getLog(NotificationFunctionServiceComponent.class); + + @Activate + protected void activate(ComponentContext ctxt) { + + SendEmailFunction sendEmailFunction = new SendEmailFunctionImpl(); + JsFunctionRegistry jsFunctionRegistry = NotificationFunctionServiceHolder.getInstance().getJsFunctionRegistry(); + jsFunctionRegistry.register(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "sendEmail", sendEmailFunction); + } + + @Deactivate + protected void deactivate(ComponentContext ctxt) { + + JsFunctionRegistry jsFunctionRegistry = NotificationFunctionServiceHolder.getInstance().getJsFunctionRegistry(); + if (jsFunctionRegistry != null) { + jsFunctionRegistry.deRegister(JsFunctionRegistry.Subsystem.SEQUENCE_HANDLER, "sendEmail"); + } + } + + @Reference( + name = "user.realmservice.default", + service = RealmService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetRealmService" + ) + protected void setRealmService(RealmService realmService) { + + if (LOG.isDebugEnabled()) { + LOG.debug("RealmService is set in the conditional authentication notification functions bundle"); + } + NotificationFunctionServiceHolder.getInstance().setRealmService(realmService); + } + + protected void unsetRealmService(RealmService realmService) { + + if (LOG.isDebugEnabled()) { + LOG.debug("RealmService is unset in the conditional authentication notification functions bundle"); + } + NotificationFunctionServiceHolder.getInstance().setRealmService(null); + } + + @Reference( + name = "registry.service", + service = RegistryService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetRegistryService" + ) + protected void setRegistryService(RegistryService registryService) { + + if (LOG.isDebugEnabled()) { + LOG.debug("RegistryService is set in the conditional authentication notification functions bundle"); + } + NotificationFunctionServiceHolder.getInstance().setRegistryService(registryService); + } + + protected void unsetRegistryService(RegistryService registryService) { + + if (LOG.isDebugEnabled()) { + LOG.debug("RegistryService is unset in the conditional authentication notification functions bundle"); + } + NotificationFunctionServiceHolder.getInstance().setRegistryService(null); + } + + @Reference( + service = JsFunctionRegistry.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetJsFunctionRegistry" + ) + public void setJsFunctionRegistry(JsFunctionRegistry jsFunctionRegistry) { + + NotificationFunctionServiceHolder.getInstance().setJsFunctionRegistry(jsFunctionRegistry); + } + + public void unsetJsFunctionRegistry(JsFunctionRegistry jsFunctionRegistry) { + + NotificationFunctionServiceHolder.getInstance().setJsFunctionRegistry(null); + } + + @Reference( + service = IdentityEventService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetIdentityEventService" + ) + protected void setIdentityEventService(IdentityEventService identityEventService) { + + if (LOG.isDebugEnabled()) { + LOG.debug("IdentityEventService is set in the conditional authentication notification functions bundle"); + } + NotificationFunctionServiceHolder.getInstance().setIdentityEventService(identityEventService); + } + + protected void unsetIdentityEventService(IdentityEventService identityEventService) { + + if (LOG.isDebugEnabled()) { + LOG.debug("IdentityEventService is unset in the conditional authentication notification functions bundle"); + } + NotificationFunctionServiceHolder.getInstance().setIdentityEventService(null); + } +} diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/internal/NotificationFunctionServiceHolder.java b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/internal/NotificationFunctionServiceHolder.java new file mode 100644 index 00000000..37c21d43 --- /dev/null +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.notification/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/notification/internal/NotificationFunctionServiceHolder.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.conditional.auth.functions.notification.internal; + +import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry; +import org.wso2.carbon.identity.event.services.IdentityEventService; +import org.wso2.carbon.registry.core.service.RegistryService; +import org.wso2.carbon.user.core.service.RealmService; + +public class NotificationFunctionServiceHolder { + + private static NotificationFunctionServiceHolder instance = new NotificationFunctionServiceHolder(); + + private RealmService realmService; + private RegistryService registryService; + private JsFunctionRegistry jsFunctionRegistry; + private IdentityEventService identityEventService; + + public static NotificationFunctionServiceHolder getInstance() { + + return instance; + } + + private NotificationFunctionServiceHolder(){ + } + + public RealmService getRealmService() { + + return realmService; + } + + public void setRealmService(RealmService realmService) { + + this.realmService = realmService; + } + + public RegistryService getRegistryService() { + + return registryService; + } + + public void setRegistryService(RegistryService registryService) { + + this.registryService = registryService; + } + + public JsFunctionRegistry getJsFunctionRegistry() { + + return jsFunctionRegistry; + } + + public void setJsFunctionRegistry(JsFunctionRegistry jsFunctionRegistry) { + + this.jsFunctionRegistry = jsFunctionRegistry; + } + + public IdentityEventService getIdentityEventService() { + + return identityEventService; + } + + public void setIdentityEventService(IdentityEventService identityEventService) { + + this.identityEventService = identityEventService; + } +} diff --git a/components/org.wso2.carbon.identity.conditional.auth.functions.user/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/user/internal/UserFunctionsServiceComponent.java b/components/org.wso2.carbon.identity.conditional.auth.functions.user/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/user/internal/UserFunctionsServiceComponent.java index c9b0836e..e63df76c 100644 --- a/components/org.wso2.carbon.identity.conditional.auth.functions.user/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/user/internal/UserFunctionsServiceComponent.java +++ b/components/org.wso2.carbon.identity.conditional.auth.functions.user/src/main/java/org/wso2/carbon/identity/conditional/auth/functions/user/internal/UserFunctionsServiceComponent.java @@ -34,7 +34,8 @@ import org.wso2.carbon.user.core.service.RealmService; /** - * OSGi declarative services component which handled registration and unregistration of user conditional auth functions. + * OSGi declarative services component which handles registration and de-registration of user conditional auth + * functions. */ @Component( diff --git a/pom.xml b/pom.xml index e547d2ed..e20ea5eb 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ components/org.wso2.carbon.identity.conditional.auth.functions.user + components/org.wso2.carbon.identity.conditional.auth.functions.notification @@ -70,6 +71,11 @@ org.wso2.carbon.identity.application.authentication.framework ${carbon.identity.framework.version} + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.event + ${carbon.identity.framework.version} + @@ -246,7 +252,7 @@ 4.4.27 - 5.11.202 + 5.11.207 2.6.0.wso2v1 1.7.3