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

Commit 3252671

Browse files
authored
Merge pull request #1252 from stormpath/issue-1251-passwordPolicy-registrationController
issue-1251 adding passwordPolicy to registration page response
2 parents 5a67922 + f96c4eb commit 3252671

File tree

6 files changed

+180
-14
lines changed

6 files changed

+180
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ protected void doConfigure(RegisterController controller, Config config) {
4444
controller.setPreRegisterHandler(config.getRegisterPreHandler());
4545
controller.setPostRegisterHandler(config.getRegisterPostHandler());
4646
controller.setAccountStoreResolver(config.getAccountStoreResolver());
47+
controller.setApplicationResolver(config.getApplicationResolver());
4748
}
4849
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import com.stormpath.sdk.servlet.http.Saver;
3535
import com.stormpath.sdk.servlet.http.authc.AccountStoreResolver;
3636
import com.stormpath.sdk.servlet.mvc.provider.AccountStoreModelFactory;
37+
import com.stormpath.sdk.servlet.mvc.provider.DefaultPasswordStrengthModelFactory;
3738
import com.stormpath.sdk.servlet.mvc.provider.ExternalAccountStoreModelFactory;
39+
import com.stormpath.sdk.servlet.mvc.provider.PasswordStrengthModelFactory;
3840
import org.slf4j.Logger;
3941
import org.slf4j.LoggerFactory;
4042

@@ -63,6 +65,7 @@ public class RegisterController extends FormController {
6365
private Saver<AuthenticationResult> authenticationResultSaver;
6466
private AccountModelFactory accountModelFactory;
6567
private AccountStoreModelFactory accountStoreModelFactory;
68+
private PasswordStrengthModelFactory passwordStrengthModelFactory;
6669
private ErrorModelFactory errorModelFactory;
6770
private WebHandler preRegisterHandler;
6871
private WebHandler postRegisterHandler;
@@ -96,6 +99,10 @@ public void setAccountStoreModelFactory(AccountStoreModelFactory accountStoreMod
9699
this.accountStoreModelFactory = accountStoreModelFactory;
97100
}
98101

102+
public void setPasswordStrengthModelFactory(PasswordStrengthModelFactory passwordStrengthModelFactory) {
103+
this.passwordStrengthModelFactory = passwordStrengthModelFactory;
104+
}
105+
99106
public void setErrorModelFactory(ErrorModelFactory errorModelFactory) {
100107
this.errorModelFactory = errorModelFactory;
101108
}
@@ -126,6 +133,9 @@ public void init() throws Exception {
126133
if (this.accountStoreModelFactory == null) {
127134
this.accountStoreModelFactory = new ExternalAccountStoreModelFactory();
128135
}
136+
if (this.passwordStrengthModelFactory == null) {
137+
this.passwordStrengthModelFactory = new DefaultPasswordStrengthModelFactory();
138+
}
129139
if (this.errorModelFactory == null) {
130140
this.errorModelFactory = new RegisterErrorModelFactory(this.messageSource);
131141
}
@@ -138,6 +148,7 @@ public void init() throws Exception {
138148
Assert.notNull(this.postRegisterHandler, "postRegisterHandler cannot be null.");
139149
Assert.notNull(this.accountModelFactory, "accountModelFactory cannot be null.");
140150
Assert.notNull(this.accountStoreModelFactory, "accountStoreModelFactory cannot be null.");
151+
Assert.notNull(this.passwordStrengthModelFactory, "passwordStrengthModelFactory cannot be null.");
141152
Assert.notNull(this.errorModelFactory, "errorModelFactory cannot be null.");
142153
Assert.notNull(this.accountStoreResolver, "accountStoreResolver cannot be null.");
143154
}
@@ -154,6 +165,7 @@ protected void appendModel(HttpServletRequest request, HttpServletResponse respo
154165
model.put("loginUri", loginUri);
155166
} else {
156167
model.put("accountStores", accountStoreModelFactory.getAccountStores(request));
168+
model.put("passwordPolicy", passwordStrengthModelFactory.getPasswordPolicy(request));
157169
}
158170
}
159171

@@ -310,4 +322,6 @@ private Map<String, Object> getCustomData(HttpServletRequest request, Form form)
310322

311323
return result;
312324
}
325+
326+
313327
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2016 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.provider;
17+
18+
import com.stormpath.sdk.directory.AccountStore;
19+
import com.stormpath.sdk.directory.AccountStoreVisitor;
20+
import com.stormpath.sdk.directory.AccountStoreVisitorAdapter;
21+
import com.stormpath.sdk.directory.Directory;
22+
import com.stormpath.sdk.directory.PasswordStrength;
23+
import com.stormpath.sdk.group.Group;
24+
import com.stormpath.sdk.impl.resource.AbstractResource;
25+
import com.stormpath.sdk.lang.Strings;
26+
import com.stormpath.sdk.organization.Organization;
27+
import com.stormpath.sdk.organization.OrganizationList;
28+
import com.stormpath.sdk.servlet.application.ApplicationResolver;
29+
import com.stormpath.sdk.servlet.client.ClientResolver;
30+
31+
import javax.servlet.http.HttpServletRequest;
32+
import java.util.HashMap;
33+
import java.util.Map;
34+
35+
/**
36+
* Returns a {@link Map} of the PasswordPolicy's Strength for the default AccountStore mapped to the Application.
37+
* Returns null if there is no default AccountStore
38+
*
39+
* @since 1.5.0
40+
*/
41+
public class DefaultPasswordStrengthModelFactory implements PasswordStrengthModelFactory {
42+
43+
public Map<String, Object> getPasswordPolicy(HttpServletRequest request) {
44+
45+
String onk = request.getParameter("organizationNameKey");
46+
47+
PasswordStrength passwordStrength;
48+
49+
if (Strings.hasText(onk)) {
50+
passwordStrength = findPasswordStrengthByOrganization(request, onk);
51+
} else {
52+
passwordStrength = getApplicationPasswordStrength(request);
53+
}
54+
55+
if (passwordStrength == null) {
56+
return null;
57+
}
58+
59+
return convertPasswordStrengthToMap(passwordStrength);
60+
}
61+
62+
private PasswordStrength getApplicationPasswordStrength(HttpServletRequest request) {
63+
AccountStore defaultAccountStore = ApplicationResolver.INSTANCE.getApplication(request).getDefaultAccountStore();
64+
65+
if (defaultAccountStore == null) {
66+
return null;
67+
}
68+
69+
final PasswordStrength[] passwordStrength = new PasswordStrength[1];
70+
defaultAccountStore.accept(new AccountStoreVisitor() {
71+
@Override
72+
public void visit(Group group) {
73+
passwordStrength[0] = group.getDirectory().getPasswordPolicy().getStrength();
74+
}
75+
76+
@Override
77+
public void visit(Directory directory) {
78+
passwordStrength[0] = directory.getPasswordPolicy().getStrength();
79+
}
80+
81+
@Override
82+
public void visit(Organization organization) {
83+
passwordStrength[0] = getOrganizationPasswordStrength(organization);
84+
}
85+
});
86+
87+
return passwordStrength[0];
88+
}
89+
90+
private PasswordStrength findPasswordStrengthByOrganization(HttpServletRequest request, String onk) {
91+
HashMap<String, Object> query = new HashMap<>();
92+
query.put("nameKey", onk);
93+
OrganizationList organizations = ClientResolver.INSTANCE.getClient(request).getOrganizations(query);
94+
95+
if (organizations.getSize() != 1) {
96+
return null;
97+
}
98+
99+
return getOrganizationPasswordStrength(organizations.single());
100+
}
101+
102+
private PasswordStrength getOrganizationPasswordStrength(Organization organization) {
103+
AccountStore organizationDefaultAccountStore = organization.getDefaultAccountStore();
104+
105+
if (organizationDefaultAccountStore == null) {
106+
return null;
107+
}
108+
109+
final PasswordStrength[] passwordStrength = new PasswordStrength[1];
110+
organizationDefaultAccountStore.accept(new AccountStoreVisitorAdapter() {
111+
@Override
112+
public void visit(Group group) {
113+
passwordStrength[0] = group.getDirectory().getPasswordPolicy().getStrength();
114+
}
115+
116+
@Override
117+
public void visit(Directory directory) {
118+
passwordStrength[0] = directory.getPasswordPolicy().getStrength();
119+
}
120+
});
121+
122+
return passwordStrength[0];
123+
}
124+
125+
private Map<String, Object> convertPasswordStrengthToMap(PasswordStrength passwordStrength) {
126+
AbstractResource abstractResource = (AbstractResource) passwordStrength;
127+
128+
Map<String, Object> strength = new HashMap<>();
129+
130+
for (String propertyName : abstractResource.getPropertyDescriptors().keySet()) {
131+
strength.put(propertyName, abstractResource.getProperty(propertyName));
132+
}
133+
134+
return strength;
135+
}
136+
}

extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/provider/ExternalAccountStoreModelFactory.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import com.stormpath.sdk.application.webconfig.ApplicationWebConfig;
2424
import com.stormpath.sdk.application.webconfig.ApplicationWebConfigStatus;
2525
import com.stormpath.sdk.directory.AccountStore;
26-
import com.stormpath.sdk.directory.AccountStoreVisitor;
26+
import com.stormpath.sdk.directory.AccountStoreVisitorAdapter;
2727
import com.stormpath.sdk.directory.Directory;
28-
import com.stormpath.sdk.group.Group;
29-
import com.stormpath.sdk.organization.Organization;
3028
import com.stormpath.sdk.provider.GoogleProvider;
3129
import com.stormpath.sdk.provider.OAuthProvider;
3230
import com.stormpath.sdk.provider.Provider;
@@ -80,7 +78,7 @@ protected String getAuthorizeBaseUri(@SuppressWarnings("UnusedParameters") HttpS
8078
return authorizeBaseUri;
8179
}
8280

83-
private class AccountStoreModelVisitor implements AccountStoreVisitor {
81+
private class AccountStoreModelVisitor extends AccountStoreVisitorAdapter {
8482

8583
private final List<AccountStoreModel> accountStores;
8684
private final String authorizeBaseUri;
@@ -90,11 +88,6 @@ public AccountStoreModelVisitor(List<AccountStoreModel> accountStores, String au
9088
this.authorizeBaseUri = authorizeBaseUri;
9189
}
9290

93-
@Override
94-
public void visit(Group group) {
95-
//Do nothing... groups cannot be external
96-
}
97-
9891
//Only directories can support provider-based workflows:
9992
@Override
10093
public void visit(Directory directory) {
@@ -118,11 +111,6 @@ public void visit(Directory directory) {
118111
}
119112
}
120113

121-
@Override
122-
public void visit(Organization organization) {
123-
//Do nothing... organizations cannot be external
124-
}
125-
126114
public List<AccountStoreModel> getAccountStores() {
127115
return accountStores;
128116
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016 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.provider;
17+
18+
import javax.servlet.http.HttpServletRequest;
19+
import java.util.Map;
20+
21+
/**
22+
* @since 1.5.0
23+
*/
24+
public interface PasswordStrengthModelFactory {
25+
Map<String, Object> getPasswordPolicy(HttpServletRequest request);
26+
}

extensions/spring/stormpath-spring-webmvc/src/main/java/com/stormpath/spring/config/AbstractStormpathWebMvcConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ public Controller stormpathRegisterController() {
11101110
c.setPreRegisterHandler(registerPreHandler);
11111111
c.setPostRegisterHandler(registerPostHandler);
11121112
c.setAccountStoreResolver(stormpathAccountStoreResolver());
1113+
c.setApplicationResolver(stormpathApplicationResolver());
11131114

11141115
return init(c);
11151116
}

0 commit comments

Comments
 (0)