/
BasicAuthenticator.java
129 lines (105 loc) · 4.42 KB
/
BasicAuthenticator.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
/*
* (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Contributors:
* Nuxeo - initial API and implementation
*
* $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
*/
package org.nuxeo.ecm.platform.ui.web.auth.plugins;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.nuxeo.common.utils.Base64;
import org.nuxeo.ecm.platform.api.login.UserIdentificationInfo;
import org.nuxeo.ecm.platform.ui.web.auth.interfaces.NuxeoAuthenticationPlugin;
public class BasicAuthenticator implements NuxeoAuthenticationPlugin {
protected static final String REALM_NAME_KEY = "RealmName";
protected static final String FORCE_PROMPT_KEY = "ForcePromptURL";
protected static final String AUTO_PROMPT_KEY = "AutoPrompt";
protected static final String PROMPT_URL_KEY = "PromptUrl";
protected static final String DEFAULT_REALMNAME = "Nuxeo 5";
protected static final String BA_HEADER_NAME = "WWW-Authenticate";
protected String realName;
protected Boolean autoPrompt = false;
protected List<String> forcePromptURLs;
public Boolean handleLoginPrompt(HttpServletRequest httpRequest,
HttpServletResponse httpResponse, String baseURL) {
try {
String baHeader = "Basic realm=\"" + realName + '\"';
httpResponse.addHeader(BA_HEADER_NAME, baHeader);
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
}
public UserIdentificationInfo handleRetrieveIdentity(
HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
String auth = httpRequest.getHeader("authorization");
if (auth != null && auth.toLowerCase().startsWith("basic")) {
int idx = auth.indexOf(' ');
String b64userPassword = auth.substring(idx + 1);
byte[] clearUp = Base64.decode(b64userPassword);
String userCredentials = new String(clearUp);
int idxOfColon = userCredentials.indexOf(':');
if (idxOfColon > 0 && idxOfColon < userCredentials.length() - 1) {
String username = userCredentials.substring(0, idxOfColon);
String password = userCredentials.substring(idxOfColon + 1);
return new UserIdentificationInfo(username, password);
} else {
return null;
}
}
return null;
}
public Boolean needLoginPrompt(HttpServletRequest httpRequest) {
if (autoPrompt) {
return true;
} else {
String requestedURI = httpRequest.getRequestURI();
String context = httpRequest.getContextPath() + '/';
requestedURI = requestedURI.substring(context.length());
for (String prefixURL : forcePromptURLs) {
if (requestedURI.startsWith(prefixURL)) {
return true;
}
}
return false;
}
}
public void initPlugin(Map<String, String> parameters) {
if (parameters.containsKey(REALM_NAME_KEY)) {
realName = parameters.get(REALM_NAME_KEY);
} else {
realName = DEFAULT_REALMNAME;
}
if (parameters.containsKey(AUTO_PROMPT_KEY)) {
autoPrompt = parameters.get(AUTO_PROMPT_KEY).equalsIgnoreCase(
"true");
}
forcePromptURLs = new ArrayList<String>();
for (String key : parameters.keySet()) {
if (key.startsWith(FORCE_PROMPT_KEY)) {
forcePromptURLs.add(parameters.get(key));
}
}
}
public List<String> getUnAuthenticatedURLPrefix() {
return null;
}
}