1
|
package eu.dnetlib.openaire.user.login.handler;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import eu.dnetlib.openaire.user.login.utils.JWTGenerator;
|
5
|
import org.apache.log4j.Logger;
|
6
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
7
|
import org.springframework.security.core.Authentication;
|
8
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
9
|
|
10
|
import javax.servlet.http.Cookie;
|
11
|
import javax.servlet.http.HttpServletRequest;
|
12
|
import javax.servlet.http.HttpServletResponse;
|
13
|
import java.io.IOException;
|
14
|
|
15
|
/**
|
16
|
* Created by stefanos on 9/5/2017.
|
17
|
*/
|
18
|
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
19
|
|
20
|
private static final Logger logger = Logger.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
|
21
|
|
22
|
private String frontEndURI;
|
23
|
private String frontPath;
|
24
|
private String frontDomain;
|
25
|
|
26
|
@Override
|
27
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IllegalArgumentException, IOException {
|
28
|
|
29
|
OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
|
30
|
|
31
|
try {
|
32
|
|
33
|
// Cookie jwt = new Cookie("XCsrfToken", JWTGenerator.generateToken(authOIDC, "my-very-secret"));
|
34
|
//Cookie openAIREUser = new Cookie("openAIREUser", new Gson().toJson(JWTGenerator.generateJsonToken(authOIDC)));
|
35
|
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
|
36
|
|
37
|
// Expire the cookies in four hours (4 * 3600)
|
38
|
// jwt.setMaxAge(14400);
|
39
|
// openAIREUser.setMaxAge(14400);
|
40
|
accessToken.setMaxAge(14400);
|
41
|
|
42
|
//TODO DELETE LOG
|
43
|
logger.info("\n////////////////////////////////////////////////////////////////////////////////////////////////\n");
|
44
|
// logger.info("jwt: " + JWTGenerator.generateToken(authOIDC, "my-very-secret"));
|
45
|
logger.info("access token: " + authOIDC.getAccessTokenValue());
|
46
|
logger.info("openAIREUser: " + JWTGenerator.generateJsonToken(authOIDC));
|
47
|
logger.info("\n////////////////////////////////////////////////////////////////////////////////////////////////\n");
|
48
|
|
49
|
//TODO DELETE LOG
|
50
|
// logger.info("\n////////////////////////////////////////////////////////////////////////////////////////////////\n");
|
51
|
// logger.info("refresh token: " + authOIDC.getRefreshTokenValue());
|
52
|
// logger.info("\n////////////////////////////////////////////////////////////////////////////////////////////////\n");
|
53
|
|
54
|
|
55
|
// jwt.setPath(frontPath);
|
56
|
// openAIREUser.setPath(frontPath);
|
57
|
accessToken.setPath(frontPath);
|
58
|
|
59
|
if (frontDomain!=null) {
|
60
|
// jwt.setDomain(frontDomain);
|
61
|
// openAIREUser.setDomain(frontDomain);
|
62
|
accessToken.setDomain(frontDomain);
|
63
|
}
|
64
|
|
65
|
// response.addCookie(jwt);
|
66
|
// response.addCookie(openAIREUser);
|
67
|
response.addCookie(accessToken);
|
68
|
response.sendRedirect(frontEndURI);
|
69
|
|
70
|
} catch (IOException e) {
|
71
|
logger.error("IOException in redirection ", e);
|
72
|
throw new IOException(e);
|
73
|
}catch (IllegalArgumentException e) {
|
74
|
logger.error("IllegalArgumentException in redirection ", e);
|
75
|
throw new IllegalArgumentException(e);
|
76
|
}
|
77
|
|
78
|
}
|
79
|
|
80
|
public String getFrontEndURI() {
|
81
|
return frontEndURI;
|
82
|
}
|
83
|
|
84
|
public void setFrontEndURI(String frontEndURI) {
|
85
|
this.frontEndURI = frontEndURI;
|
86
|
}
|
87
|
|
88
|
public String getFrontPath() {
|
89
|
return frontPath;
|
90
|
}
|
91
|
|
92
|
public void setFrontPath(String frontPath) {
|
93
|
this.frontPath = frontPath;
|
94
|
}
|
95
|
|
96
|
public String getFrontDomain() {
|
97
|
return frontDomain;
|
98
|
}
|
99
|
|
100
|
public void setFrontDomain(String frontDomain) {
|
101
|
this.frontDomain = frontDomain;
|
102
|
}
|
103
|
}
|
104
|
|
105
|
|