aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/api/SecurityRequestFilter.java46
-rw-r--r--src/org/traccar/api/UserPrincipal.java6
-rw-r--r--src/org/traccar/api/resource/ServerResource.java49
-rw-r--r--src/org/traccar/web/WebServer.java4
4 files changed, 86 insertions, 19 deletions
diff --git a/src/org/traccar/api/SecurityRequestFilter.java b/src/org/traccar/api/SecurityRequestFilter.java
index e6c02bfc4..d4f685000 100644
--- a/src/org/traccar/api/SecurityRequestFilter.java
+++ b/src/org/traccar/api/SecurityRequestFilter.java
@@ -20,12 +20,15 @@ import org.traccar.api.resource.SessionResource;
import org.traccar.model.User;
import java.nio.charset.Charset;
+import java.security.Security;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
+import javax.ws.rs.core.SecurityContext;
import javax.xml.bind.DatatypeConverter;
public class SecurityRequestFilter implements ContainerRequestFilter {
@@ -48,25 +51,40 @@ public class SecurityRequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) {
- if (requestContext.getUriInfo().getPath().equals("session")) {
+ String sessionPath = SessionResource.class.getAnnotation(Path.class).value();
+ if (sessionPath.equals(requestContext.getUriInfo().getPath())) {
return;
}
- long userId = (Long) req.getSession().getAttribute(SessionResource.USER_ID_KEY);
- // TODO use session
+ SecurityContext securityContext = null;
- try {
- String[] auth = decodeBasicAuth(requestContext.getHeaderString(AUTHORIZATION_HEADER));
- User user = Context.getDataManager().login(auth[0], auth[1]);
- if (user != null) {
- requestContext.setSecurityContext(
- new UserSecurityContext(new UserPrincipal(user.getId(), user.getName())));
- } else {
- throw new WebApplicationException(
- Response.status(Response.Status.UNAUTHORIZED).header(WWW_AUTHENTICATE, BASIC_REALM).build());
+ String authHeader = requestContext.getHeaderString(AUTHORIZATION_HEADER);
+ if (authHeader != null) {
+
+ try {
+ String[] auth = decodeBasicAuth(authHeader);
+ User user = Context.getDataManager().login(auth[0], auth[1]);
+ if (user != null) {
+ securityContext = new UserSecurityContext(new UserPrincipal(user.getId()));
+ }
+ } catch (SQLException e) {
+ throw new WebApplicationException(e);
+ }
+
+ } else if (req.getSession() != null) {
+
+ Long userId = (Long) req.getSession().getAttribute(SessionResource.USER_ID_KEY);
+ if (userId != null) {
+ securityContext = new UserSecurityContext(new UserPrincipal(userId));
}
- } catch (SQLException e) {
- throw new WebApplicationException(e);
+
+ }
+
+ if (securityContext != null) {
+ requestContext.setSecurityContext(securityContext);
+ } else {
+ throw new WebApplicationException(
+ Response.status(Response.Status.UNAUTHORIZED).header(WWW_AUTHENTICATE, BASIC_REALM).build());
}
}
diff --git a/src/org/traccar/api/UserPrincipal.java b/src/org/traccar/api/UserPrincipal.java
index d858b6f47..25fcdb233 100644
--- a/src/org/traccar/api/UserPrincipal.java
+++ b/src/org/traccar/api/UserPrincipal.java
@@ -19,12 +19,10 @@ import java.security.Principal;
public class UserPrincipal implements Principal {
- private String name;
private long userId;
- public UserPrincipal(long userId, String name) {
+ public UserPrincipal(long userId) {
this.userId = userId;
- this.name = name;
}
public Long getUserId() {
@@ -33,7 +31,7 @@ public class UserPrincipal implements Principal {
@Override
public String getName() {
- return name;
+ return null;
}
}
diff --git a/src/org/traccar/api/resource/ServerResource.java b/src/org/traccar/api/resource/ServerResource.java
new file mode 100644
index 000000000..fc04ee248
--- /dev/null
+++ b/src/org/traccar/api/resource/ServerResource.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * 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 org.traccar.api.resource;
+
+import org.traccar.Context;
+import org.traccar.api.BaseResource;
+import org.traccar.model.Server;
+import org.traccar.model.User;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.sql.SQLException;
+
+@Path("server")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+public class ServerResource extends BaseResource {
+
+ @GET
+ public Server get() {
+ try {
+ return Context.getDataManager().getServer();
+ } catch (SQLException e) {
+ throw new WebApplicationException(e);
+ }
+ }
+
+}
diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java
index f139b1c7a..ad40e246f 100644
--- a/src/org/traccar/web/WebServer.java
+++ b/src/org/traccar/web/WebServer.java
@@ -30,6 +30,7 @@ import org.traccar.Config;
import org.traccar.api.CorsResponseFilter;
import org.traccar.api.SecurityRequestFilter;
import org.traccar.api.resource.DeviceResource;
+import org.traccar.api.resource.ServerResource;
import org.traccar.api.resource.SessionResource;
import org.traccar.api.resource.UserResource;
import org.traccar.helper.Log;
@@ -137,7 +138,8 @@ public class WebServer {
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(SecurityRequestFilter.class);
resourceConfig.register(CorsResponseFilter.class);
- resourceConfig.registerClasses(SessionResource.class, DeviceResource.class, UserResource.class);
+ resourceConfig.registerClasses(
+ ServerResource.class, SessionResource.class, DeviceResource.class, UserResource.class);
ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
ServletHolder servletHolder = new ServletHolder(new ServletContainer(resourceConfig));
servletHandler.addServlet(servletHolder, "/rest/*");