118 lines
3.9 KiB
Java
118 lines
3.9 KiB
Java
package eu.dnetlib.uoaauthorizationlibrary.utils;
|
|
|
|
import com.google.gson.Gson;
|
|
import eu.dnetlib.uoaauthorizationlibrary.configuration.SecurityConfig;
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.servlet.http.Cookie;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.StringReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
|
|
@Component
|
|
public class AuthorizationUtils {
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
private SecurityConfig securityConfig;
|
|
|
|
@Autowired
|
|
AuthorizationUtils(SecurityConfig securityConfig) {
|
|
this.securityConfig = securityConfig;
|
|
}
|
|
|
|
public String getToken(HttpServletRequest request) {
|
|
return this.getCookie(request, "AccessToken");
|
|
}
|
|
|
|
public boolean checkCookies(HttpServletRequest request) {
|
|
boolean valid = true;
|
|
String cookieValue = this.getCookie(request, "AccessToken");
|
|
if (cookieValue == null || cookieValue.isEmpty()) {
|
|
log.info("no cookie available ");
|
|
valid = false;
|
|
}
|
|
return valid;
|
|
}
|
|
|
|
private String getCookie(HttpServletRequest request, String cookieName) {
|
|
if (request.getCookies() == null) {
|
|
return null;
|
|
}
|
|
for (Cookie c : request.getCookies()) {
|
|
if (c.getName().equals(cookieName)) {
|
|
return c.getValue();
|
|
}
|
|
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public UserInfo getUserInfo(String accessToken) {
|
|
String url = securityConfig.getUserInfoUrl() + accessToken;
|
|
try {
|
|
URL obj = new URL(url);
|
|
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
|
if (con.getResponseCode() != 200) {
|
|
log.debug("User info response code is: " + con.getResponseCode());
|
|
return null;
|
|
}
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
StringBuilder response = new StringBuilder();
|
|
String inputLine;
|
|
while ((inputLine = in.readLine()) != null) {
|
|
response.append(inputLine).append("\n");
|
|
}
|
|
in.close();
|
|
return json2UserInfo(response.toString());
|
|
} catch (Exception e) {
|
|
log.error("An error occured while trying to fetch user info ", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private UserInfo json2UserInfo(String json) {
|
|
if (json == null) {
|
|
return null;
|
|
}
|
|
BufferedReader br = new BufferedReader(new StringReader(json));
|
|
//convert the json string back to object
|
|
Gson gson = new Gson();
|
|
UserInfo userInfo = null;
|
|
try {
|
|
userInfo = gson.fromJson(br, UserInfo.class);
|
|
} catch (Exception e) {
|
|
log.debug("Error in parsing json response. Given json is : " + json, e);
|
|
return null;
|
|
}
|
|
try {
|
|
if (userInfo != null && userInfo.getEdu_person_entitlements() != null) {
|
|
for (int i = 0; i < userInfo.getEdu_person_entitlements().size(); i++) {
|
|
String role = userInfo.getEdu_person_entitlements().get(i);
|
|
role = role.split(":")[role.split(":").length - 1];
|
|
role = role.replace("+", " ");
|
|
userInfo.getEdu_person_entitlements().set(i, role);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
log.debug("Error in parsing Edu_person_entitlements : ", e);
|
|
return null;
|
|
}
|
|
return userInfo;
|
|
}
|
|
|
|
public boolean isAuthorized(String token) {
|
|
UserInfo userInfo = getUserInfo(token);
|
|
if (userInfo != null) {
|
|
return true;
|
|
} else {
|
|
log.debug(" User has no Valid UserInfo");
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|