Project

General

Profile

1
package eu.dnetlib.repo.manager.config;
2

    
3
import com.google.gson.JsonObject;
4
import org.apache.log4j.Logger;
5
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
6
import org.springframework.beans.factory.annotation.Value;
7
import org.springframework.security.core.Authentication;
8
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
9

    
10
import javax.servlet.ServletException;
11
import javax.servlet.http.Cookie;
12
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletResponse;
14
import java.io.IOException;
15
import java.net.URLEncoder;
16
import com.google.gson.*;
17

    
18
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
19

    
20
    private String frontEndURI;
21

    
22
    private static final Logger LOGGER = Logger
23
            .getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
24

    
25
    public void init(){
26
        LOGGER.debug("Front end uri : " + frontEndURI);
27
    }
28

    
29

    
30
    @Value("${aai.mode}")
31
    private String aai_mode;
32

    
33
    @Override
34
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
35

    
36
        OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
37
        JsonObject userInfo = new JsonObject();
38

    
39
        if (authOIDC.getUserInfo().getSub() == null)
40
            userInfo.addProperty("sub", "");
41
        else
42
            userInfo.addProperty("sub", URLEncoder.encode(authOIDC.getUserInfo().getSub(), "UTF-8"));
43

    
44

    
45
        if(authOIDC.getUserInfo().getName() != null)
46
            userInfo.addProperty("fullname", URLEncoder.encode(authOIDC.getUserInfo().getName(), "UTF-8"));
47

    
48
        if (authOIDC.getUserInfo().getGivenName() == null)
49
            userInfo.addProperty("firstname", "");
50
        else
51
            userInfo.addProperty("firstname", URLEncoder.encode(authOIDC.getUserInfo().getGivenName(), "UTF-8") + "");
52

    
53
        if (authOIDC.getUserInfo().getFamilyName() == null)
54
            userInfo.addProperty("lastname",  "");
55
        else
56
            userInfo.addProperty("lastname", URLEncoder.encode(authOIDC.getUserInfo().getFamilyName(), "UTF-8") + "");
57

    
58
        userInfo.addProperty("email", authOIDC.getUserInfo().getEmail() + "");
59
        if (authOIDC.getUserInfo().getSource().getAsJsonArray("edu_person_entitlements") == null)
60
            userInfo.addProperty("role",  "");
61
        else
62
            userInfo.addProperty("role", URLEncoder.encode(authOIDC.getUserInfo()
63
                    .getSource().getAsJsonArray("edu_person_entitlements").toString(), "UTF-8") + "");
64

    
65

    
66
        Cookie openAIREUser = new Cookie("openAIREUser", new Gson().toJson(userInfo) );
67
        openAIREUser.setMaxAge(14400);
68
        openAIREUser.setPath("/");
69
        if(aai_mode.equalsIgnoreCase("production") || aai_mode.equalsIgnoreCase("beta"))
70
            openAIREUser .setDomain(".openaire.eu");
71
//        openAIREUser.setDomain(".athenarc.gr");
72
        response.addCookie(openAIREUser);
73

    
74

    
75
        Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
76
        accessToken.setMaxAge(14400);
77
        if(aai_mode.equalsIgnoreCase("production") || aai_mode.equalsIgnoreCase("beta"))
78
            accessToken.setDomain(".openaire.eu");
79
        accessToken.setPath("/");
80

    
81
//        accessToken.setDomain(".athenarc.gr");
82
        response.addCookie(accessToken);
83
        response.sendRedirect(frontEndURI);
84
    }
85

    
86
    public String getFrontEndURI() {
87
        return frontEndURI;
88
    }
89

    
90
    public void setFrontEndURI(String frontEndURI) {
91
        this.frontEndURI = frontEndURI;
92
    }
93
}
94

    
(3-3/6)