Commit bf707a77 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

new REST API method added for users: logout, tokenStatus

parent ed91ff6b
package lcsb.mapviewer.api.controller;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
......@@ -10,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.services.SecurityException;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.view.AuthenticationToken;
......@@ -22,12 +26,25 @@ public class UserController extends BaseController {
private IUserService userService;
@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) {
AuthenticationToken token = userService.login(login, password);
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
* @see #userService
......
......@@ -540,10 +540,11 @@ public class UserService implements IUserService {
return result;
}
public void logout(AuthenticationToken result) {
@Override
public void logout(AuthenticationToken token) {
synchronized (authenticationTokens) {
authenticationTokens.remove(result.getId());
authenticatedUsers.remove(result);
authenticationTokens.remove(token.getId());
authenticatedUsers.remove(token);
}
}
......@@ -551,4 +552,10 @@ public class UserService implements IUserService {
public boolean userHasPrivilege(AuthenticationToken token, PrivilegeType type, Object 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 {
AuthenticationToken getToken(String token) throws SecurityException;
boolean userHasPrivilege(AuthenticationToken token, PrivilegeType type, Object object);
void logout(String tokenString) throws SecurityException;
void logout(AuthenticationToken token);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment