Skip to content

Commit

Permalink
Bootstrap and zonify login.signup and login.passwd
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanik committed Feb 9, 2016
1 parent b65f278 commit 1a482b7
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 79 deletions.
Expand Up @@ -12,13 +12,17 @@
*******************************************************************************/
package org.cloudfoundry.identity.uaa.zone;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;

import javax.validation.constraints.NotNull;
import java.util.Calendar;
import java.util.Date;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class IdentityZone {
public static final IdentityZone getUaa() {
Calendar calendar = Calendar.getInstance();
Expand All @@ -40,7 +44,7 @@ public static final IdentityZone getUaa() {
@NotNull
private String subdomain;

private IdentityZoneConfiguration config;
private IdentityZoneConfiguration config = new IdentityZoneConfiguration();


@NotNull
Expand Down
Expand Up @@ -18,12 +18,17 @@
import org.cloudfoundry.identity.uaa.zone.TokenPolicy;
import org.springframework.beans.factory.InitializingBean;

import java.util.Map;

import static org.springframework.util.StringUtils.hasText;

public class IdentityZoneConfigurationBootstrap implements InitializingBean {

private TokenPolicy tokenPolicy;
private IdentityZoneProvisioning provisioning;
private boolean selfServiceLinksEnabled = true;
private String homeRedirect = null;
private Map<String,String> selfServiceLinks;


public IdentityZoneConfigurationBootstrap(IdentityZoneProvisioning provisioning) {
Expand All @@ -36,10 +41,22 @@ public void afterPropertiesSet() {
IdentityZoneConfiguration definition = new IdentityZoneConfiguration(tokenPolicy);
definition.getLinks().getService().setSelfServiceLinksEnabled(selfServiceLinksEnabled);
definition.getLinks().setHomeRedirect(homeRedirect);
if (selfServiceLinks!=null) {
String signup = selfServiceLinks.get("signup");
String passwd = selfServiceLinks.get("passwd");
if (hasText(signup)) {
definition.getLinks().getService().setSignup(signup);
}
if (hasText(passwd)) {
definition.getLinks().getService().setPasswd(passwd);
}
}
identityZone.setConfig(definition);
provisioning.update(identityZone);
}



public void setTokenPolicy(TokenPolicy tokenPolicy) {
this.tokenPolicy = tokenPolicy;
}
Expand All @@ -55,4 +72,8 @@ public void setHomeRedirect(String homeRedirect) {
this.homeRedirect = homeRedirect;
}
}

public void setSelfServiceLinks(Map<String, String> links) {
this.selfServiceLinks = links;
}
}
Expand Up @@ -108,8 +108,6 @@ public class LoginInfoEndpoint {

private Properties buildProperties = new Properties();

private Map<String, String> links = new HashMap<>();

private String baseUrl;

private String externalLoginUrl;
Expand Down Expand Up @@ -493,6 +491,8 @@ public String generatePasscode(Map<String, Object> model, Principal principal)
IdentityProvider<UaaIdentityProviderDefinition> uaaIdp = providerProvisioning.retrieveByOrigin(OriginKeys.UAA, IdentityZoneHolder.get().getId());
boolean disableInternalUserManagement = (uaaIdp.getConfig()!=null) ? uaaIdp.getConfig().isDisableInternalUserManagement() : false;
boolean selfServiceLinksEnabled = (zone.getConfig()!=null) ? zone.getConfig().getLinks().getService().isSelfServiceLinksEnabled() : true;
String signup = zone.getConfig()!=null ? zone.getConfig().getLinks().getService().getSignup() : null;
String passwd = zone.getConfig()!=null ? zone.getConfig().getLinks().getService().getPasswd() : null;
Map<String, Object> model = new HashMap<>();
model.put(OriginKeys.UAA, addSubdomainToUrl(getUaaBaseUrl()));
if (getBaseUrl().contains("localhost:")) {
Expand All @@ -508,13 +508,13 @@ public String generatePasscode(Map<String, Object> model, Principal principal)
model.put(FORGOT_PASSWORD_LINK, "/forgot_password");
model.put("passwd", "/forgot_password");
if(IdentityZoneHolder.isUaa()) {
if (hasText(links.get("signup"))) {
model.put(CREATE_ACCOUNT_LINK, links.get("signup"));
model.put("register", getLinks().get("signup"));
if (hasText(signup)) {
model.put(CREATE_ACCOUNT_LINK, signup);
model.put("register", signup);
}
if (hasText(links.get("passwd"))) {
model.put(FORGOT_PASSWORD_LINK, links.get("passwd"));
model.put("passwd", links.get("passwd"));
if (hasText(passwd)) {
model.put(FORGOT_PASSWORD_LINK, passwd);
model.put("passwd", passwd);
}
}
}
Expand All @@ -535,14 +535,6 @@ public void setUaaBaseUrl(String baseUrl) {
}
}

public Map<String, String> getLinks() {
return links;
}

public void setLinks(Map<String, String> links) {
this.links = links;
}

public String getBaseUrl() {
return baseUrl;
}
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.cloudfoundry.identity.uaa.zone.JdbcIdentityZoneProvisioning;
import org.cloudfoundry.identity.uaa.zone.KeyPair;
import org.cloudfoundry.identity.uaa.zone.TokenPolicy;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
Expand Down Expand Up @@ -57,13 +58,18 @@ public class IdentityZoneConfigurationBootstrapTests extends JdbcTestBase {
public static final String PASSWORD = "password";

public static final String ID = "id";


private IdentityZoneProvisioning provisioning;
private IdentityZoneConfigurationBootstrap bootstrap;
private Map<String, String> links = new HashMap<>();;

@Before
public void configureProvisioning() {
provisioning = new JdbcIdentityZoneProvisioning(jdbcTemplate);
bootstrap = new IdentityZoneConfigurationBootstrap(provisioning);
}

@Test
public void tokenPolicy_configured_fromValuesInYaml() throws Exception {
IdentityZoneProvisioning provisioning = new JdbcIdentityZoneProvisioning(jdbcTemplate);
IdentityZoneConfigurationBootstrap bootstrap = new IdentityZoneConfigurationBootstrap(provisioning);
TokenPolicy tokenPolicy = new TokenPolicy();
KeyPair key = new KeyPair(PRIVATE_KEY, PUBLIC_KEY, PASSWORD);
Map<String,KeyPair> keys = new HashMap<>();
Expand All @@ -83,8 +89,6 @@ public void tokenPolicy_configured_fromValuesInYaml() throws Exception {

@Test
public void disable_self_service_links() throws Exception {
IdentityZoneProvisioning provisioning = new JdbcIdentityZoneProvisioning(jdbcTemplate);
IdentityZoneConfigurationBootstrap bootstrap = new IdentityZoneConfigurationBootstrap(provisioning);
bootstrap.setSelfServiceLinksEnabled(false);
bootstrap.afterPropertiesSet();

Expand All @@ -94,8 +98,6 @@ public void disable_self_service_links() throws Exception {

@Test
public void set_home_redirect() throws Exception {
IdentityZoneProvisioning provisioning = new JdbcIdentityZoneProvisioning(jdbcTemplate);
IdentityZoneConfigurationBootstrap bootstrap = new IdentityZoneConfigurationBootstrap(provisioning);
bootstrap.setHomeRedirect("http://some.redirect.com/redirect");
bootstrap.afterPropertiesSet();

Expand All @@ -105,12 +107,32 @@ public void set_home_redirect() throws Exception {

@Test
public void null_home_redirect() throws Exception {
IdentityZoneProvisioning provisioning = new JdbcIdentityZoneProvisioning(jdbcTemplate);
IdentityZoneConfigurationBootstrap bootstrap = new IdentityZoneConfigurationBootstrap(provisioning);
bootstrap.setHomeRedirect("null");
bootstrap.afterPropertiesSet();

IdentityZone zone = provisioning.retrieve(IdentityZone.getUaa().getId());
assertNull(zone.getConfig().getLinks().getHomeRedirect());
}

@Test
public void signup_link_configured() throws Exception {
links.put("signup", "/configured_signup");
bootstrap.setSelfServiceLinks(links);
bootstrap.afterPropertiesSet();

IdentityZone zone = provisioning.retrieve(IdentityZone.getUaa().getId());
assertEquals("/configured_signup", zone.getConfig().getLinks().getService().getSignup());
assertEquals("/forgot_password", zone.getConfig().getLinks().getService().getPasswd());
}

@Test
public void passwd_link_configured() throws Exception {
links.put("passwd", "/configured_passwd");
bootstrap.setSelfServiceLinks(links);
bootstrap.afterPropertiesSet();

IdentityZone zone = provisioning.retrieve(IdentityZone.getUaa().getId());
assertEquals("/create_account", zone.getConfig().getLinks().getService().getSignup());
assertEquals("/configured_passwd", zone.getConfig().getLinks().getService().getPasswd());
}
}
Expand Up @@ -57,34 +57,35 @@ public class LoginInfoEndpointTests {
public static final String HTTP_LOCALHOST_8080_UAA = "http://localhost:8080/uaa";
private UaaPrincipal marissa;
private List<Prompt> prompts;
private Map<String, String> linksSet = new HashMap<>();
private ExtendedModelMap model = new ExtendedModelMap();
private SamlIdentityProviderConfigurator mockIDPConfigurator;
private List<SamlIdentityProviderDefinition> idps;
private IdentityProviderProvisioning identityProviderProvisioning;
private IdentityProvider uaaProvider;
private IdentityZoneConfiguration originalConfiguration;

@Before
public void setUpPrincipal() {
IdentityZoneHolder.clear();
marissa = new UaaPrincipal("marissa-id","marissa","marissa@test.org","origin",null, IdentityZoneHolder.get().getId());
prompts = new LinkedList<>();
prompts.add(new Prompt("username", "text", "Email"));
prompts.add(new Prompt("password", "password", "Password"));
prompts.add(new Prompt("passcode", "text", "One Time Code ( Get one at "+HTTP_LOCALHOST_8080_UAA+"/passcode )"));
linksSet.put("register", "/create_account");
linksSet.put("passwd", "/forgot_password");
mockIDPConfigurator = mock(SamlIdentityProviderConfigurator.class);
identityProviderProvisioning = mock(IdentityProviderProvisioning.class);
uaaProvider = new IdentityProvider();
when(identityProviderProvisioning.retrieveByOrigin(eq(OriginKeys.UAA), anyString())).thenReturn(uaaProvider);
when(identityProviderProvisioning.retrieveByOrigin(eq(OriginKeys.LDAP), anyString())).thenReturn(new IdentityProvider());
idps = getIdps();
originalConfiguration = IdentityZoneHolder.get().getConfig();
IdentityZoneHolder.get().setConfig(new IdentityZoneConfiguration());
}

@Before
@After
public void clearZoneHolder() {
IdentityZoneHolder.clear();
IdentityZoneHolder.get().setConfig(originalConfiguration);
}

@Test
Expand All @@ -111,10 +112,8 @@ public void testLoginReturnsOtherZone() throws Exception {
@Test
public void customSelfserviceLinks_OnlyApplyToDefaultZone_Html() throws Exception {
LoginInfoEndpoint endpoint = getEndpoint();
Map<String,String> links = new HashMap<>();
links.put("signup", "http://custom_signup_link");
links.put("passwd", "http://custom_passwd_link");
endpoint.setLinks(links);
IdentityZoneHolder.get().getConfig().getLinks().getService().setSignup("http://custom_signup_link");
IdentityZoneHolder.get().getConfig().getLinks().getService().setPasswd("http://custom_passwd_link");
endpoint.loginForHtml(model, null, new MockHttpServletRequest());
assertEquals("http://custom_signup_link", ((Map<String, String>) model.asMap().get("links")).get("createAccountLink"));
assertEquals("http://custom_passwd_link", ((Map<String, String>) model.asMap().get("links")).get("forgotPasswordLink"));
Expand All @@ -138,10 +137,8 @@ public void customSelfserviceLinks_OnlyApplyToDefaultZone_Html() throws Exceptio
@Test
public void customSelfserviceLinks_OnlyApplyToDefaultZone_Json() throws Exception {
LoginInfoEndpoint endpoint = getEndpoint();
Map<String,String> links = new HashMap<>();
links.put("signup", "http://custom_signup_link");
links.put("passwd", "http://custom_passwd_link");
endpoint.setLinks(links);
IdentityZoneHolder.get().getConfig().getLinks().getService().setSignup("http://custom_signup_link");
IdentityZoneHolder.get().getConfig().getLinks().getService().setPasswd("http://custom_passwd_link");
endpoint.loginForJson(model, null);
assertNull(((Map<String, String>) model.asMap().get("links")).get("createAccountLink"));
assertNull(((Map<String, String>) model.asMap().get("links")).get("forgotPasswordLink"));
Expand Down Expand Up @@ -183,7 +180,6 @@ public void check_links_urls(IdentityZone zone) throws Exception {
LoginInfoEndpoint endpoint = getEndpoint();
String baseUrl = "http://uaa.domain.com";
endpoint.setBaseUrl(baseUrl);
endpoint.setLinks(linksSet);
endpoint.infoForJson(model, null);
assertEquals(addSubdomainToUrl(baseUrl), ((Map<String, String>) model.asMap().get("links")).get("uaa"));
assertEquals(addSubdomainToUrl(baseUrl.replace("uaa", "login")), ((Map<String, String>) model.asMap().get("links")).get("login"));
Expand All @@ -209,7 +205,6 @@ public void no_self_service_links_if_self_service_disabled() throws Exception {
zone.getConfig().getLinks().getService().setSelfServiceLinksEnabled(false);
IdentityZoneHolder.set(zone);
LoginInfoEndpoint endpoint = getEndpoint();
endpoint.setLinks(linksSet);
endpoint.infoForJson(model, null);
Map<String, Object> links = (Map<String, Object>) model.asMap().get("links");
assertNotNull(links);
Expand All @@ -220,7 +215,6 @@ public void no_self_service_links_if_self_service_disabled() throws Exception {
@Test
public void no_ui_links_for_json() throws Exception {
LoginInfoEndpoint endpoint = getEndpoint();
endpoint.setLinks(linksSet);
endpoint.infoForJson(model, null);
Map<String, Object> links = (Map<String, Object>) model.asMap().get("links");
assertNotNull(links);
Expand All @@ -240,7 +234,6 @@ public void saml_links_for_json() throws Exception {
endpoint.setIdpDefinitions(mockIDPConfigurator);
when(mockIDPConfigurator.getIdentityProviderDefinitions(anyObject(), anyObject())).thenReturn(idps);
endpoint.setIdpDefinitions(mockIDPConfigurator);
endpoint.setLinks(linksSet);
endpoint.infoForJson(model, null);
Map<String, Object> links = (Map<String, Object>) model.asMap().get("links");
assertEquals("http://someurl", links.get("login"));
Expand All @@ -258,7 +251,6 @@ public void saml_links_for_json() throws Exception {
public void saml_links_for_html() throws Exception {
LoginInfoEndpoint endpoint = getEndpoint();
endpoint.setIdpDefinitions(mockIDPConfigurator);
endpoint.setLinks(linksSet);
endpoint.infoForHtml(model, null);
Map<String, Object> links = (Map<String, Object>) model.asMap().get("links");
assertNotNull(links);
Expand All @@ -273,10 +265,6 @@ public void no_self_service_links_if_internal_user_management_disabled() throws
uaaIdentityProviderDefinition.setDisableInternalUserManagement(true);
uaaProvider.setConfig(uaaIdentityProviderDefinition);
LoginInfoEndpoint endpoint = getEndpoint();
Map<String, String> linksSet = new HashMap<>();
linksSet.put("register", "/create_account");
linksSet.put("passwd", "/forgot_password");
endpoint.setLinks(linksSet);
endpoint.infoForJson(model, null);
Map<String, Object> links = (Map<String, Object>) model.asMap().get("links");
assertNotNull(links);
Expand Down
2 changes: 1 addition & 1 deletion uaa/src/main/webapp/WEB-INF/spring-servlet.xml
Expand Up @@ -373,7 +373,6 @@
<bean id="loginInfoEndpoint" class="org.cloudfoundry.identity.uaa.login.LoginInfoEndpoint">
<property name="authenticationManager" ref="zoneAwareAuthzAuthenticationManager"/>
<property name="uaaBaseUrl" ref="uaaUrl" />
<property name="links" ref="links" />
<property name="entityID" ref="samlEntityID"/>
<property name="idpDefinitions" ref="metaDataProviders"/>
<property name="clientDetailsService" ref="jdbcClientDetailsService"/>
Expand Down Expand Up @@ -402,6 +401,7 @@
<constructor-arg name="provisioning" ref="identityZoneProvisioning"/>
<property name="tokenPolicy" ref="uaaTokenPolicy"/>
<property name="selfServiceLinksEnabled" value="${login.selfServiceLinksEnabled:true}"/>
<property name="selfServiceLinks" ref="links" />
<property name="homeRedirect" value="${login.homeRedirect:null}"/>
</bean>

Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.cloudfoundry.identity.uaa.security.web.CorsFilter;
import org.cloudfoundry.identity.uaa.util.PredicateMatcher;
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneConfiguration;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneProvisioning;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneResolvingFilter;
Expand Down Expand Up @@ -76,6 +77,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -164,6 +166,8 @@ public void testRootContextDefaults() throws Exception {
assertTrue(zoneProvisioning.retrieve(IdentityZone.getUaa().getId()).getConfig().getLinks().getService().isSelfServiceLinksEnabled());
assertNull(zoneProvisioning.retrieve(IdentityZone.getUaa().getId()).getConfig().getLinks().getHomeRedirect());

Object links = context.getBean("links");
assertEquals(Collections.EMPTY_MAP, links);

//check java mail sender
EmailService emailService = context.getBean("emailService", EmailService.class);
Expand Down Expand Up @@ -260,9 +264,11 @@ public void testPropertyValuesWhenSetInYaml() throws Exception {
context = getServletContext(null, "login.yml", "test/bootstrap/bootstrap-test.yml", "file:./src/main/webapp/WEB-INF/spring-servlet.xml");

IdentityZoneProvisioning zoneProvisioning = context.getBean(IdentityZoneProvisioning.class);
assertFalse(zoneProvisioning.retrieve(IdentityZone.getUaa().getId()).getConfig().getLinks().getService().isSelfServiceLinksEnabled());
assertEquals("http://some.redirect.com/redirect", zoneProvisioning.retrieve(IdentityZone.getUaa().getId()).getConfig().getLinks().getHomeRedirect());

IdentityZoneConfiguration zoneConfig = zoneProvisioning.retrieve(IdentityZone.getUaa().getId()).getConfig();
assertFalse(zoneConfig.getLinks().getService().isSelfServiceLinksEnabled());
assertEquals("http://some.redirect.com/redirect", zoneConfig.getLinks().getHomeRedirect());
assertEquals("/configured_signup", zoneConfig.getLinks().getService().getSignup());
assertEquals("/configured_passwd", zoneConfig.getLinks().getService().getPasswd());

IdentityProviderProvisioning idpProvisioning = context.getBean(IdentityProviderProvisioning.class);
IdentityProvider<UaaIdentityProviderDefinition> uaaIdp = idpProvisioning.retrieveByOrigin(OriginKeys.UAA, IdentityZone.getUaa().getId());
Expand Down

0 comments on commit 1a482b7

Please sign in to comment.