Skip to content
Snippets Groups Projects
Commit bf707a77 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

new REST API method added for users: logout, tokenStatus

parent ed91ff6b
No related branches found
No related tags found
1 merge request!5Frontend refactor
package lcsb.mapviewer.api.controller; package lcsb.mapviewer.api.controller;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -10,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -10,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.api.BaseController; import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.Configuration; import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.services.SecurityException;
import lcsb.mapviewer.services.interfaces.IUserService; import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.view.AuthenticationToken; import lcsb.mapviewer.services.view.AuthenticationToken;
...@@ -22,12 +26,25 @@ public class UserController extends BaseController { ...@@ -22,12 +26,25 @@ public class UserController extends BaseController {
private IUserService userService; private IUserService userService;
@RequestMapping(value = "/login", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE }) @RequestMapping(value = "/login", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public AuthenticationToken greeting(@RequestParam(value = "login", defaultValue = Configuration.ANONYMOUS_LOGIN) String login, public AuthenticationToken login(@RequestParam(value = "login", defaultValue = Configuration.ANONYMOUS_LOGIN) String login,
@RequestParam(value = "password", required = false) String password) { @RequestParam(value = "password", required = false) String password) {
AuthenticationToken token = userService.login(login, password); AuthenticationToken token = userService.login(login, password);
return token; return token;
} }
@RequestMapping(value = "/tokenStatus", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public AuthenticationToken tokenSatus(@RequestParam(value = "token", required = false) String token) throws SecurityException {
return userService.getToken(token);
}
@RequestMapping(value = "/logout", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, String> logout(@RequestParam(value = "token", required = false) String token) throws SecurityException {
userService.logout(token);
Map<String, String> response = new HashMap<>();
response.put("status", "OK");
return response;
}
/** /**
* @return the userService * @return the userService
* @see #userService * @see #userService
......
...@@ -540,10 +540,11 @@ public class UserService implements IUserService { ...@@ -540,10 +540,11 @@ public class UserService implements IUserService {
return result; return result;
} }
public void logout(AuthenticationToken result) { @Override
public void logout(AuthenticationToken token) {
synchronized (authenticationTokens) { synchronized (authenticationTokens) {
authenticationTokens.remove(result.getId()); authenticationTokens.remove(token.getId());
authenticatedUsers.remove(result); authenticatedUsers.remove(token);
} }
} }
...@@ -551,4 +552,10 @@ public class UserService implements IUserService { ...@@ -551,4 +552,10 @@ public class UserService implements IUserService {
public boolean userHasPrivilege(AuthenticationToken token, PrivilegeType type, Object object) { public boolean userHasPrivilege(AuthenticationToken token, PrivilegeType type, Object object) {
return userHasPrivilege(getUserByToken(token), type, object); return userHasPrivilege(getUserByToken(token), type, object);
} }
@Override
public void logout(String tokenString) throws SecurityException {
AuthenticationToken token = getToken(tokenString);
logout(token);
}
} }
...@@ -249,4 +249,8 @@ public interface IUserService { ...@@ -249,4 +249,8 @@ public interface IUserService {
AuthenticationToken getToken(String token) throws SecurityException; AuthenticationToken getToken(String token) throws SecurityException;
boolean userHasPrivilege(AuthenticationToken token, PrivilegeType type, Object object); boolean userHasPrivilege(AuthenticationToken token, PrivilegeType type, Object object);
void logout(String tokenString) throws SecurityException;
void logout(AuthenticationToken token);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment