Skip to content

Commit dc4bcb1

Browse files
committed
Refactor reset domain link formatting
1 parent cf9ff10 commit dc4bcb1

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

server/src/main/java/org/apache/cloudstack/user/UserPasswordResetManagerImpl.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.cloudstack.utils.mailing.MailAddress;
3939
import org.apache.cloudstack.utils.mailing.SMTPMailProperties;
4040
import org.apache.cloudstack.utils.mailing.SMTPMailSender;
41+
import org.apache.http.conn.util.InetAddressUtils;
4142

4243
import javax.inject.Inject;
4344
import javax.naming.ConfigurationException;
@@ -191,8 +192,9 @@ public void setResetTokenAndSend(UserAccount userAccount) {
191192

192193
String requestDomain = CallContext.current().getRequestRemoteAddress();
193194
String resetLinkDomain = getResetLinkDomain(requestDomain);
195+
String formattedResetLinkDomain = formatResetLinkDomain(resetLinkDomain);
194196
String resetLink = String.format("%s/client/#/user/resetPassword?username=%s&token=%s",
195-
resetLinkDomain, username, resetToken);
197+
formattedResetLinkDomain, username, resetToken);
196198
String content = getMessageBody(userAccount, resetToken, resetLink);
197199

198200
SMTPMailProperties mailProperties = new SMTPMailProperties();
@@ -214,46 +216,41 @@ public void setResetTokenAndSend(UserAccount userAccount) {
214216
}
215217

216218
private String getResetLinkDomain(String requestDomain) {
217-
String domain = "";
218-
219219
if (StringUtils.isNotBlank(requestDomain)) {
220220
logger.debug("Searching for GUI theme with common name that matches the request's domain: [{}]", requestDomain);
221221
List<Long> commonNameDetails = guiThemeDetailsDao.listGuiThemeIdsByCommonName(requestDomain);
222222

223223
if (!commonNameDetails.isEmpty()) {
224224
logger.debug("GUI theme with ID {} was found; using request's domain for password reset link.", commonNameDetails.get(0));
225-
domain = requestDomain;
225+
return requestDomain;
226226
} else {
227227
logger.debug("No GUI theme was found with a common name that matches the request's domain.");
228228
}
229229
}
230230

231231
String configurationDomain = UserPasswordResetDomainURL.value();
232-
if (StringUtils.isBlank(domain) && !StringUtils.isBlank(configurationDomain)) {
232+
if (StringUtils.isNotBlank(configurationDomain)) {
233233
logger.debug("Defaulting reset link's domain to the [{}] configuration value: [{}].", UserPasswordResetDomainURL.key(), UserPasswordResetDomainURL.value());
234-
domain = configurationDomain;
234+
return configurationDomain;
235235
}
236236

237-
if (StringUtils.isBlank(domain)) {
238-
logger.debug("Using the first IP address in the [{}] configuration for the reset password email domain because the [{}] configuration is not defined.", ManagementServerAddresses.key(), UserPasswordResetDomainURL.key());
239-
domain = ManagementServerAddresses.value().split(",")[0];
237+
logger.debug("Using the first IP address in the [{}] configuration for the reset password email domain because the [{}] configuration is not defined.", ManagementServerAddresses.key(), UserPasswordResetDomainURL.key());
238+
return ManagementServerAddresses.value().split(",")[0];
239+
}
240240

241-
if (ServerProperties.isHttpsEnabled()) {
242-
domain = "https://" + domain + ":" + ServerProperties.getHttpsPort();
243-
} else {
244-
domain = "http://" + domain + ":" + ServerProperties.getHttpPort();
245-
}
241+
private String formatResetLinkDomain(String resetLinkDomain) {
242+
String protocol = ServerProperties.isHttpsEnabled() ? "https" : "http";
243+
244+
if (InetAddressUtils.isIPv4Address(resetLinkDomain)) {
245+
int port = protocol.equals("https") ? ServerProperties.getHttpsPort() : ServerProperties.getHttpPort();
246+
resetLinkDomain = resetLinkDomain + ":" + port;
246247
}
247248

248-
if (!domain.startsWith("http")) {
249-
if (ServerProperties.isHttpsEnabled()) {
250-
domain = "https://" + domain;
251-
} else {
252-
domain = "http://" + domain;
253-
}
249+
if (!resetLinkDomain.startsWith("http")) {
250+
resetLinkDomain = protocol + "://" + resetLinkDomain;
254251
}
255252

256-
return domain.replaceAll("/+$", "");
253+
return resetLinkDomain.replaceAll("/+$", "");
257254
}
258255

259256
@Override

0 commit comments

Comments
 (0)