Commit 0de8fa50 authored by Valentin Suhnjov's avatar Valentin Suhnjov

Merge branch 'feature/remove-authentication' into develop

parents f3902ba6 df6998e1
package ee.eesti.riha.rest.auth;
import java.util.List;
import ee.eesti.riha.rest.model.readonly.Role_right;
// TODO: Auto-generated Javadoc
/**
* The Class AuthInfo.
*/
/**
* @author Praktikant
*
*/
public class AuthInfo {
private String user_code;
private String user_name;
private String org_code;
private String org_name;
private String role_code;
private String role_name;
private String token;
private List<SimpleRoleRight> role_right;
public static final AuthInfo DEFAULT = new AuthInfo("UNAUTHORIZED", "UNAUTHORIZED", "DEFAULT");
/**
* Instantiates a new auth info.
*/
public AuthInfo() {
}
/**
* Instantiates a new auth info.
*
* @param isikuKood the isiku kood
* @param asutus the asutus
* @param roll the roll
*/
public AuthInfo(String isikuKood, String asutus, String roll) {
this.user_code = isikuKood;
this.org_code = asutus;
this.role_code = roll;
}
/**
* Instantiates a new auth info.
*
* @param isikuKood the isiku kood
* @param asutus the asutus
* @param roll the roll
* @param token the token
*/
public AuthInfo(String isikuKood, String asutus, String roll, String token) {
this.user_code = isikuKood;
this.org_code = asutus;
this.role_code = roll;
this.token = token;
}
/**
* Convert 3rd party data to be suitable for rest api.
*
* @param authInfo3rdParty the auth info3rd party
*/
public AuthInfo(AuthInfo3rdParty authInfo3rdParty) {
this.user_code = authInfo3rdParty.getIsikuKood();
this.org_code = authInfo3rdParty.getAsutus();
this.role_code = authInfo3rdParty.getRoll();
this.token = authInfo3rdParty.getToken();
}
/**
* Gets the isiku kood.
*
* @return the isiku kood
*/
public String getUser_code() {
return user_code;
}
/**
* Sets the isiku kood.
*
* @param aUser_code the new user_code
*/
public void setUser_code(String aUser_code) {
user_code = aUser_code;
}
/**
* Gets the org_code.
*
* @return the org_code
*/
public String getOrg_code() {
return org_code;
}
/**
* Sets the org_code.
*
* @param aOrg_code the new org_code
*/
public void setOrg_code(String aOrg_code) {
org_code = aOrg_code;
}
/**
* Gets the role_code.
*
* @return the role_code
*/
public String getRole_code() {
return role_code;
}
/**
* Sets the role_code.
*
* @param aRole_code the new role_code
*/
public void setRole_code(String aRole_code) {
role_code = aRole_code;
}
/**
* Gets the token.
*
* @return the token
*/
public String getToken() {
return token;
}
/**
* Sets the token.
*
* @param aToken the new token
*/
public void setToken(String aToken) {
token = aToken;
}
/**
* Gets the user_name.
*
* @return the user_name
*/
public String getUser_name() {
return user_name;
}
/**
* Sets the user_name.
*
* @param user_name the new user_name
*/
public void setUser_name(String user_name) {
this.user_name = user_name;
}
/**
* Gets the org_name.
*
* @return the org_name
*/
public String getOrg_name() {
return org_name;
}
/**
* Sets the org_name.
*
* @param org_name the new org_name
*/
public void setOrg_name(String org_name) {
this.org_name = org_name;
}
/**
* Gets the role_name.
*
* @return the role_name
*/
public String getRole_name() {
return role_name;
}
/**
* Sets the role_name.
*
* @param role_name the new role_name
*/
public void setRole_name(String role_name) {
this.role_name = role_name;
}
/**
* Gets the role_right.
*
* @return the role_right
*/
public List<SimpleRoleRight> getRole_right() {
return role_right;
}
/**
* Sets the role_right.
*
* @param role_right the new role_right
*/
public void setRole_right(List<SimpleRoleRight> role_right) {
this.role_right = role_right;
}
public static class SimpleRoleRight {
private int create;
private int read;
private int update;
private int delete;
private int access_restriction;
private String kind;
public SimpleRoleRight(int create, int read, int update, int delete, int access_restriction, String kind) {
this.create = create;
this.read = read;
this.update = update;
this.delete = delete;
this.access_restriction = access_restriction;
this.kind = kind;
}
public SimpleRoleRight(Role_right role_right, String kindName) {
this(role_right.getCreate(), role_right.getRead(),
role_right.getUpdate(), role_right.getDelete(),
role_right.getAccess_restriction(), kindName);
}
public int getCreate() {
return create;
}
public int getRead() {
return read;
}
public int getUpdate() {
return update;
}
public int getDelete() {
return delete;
}
public int getAccess_restriction() {
return access_restriction;
}
public String getKind() {
return kind;
}
}
}
package ee.eesti.riha.rest.auth;
/**
* Authentication data that comes from 3rd party service
*
*/
public class AuthInfo3rdParty {
private String isikuKood;
private String asutus;
private String roll;
private String token;
/**
* Instantiates a new auth info.
*/
public AuthInfo3rdParty() {
}
/**
* Instantiates a new auth info.
*
* @param isikuKood the isiku kood
* @param asutus the asutus
* @param roll the roll
*/
public AuthInfo3rdParty(String isikuKood, String asutus, String roll) {
this.isikuKood = isikuKood;
this.asutus = asutus;
this.roll = roll;
}
/**
* Instantiates a new auth info.
*
* @param isikuKood the isiku kood
* @param asutus the asutus
* @param roll the roll
* @param token the token
*/
public AuthInfo3rdParty(String isikuKood, String asutus, String roll, String token) {
this.isikuKood = isikuKood;
this.asutus = asutus;
this.roll = roll;
this.token = token;
}
/**
* Gets the isiku kood.
*
* @return the isiku kood
*/
public String getIsikuKood() {
return isikuKood;
}
/**
* Sets the isiku kood.
*
* @param aIsikuKood the new isiku kood
*/
public void setIsikuKood(String aIsikuKood) {
isikuKood = aIsikuKood;
}
/**
* Gets the asutus.
*
* @return the asutus
*/
public String getAsutus() {
return asutus;
}
/**
* Sets the asutus.
*
* @param aAsutus the new asutus
*/
public void setAsutus(String aAsutus) {
asutus = aAsutus;
}
/**
* Gets the roll.
*
* @return the roll
*/
public String getRoll() {
return roll;
}
/**
* Sets the roll.
*
* @param aRoll the new roll
*/
public void setRoll(String aRoll) {
roll = aRoll;
}
/**
* Gets the token.
*
* @return the token
*/
public String getToken() {
return token;
}
/**
* Sets the token.
*
* @param aToken the new token
*/
public void setToken(String aToken) {
token = aToken;
}
}
package ee.eesti.riha.rest.auth;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
/**
* Service to connect to 3rd party authentication service
*
*/
@Produces(MediaType.APPLICATION_JSON + "; charset=UTF-8")
public interface AuthService {
/**
* Checks if token is valid.
*
* @param sessionId the session id
* @return authenticated user if valid
*/
@GET
Object isValid(@QueryParam(value = "sessionId") String sessionId);
}
package ee.eesti.riha.rest.auth;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
// TODO: Auto-generated Javadoc
/**
* Fake implementation of AuthService for testing.
*/
@Component
public class AuthServiceImpl implements AuthService {
private static final Logger LOG = LoggerFactory.getLogger(AuthServiceImpl.class);
/**
* Instantiates a new auth service impl.
*/
public AuthServiceImpl() {
LOG.info("AUTH constructor");
}
/**
* Instantiates a new auth service impl.
*
* @param s the s
*/
public AuthServiceImpl(String s) {
LOG.info("AUTH constructor :: " + s);
}
/*
* (non-Javadoc)
*
* @see ee.eesti.riha.rest.auth.AuthService#isValid(java.lang.String)
*/
@Override
public Object isValid(String token) {
LOG.info("AUTH isValid called");
if (StringUtils.isEmpty(token)) {
return null;
}
// return new Object();
return new AuthInfo("35512121234", "Jaan Mets", "70000833", "test");
}
}
package ee.eesti.riha.rest.auth;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import ee.eesti.riha.rest.util.PropsReader;
// TODO: Auto-generated Javadoc
/**
* The Class AuthServiceProvider.
*/
public final class AuthServiceProvider {
// public static final String AUTH_SERVICE_URL = "http://localhost:1234";
public static final String AUTH_SERVICE_URL = PropsReader.get("AUTH_SERVICE_URL");
private static AuthServiceProvider instance = new AuthServiceProvider();
/**
* Instantiates a new auth service provider.
*/
private AuthServiceProvider() {
}
/**
* Gets the single instance of AuthServiceProvider.
*
* @return single instance of AuthServiceProvider
*/
public static AuthServiceProvider getInstance() {
return instance;
}
private AuthService authService;
/**
* Gets the.
*
* @param url the url
* @return the auth service
*/
private AuthService get(String url) {
if (authService == null) {
authService = create(url);
}
return authService;
}
/**
* Gets the.
*
* @return the auth service
*/
public AuthService get() {
return get(AUTH_SERVICE_URL);
// use fake
// return get(null);
}
/**
* Sets the.
*
* @param aAuthService the auth service
*/
public void set(AuthService aAuthService) {
this.authService = aAuthService;
}
/**
* Creates the.
*
* @param url the url
* @return the auth service
*/
private static AuthService create(String url) {
if (StringUtils.isEmpty(url)) {
return new AuthServiceImpl();
} else {
return JAXRSClientFactory.create(url, AuthService.class);
}
}
}
package ee.eesti.riha.rest.auth;
/**
* The Interface TokenStore.
*/
public interface TokenStore {
/**
* Adds the token.
*
* @param token the token
* @param authInfo the auth info
*/
void addToken(String token, AuthInfo authInfo);
/**
* Return personCode corresponding to token or null if token does not exist.
*
* @param token the token
* @return the auth info
*/
AuthInfo tokenExists(String token);
/**
* Clear.
*/
void clear();
}
package ee.eesti.riha.rest.auth;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import ee.eesti.riha.rest.logic.Finals;
import ee.eesti.riha.rest.logic.util.StringHelper;
import ee.eesti.riha.rest.util.PropsReader;
// TODO: Auto-generated Javadoc
/**
* The Class TokenStoreImpl.
*/
@Component
public class TokenStoreImpl implements TokenStore {
// key token
private static Map<String, AuthInfo> tokenStorage = new HashMap<>();
// private static AuthInfo testAuthInfo = new AuthInfo("TEST_ISIKUKOOD", "TEST_ASUTUS", "TEST_ROLL");
private static AuthInfo testAuthInfo = new AuthInfo("TEST_ISIKUKOOD", "TEST_ASUTUS", "ROLL_RIHA_ADMINISTRAATOR");
private static boolean isTest = false;
private static final int NUM_OF_TIMEOUT_MAPS = 3;
// 3 maps: 1 to be deleted, 1 buffer, 1 where to put new ones
private static List<Map<String, String>> timeOutStorage = new ArrayList<>(NUM_OF_TIMEOUT_MAPS);
// public static final long TIME_OUT_IN_MS = 3600 * 1000L;
public static final long TIME_OUT_IN_MS = Long.valueOf(PropsReader.get("TIME_OUT_IN_MS"));
private static final int MAX_INDEX = 2;
private static long timeOutInMS = TIME_OUT_IN_MS;
private static int saveIndex = 0;
private static int counter = 0;
private static Date startNew = new Date();
private static final Logger LOG = LoggerFactory.getLogger(TokenStoreImpl.class);
/**
* Next.
*
* @param x the x
* @return the int
*/
public static int next(int x) {
if (x + 1 > MAX_INDEX) {
return 0;
} else {
return x + 1;
}
}
/**
* Previous.
*
* @param x the x
* @return the int
*/
public static int previous(int x) {
if (x - 1 < 0) {
return MAX_INDEX;
} else {
return x - 1;
}
}
static {
timeOutStorage.add(new HashMap<String, String>());
timeOutStorage.add(new HashMap<String, String>());
timeOutStorage.add(new HashMap<String, String>());
}
// LOGIC
// 1. token comes in
// 2. save token in tokenStorage
// 3. save token in timeOutStorage.get(saveIndex)
// 4. if timeout amount of time has passed then saveindex++
// 5. if timeout passes 2nd time then saveIndex++
// and empty timeOutStorage(saveIndex.previous.prevuois)
// 6. after that every timeout one will be emptied
// 7. if tokenExists called then save timeOut in new, delete in old
// 8. during emptying corresponding entries must be deleted in tokenStorage
// 9. clear clears all
/**
* List put and remove old.
*
* @param token the token
*/
private static void listPutAndRemoveOld(String token) {
String existing = timeOutStorage.get(saveIndex).put(token, token);
String removed = timeOutStorage.get(previous(saveIndex)).remove(token);
LOG.info("EXISTING: " + existing);
LOG.info("REMOVED: " + removed);
}
/**