1
|
package eu.dnetlib.openaire.rest.inputHandler;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import eu.dnetlib.openaire.rest.Authorization;
|
5
|
import eu.dnetlib.openaire.rest.UserInfo;
|
6
|
import io.jsonwebtoken.Claims;
|
7
|
import io.jsonwebtoken.Jwts;
|
8
|
import org.apache.log4j.BasicConfigurator;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.context.ApplicationContext;
|
11
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
12
|
|
13
|
import javax.xml.bind.DatatypeConverter;
|
14
|
import java.io.BufferedReader;
|
15
|
import java.io.IOException;
|
16
|
import java.io.InputStreamReader;
|
17
|
import java.io.StringReader;
|
18
|
import java.net.HttpURLConnection;
|
19
|
import java.net.URL;
|
20
|
|
21
|
/**
|
22
|
* Created by sofia on 20/4/2017.
|
23
|
*/
|
24
|
public class UserHandler{
|
25
|
private static Logger logger = Logger.getLogger(UserHandler.class);
|
26
|
|
27
|
String userInfoUrl = null;
|
28
|
public String getMail(String token) {
|
29
|
|
30
|
Claims claims = Jwts.parser()
|
31
|
.setSigningKey(DatatypeConverter.parseBase64Binary("my-very-secret"))
|
32
|
.parseClaimsJws(token).getBody();
|
33
|
|
34
|
return (String) claims.get("email");
|
35
|
|
36
|
}
|
37
|
|
38
|
public UserInfo getUserInfo(String accessToken){
|
39
|
String url=userInfoUrl+accessToken;
|
40
|
URL obj = null;
|
41
|
String responseStr=null;
|
42
|
logger.debug("User info url is "+url);
|
43
|
|
44
|
try {
|
45
|
obj = new URL(url);
|
46
|
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
47
|
logger.debug("User info response code is: " + con.getResponseCode());
|
48
|
if (con.getResponseCode() != 200) {
|
49
|
return null;
|
50
|
}
|
51
|
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
52
|
StringBuffer response = new StringBuffer();
|
53
|
String inputLine;
|
54
|
while ((inputLine = in.readLine()) != null) {
|
55
|
response.append(inputLine).append("\n");
|
56
|
}
|
57
|
in.close();
|
58
|
responseStr = response.toString();
|
59
|
}catch(Exception e){
|
60
|
logger.error("An error occured while trying to fetch user info ",e);
|
61
|
return null;
|
62
|
}
|
63
|
return json2UserInfo(responseStr);
|
64
|
}
|
65
|
|
66
|
private UserInfo json2UserInfo(String json) {
|
67
|
|
68
|
logger.debug("Try to create userInfo class from json: "+json);
|
69
|
if (json == null){
|
70
|
return null;
|
71
|
}
|
72
|
|
73
|
BufferedReader br = new BufferedReader(new StringReader(json));
|
74
|
//convert the json string back to object
|
75
|
Gson gson = new Gson();
|
76
|
UserInfo userInfo = null;
|
77
|
try {
|
78
|
userInfo = gson.fromJson(br, UserInfo.class);
|
79
|
}catch(Exception e){
|
80
|
logger.debug("Error in parsing json response. Given json is : "+json, e);
|
81
|
return null;
|
82
|
}
|
83
|
|
84
|
logger.debug("Original response.........: "+userInfo.toString());
|
85
|
try {
|
86
|
if(userInfo != null && userInfo.getEdu_person_entitlements() != null ) {
|
87
|
|
88
|
for (int i = 0; i < userInfo.getEdu_person_entitlements().size(); i++) {
|
89
|
String role = userInfo.getEdu_person_entitlements().get(i);
|
90
|
role = role.split(":")[role.split(":").length-1];
|
91
|
role = role.replace("+"," ");
|
92
|
logger.debug("Adding parsed role : "+role);
|
93
|
userInfo.getEdu_person_entitlements().set(i,role);
|
94
|
}
|
95
|
}
|
96
|
}catch(Exception e){
|
97
|
logger.debug("Error in parsing Edu_person_entitlements : ",e);
|
98
|
return null;
|
99
|
}
|
100
|
logger.debug("After handling roles : "+userInfo.toString());
|
101
|
|
102
|
|
103
|
return userInfo;
|
104
|
}
|
105
|
|
106
|
public String getUserInfoUrl() {
|
107
|
return userInfoUrl;
|
108
|
}
|
109
|
|
110
|
public void setUserInfoUrl(String userInfoUrl) {
|
111
|
this.userInfoUrl = userInfoUrl;
|
112
|
}
|
113
|
|
114
|
public static void main(String[] args) throws IOException {
|
115
|
// BasicConfigurator.configure();
|
116
|
// ApplicationContext context = new ClassPathXmlApplicationContext("eu/dnetlib/openaire/rest/springContext-claims-authorization.xml");
|
117
|
//
|
118
|
// Authorization authorization = context.getBean(Authorization.class);
|
119
|
// UserHandler userHandler = context.getBean(UserHandler.class);
|
120
|
// System.out.println("\n\nAdmin Roles "+authorization.getAdminRoles().size()+" "+authorization.getAdminRoles()+"\n\n\n");
|
121
|
// System.out.println("\n\ngetProjectCuratorRoles Roles "+authorization.getProjectCuratorRoles().size()+" "+authorization.getProjectCuratorRoles()+"\n\n\n");
|
122
|
// System.out.println("\n\ngetRegisteredRoles Roles "+authorization.getRegisteredRoles().size()+" "+authorization.getRegisteredRoles()+"\n\n\n");
|
123
|
// System.out.println("\n\nIs Admin::\n"+authorization.isRegistered("eyJraWQiOiJvaWRjIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiIwOTMxNzMwMTMyODMzNjMyQG9wZW5taW50ZWQuZXUiLCJhenAiOiIyNGU4MzE3Ni0xMzEyLTRiYTMtYmMwYi1mZmVlYmVhMTYwM2UiLCJpc3MiOiJodHRwczpcL1wvYWFpLm9wZW5taW50ZWQuZXVcL29pZGNcLyIsImV4cCI6MTQ5OTg4MjI4NCwiaWF0IjoxNDk5ODY3ODg0LCJqdGkiOiIxNzJiOWNkYS1kNmZjLTQ3MTgtYTI1My02YTJjYmNiYmI0ZWIifQ.XtKm96oOeRe0_7mzr_NPdFAG6JJN3dh9Oy6-bFHE59UaN3TMuZAYgLND49vryWK_m4s23F_1TvZAN7WMDnkx3YH6RqTUsMS57sJGIOoDg4eYSzgDfLriyaANqp1Fh2PotMXWuFZXmtKdyLYw2jfcnBXjfd7DkMHylsFWII88GQVRFujsrJfjr5DKcz09Ad_tF874r-Emq0ARxtSHjmjp56cKmiia9GcjOgMVRMdUgArVKPToiMoYizXyG51t3yOnYI6JI0batB3UjmocX-YXBJmLZfS3zqlaKuf_hHgcs09EQK_McJ_igpgZ9CeBUHyDzzITtniLmftKB7o_AfxaMw")
|
124
|
// +"\n\n\n");
|
125
|
// userHandler.json2UserInfo("{\"sub\":\"123456@openminted.eu\",\"name\":\"όνομα και επιθετο\",\"given_name\":\"όνομα\",\"family_name\":\"επίθετο\",\"email\":\"mail1@mail.com\",\"edu_person_entitlements\":[\"urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE+Super+Administrator\",\"urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE+User+Manager\"]}");
|
126
|
// UserInfo u = userHandler.json2UserInfo("{\"sub\":\"12345678@openminted.eu\",\"name\":\" a name\",\"given_name\":\"a\",\"family_name\":\"name\",\"email\":\"mail2@mail.com\",\"edu_person_scoped_affiliations\":[\"faculty@university\"],\"edu_person_entitlements\":[\"urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE+Super+Administrator\"]}");
|
127
|
// boolean b=authorization.isProjectCurator(u);
|
128
|
// System.out.println(b);
|
129
|
// userHandler.json2UserInfo("{\"sub\":\"1234567899@openminted.eu\",\"name\":\" test name\",\"given_name\":\"test\",\"family_name\":\"test surname\",\"email\":\"mail2@mail.com\",\"edu_person_scoped_affiliations\":[\"faculty@uni\"]}");
|
130
|
// userHandler.json2UserInfo(null);
|
131
|
// userHandler.json2UserInfo("mpla mpla mpla .... {}");
|
132
|
// userHandler.getUserInfo("eyJraWQiOiJvaWRjIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiIwOTMxNzMwMTMyODMzNjMyQG9wZW5taW50ZWQuZXUiLCJhenAiOiIyNGU4MzE3Ni0xMzEyLTRiYTMtYmMwYi1mZmVlYmVhMTYwM2UiLCJpc3MiOiJodHRwczpcL1wvYWFpLm9wZW5taW50ZWQuZXVcL29pZGNcLyIsImV4cCI6MTQ5ODQ4NTk3NiwiaWF0IjoxNDk4NDcxNTc2LCJqdGkiOiJkMWRlZjc1Yi00MTEyLTRiZDktYTIyNi0wZThhOWI2M2Y3MWQifQ.WVYOb_yO8OaxIIt2jRYEDQBhGGFRDTBw3DgtVV_smuN5yx1ScCj6aehLu3JKPSArme4m2SGF4TEGhpwNJkwhM2WapGtxmtuCmCzYIo_QlC1Yki9hr2OT2rXMcQsJCiKaBSf6pLue6Sn78GMB5yaUTvOQHRgidXGiZXH5lsuZUx15Q6Equ_wzond_rgP9mRheRkTyIFuvvg4PuzmudBc11Ty863vIIQtoWF7_p98zTbHxiNF9lLPwzPZKxDoQ8JeayQEC-jsWVLgxmp-h0jG_Ko5jFVVJeeosqMMucOrs2FT_NKHVYVqB6VVh0C6nOufeiLrNDeMUlDT4dAvKD2zE9w");
|
133
|
}
|
134
|
}
|