Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit 7ededae

Browse files
author
Mario
committed
1242 Extracted Login and Register IDSite controllers to their own sub-classes
1 parent 598edc6 commit 7ededae

File tree

12 files changed

+213
-48
lines changed

12 files changed

+213
-48
lines changed
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626
/**
2727
* @since 1.0.0
2828
*/
29-
public abstract class IDSiteFilterFactory extends ControllerFilterFactory<IdSiteController> {
29+
public abstract class AbstractIDSiteFilterFactory<T extends IdSiteController> extends ControllerFilterFactory<T> {
3030

3131
@Override
32-
protected IdSiteController newController() {
33-
return new IdSiteController();
34-
}
32+
protected abstract T newController();
3533

3634
@Override
37-
protected void configure(IdSiteController controller, Config config) throws Exception {
35+
protected void configure(T controller, Config config) throws Exception {
3836

3937
SubdomainResolver subdomainResolver = new SubdomainResolver();
4038
subdomainResolver.setBaseDomainName(config.get("stormpath.web.application.domain"));
@@ -58,5 +56,5 @@ protected void configure(IdSiteController controller, Config config) throws Exce
5856
doConfigure(controller, config);
5957
}
6058

61-
public abstract void doConfigure(IdSiteController controller, Config config);
59+
public abstract void doConfigure(T controller, Config config);
6260
}

extensions/servlet/src/main/java/com/stormpath/sdk/servlet/config/filter/IDSiteForgotFilterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* @since 1.0.0
2323
*/
24-
public class IDSiteForgotFilterFactory extends IDSiteFilterFactory {
24+
public class IDSiteForgotFilterFactory extends AbstractIDSiteFilterFactory<IdSiteController> {
2525

2626
@Override
2727
protected IdSiteController newController() {

extensions/servlet/src/main/java/com/stormpath/sdk/servlet/config/filter/IDSiteLoginFilterFactory.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 Stormpath, Inc.
2+
* Copyright 2017 Stormpath, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,18 +17,19 @@
1717

1818
import com.stormpath.sdk.servlet.config.Config;
1919
import com.stormpath.sdk.servlet.mvc.IdSiteController;
20+
import com.stormpath.sdk.servlet.mvc.IdSiteLoginController;
2021

2122
/**
22-
* @since 1.0.0
23+
* @since 1.5.0
2324
*/
24-
public class IDSiteLoginFilterFactory extends IDSiteFilterFactory {
25+
public class IDSiteLoginFilterFactory extends AbstractIDSiteFilterFactory<IdSiteLoginController> {
2526

2627
@Override
27-
protected IdSiteController newController() {
28-
return new IdSiteController();
28+
protected IdSiteLoginController newController() {
29+
return new IdSiteLoginController();
2930
}
3031

31-
public void doConfigure(IdSiteController controller, Config config) {
32+
public void doConfigure(IdSiteLoginController controller, Config config) {
3233
controller.setIdSiteUri(config.get("stormpath.web.idSite.loginUri"));
3334
controller.setNextUri(config.get("stormpath.web.login.nextUri"));
3435
controller.setPreLoginHandler(config.getLoginPreHandler());

extensions/servlet/src/main/java/com/stormpath/sdk/servlet/config/filter/IDSiteRegisterFilterFactory.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 Stormpath, Inc.
2+
* Copyright 2017 Stormpath, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,20 +16,20 @@
1616
package com.stormpath.sdk.servlet.config.filter;
1717

1818
import com.stormpath.sdk.servlet.config.Config;
19-
import com.stormpath.sdk.servlet.mvc.IdSiteController;
19+
import com.stormpath.sdk.servlet.mvc.IdSiteRegisterController;
2020

2121
/**
22-
* @since 1.0.0
22+
* @since 1.5.0
2323
*/
24-
public class IDSiteRegisterFilterFactory extends IDSiteFilterFactory {
24+
public class IDSiteRegisterFilterFactory extends AbstractIDSiteFilterFactory<IdSiteRegisterController> {
2525

2626
@Override
27-
protected IdSiteController newController() {
28-
return new IdSiteController();
27+
protected IdSiteRegisterController newController() {
28+
return new IdSiteRegisterController();
2929
}
3030

3131
@Override
32-
public void doConfigure(IdSiteController controller, Config config) {
32+
public void doConfigure(IdSiteRegisterController controller, Config config) {
3333
controller.setIdSiteUri(config.get("stormpath.web.idSite.registerUri"));
3434
controller.setNextUri(config.get("stormpath.web.register.nextUri"));
3535
controller.setPreRegisterHandler(config.getRegisterPreHandler());

extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/CallbackController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ protected ViewModel onAuthentication(HttpServletRequest request,
107107

108108
publish(new DefaultSuccessfulAuthenticationRequestEvent(request, response, null, authcResult));
109109

110-
if (postLoginHandler != null) {
111-
if (!postLoginHandler.handle(request, response, result.getAccount())) {
112-
return null;
113-
}
110+
if (postLoginHandler != null && !postLoginHandler.handle(request, response, result.getAccount())) {
111+
return null;
114112
}
115113

116114
return new DefaultViewModel(loginNextUri).setRedirect(true);

extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/IdSiteController.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ public class IdSiteController extends AbstractController {
4040

4141
private Resolver<IdSiteOrganizationContext> idSiteOrganizationResolver;
4242

43-
protected WebHandler preRegisterHandler;
44-
protected WebHandler preLoginHandler;
45-
4643
public void setServerUriResolver(ServerUriResolver serverUriResolver) {
4744
this.serverUriResolver = serverUriResolver;
4845
}
@@ -68,22 +65,13 @@ public void init() {
6865
Assert.notNull(callbackUri, "callbackUri must be configured.");
6966
Assert.notNull(idSiteOrganizationResolver, "idSiteOrganizationResolver must be configured.");
7067
Assert.notNull(alreadyLoggedInUri, "alreadyLoggedInUri must be configured.");
71-
Assert.isTrue(!(preRegisterHandler != null && preLoginHandler != null), "This IDSite controller must contain only one of preRegisterHandler and preLoginHandler");
7268
}
7369

7470
@Override
7571
public boolean isNotAllowedIfAuthenticated() {
7672
return false;
7773
}
7874

79-
public void setPreLoginHandler(WebHandler preLoginHandler) {
80-
this.preLoginHandler = preLoginHandler;
81-
}
82-
83-
public void setPreRegisterHandler(WebHandler preRegisterHandler) {
84-
this.preRegisterHandler = preRegisterHandler;
85-
}
86-
8775
protected Application getApplication(HttpServletRequest request) {
8876
return ApplicationResolver.INSTANCE.getApplication(request);
8977
}
@@ -108,16 +96,6 @@ protected String buildCallbackUri(HttpServletRequest request) {
10896
@Override
10997
protected ViewModel doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
11098

111-
if (preLoginHandler != null) {
112-
if (!preLoginHandler.handle(request, response, null)) {
113-
return null;
114-
}
115-
} else if (preRegisterHandler != null) {
116-
if (!preRegisterHandler.handle(request, response, null)) {
117-
return null;
118-
}
119-
}
120-
12199
//Let's redirect to "alreadyLoggedInUri" if the user is already logged in
122100
if (AccountResolver.INSTANCE.getAccount(request) != null) {
123101
return new DefaultViewModel(alreadyLoggedInUri).setRedirect(true);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.sdk.servlet.mvc;
17+
18+
import javax.servlet.http.HttpServletRequest;
19+
import javax.servlet.http.HttpServletResponse;
20+
21+
/**
22+
* @since 1.5.0
23+
*/
24+
public class IdSiteLoginController extends IdSiteController {
25+
26+
protected WebHandler preLoginHandler;
27+
28+
public void setPreLoginHandler(WebHandler preLoginHandler) {
29+
this.preLoginHandler = preLoginHandler;
30+
}
31+
32+
@Override
33+
protected ViewModel doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
34+
35+
if (preLoginHandler != null) {
36+
if (!preLoginHandler.handle(request, response, null)) {
37+
return null;
38+
}
39+
}
40+
41+
return super.doGet(request, response);
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.sdk.servlet.mvc;
17+
18+
import javax.servlet.http.HttpServletRequest;
19+
import javax.servlet.http.HttpServletResponse;
20+
21+
/**
22+
* @since 1.5.0
23+
*/
24+
public class IdSiteRegisterController extends IdSiteController {
25+
26+
protected WebHandler preRegisterHandler;
27+
28+
public void setPreRegisterHandler(WebHandler preRegisterHandler) {
29+
this.preRegisterHandler = preRegisterHandler;
30+
}
31+
32+
@Override
33+
protected ViewModel doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
34+
35+
if (preRegisterHandler != null) {
36+
if (!preRegisterHandler.handle(request, response, null)) {
37+
return null;
38+
}
39+
}
40+
41+
return super.doGet(request, response);
42+
}
43+
}

extensions/spring/boot/stormpath-webmvc-spring-boot-starter/src/test/groovy/com/stormpath/spring/boot/autoconfigure/StormpathWebSecurityAutoConfigurationIT.groovy

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,62 @@ package com.stormpath.spring.boot.autoconfigure
1717

1818
import autoconfigure.StormpathWebSecurityAutoConfigurationTestApplication
1919
import com.stormpath.sdk.servlet.csrf.CsrfTokenManager
20+
import com.stormpath.sdk.servlet.filter.StormpathFilter
21+
import com.stormpath.sdk.servlet.mvc.Controller
22+
import com.stormpath.sdk.servlet.mvc.IdSiteLoginController
23+
import com.stormpath.sdk.servlet.mvc.LoginController
2024
import com.stormpath.spring.config.TwoAppTenantStormpathTestConfiguration
2125
import org.springframework.beans.factory.annotation.Autowired
26+
import org.springframework.beans.factory.annotation.Qualifier
27+
import org.springframework.beans.factory.annotation.Value
2228
import org.springframework.boot.test.context.SpringBootTest
29+
import org.springframework.test.context.TestPropertySource
2330
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests
2431
import org.springframework.test.context.web.WebAppConfiguration
2532
import org.testng.annotations.Test
2633

2734
import static org.testng.Assert.assertEquals
35+
import static org.testng.Assert.assertNotNull
36+
import static org.testng.Assert.assertTrue
2837

2938
/**
3039
* @since 1.0.RC5.2
3140
*/
3241
@SpringBootTest(classes = [StormpathWebSecurityAutoConfigurationTestApplication.class, TwoAppTenantStormpathTestConfiguration.class])
3342
@WebAppConfiguration
43+
@TestPropertySource(locations = "classpath:it.application.properties")
3444
class StormpathWebSecurityAutoConfigurationIT extends AbstractTestNGSpringContextTests {
3545

3646
@Autowired
3747
CsrfTokenManager csrfTokenManager
3848

49+
@Autowired
50+
@Qualifier("stormpathLoginController")
51+
Controller login
52+
53+
@Autowired
54+
@Qualifier("stormpathRegisterController")
55+
Controller register
56+
57+
@Autowired
58+
@Qualifier("stormpathIdSiteResultController")
59+
Controller idSiteResultController
60+
61+
@Value("#{ @environment['stormpath.web.idSite.enabled'] ?: false }")
62+
protected boolean idSiteEnabled;
63+
3964
@Test
4065
void testCsrfTokenManager() {
4166
assertEquals csrfTokenManager.tokenName, 'csrfToken'
4267
}
68+
69+
//asserts https://github.com/stormpath/stormpath-sdk-java/issues/1242
70+
@Test
71+
void assertPrePostHandlersArePresentWhenIDSiteIsEnabled() {
72+
assertTrue idSiteEnabled
73+
assertTrue(register.preRegisterHandler instanceof StormpathWebSecurityAutoConfigurationTestApplication.CustomRegisterPreHandler)
74+
assertTrue(idSiteResultController.postRegisterHandler instanceof StormpathWebSecurityAutoConfigurationTestApplication.CustomRegisterPostHandler)
75+
assertTrue(login.preLoginHandler instanceof StormpathWebSecurityAutoConfigurationTestApplication.CustomLoginPreHandler)
76+
assertTrue(idSiteResultController.postLoginHandler instanceof StormpathWebSecurityAutoConfigurationTestApplication.CustomLoginPostHandler)
77+
}
4378
}

extensions/spring/boot/stormpath-webmvc-spring-boot-starter/src/test/java/autoconfigure/StormpathWebSecurityAutoConfigurationTestApplication.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
*/
1616
package autoconfigure;
1717

18+
import com.stormpath.sdk.account.Account;
19+
import com.stormpath.sdk.servlet.mvc.WebHandler;
1820
import org.slf4j.Logger;
1921
import org.slf4j.LoggerFactory;
2022
import org.springframework.boot.SpringApplication;
2123
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2224
import org.springframework.context.ApplicationContext;
25+
import org.springframework.context.annotation.Bean;
2326
import org.springframework.context.annotation.Configuration;
2427

28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
2530
import java.util.Arrays;
2631

2732
/**
@@ -46,4 +51,51 @@ public static void main(String[] args) {
4651
}
4752
}
4853

54+
@Bean
55+
public WebHandler registerPreHandler() {
56+
return new CustomRegisterPreHandler();
57+
}
58+
59+
@Bean
60+
public WebHandler registerPostHandler() {
61+
return new CustomRegisterPostHandler();
62+
}
63+
64+
@Bean
65+
public WebHandler loginPreHandler() {
66+
return new CustomLoginPreHandler();
67+
}
68+
69+
@Bean
70+
public WebHandler loginPostHandler() {
71+
return new CustomLoginPostHandler();
72+
}
73+
74+
public class CustomRegisterPreHandler implements WebHandler {
75+
@Override
76+
public boolean handle(HttpServletRequest request, HttpServletResponse response, Account account) {
77+
return false;
78+
}
79+
}
80+
81+
public class CustomRegisterPostHandler implements WebHandler {
82+
@Override
83+
public boolean handle(HttpServletRequest request, HttpServletResponse response, Account account) {
84+
return false;
85+
}
86+
}
87+
88+
public class CustomLoginPreHandler implements WebHandler {
89+
@Override
90+
public boolean handle(HttpServletRequest request, HttpServletResponse response, Account account) {
91+
return false;
92+
}
93+
}
94+
95+
public class CustomLoginPostHandler implements WebHandler {
96+
@Override
97+
public boolean handle(HttpServletRequest request, HttpServletResponse response, Account account) {
98+
return false;
99+
}
100+
}
49101
}

0 commit comments

Comments
 (0)