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

Commit f96c4eb

Browse files
committed
issue-1251 adding PasswordStrengthModelFactory
1 parent 74dbf11 commit f96c4eb

File tree

3 files changed

+174
-100
lines changed

3 files changed

+174
-100
lines changed

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

Lines changed: 12 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@
2222
import com.stormpath.sdk.cache.Cache;
2323
import com.stormpath.sdk.client.Client;
2424
import com.stormpath.sdk.directory.AccountStore;
25-
import com.stormpath.sdk.directory.AccountStoreVisitor;
2625
import com.stormpath.sdk.directory.AccountStoreVisitorAdapter;
2726
import com.stormpath.sdk.directory.Directory;
28-
import com.stormpath.sdk.directory.PasswordStrength;
29-
import com.stormpath.sdk.group.Group;
30-
import com.stormpath.sdk.impl.resource.AbstractResource;
3127
import com.stormpath.sdk.lang.Assert;
32-
import com.stormpath.sdk.lang.Strings;
3328
import com.stormpath.sdk.organization.Organization;
34-
import com.stormpath.sdk.organization.OrganizationList;
3529
import com.stormpath.sdk.servlet.account.event.impl.DefaultRegisteredAccountRequestEvent;
3630
import com.stormpath.sdk.servlet.application.ApplicationResolver;
3731
import com.stormpath.sdk.servlet.authc.impl.TransientAuthenticationResult;
@@ -40,15 +34,16 @@
4034
import com.stormpath.sdk.servlet.http.Saver;
4135
import com.stormpath.sdk.servlet.http.authc.AccountStoreResolver;
4236
import com.stormpath.sdk.servlet.mvc.provider.AccountStoreModelFactory;
37+
import com.stormpath.sdk.servlet.mvc.provider.DefaultPasswordStrengthModelFactory;
4338
import com.stormpath.sdk.servlet.mvc.provider.ExternalAccountStoreModelFactory;
39+
import com.stormpath.sdk.servlet.mvc.provider.PasswordStrengthModelFactory;
4440
import org.slf4j.Logger;
4541
import org.slf4j.LoggerFactory;
4642

4743
import javax.servlet.http.HttpServletRequest;
4844
import javax.servlet.http.HttpServletResponse;
4945
import java.util.Arrays;
5046
import java.util.Collections;
51-
import java.util.HashMap;
5247
import java.util.LinkedHashMap;
5348
import java.util.List;
5449
import java.util.Map;
@@ -70,6 +65,7 @@ public class RegisterController extends FormController {
7065
private Saver<AuthenticationResult> authenticationResultSaver;
7166
private AccountModelFactory accountModelFactory;
7267
private AccountStoreModelFactory accountStoreModelFactory;
68+
private PasswordStrengthModelFactory passwordStrengthModelFactory;
7369
private ErrorModelFactory errorModelFactory;
7470
private WebHandler preRegisterHandler;
7571
private WebHandler postRegisterHandler;
@@ -103,6 +99,10 @@ public void setAccountStoreModelFactory(AccountStoreModelFactory accountStoreMod
10399
this.accountStoreModelFactory = accountStoreModelFactory;
104100
}
105101

102+
public void setPasswordStrengthModelFactory(PasswordStrengthModelFactory passwordStrengthModelFactory) {
103+
this.passwordStrengthModelFactory = passwordStrengthModelFactory;
104+
}
105+
106106
public void setErrorModelFactory(ErrorModelFactory errorModelFactory) {
107107
this.errorModelFactory = errorModelFactory;
108108
}
@@ -133,6 +133,9 @@ public void init() throws Exception {
133133
if (this.accountStoreModelFactory == null) {
134134
this.accountStoreModelFactory = new ExternalAccountStoreModelFactory();
135135
}
136+
if (this.passwordStrengthModelFactory == null) {
137+
this.passwordStrengthModelFactory = new DefaultPasswordStrengthModelFactory();
138+
}
136139
if (this.errorModelFactory == null) {
137140
this.errorModelFactory = new RegisterErrorModelFactory(this.messageSource);
138141
}
@@ -145,6 +148,7 @@ public void init() throws Exception {
145148
Assert.notNull(this.postRegisterHandler, "postRegisterHandler cannot be null.");
146149
Assert.notNull(this.accountModelFactory, "accountModelFactory cannot be null.");
147150
Assert.notNull(this.accountStoreModelFactory, "accountStoreModelFactory cannot be null.");
151+
Assert.notNull(this.passwordStrengthModelFactory, "passwordStrengthModelFactory cannot be null.");
148152
Assert.notNull(this.errorModelFactory, "errorModelFactory cannot be null.");
149153
Assert.notNull(this.accountStoreResolver, "accountStoreResolver cannot be null.");
150154
}
@@ -161,7 +165,7 @@ protected void appendModel(HttpServletRequest request, HttpServletResponse respo
161165
model.put("loginUri", loginUri);
162166
} else {
163167
model.put("accountStores", accountStoreModelFactory.getAccountStores(request));
164-
model.put("passwordPolicy", getPasswordPolicy(request));
168+
model.put("passwordPolicy", passwordStrengthModelFactory.getPasswordPolicy(request));
165169
}
166170
}
167171

@@ -319,97 +323,5 @@ private Map<String, Object> getCustomData(HttpServletRequest request, Form form)
319323
return result;
320324
}
321325

322-
private Map<String, Object> getPasswordPolicy(HttpServletRequest request) {
323-
324-
String onk = request.getParameter("organizationNameKey");
325-
326-
PasswordStrength passwordStrength;
327-
328-
if (Strings.hasText(onk)) {
329-
passwordStrength = findPasswordStrengthByOrganization(onk);
330-
} else {
331-
passwordStrength = getApplicationPasswordStrength(request);
332-
}
333-
334-
if (passwordStrength == null) {
335-
return null;
336-
}
337-
338-
return convertPasswordStrengthToMap(passwordStrength);
339-
}
340-
341-
private PasswordStrength getApplicationPasswordStrength(HttpServletRequest request) {
342-
AccountStore defaultAccountStore = getApplication(request).getDefaultAccountStore();
343-
344-
if (defaultAccountStore == null) {
345-
return null;
346-
}
347-
348-
final PasswordStrength[] passwordStrength = new PasswordStrength[1];
349-
defaultAccountStore.accept(new AccountStoreVisitor() {
350-
@Override
351-
public void visit(Group group) {
352-
passwordStrength[0] = group.getDirectory().getPasswordPolicy().getStrength();
353-
}
354-
355-
@Override
356-
public void visit(Directory directory) {
357-
passwordStrength[0] = directory.getPasswordPolicy().getStrength();
358-
}
359-
360-
@Override
361-
public void visit(Organization organization) {
362-
passwordStrength[0] = getOrganizationPasswordStrength(organization);
363-
}
364-
});
365-
366-
return passwordStrength[0];
367-
}
368-
369-
private PasswordStrength findPasswordStrengthByOrganization(String onk) {
370-
HashMap<String, Object> query = new HashMap<>();
371-
query.put("nameKey", onk);
372-
OrganizationList organizations = client.getOrganizations(query);
373326

374-
if (organizations.getSize() != 1) {
375-
return null;
376-
}
377-
378-
return getOrganizationPasswordStrength(organizations.single());
379-
}
380-
381-
private PasswordStrength getOrganizationPasswordStrength(Organization organization) {
382-
AccountStore organizationDefaultAccountStore = organization.getDefaultAccountStore();
383-
384-
if (organizationDefaultAccountStore == null) {
385-
return null;
386-
}
387-
388-
final PasswordStrength[] passwordStrength = new PasswordStrength[1];
389-
organizationDefaultAccountStore.accept(new AccountStoreVisitorAdapter() {
390-
@Override
391-
public void visit(Group group) {
392-
passwordStrength[0] = group.getDirectory().getPasswordPolicy().getStrength();
393-
}
394-
395-
@Override
396-
public void visit(Directory directory) {
397-
passwordStrength[0] = directory.getPasswordPolicy().getStrength();
398-
}
399-
});
400-
401-
return passwordStrength[0];
402-
}
403-
404-
private Map<String, Object> convertPasswordStrengthToMap(PasswordStrength passwordStrength) {
405-
AbstractResource abstractResource = (AbstractResource) passwordStrength;
406-
407-
Map<String, Object> strength = new HashMap<>();
408-
409-
for (String propertyName : abstractResource.getPropertyDescriptors().keySet()) {
410-
strength.put(propertyName, abstractResource.getProperty(propertyName));
411-
}
412-
413-
return strength;
414-
}
415327
}
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+
}
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+
}

0 commit comments

Comments
 (0)