Commit 76f0b8cb authored by Valentin Suhnjov's avatar Valentin Suhnjov

RIHAKB-130. Remove dead code

parent 445c43e9
......@@ -64,18 +64,6 @@ public class AuthInfo {
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.
*
......
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.logic;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import ee.eesti.riha.rest.auth.AuthInfo;
import ee.eesti.riha.rest.auth.AuthInfo.SimpleRoleRight;
import ee.eesti.riha.rest.auth.AuthInfo3rdParty;
import ee.eesti.riha.rest.dao.KindRepository;
import ee.eesti.riha.rest.dao.Role_rightRepository;
import ee.eesti.riha.rest.error.RihaRestException;
import ee.eesti.riha.rest.logic.util.JsonHelper;
import ee.eesti.riha.rest.logic.util.QueryHolder;
import ee.eesti.riha.rest.model.readonly.Kind;
import ee.eesti.riha.rest.model.readonly.Role_right;
@Component
public class AuthInfoCreator {
@Autowired
Role_rightRepository roleRepository;
@Autowired
KindRepository kindRepository;
@Autowired
ChangeLogic changeLogic;
public AuthInfo convert(AuthInfo3rdParty authInfo3rdParty) throws RihaRestException {
System.out.println(JsonHelper.GSON.toJson(authInfo3rdParty));
AuthInfo authInfo = new AuthInfo(authInfo3rdParty);
// no role given means not authenticated
if (StringUtils.isEmpty(authInfo3rdParty.getRoll()) || authInfo.getRole_code().equals("null")) {
authInfo.setRole_code("DEFAULT");
}
authInfo.setRole_name(authInfo.getRole_code());
// add names to authInfo
findNamesForCodes(authInfo3rdParty, authInfo);
List<SimpleRoleRight> simpleRoles = findRoles(authInfo);
authInfo.setRole_right(simpleRoles);
return authInfo;
}
private void findNamesForCodes(AuthInfo3rdParty authInfo3rdParty, AuthInfo authInfo) throws RihaRestException {
String personCode = StringUtils.defaultString(authInfo3rdParty.getIsikuKood());
String organizationCode = StringUtils.defaultString(authInfo3rdParty.getAsutus());
String json = "{\"persons\":[\"" + personCode + "\"], "
+ "\"organizations\":[\"" + organizationCode + "\"]}";
QueryHolder queryHolder = QueryHolder.create(JsonHelper.GSON, json);
Map<String, Map<String, String>> names = changeLogic.doGetNames(queryHolder);
authInfo.setOrg_name(names.get("organizations").get(authInfo3rdParty.getAsutus()));
authInfo.setUser_name(names.get("persons").get(authInfo3rdParty.getIsikuKood()));
}
public List<SimpleRoleRight> findRoles(AuthInfo authInfo) {
List<Role_right> roles = roleRepository.getByName(authInfo.getRole_code());
// unknown role, then grant authenticated role
if (roles.isEmpty()) {
roles = roleRepository.getByName("AUTHENTICATED");
}
List<SimpleRoleRight> simpleRoles = new ArrayList<>();
for (Role_right role : roles) {
Kind kind = kindRepository.getById(role.getKind_id());
SimpleRoleRight simpleRole = new SimpleRoleRight(role, kind.getName());
simpleRoles.add(simpleRole);
}
return simpleRoles;
}
}
package ee.eesti.riha.rest.logic;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.HttpHeaders;
import org.apache.commons.lang3.StringUtils;
......@@ -17,14 +7,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ee.eesti.riha.rest.auth.AuthInfo;
import ee.eesti.riha.rest.auth.AuthInfo3rdParty;
import ee.eesti.riha.rest.auth.AuthService;
import ee.eesti.riha.rest.auth.TokenStore;
import ee.eesti.riha.rest.error.ErrorCodes;
import ee.eesti.riha.rest.error.RihaRestError;
import ee.eesti.riha.rest.error.RihaRestException;
import ee.eesti.riha.rest.logic.util.JsonHelper;
import ee.eesti.riha.rest.logic.util.StringHelper;
// TODO: Auto-generated Javadoc
/**
......@@ -38,32 +24,6 @@ public final class TokenValidator {
}
/**
* Check whether HTTP headers are ok (currently checks only X-Auth-Token).
*
* @param httpHeaders the http headers
* @return true, if successful
* @throws RihaRestException the riha rest exception
*/
public static boolean areHeadersOk(HttpHeaders httpHeaders) throws RihaRestException {
if (!tokenNotEmpty(httpHeaders.getHeaderString(Finals.X_AUTH_TOKEN))) {
RihaRestError error = new RihaRestError();
error.setErrcode(ErrorCodes.NO_HTTP_AUTH_TOKEN_PROVIDED);
error.setErrmsg(ErrorCodes.NO_HTTP_AUTH_TOKEN_PROVIDED_MSG);
throw new RihaRestException(error);
}
if (!isTokenValid(httpHeaders.getHeaderString(Finals.X_AUTH_TOKEN))) {
RihaRestError error = new RihaRestError();
error.setErrcode(ErrorCodes.AUTH_TOKEN_INVALID);
error.setErrmsg(ErrorCodes.AUTH_TOKEN_INVALID_MSG);
throw new RihaRestException(error);
}
return true;
}
/**
* Gets the token.
*
......@@ -102,44 +62,6 @@ public final class TokenValidator {
return true;
}
/**
* Return user if token is valid, else throw exception.
*
* @param token the token
* @param service the service
* @return the object
* @throws RihaRestException the riha rest exception
*/
public static Object isTokenOk3rdParty(String token, AuthService service) throws RihaRestException {
// throws error if not ok
isTokenOk(token);
// AuthInfo user = null;
AuthInfo3rdParty user = null;
try {
InputStream is = (InputStream) service.isValid(token);
// user = getObjectFromClient(is, AuthInfo.class);
user = getObjectFromClient(is, AuthInfo3rdParty.class);
if (user == null) {
// if (user == null || areFieldsNull(user)) {
RihaRestError error = new RihaRestError();
error.setErrcode(ErrorCodes.THIRD_PARTY_AUTH_TOKEN_INVALID);
error.setErrmsg(ErrorCodes.THIRD_PARTY_AUTH_TOKEN_INVALID_MSG);
error.setErrtrace(token + " -> " + JsonHelper.GSON.toJson(user));
throw new RihaRestException(error);
}
LOG.info("TOKEN IS OK " + user);
} catch (RihaRestException e) {
throw e;
} catch (Exception e) {
// e.printStackTrace();
handleException(e);
}
return user;
}
/**
* Return personCode if token is valid, else throw exception.
*
......@@ -163,30 +85,6 @@ public final class TokenValidator {
return user;
}
/**
* Handle exception.
*
* @param e the e
* @throws RihaRestException the riha rest exception
*/
private static void handleException(Exception e) throws RihaRestException {
// e.printStackTrace();
LOG.info("token exception: " + e);
if (e.getClass() == ProcessingException.class
&& (e.getCause() != null && e.getCause().getClass() == ConnectException.class)
|| e.getClass() == NotFoundException.class) {
RihaRestError error = new RihaRestError();
error.setErrcode(ErrorCodes.CANT_CONNECT_TO_AUTH);
error.setErrmsg(ErrorCodes.CANT_CONNECT_TO_AUTH_MSG);
error.setErrtrace(e.toString());
throw new RihaRestException(error);
}
throw new RihaRestException(MyExceptionHandler.unmapped(e, "Viga autentimsteenusega"));
}
/**
* Checks if is token valid.
*
......@@ -211,51 +109,4 @@ public final class TokenValidator {
return false;
}
/**
* Are fields null.
*
* @param authInfo the auth info
* @return true, if successful
*/
private static boolean areFieldsNull(AuthInfo authInfo) {
return StringHelper
.multipleEquals("null", authInfo.getOrg_code(), authInfo.getRole_code(), authInfo.getUser_code());
}
/**
* Gets the object from client.
*
* @param <T> the generic type
* @param inputStream the input stream
* @param clazz the clazz
* @return the object from client
* @throws IOException Signals that an I/O exception has occurred.
*/
public static <T> T getObjectFromClient(InputStream inputStream, Class<T> clazz) throws IOException {
// riha authentication service returns ISO_8859_1
String json = readStream(inputStream, StandardCharsets.ISO_8859_1);
T result = JsonHelper.GSON.fromJson(json, clazz);
return result;
}
/**
* Read stream.
*
* @param inputStream the input stream
* @param charset the charset
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
*/
public static String readStream(InputStream inputStream, Charset charset) throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
LOG.info(sb.toString());
return sb.toString();
}
}
}
Markdown is supported
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