1
|
package eu.dnetlib.uoaauthorizationlibrary.utils;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import eu.dnetlib.uoaauthorizationlibrary.configuration.SecurityConfig;
|
5
|
import org.apache.log4j.Logger;
|
6
|
import org.springframework.beans.factory.annotation.Autowired;
|
7
|
import org.springframework.stereotype.Component;
|
8
|
|
9
|
import javax.servlet.http.Cookie;
|
10
|
import javax.servlet.http.HttpServletRequest;
|
11
|
import java.io.BufferedReader;
|
12
|
import java.io.InputStreamReader;
|
13
|
import java.io.StringReader;
|
14
|
import java.net.HttpURLConnection;
|
15
|
import java.net.URL;
|
16
|
|
17
|
@Component
|
18
|
public class AuthorizationUtils {
|
19
|
private final Logger log = Logger.getLogger(this.getClass());
|
20
|
private SecurityConfig securityConfig;
|
21
|
|
22
|
@Autowired
|
23
|
AuthorizationUtils(SecurityConfig securityConfig) {
|
24
|
this.securityConfig = securityConfig;
|
25
|
}
|
26
|
|
27
|
public String getToken(HttpServletRequest request) {
|
28
|
return this.getCookie(request, "AccessToken");
|
29
|
}
|
30
|
|
31
|
public boolean checkCookies(HttpServletRequest request) {
|
32
|
boolean valid = true;
|
33
|
String cookieValue = this.getCookie(request, "AccessToken");
|
34
|
if (cookieValue == null || cookieValue.isEmpty()) {
|
35
|
log.info("no cookie available ");
|
36
|
valid = false;
|
37
|
}
|
38
|
return valid;
|
39
|
}
|
40
|
|
41
|
private String getCookie(HttpServletRequest request, String cookieName) {
|
42
|
if (request.getCookies() == null) {
|
43
|
return null;
|
44
|
}
|
45
|
for (Cookie c : request.getCookies()) {
|
46
|
if (c.getName().equals(cookieName)) {
|
47
|
return c.getValue();
|
48
|
}
|
49
|
|
50
|
}
|
51
|
return null;
|
52
|
}
|
53
|
|
54
|
public UserInfo getUserInfo(String accessToken) {
|
55
|
String url = securityConfig.getUserInfoUrl() + accessToken;
|
56
|
try {
|
57
|
URL obj = new URL(url);
|
58
|
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
59
|
if (con.getResponseCode() != 200) {
|
60
|
log.debug("User info response code is: " + con.getResponseCode());
|
61
|
return null;
|
62
|
}
|
63
|
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
64
|
StringBuilder response = new StringBuilder();
|
65
|
String inputLine;
|
66
|
while ((inputLine = in.readLine()) != null) {
|
67
|
response.append(inputLine).append("\n");
|
68
|
}
|
69
|
in.close();
|
70
|
return json2UserInfo(response.toString());
|
71
|
} catch (Exception e) {
|
72
|
log.error("An error occured while trying to fetch user info ", e);
|
73
|
return null;
|
74
|
}
|
75
|
}
|
76
|
|
77
|
private UserInfo json2UserInfo(String json) {
|
78
|
if (json == null) {
|
79
|
return null;
|
80
|
}
|
81
|
BufferedReader br = new BufferedReader(new StringReader(json));
|
82
|
//convert the json string back to object
|
83
|
Gson gson = new Gson();
|
84
|
UserInfo userInfo = null;
|
85
|
try {
|
86
|
userInfo = gson.fromJson(br, UserInfo.class);
|
87
|
} catch (Exception e) {
|
88
|
log.debug("Error in parsing json response. Given json is : " + json, e);
|
89
|
return null;
|
90
|
}
|
91
|
try {
|
92
|
if (userInfo != null && userInfo.getEdu_person_entitlements() != null) {
|
93
|
for (int i = 0; i < userInfo.getEdu_person_entitlements().size(); i++) {
|
94
|
String role = userInfo.getEdu_person_entitlements().get(i);
|
95
|
role = role.split(":")[role.split(":").length - 1];
|
96
|
role = role.replace("+", " ");
|
97
|
userInfo.getEdu_person_entitlements().set(i, role);
|
98
|
}
|
99
|
}
|
100
|
} catch (Exception e) {
|
101
|
log.debug("Error in parsing Edu_person_entitlements : ", e);
|
102
|
return null;
|
103
|
}
|
104
|
return userInfo;
|
105
|
}
|
106
|
|
107
|
public boolean isAuthorized(String token) {
|
108
|
UserInfo userInfo = getUserInfo(token);
|
109
|
if (userInfo != null) {
|
110
|
return true;
|
111
|
} else {
|
112
|
log.debug(" User has no Valid UserInfo");
|
113
|
return false;
|
114
|
}
|
115
|
|
116
|
}
|
117
|
}
|