This repository has been archived by the owner on Dec 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
/
LoginController.java
277 lines (236 loc) · 11.5 KB
/
LoginController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright 2015 Stormpath, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stormpath.sdk.servlet.mvc;
import com.stormpath.sdk.account.Account;
import com.stormpath.sdk.authc.AuthenticationResult;
import com.stormpath.sdk.http.HttpMethod;
import com.stormpath.sdk.lang.Assert;
import com.stormpath.sdk.lang.Collections;
import com.stormpath.sdk.oauth.AccessTokenResult;
import com.stormpath.sdk.servlet.authc.impl.DefaultSuccessfulAuthenticationRequestEvent;
import com.stormpath.sdk.servlet.authc.impl.TransientAuthenticationResult;
import com.stormpath.sdk.servlet.form.Form;
import com.stormpath.sdk.servlet.http.Resolver;
import com.stormpath.sdk.servlet.http.Saver;
import com.stormpath.sdk.servlet.mvc.provider.AccountStoreModel;
import com.stormpath.sdk.servlet.mvc.provider.AccountStoreModelFactory;
import com.stormpath.sdk.servlet.mvc.provider.ExternalAccountStoreModelFactory;
import com.stormpath.sdk.servlet.oauth.OAuthTokenResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* @since 1.0.RC4
*/
public class LoginController extends FormController {
private static final Logger log = LoggerFactory.getLogger(LoginController.class);
private String forgotPasswordUri;
private String verifyUri;
private String registerUri;
private String logoutUri;
private String samlUri;
private boolean verifyEnabled = true;
private boolean forgotPasswordEnabled = true;
private boolean idSiteEnabled;
private boolean callbackEnabled;
private Saver<AuthenticationResult> authenticationResultSaver;
private ErrorModelFactory errorModelFactory;
private LoginFormStatusResolver loginFormStatusResolver;
private AccountStoreModelFactory accountStoreModelFactory = new ExternalAccountStoreModelFactory();
private AccountModelFactory accountModelFactory = new DefaultAccountModelFactory();
private WebHandler preLoginHandler;
private WebHandler postLoginHandler;
private Resolver<Boolean> registerEnabledResolver;
public void setForgotPasswordUri(String forgotPasswordUri) {
this.forgotPasswordUri = forgotPasswordUri;
}
public void setVerifyUri(String verifyUri) {
this.verifyUri = verifyUri;
}
public void setRegisterUri(String registerUri) {
this.registerUri = registerUri;
}
public void setLogoutUri(String logoutUri) {
this.logoutUri = logoutUri;
}
public void setSamlUri(String samlUri) {
this.samlUri = samlUri;
}
public void setVerifyEnabled(boolean verifyEnabled) {
this.verifyEnabled = verifyEnabled;
}
public void setForgotPasswordEnabled(boolean forgotPasswordEnabled) {
this.forgotPasswordEnabled = forgotPasswordEnabled;
}
public void setAuthenticationResultSaver(Saver<AuthenticationResult> authenticationResultSaver) {
this.authenticationResultSaver = authenticationResultSaver;
}
public void setErrorModelFactory(ErrorModelFactory errorModelFactory) {
this.errorModelFactory = errorModelFactory;
}
public void setLoginFormStatusResolver(LoginFormStatusResolver loginFormStatusResolver) {
this.loginFormStatusResolver = loginFormStatusResolver;
}
public void setAccountStoreModelFactory(AccountStoreModelFactory accountStoreModelFactory) {
this.accountStoreModelFactory = accountStoreModelFactory;
}
public void setAccountModelFactory(AccountModelFactory accountModelFactory) {
this.accountModelFactory = accountModelFactory;
}
public void setPreLoginHandler(WebHandler preLoginHandler) {
this.preLoginHandler = preLoginHandler;
}
public void setPostLoginHandler(WebHandler postLoginHandler) {
this.postLoginHandler = postLoginHandler;
}
public void setIdSiteEnabled(boolean idSiteEnabled) {
this.idSiteEnabled = idSiteEnabled;
}
public void setCallbackEnabled(boolean callbackEnabled) {
this.callbackEnabled = callbackEnabled;
}
public void setRegisterEnabledResolver(Resolver<Boolean> registerEnabledResolver) {
this.registerEnabledResolver = registerEnabledResolver;
}
@Override
public void init() throws Exception {
super.init();
Assert.hasText(this.verifyUri, "verifyUri property cannot be null or empty.");
if (this.loginFormStatusResolver == null) {
this.loginFormStatusResolver = new DefaultLoginFormStatusResolver(this.messageSource, this.verifyUri);
}
if (this.errorModelFactory == null) {
this.errorModelFactory = new LoginErrorModelFactory(this.messageSource);
}
if (this.accountStoreModelFactory == null) {
this.accountStoreModelFactory = new ExternalAccountStoreModelFactory();
}
if (this.accountModelFactory == null) {
this.accountModelFactory = new DefaultAccountModelFactory();
}
Assert.hasText(this.forgotPasswordUri, "forgotPasswordUri property cannot be null or empty.");
Assert.hasText(this.registerUri, "registerUri property cannot be null or empty.");
Assert.notNull(this.registerEnabledResolver, "registerEnabledResolver cannot be null.");
Assert.hasText(this.logoutUri, "logoutUri property cannot be null or empty.");
Assert.hasText(this.samlUri, "samlUri property cannot be null or empty.");
Assert.notNull(this.authenticationResultSaver, "authenticationResultSaver property cannot be null.");
Assert.notNull(this.errorModelFactory, "errorModelFactory cannot be null.");
Assert.notNull(this.preLoginHandler, "preLoginHandler cannot be null.");
Assert.notNull(this.postLoginHandler, "postLoginHandler cannot be null.");
Assert.notNull(this.loginFormStatusResolver, "loginFormStatusResolver cannot be null.");
Assert.notNull(this.accountStoreModelFactory, "accountStoreModelFactory cannot be null.");
Assert.notNull(this.accountModelFactory, "accountModelFactory cannot be null.");
Assert.notNull(this.applicationResolver, "applicationResolver cannot be null.");
}
@Override
public boolean isNotAllowedIfAuthenticated() {
return true;
}
@Override
protected void appendModel(HttpServletRequest request, HttpServletResponse response, Form form, List<ErrorModel> errors,
Map<String, Object> model) {
final List<AccountStoreModel> accountStores = accountStoreModelFactory.getAccountStores(request);
// 748: If stormpath.web.idSite.enabled is false and stormpath.web.callback.enabled is false AND
// there are SAML directories mapped to the application, that is a configuration error.
if (!idSiteEnabled && !callbackEnabled && containsSaml(accountStores)) {
String errorMsg = "ID Site is disabled and callbacks are disabled, yet this application has SAML directories. Please enable callbacks or remove SAML directories.";
log.warn(errorMsg);
if (errors == null) {
errors = new ArrayList<>();
}
// only add to errors on GET, not POST
if (request.getMethod().equals(HttpMethod.GET.name())) {
errors.add(ErrorModel.builder().setStatus(HttpServletResponse.SC_OK).setMessage(errorMsg).build());
}
}
model.put("accountStores", accountStores);
if (isHtmlPreferred(request, response)) {
model.put("forgotPasswordEnabled", forgotPasswordEnabled);
model.put("forgotPasswordUri", forgotPasswordUri);
model.put("verifyEnabled", verifyEnabled);
model.put("verifyUri", verifyUri);
model.put("registerEnabled", registerEnabledResolver.get(request, response));
model.put("registerUri", registerUri);
model.put("samlUri", samlUri);
model.put("oauthStateToken", UUID.randomUUID().toString());
String status = request.getParameter("status");
if (status != null) {
model.put("status", loginFormStatusResolver.getStatusMessage(request, status));
}
}
}
/**
* Returns {@code true} if the specified list represents a SAML-based Account Provider, {@code false} otherwise.
*
* @param accountStores the list of account store models to check
* @return {@code true} if the specified list represents a SAML-based Account Provider, {@code false} otherwise.
* @see <a href="https://github.com/stormpath/stormpath-sdk-java/issues/748">Issue 748</a>
* @see <a href="https://github.com/stormpath/stormpath-sdk-java/issues/771">Issue 771</a>
* @since 1.0.0
*/
private boolean containsSaml(List<AccountStoreModel> accountStores) {
for (AccountStoreModel accountStore : accountStores) {
if ("saml".equalsIgnoreCase(accountStore.getProvider().getProviderId())) {
return true;
}
}
return false;
}
@Override
protected List<ErrorModel> toErrors(HttpServletRequest request, Form form, Exception e) {
return Collections.toList(errorModelFactory.toError(request, e));
}
@Override
protected ViewModel onValidSubmit(HttpServletRequest req, HttpServletResponse resp, Form form) throws Exception {
if (preLoginHandler != null) {
if (!preLoginHandler.handle(req, resp, null)) {
return null;
}
}
// check to see if account already exists in request
Account account = (Account) req.getAttribute(Account.class.getName());
if (account != null) {
AuthenticationResult authcResult = new TransientAuthenticationResult(account);
authenticationResultSaver.set(req, resp, authcResult);
eventPublisher.publish(new DefaultSuccessfulAuthenticationRequestEvent(req, resp, null, authcResult));
} else {
String usernameOrEmail = form.getFieldValue("login");
String password = form.getFieldValue("password");
req.login(usernameOrEmail, password);
AccessTokenResult result = (AccessTokenResult) req.getAttribute(OAuthTokenResolver.REQUEST_ATTR_NAME);
account = result.getAccount();
saveResult(req, resp, result);
}
if (postLoginHandler != null) {
if (!postLoginHandler.handle(req, resp, account)) {
return null;
}
}
if (isJsonPreferred(req, resp)) {
return new DefaultViewModel(view, java.util.Collections.singletonMap("account", accountModelFactory.toMap(account, java.util.Collections.<String>emptyList())));
}
//otherwise HTML view:
return new DefaultViewModel(getNextUri(req)).setRedirect(true);
}
protected void saveResult(HttpServletRequest request, HttpServletResponse response, AuthenticationResult result) {
authenticationResultSaver.set(request, response, result);
}
}