Project

General

Profile

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);
81
            return null;
82
        }
83
        logger.debug("Original response : "+userInfo.toString());
84

    
85
        if(userInfo != null && userInfo.getEdu_person_entitlements() != null ) {
86
            for (int i = 0; i < userInfo.getEdu_person_entitlements().size(); i++) {
87
                String role = userInfo.getEdu_person_entitlements().get(i);
88
                role = role.split("urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE\\+")[1];
89
                role = role.replace("+"," ");
90
                userInfo.getEdu_person_entitlements().set(i,role);
91
            }
92
        }
93
        logger.debug("After handling roles : "+userInfo.toString());
94

    
95

    
96
        return userInfo;
97
    }
98

    
99
    public String getUserInfoUrl() {
100
        return userInfoUrl;
101
    }
102

    
103
    public void setUserInfoUrl(String userInfoUrl) {
104
        this.userInfoUrl = userInfoUrl;
105
    }
106

    
107
    public static void main(String[] args) throws IOException {
108
        BasicConfigurator.configure();
109
        ApplicationContext context = new ClassPathXmlApplicationContext("eu/dnetlib/openaire/rest/springContext-claims-authorization.xml");
110

    
111
        Authorization authorization =  context.getBean(Authorization.class);
112
        UserHandler userHandler = context.getBean(UserHandler.class);
113
        System.out.println("\n\nAdmin Roles "+authorization.getAdminRoles().size()+"   "+authorization.getAdminRoles()+"\n\n\n");
114
        System.out.println("\n\ngetProjectCuratorRoles Roles "+authorization.getProjectCuratorRoles().size()+"   "+authorization.getProjectCuratorRoles()+"\n\n\n");
115
        System.out.println("\n\ngetRegisteredRoles Roles "+authorization.getRegisteredRoles().size()+"   "+authorization.getRegisteredRoles()+"\n\n\n");
116

    
117
        System.out.println("\n\nIs Admin::\n"+authorization.isAdmin("eyJraWQiOiJvaWRjIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiIwOTMxNzMwMTMyODMzNjMyQG9wZW5taW50ZWQuZXUiLCJhenAiOiIyNGU4MzE3Ni0xMzEyLTRiYTMtYmMwYi1mZmVlYmVhMTYwM2UiLCJpc3MiOiJodHRwczpcL1wvYWFpLm9wZW5taW50ZWQuZXVcL29pZGNcLyIsImV4cCI6MTQ5ODQ4NTk3NiwiaWF0IjoxNDk4NDcxNTc2LCJqdGkiOiJkMWRlZjc1Yi00MTEyLTRiZDktYTIyNi0wZThhOWI2M2Y3MWQifQ.WVYOb_yO8OaxIIt2jRYEDQBhGGFRDTBw3DgtVV_smuN5yx1ScCj6aehLu3JKPSArme4m2SGF4TEGhpwNJkwhM2WapGtxmtuCmCzYIo_QlC1Yki9hr2OT2rXMcQsJCiKaBSf6pLue6Sn78GMB5yaUTvOQHRgidXGiZXH5lsuZUx15Q6Equ_wzond_rgP9mRheRkTyIFuvvg4PuzmudBc11Ty863vIIQtoWF7_p98zTbHxiNF9lLPwzPZKxDoQ8JeayQEC-jsWVLgxmp-h0jG_Ko5jFVVJeeosqMMucOrs2FT_NKHVYVqB6VVh0C6nOufeiLrNDeMUlDT4dAvKD2zE9w")
118
                +"\n\n\n");
119
//        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\"]}");
120
//        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\"]}");
121
//        boolean b=authorization.isProjectCurator(u);
122
//        System.out.println(b);
123
//        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\"]}");
124
//        UserHandler.json2UserInfo(null);
125
//        UserHandler.json2UserInfo("mpla mpla mpla .... {}");
126
//        userHandler.getUserInfo("eyJraWQiOiJvaWRjIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiIwOTMxNzMwMTMyODMzNjMyQG9wZW5taW50ZWQuZXUiLCJhenAiOiIyNGU4MzE3Ni0xMzEyLTRiYTMtYmMwYi1mZmVlYmVhMTYwM2UiLCJpc3MiOiJodHRwczpcL1wvYWFpLm9wZW5taW50ZWQuZXVcL29pZGNcLyIsImV4cCI6MTQ5ODQ4NTk3NiwiaWF0IjoxNDk4NDcxNTc2LCJqdGkiOiJkMWRlZjc1Yi00MTEyLTRiZDktYTIyNi0wZThhOWI2M2Y3MWQifQ.WVYOb_yO8OaxIIt2jRYEDQBhGGFRDTBw3DgtVV_smuN5yx1ScCj6aehLu3JKPSArme4m2SGF4TEGhpwNJkwhM2WapGtxmtuCmCzYIo_QlC1Yki9hr2OT2rXMcQsJCiKaBSf6pLue6Sn78GMB5yaUTvOQHRgidXGiZXH5lsuZUx15Q6Equ_wzond_rgP9mRheRkTyIFuvvg4PuzmudBc11Ty863vIIQtoWF7_p98zTbHxiNF9lLPwzPZKxDoQ8JeayQEC-jsWVLgxmp-h0jG_Ko5jFVVJeeosqMMucOrs2FT_NKHVYVqB6VVh0C6nOufeiLrNDeMUlDT4dAvKD2zE9w");
127
    }
128
}
    (1-1/1)