8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: EmailPassword.java,v 1.2 2008/06/25 05:43:41 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
92813213f3a80b0a65195e59993b49b66d160f69Peter Major/**
92813213f3a80b0a65195e59993b49b66d160f69Peter Major * Portions Copyrighted 2013 ForgeRock, Inc.
92813213f3a80b0a65195e59993b49b66d160f69Peter Major */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.password.plugins;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.AMSendMail;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.cdm.G11NSettings;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.idm.AMIdentity;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.idm.IdRepoException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.password.ui.model.PWResetException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.password.ui.model.PWResetModel;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.password.ui.model.PWResetModelImpl;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.password.ui.model.PWResetResBundleCacher;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.Constants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.text.MessageFormat;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Locale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.ResourceBundle;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.mail.SendFailedException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.mail.MessagingException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>EmailPassword</code> defines a set of methods
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * that are required to notify a user when a password is changed.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class EmailPassword implements NotifyPassword {
92813213f3a80b0a65195e59993b49b66d160f69Peter Major
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private PWResetModel model = new PWResetModelImpl();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static private G11NSettings g11nSettings = G11NSettings.getInstance();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static private String bundleName = PWResetModel.DEFAULT_RB;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Locale userLocale = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Notifies user when password is changed.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param user <code>AMIdentity</code> object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param password new password
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param locale user locale
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws PWResetException if password cannot be notified
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
92813213f3a80b0a65195e59993b49b66d160f69Peter Major public void notifyPassword(AMIdentity user, String password, Locale locale) throws PWResetException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceBundle rb = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
92813213f3a80b0a65195e59993b49b66d160f69Peter Major Set<String> set = user.getAttribute(model.getMailAttribute(user.getRealm()));
92813213f3a80b0a65195e59993b49b66d160f69Peter Major Set<String> localeSet = user.getAttribute(Constants.USER_LOCALE_ATTR);
92813213f3a80b0a65195e59993b49b66d160f69Peter Major
92813213f3a80b0a65195e59993b49b66d160f69Peter Major if (localeSet == null || localeSet.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster userLocale = locale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
92813213f3a80b0a65195e59993b49b66d160f69Peter Major String localeStr = localeSet.iterator().next();
92813213f3a80b0a65195e59993b49b66d160f69Peter Major userLocale = com.sun.identity.shared.locale.Locale.getLocale(localeStr);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
92813213f3a80b0a65195e59993b49b66d160f69Peter Major
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rb = PWResetResBundleCacher.getBundle(bundleName, userLocale);
92813213f3a80b0a65195e59993b49b66d160f69Peter Major
92813213f3a80b0a65195e59993b49b66d160f69Peter Major if (set == null || set.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster model.debugWarning("There is no email address for this user.");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PWResetException(rb.getString("noEmail.message"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
92813213f3a80b0a65195e59993b49b66d160f69Peter Major String emailAddress = set.iterator().next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sendEmailToUser(emailAddress, password);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster model.debugWarning("EmailPassword.notifyPassword", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PWResetException(e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (IdRepoException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster model.debugWarning("EmailPassword.notifyPassword", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PWResetException(e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SendFailedException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster model.debugWarning("EmailPassword.notifyPassword", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PWResetException(rb.getString("sendEmailFailed.message"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (MessagingException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster model.debugWarning ("EmailPassword.notifyPassword", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PWResetException(e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Sents reset password email to the user.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param emailAddres email address of the user
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param password new password
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void sendEmailToUser(String emailAddress, String password)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws MessagingException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String obj[] = new String[1];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster obj[0] = password;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceBundle rb = PWResetResBundleCacher.getBundle(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bundleName,userLocale);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = MessageFormat.format(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rb.getString("resetPassMail.message"), (Object[])obj);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String subject = rb.getString("resetSubject.message");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String to[] = new String[1];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster to[0] = emailAddress;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String from = rb.getString("fromAddress.label");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String charset = g11nSettings.getDefaultCharsetForLocale(userLocale);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AMSendMail sendMail = new AMSendMail();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sendMail.postMail(to, subject, msg, from,charset);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}