Skip to content

Commit 2f0d219

Browse files
committed
Add default recovery portal URL
1 parent 7550a75 commit 2f0d219

3 files changed

Lines changed: 52 additions & 20 deletions

File tree

components/org.wso2.carbon.identity.branding.preference.management.core/src/main/java/org/wso2/carbon/identity/branding/preference/management/core/listener/PortalURLResolver.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.wso2.carbon.identity.branding.preference.management.core.exception.BrandingPreferenceMgtClientException;
2626
import org.wso2.carbon.identity.branding.preference.management.core.exception.BrandingPreferenceMgtException;
2727
import org.wso2.carbon.identity.branding.preference.management.core.model.BrandingPreference;
28-
import org.wso2.carbon.identity.core.ServiceURLBuilder;
2928
import org.wso2.carbon.identity.core.URLBuilderException;
3029
import org.wso2.carbon.identity.core.context.model.Flow;
3130
import org.wso2.carbon.identity.flow.execution.engine.listener.AbstractFlowExecutionListener;
@@ -37,6 +36,9 @@
3736
import static org.wso2.carbon.identity.branding.preference.management.core.constant.BrandingPreferenceMgtConstants.BRANDING_URLS;
3837
import static org.wso2.carbon.identity.branding.preference.management.core.constant.BrandingPreferenceMgtConstants.DEFAULT_LOCALE;
3938
import static org.wso2.carbon.identity.branding.preference.management.core.constant.BrandingPreferenceMgtConstants.ORGANIZATION_TYPE;
39+
import static org.wso2.carbon.identity.branding.preference.management.core.util.BrandingPreferenceMgtUtils.DEFAULT_REGISTRATION_PORTAL_URL;
40+
import static org.wso2.carbon.identity.branding.preference.management.core.util.BrandingPreferenceMgtUtils.REGISTRATION;
41+
import static org.wso2.carbon.identity.branding.preference.management.core.util.BrandingPreferenceMgtUtils.buildDefaultPortalUrl;
4042

4143
/**
4244
* This class is responsible for injecting the portal URL during flow execution.
@@ -46,8 +48,6 @@ public class PortalURLResolver extends AbstractFlowExecutionListener {
4648
private static final Log LOG = LogFactory.getLog(PortalURLResolver.class);
4749
private final BrandingPreferenceManagerImpl brandingPreferenceManager;
4850
public static final String SELF_SIGN_UP_URL = "selfSignUpURL";
49-
public static final String DEFAULT_REGISTRATION_PORTAL_URL = "/authenticationendpoint/register.do";
50-
public static final String REGISTRATION = "REGISTRATION";
5151

5252
public PortalURLResolver(BrandingPreferenceManagerImpl brandingPreferenceManager) {
5353

@@ -75,10 +75,11 @@ public boolean isEnabled() {
7575
@Override
7676
public boolean doPreExecute(FlowExecutionContext context) {
7777

78+
String flowType = context.getFlowType();
79+
if (StringUtils.isNotBlank(context.getPortalUrl())) {
80+
return true;
81+
}
7882
try {
79-
if (StringUtils.isNotBlank(context.getPortalUrl())) {
80-
return true;
81-
}
8283
String applicationId = context.getApplicationId();
8384
String tenantDomain = context.getTenantDomain();
8485
String type = StringUtils.isBlank(applicationId) ? ORGANIZATION_TYPE : APPLICATION_TYPE;
@@ -90,27 +91,26 @@ public boolean doPreExecute(FlowExecutionContext context) {
9091
Map<String, Object> prefMap = (Map<String, Object>) preference.getPreference();
9192
Map<String, String> urlMap = (Map<String, String>) prefMap.get(BRANDING_URLS);
9293

93-
if (REGISTRATION.equals(context.getFlowType()) || Flow.Name.USER_REGISTRATION.toString().
94-
equalsIgnoreCase(context.getFlowType())) {
94+
if (REGISTRATION.equalsIgnoreCase(flowType) || Flow.Name.USER_REGISTRATION.name().
95+
equalsIgnoreCase(flowType)) {
9596
String signUpUrl = (urlMap != null) ? urlMap.get(SELF_SIGN_UP_URL) : null;
96-
9797
if (StringUtils.isNotBlank(signUpUrl)) {
9898
context.setPortalUrl(signUpUrl);
9999
} else {
100100
logMissingSelfSignupUrl(context);
101-
context.setPortalUrl(buildDefaultRegistrationUrl());
101+
context.setPortalUrl(buildDefaultPortalUrl(context.getApplicationId(), flowType));
102102
}
103103
}
104104
}
105105
if (StringUtils.isBlank(context.getPortalUrl())) {
106106
logMissingSelfSignupUrl(context);
107-
context.setPortalUrl(buildDefaultRegistrationUrl());
107+
context.setPortalUrl(buildDefaultPortalUrl(context.getApplicationId(), flowType));
108108
}
109109
return true;
110110
} catch (BrandingPreferenceMgtClientException e) {
111111
logMissingSelfSignupUrl(context);
112112
try {
113-
context.setPortalUrl(buildDefaultRegistrationUrl());
113+
context.setPortalUrl(buildDefaultPortalUrl(context.getApplicationId(), flowType));
114114
} catch (URLBuilderException ex) {
115115
LOG.error("Failed to build default registration URL for tenant: " + context.getTenantDomain(), ex);
116116
return false;
@@ -130,12 +130,4 @@ private static void logMissingSelfSignupUrl(FlowExecutionContext context) {
130130
LOG.debug("Self sign-up URL not configured for tenant: " + context.getTenantDomain() + ". Using default URL: "
131131
+ DEFAULT_REGISTRATION_PORTAL_URL);
132132
}
133-
134-
private String buildDefaultRegistrationUrl() throws URLBuilderException {
135-
136-
return ServiceURLBuilder.create()
137-
.addPath(DEFAULT_REGISTRATION_PORTAL_URL)
138-
.build()
139-
.getAbsolutePublicURL();
140-
}
141133
}

components/org.wso2.carbon.identity.branding.preference.management.core/src/main/java/org/wso2/carbon/identity/branding/preference/management/core/util/BrandingPreferenceMgtUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import org.wso2.carbon.identity.branding.preference.management.core.exception.BrandingPreferenceMgtServerException;
3232
import org.wso2.carbon.identity.branding.preference.management.core.model.BrandingPreference;
3333
import org.wso2.carbon.identity.branding.preference.management.core.model.CustomLayoutContent;
34+
import org.wso2.carbon.identity.core.ServiceURLBuilder;
35+
import org.wso2.carbon.identity.core.URLBuilderException;
36+
import org.wso2.carbon.identity.core.context.model.Flow;
3437
import org.wso2.carbon.identity.core.util.IdentityUtil;
3538

3639
import java.nio.charset.StandardCharsets;
@@ -62,6 +65,9 @@
6265
public class BrandingPreferenceMgtUtils {
6366

6467
private static final Log log = LogFactory.getLog(BrandingPreferenceMgtUtils.class);
68+
public static final String DEFAULT_REGISTRATION_PORTAL_URL = "/authenticationendpoint/register.do";
69+
public static final String DEFAULT_RECOVERY_PORTAL_URL = "/authenticationendpoint/recovery.do";
70+
public static final String REGISTRATION = "REGISTRATION";
6571

6672
/**
6773
* Check whether the given string is a valid preference object or not.
@@ -401,4 +407,18 @@ public static void handleBrandingMgtException(Throwable t) throws BrandingPrefer
401407
throw (BrandingPreferenceMgtException) t;
402408
}
403409
}
410+
411+
public static String buildDefaultPortalUrl(String applicationId, String flowType) throws URLBuilderException {
412+
413+
ServiceURLBuilder builder;
414+
if (Flow.Name.USER_REGISTRATION.name().equalsIgnoreCase(flowType) || REGISTRATION.equalsIgnoreCase(flowType)) {
415+
builder = ServiceURLBuilder.create().addPath(DEFAULT_REGISTRATION_PORTAL_URL);
416+
if (StringUtils.isNotBlank(applicationId)) {
417+
builder.addParameter("applicationId", applicationId);
418+
}
419+
} else {
420+
builder = ServiceURLBuilder.create().addPath(DEFAULT_RECOVERY_PORTAL_URL);
421+
}
422+
return builder.build().getAbsolutePublicURL();
423+
}
404424
}

components/org.wso2.carbon.identity.branding.preference.management.core/src/test/java/org/wso2/carbon/identity/branding/preference/management/core/listener/PortalURLResolverTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.wso2.carbon.identity.core.ServiceURL;
3131
import org.wso2.carbon.identity.core.ServiceURLBuilder;
3232
import org.wso2.carbon.identity.core.URLBuilderException;
33+
import org.wso2.carbon.identity.core.context.model.Flow;
3334
import org.wso2.carbon.identity.flow.execution.engine.model.FlowExecutionContext;
3435

3536
import java.util.HashMap;
@@ -127,6 +128,25 @@ public void fallBackToDefaultUrl() throws Exception {
127128
}
128129
}
129130

131+
@Test
132+
public void fallBackToDefaultRecoveryUrl() throws Exception {
133+
134+
when(flowContext.getPortalUrl()).thenReturn(null);
135+
when(flowContext.getApplicationId()).thenReturn(null);
136+
when(flowContext.getTenantDomain()).thenReturn("wso2.com");
137+
when(flowContext.getFlowType()).thenReturn(Flow.Name.INVITED_USER_REGISTRATION.name());
138+
139+
when(brandingPreferenceManager.getBrandingPreference(ORGANIZATION_TYPE, "wso2.com", DEFAULT_LOCALE))
140+
.thenReturn(null);
141+
142+
try (MockedStatic<ServiceURLBuilder> serviceURLBuilder = mockStatic(ServiceURLBuilder.class)) {
143+
mockServiceURLBuilder();
144+
boolean result = resolver.doPreExecute(flowContext);
145+
assertTrue(result);
146+
verify(flowContext).setPortalUrl("https://default.url");
147+
}
148+
}
149+
130150
@Test
131151
public void prefWithoutSelfSignupUrl() throws Exception {
132152

0 commit comments

Comments
 (0)