Project

General

Profile

Download (8.19 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2021 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.api.service.security;
10

    
11
import java.util.HashMap;
12
import java.util.Map;
13
import java.util.Optional;
14

    
15
import javax.mail.internet.AddressException;
16
import javax.mail.internet.InternetAddress;
17

    
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Qualifier;
20
import org.springframework.dao.DataAccessException;
21
import org.springframework.mail.MailException;
22
import org.springframework.scheduling.annotation.Async;
23
import org.springframework.scheduling.annotation.AsyncResult;
24
import org.springframework.security.core.userdetails.UserDetails;
25
import org.springframework.security.core.userdetails.UsernameNotFoundException;
26
import org.springframework.stereotype.Service;
27
import org.springframework.transaction.annotation.Transactional;
28
import org.springframework.util.Assert;
29
import org.springframework.util.concurrent.ListenableFuture;
30

    
31
import eu.etaxonomy.cdm.api.security.AbstractRequestToken;
32
import eu.etaxonomy.cdm.api.security.IAbstractRequestTokenStore;
33
import eu.etaxonomy.cdm.api.security.PasswordResetRequest;
34
import eu.etaxonomy.cdm.model.permission.User;
35

    
36
/**
37
 * @author a.kohlbecker
38
 * @since Oct 26, 2021
39
 */
40
@Service
41
@Transactional(readOnly = false)
42
public class PasswordResetService extends AccountSelfManagementService implements IPasswordResetService {
43

    
44
    @Autowired
45
    @Qualifier("passwordResetTokenStore")
46
    IAbstractRequestTokenStore<PasswordResetRequest> passwordResetTokenStore;
47

    
48
    /**
49
     * Create a request token and send it to the user via email.
50
     *
51
     * Must conform to the recommendations of <a href=
52
     * "https://cheatsheetseries.owasp.org/cheatsheets/Forgot_Password_Cheat_Sheet.html">
53
     * https://cheatsheetseries.owasp.org/cheatsheets/Forgot_Password_Cheat_Sheet.html</a>
54
     *
55
     * <ul>
56
     * <li>Hides internal processing time differences by sending the email
57
     * asynchronously</li>
58
     * <li>Access to the method is rate limited, see {@link #RATE_LIMIT}</li>
59
     * </ul>
60
     *
61
     * @param userNameOrEmail
62
     *            The user name or email address of the user requesting for a
63
     *            password reset.
64
     * @param passwordRequestFormUrlTemplate
65
     *            A template string for {@code String.format()} for the URL to
66
     *            the request form in which the user can enter the new password.
67
     *            The template string must contain one string placeholder
68
     *            {@code %s} for the request token string.
69
     * @return A <code>Future</code> for a <code>Boolean</code> flag. The
70
     *         boolean value will be <code>false</code> in case the max access
71
     *         rate for this method has been exceeded and a time out has
72
     *         occurred. Internal error states that may
73
     *         expose sensitive information are intentionally hidden this way
74
     *         (see above link to the Forgot_Password_Cheat_Sheet).
75
     * @throws MailException
76
     *             in case sending the email has failed
77
     */
78
    @Override
79
    @Async
80
    public ListenableFuture<Boolean> emailResetToken(String userNameOrEmail, String passwordRequestFormUrlTemplate) throws MailException {
81

    
82
        if(logger.isTraceEnabled()) {
83
            logger.trace("emailResetToken trying to aquire from rate limiter [rate: " + emailResetToken_rateLimiter.getRate() + ", timeout: " + getRateLimiterTimeout().toMillis() + "ms]");
84
        }
85
        if (emailResetToken_rateLimiter.tryAcquire(getRateLimiterTimeout())) {
86
            logger.trace("emailResetToken allowed by rate limiter");
87
            try {
88
                User user = findUser(userNameOrEmail);
89
                AbstractRequestToken resetRequest = passwordResetTokenStore.create(user);
90
                String passwordRequestFormUrl = String.format(passwordRequestFormUrlTemplate, resetRequest.getToken());
91
                Map<String, String> additionalValues = new HashMap<>();
92
                additionalValues.put("linkUrl", passwordRequestFormUrl);
93
                sendEmail(user.getEmailAddress(), user.getUsername(),
94
                        UserAccountEmailTemplates.RESET_REQUEST_EMAIL_SUBJECT_TEMPLATE,
95
                        UserAccountEmailTemplates.REGISTRATION_REQUEST_EMAIL_BODY_TEMPLATE, additionalValues);
96
                logger.info("A password reset request for  " + user.getUsername() + " has been send to "
97
                        + user.getEmailAddress());
98
            } catch (UsernameNotFoundException e) {
99
                logger.warn("Password reset request for unknown user, cause: " + e.getMessage());
100
            } catch (MailException e) {
101
                throw e;
102
            }
103
            return new AsyncResult<Boolean>(true);
104
        } else {
105
            logger.trace("blocked by rate limiter");
106
            return new AsyncResult<Boolean>(false);
107
        }
108
    }
109

    
110
    /**
111
    *
112
    * @param token
113
    *            the token string
114
    * @param newPassword
115
    *            The new password to set
116
    * @return A <code>Future</code> for a <code>Boolean</code> flag. The
117
    *         boolean value will be <code>false</code> in case the max access
118
    *         rate for this method has been exceeded and a time out has
119
    *         occurred.
120
    * @throws AccountSelfManagementException
121
    *             in case an invalid token has been used
122
    * @throws MailException
123
    *             in case sending the email has failed
124
    */
125
   @Override
126
   @Async
127
   public ListenableFuture<Boolean> resetPassword(String token, String newPassword) throws AccountSelfManagementException, MailException {
128

    
129
       if (resetPassword_rateLimiter.tryAcquire(getRateLimiterTimeout())) {
130

    
131
           Optional<PasswordResetRequest> resetRequest = passwordResetTokenStore.findResetRequest(token);
132
           if (resetRequest.isPresent()) {
133
               try {
134
                   UserDetails user = userService.loadUserByUsername(resetRequest.get().getUserName());
135
                   Assert.isAssignable(user.getClass(), User.class);
136
                   userService.encodeUserPassword((User)user, newPassword);
137
                   userDao.saveOrUpdate((User)user);
138
                   passwordResetTokenStore.remove(token);
139
                   sendEmail(resetRequest.get().getUserEmail(), resetRequest.get().getUserName(),
140
                           UserAccountEmailTemplates.RESET_SUCCESS_EMAIL_SUBJECT_TEMPLATE,
141
                           UserAccountEmailTemplates.RESET_SUCCESS_EMAIL_BODY_TEMPLATE, null);
142
                   return new AsyncResult<Boolean>(true);
143
               } catch (DataAccessException | IllegalArgumentException | UsernameNotFoundException e) {
144
                   logger.error("Failed to change password of User " + resetRequest.get().getUserName(), e);
145
                   sendEmail(resetRequest.get().getUserEmail(), resetRequest.get().getUserName(),
146
                           UserAccountEmailTemplates.RESET_FAILED_EMAIL_SUBJECT_TEMPLATE,
147
                           UserAccountEmailTemplates.RESET_FAILED_EMAIL_BODY_TEMPLATE, null);
148
               }
149
           } else {
150
               throw new AccountSelfManagementException("Invalid password reset token");
151
           }
152
       }
153
       return new AsyncResult<Boolean>(false);
154
   }
155

    
156
    /**
157
     *
158
     * @param userNameOrEmail
159
     * @return
160
     */
161
    protected User findUser(String userNameOrEmail) throws UsernameNotFoundException, EmailAddressNotFoundException {
162

    
163
        User user;
164
        try {
165
            InternetAddress emailAddr = new InternetAddress(userNameOrEmail);
166
            emailAddr.validate();
167
            user = userDao.findByEmailAddress(userNameOrEmail);
168
            if (user == null) {
169
                throw new EmailAddressNotFoundException(
170
                        "No user with the email address'" + userNameOrEmail + "' found.");
171
            }
172
        } catch (AddressException ex) {
173
            user = userDao.findUserByUsername(userNameOrEmail);
174
            if (user == null) {
175
                throw new UsernameNotFoundException("No user with the user name: '" + userNameOrEmail + "' found.");
176
            }
177
        }
178
        return user;
179
    }
180
}
(9-9/10)