Skip to content

Commit 3a15709

Browse files
committed
Added /$login path to basic auth to force login when anonymous access is allowed
1 parent e561b23 commit 3a15709

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
**New Features**
66
* Added support for headers in JSON-Batch requets, enabling JSON-Patch requests in JSON-Batch requests.
7+
* Added `/$login` path to basic auth to force login when anonymous access is allowed.
78

89
**Internal changes & Bugfixes**
9-
* Fixed #1754: resultTime/phenomenonTime mixup in MultiDatastream
10+
* Fixed #1754: resultTime/phenomenonTime mixup in MultiDatastream.
11+
* Improved loading speed of custom data models.
1012

1113

1214
## Release version 2.2.0

FROST-Server.Auth.Basic/src/main/java/de/fraunhofer/iosb/ilt/frostserver/auth/basic/BasicAuthFilter.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class BasicAuthFilter implements Filter {
8282
private static final String AUTHORIZATION_HEADER = "Authorization";
8383
private static final String AUTHORIZATION_REQUIRED_HEADER = "WWW-Authenticate";
8484
private static final String BASIC_PREFIX = "Basic ";
85+
private static final String LOGIN_PATH = "/$login";
8586
private static final UserData USER_DATA_NO_USER = new UserData(null, null);
8687

8788
private boolean allowAnonymous;
@@ -94,6 +95,8 @@ public class BasicAuthFilter implements Filter {
9495

9596
private String roleAdmin;
9697

98+
private String serviceRootUrl;
99+
97100
@Override
98101
public void init(FilterConfig filterConfig) throws ServletException {
99102
LOGGER.info("Turning on Basic authentication.");
@@ -117,6 +120,7 @@ public void init(FilterConfig filterConfig) throws ServletException {
117120
CoreSettings coreSettings = (CoreSettings) attribute;
118121
Settings authSettings = coreSettings.getAuthSettings();
119122

123+
serviceRootUrl = coreSettings.getQueryDefaults().getServiceRootUrl();
120124
databaseHandler = DatabaseHandler.getInstance(coreSettings);
121125
String realmName = authSettings.get(TAG_AUTH_REALM_NAME, BasicAuthProvider.class);
122126
authHeaderValue = "Basic realm=\"" + realmName + "\", charset=\"UTF-8\"";
@@ -181,26 +185,41 @@ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain
181185
final HttpServletRequest request = (HttpServletRequest) req;
182186
final HttpServletResponse response = (HttpServletResponse) resp;
183187

184-
final HttpMethod method;
185-
try {
186-
method = HttpMethod.valueOf(request.getMethod().toUpperCase());
187-
} catch (IllegalArgumentException exc) {
188-
LOGGER.debug("Rejecting request: Unknown method: {}.", request.getMethod());
189-
LOGGER.trace("", exc);
190-
throwAuthRequired(response);
191-
return;
192-
}
193-
194188
UserData userData = findCredentials(request);
189+
String pathInfo = request.getPathInfo();
190+
if (pathInfo == null) {
191+
pathInfo = "";
192+
}
195193

194+
if (pathInfo.endsWith(LOGIN_PATH)) {
195+
if (userData == USER_DATA_NO_USER) {
196+
// Login path requested, force login.
197+
throwAuthRequired(response);
198+
return;
199+
} else {
200+
response.sendRedirect(serviceRootUrl + pathInfo.substring(0, pathInfo.length() - LOGIN_PATH.length()));
201+
return;
202+
}
203+
}
196204
if (authenticateOnly) {
197205
if (!allowAnonymous && userData == USER_DATA_NO_USER) {
198206
// We only authenticate, there is no user, but we don't allow anonymous.
199207
throwAuthRequired(response);
208+
return;
200209
} else {
201210
boolean admin = userData.roles.contains(roleAdmin);
202211
chain.doFilter(new RequestWrapper(request, new PrincipalExtended(userData.userName, admin, userData.roles)), response);
212+
return;
203213
}
214+
}
215+
216+
final HttpMethod method;
217+
try {
218+
method = HttpMethod.valueOf(request.getMethod().toUpperCase());
219+
} catch (IllegalArgumentException exc) {
220+
LOGGER.debug("Rejecting request: Unknown method: {}.", request.getMethod());
221+
LOGGER.trace("", exc);
222+
throwAuthRequired(response);
204223
return;
205224
}
206225

0 commit comments

Comments
 (0)