Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement.security;
2

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

    
9
import javax.servlet.http.Cookie;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12
import java.io.IOException;
13

    
14
/**
15
 * Created by stefanos on 9/5/2017.
16
 */
17
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
18

    
19
    private static final Logger logger = Logger.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
20

    
21
    private String frontEndURI;
22
    private String frontPath;
23
    private String frontDomain;
24

    
25
    @Override
26
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IllegalArgumentException, IOException   {
27

    
28
        OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
29

    
30
        try {
31

    
32
//            Cookie jwt = new Cookie("XCsrfToken", JWTGenerator.generateToken(authOIDC, "my-very-secret"));
33
            Cookie openAIREUser = new Cookie("openAIREUser",  new Gson().toJson(JWTGenerator.generateJsonToken(authOIDC)));
34
            Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
35

    
36
            // Expire the cookies in four hours (4 * 3600)
37
//            jwt.setMaxAge(14400);
38
            openAIREUser.setMaxAge(14400);
39
            accessToken.setMaxAge(14400);
40

    
41
            //TODO DELETE LOG
42
            logger.info("\n////////////////////////////////////////////////////////////////////////////////////////////////\n");
43
//            logger.info("jwt: " + JWTGenerator.generateToken(authOIDC, "my-very-secret"));
44
            logger.info("access token: " + authOIDC.getAccessTokenValue());
45
            logger.info("openAIREUser: " + JWTGenerator.generateJsonToken(authOIDC));
46
            logger.info("\n////////////////////////////////////////////////////////////////////////////////////////////////\n");
47

    
48
            //TODO DELETE LOG
49
//            logger.info("\n////////////////////////////////////////////////////////////////////////////////////////////////\n");
50
//            logger.info("refresh token: " + authOIDC.getRefreshTokenValue());
51
//            logger.info("\n////////////////////////////////////////////////////////////////////////////////////////////////\n");
52

    
53

    
54
//            jwt.setPath(frontPath);
55
            openAIREUser.setPath(frontPath);
56
            accessToken.setPath(frontPath);
57

    
58
            if (frontDomain!=null) {
59
//                jwt.setDomain(frontDomain);
60
                openAIREUser.setDomain(frontDomain);
61
                accessToken.setDomain(frontDomain);
62
            }
63

    
64
//            response.addCookie(jwt);
65
            response.addCookie(openAIREUser);
66
            response.addCookie(accessToken);
67
            response.sendRedirect(frontEndURI);
68

    
69
        } catch (IOException e) {
70
            logger.error("IOException in redirection ", e);
71
            throw new IOException(e);
72
        }catch (IllegalArgumentException e) {
73
            logger.error("IllegalArgumentException in redirection ", e);
74
            throw new IllegalArgumentException(e);
75
        }
76

    
77
    }
78

    
79
    public String getFrontEndURI() {
80
        return frontEndURI;
81
    }
82

    
83
    public void setFrontEndURI(String frontEndURI) {
84
        this.frontEndURI = frontEndURI;
85
    }
86

    
87
    public String getFrontPath() {
88
        return frontPath;
89
    }
90

    
91
    public void setFrontPath(String frontPath) {
92
        this.frontPath = frontPath;
93
    }
94

    
95
    public String getFrontDomain() {
96
        return frontDomain;
97
    }
98

    
99
    public void setFrontDomain(String frontDomain) {
100
        this.frontDomain = frontDomain;
101
    }
102
}
103

    
104

    
(1-1/2)