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.apache.log4j.Logger;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.beans.factory.annotation.Qualifier;
|
21
|
import org.springframework.dao.DataAccessException;
|
22
|
import org.springframework.mail.MailException;
|
23
|
import org.springframework.scheduling.annotation.Async;
|
24
|
import org.springframework.scheduling.annotation.AsyncResult;
|
25
|
import org.springframework.stereotype.Service;
|
26
|
import org.springframework.transaction.annotation.Transactional;
|
27
|
import org.springframework.util.concurrent.ListenableFuture;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.security.AbstractRequestToken;
|
30
|
import eu.etaxonomy.cdm.api.security.AccountCreationRequest;
|
31
|
import eu.etaxonomy.cdm.api.security.IAbstractRequestTokenStore;
|
32
|
import eu.etaxonomy.cdm.api.security.PasswordResetRequest;
|
33
|
import eu.etaxonomy.cdm.model.permission.User;
|
34
|
|
35
|
/**
|
36
|
* @author a.kohlbecker
|
37
|
* @since Oct 26, 2021
|
38
|
*/
|
39
|
@Service
|
40
|
@Transactional(readOnly = true)
|
41
|
public class AccountRegistrationService extends AccountSelfManagementService implements IAccountRegistrationService {
|
42
|
|
43
|
private static Logger logger = Logger.getLogger(PasswordResetRequest.class);
|
44
|
|
45
|
@Autowired
|
46
|
@Qualifier("accountCreationRequestTokenStore")
|
47
|
private IAbstractRequestTokenStore<AccountCreationRequest> accountRegistrationTokenStore;
|
48
|
|
49
|
@Override
|
50
|
@Async
|
51
|
public ListenableFuture<Boolean> emailAccountRegistrationRequest(String emailAddress,
|
52
|
String userName, String password, String accountCreationRequestFormUrlTemplate) throws MailException, AddressException {
|
53
|
|
54
|
if(logger.isTraceEnabled()) {
|
55
|
logger.trace("emailAccountRegistrationConfirmation() trying to aquire from rate limiter [rate: " + emailResetToken_rateLimiter.getRate() + ", timeout: " + getRateLimiterTimeout().toMillis() + "ms]");
|
56
|
}
|
57
|
if (emailResetToken_rateLimiter.tryAcquire(getRateLimiterTimeout())) {
|
58
|
logger.trace("emailAccountRegistrationConfirmation() allowed by rate limiter");
|
59
|
try {
|
60
|
emailAddressValidAndUnused(emailAddress);
|
61
|
User user = User.NewInstance(userName, password);
|
62
|
user.setEmailAddress(emailAddress);
|
63
|
AbstractRequestToken resetRequest = accountRegistrationTokenStore.create(user);
|
64
|
String passwordRequestFormUrl = String.format(accountCreationRequestFormUrlTemplate, resetRequest.getToken());
|
65
|
Map<String, String> additionalValues = new HashMap<>();
|
66
|
additionalValues.put("linkUrl", passwordRequestFormUrl);
|
67
|
sendEmail(user.getEmailAddress(), user.getUsername(),
|
68
|
UserAccountEmailTemplates.REGISTRATION_REQUEST_EMAIL_SUBJECT_TEMPLATE,
|
69
|
UserAccountEmailTemplates.REGISTRATION_REQUEST_EMAIL_BODY_TEMPLATE, additionalValues);
|
70
|
logger.info("An account creartion request has been send to "
|
71
|
+ user.getEmailAddress());
|
72
|
return new AsyncResult<Boolean>(true);
|
73
|
} catch (MailException e) {
|
74
|
throw e;
|
75
|
}
|
76
|
} else {
|
77
|
logger.trace("blocked by rate limiter");
|
78
|
return new AsyncResult<Boolean>(false);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
@Async
|
84
|
@Transactional(readOnly = false)
|
85
|
public ListenableFuture<Boolean> createUserAccount(String token, String givenName, String familyName, String prefix)
|
86
|
throws MailException, AccountSelfManagementException, AddressException {
|
87
|
|
88
|
if (resetPassword_rateLimiter.tryAcquire(getRateLimiterTimeout())) {
|
89
|
|
90
|
Optional<AccountCreationRequest> creationRequest = accountRegistrationTokenStore.findResetRequest(token);
|
91
|
if (creationRequest.isPresent()) {
|
92
|
try {
|
93
|
// check again if the email address is still unused
|
94
|
emailAddressValidAndUnused(creationRequest.get().getUserEmail());
|
95
|
User newUser = User.NewInstance(creationRequest.get().getUserName(), creationRequest.get().getEncryptedPassword());
|
96
|
userDao.saveOrUpdate(newUser);
|
97
|
accountRegistrationTokenStore.remove(token);
|
98
|
sendEmail(creationRequest.get().getUserEmail(), creationRequest.get().getUserName(),
|
99
|
UserAccountEmailTemplates.REGISTRATION_SUCCESS_EMAIL_SUBJECT_TEMPLATE,
|
100
|
UserAccountEmailTemplates.REGISTRATION_SUCCESS_EMAIL_BODY_TEMPLATE, null);
|
101
|
return new AsyncResult<Boolean>(true);
|
102
|
} catch (DataAccessException e) {
|
103
|
String message = "Failed to create a new user [userName: " + creationRequest.get().getUserName() + ", email: " + creationRequest.get().getUserEmail() + "]";
|
104
|
logger.error(message, e);
|
105
|
throw new AccountSelfManagementException(message);
|
106
|
}
|
107
|
} else {
|
108
|
throw new AccountSelfManagementException("Invalid account creation token");
|
109
|
}
|
110
|
}
|
111
|
return new AsyncResult<Boolean>(false);
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Throws exceptions in case of any problems, returns silently in case
|
116
|
* everything is OK.
|
117
|
*
|
118
|
* @param userNameOrEmail
|
119
|
* @throws AddressException
|
120
|
* in case the <code>emailAddress</code> is invalid
|
121
|
* @throws EmailAddressAlreadyInUseException
|
122
|
* in case the <code>emailAddress</code> is in use
|
123
|
*/
|
124
|
protected void emailAddressValidAndUnused(String emailAddress)
|
125
|
throws AddressException, EmailAddressAlreadyInUseException {
|
126
|
InternetAddress emailAddr = new InternetAddress(emailAddress);
|
127
|
emailAddr.validate();
|
128
|
if (userDao.findByEmailAddress(emailAddr.toString()) != null) {
|
129
|
throw new EmailAddressAlreadyInUseException("Email address is already in use");
|
130
|
}
|
131
|
}
|
132
|
|
133
|
}
|