aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/api/resource/UserResource.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/api/resource/UserResource.java')
-rw-r--r--src/main/java/org/traccar/api/resource/UserResource.java57
1 files changed, 42 insertions, 15 deletions
diff --git a/src/main/java/org/traccar/api/resource/UserResource.java b/src/main/java/org/traccar/api/resource/UserResource.java
index e41ebbe61..47ea9b07c 100644
--- a/src/main/java/org/traccar/api/resource/UserResource.java
+++ b/src/main/java/org/traccar/api/resource/UserResource.java
@@ -15,6 +15,11 @@
*/
package org.traccar.api.resource;
+import com.warrenstrange.googleauth.GoogleAuthenticator;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.ws.rs.DELETE;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.core.Context;
import org.traccar.api.BaseObjectResource;
import org.traccar.config.Config;
import org.traccar.config.Keys;
@@ -28,18 +33,17 @@ import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Request;
-import javax.annotation.security.PermitAll;
-import javax.inject.Inject;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
+import jakarta.annotation.security.PermitAll;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
import java.util.Collection;
-import java.util.Date;
@Path("users")
@Produces(MediaType.APPLICATION_JSON)
@@ -49,6 +53,9 @@ public class UserResource extends BaseObjectResource<User> {
@Inject
private Config config;
+ @Context
+ private HttpServletRequest request;
+
public UserResource() {
super(User.class);
}
@@ -91,11 +98,11 @@ public class UserResource extends BaseObjectResource<User> {
if (!permissionsService.getServer().getRegistration()) {
throw new SecurityException("Registration disabled");
}
- entity.setDeviceLimit(config.getInteger(Keys.USERS_DEFAULT_DEVICE_LIMIT));
- int expirationDays = config.getInteger(Keys.USERS_DEFAULT_EXPIRATION_DAYS);
- if (expirationDays > 0) {
- entity.setExpirationTime(new Date(System.currentTimeMillis() + expirationDays * 86400000L));
+ if (permissionsService.getServer().getBoolean(Keys.WEB_TOTP_FORCE.getKey())
+ && entity.getTotpKey() == null) {
+ throw new SecurityException("One-time password key is required");
}
+ UserUtil.setUserDefaults(entity, config);
}
}
@@ -117,4 +124,24 @@ public class UserResource extends BaseObjectResource<User> {
return Response.ok(entity).build();
}
+ @Path("{id}")
+ @DELETE
+ public Response remove(@PathParam("id") long id) throws Exception {
+ Response response = super.remove(id);
+ if (getUserId() == id) {
+ request.getSession().removeAttribute(SessionResource.USER_ID_KEY);
+ }
+ return response;
+ }
+
+ @Path("totp")
+ @PermitAll
+ @POST
+ public String generateTotpKey() throws StorageException {
+ if (!permissionsService.getServer().getBoolean(Keys.WEB_TOTP_ENABLE.getKey())) {
+ throw new SecurityException("One-time password is disabled");
+ }
+ return new GoogleAuthenticator().createCredentials().getKey();
+ }
+
}