1
|
package eu.dnetlib.openaire.user.login.handler;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import com.google.gson.JsonParser;
|
5
|
import com.nimbusds.jwt.JWT;
|
6
|
import eu.dnetlib.openaire.user.login.utils.JWTGenerator;
|
7
|
import io.jsonwebtoken.Jwt;
|
8
|
import io.jsonwebtoken.Jwts;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
11
|
import org.springframework.security.core.Authentication;
|
12
|
import org.springframework.security.core.context.SecurityContextHolder;
|
13
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
14
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
15
|
import org.springframework.session.Session;
|
16
|
|
17
|
import javax.servlet.http.Cookie;
|
18
|
import javax.servlet.http.HttpServletRequest;
|
19
|
import javax.servlet.http.HttpServletResponse;
|
20
|
import java.io.IOException;
|
21
|
import java.text.ParseException;
|
22
|
import java.util.Arrays;
|
23
|
import java.util.Base64;
|
24
|
import java.util.Date;
|
25
|
import java.util.regex.Matcher;
|
26
|
import java.util.regex.Pattern;
|
27
|
|
28
|
/**
|
29
|
* Created by stefanos on 9/5/2017.
|
30
|
*/
|
31
|
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
32
|
|
33
|
private static final Logger logger = Logger.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
|
34
|
|
35
|
private String frontEndURI;
|
36
|
private String frontPath;
|
37
|
private String frontDomain;
|
38
|
|
39
|
@Override
|
40
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IllegalArgumentException, IOException {
|
41
|
|
42
|
OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
|
43
|
request.getSession().setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, authOIDC.getUserInfo().getEmail());
|
44
|
try {
|
45
|
|
46
|
// Cookie jwt = new Cookie("XCsrfToken", JWTGenerator.generateToken(authOIDC, "my-very-secret"));
|
47
|
//Cookie openAIREUser = new Cookie("openAIREUser", new Gson().toJson(JWTGenerator.generateJsonToken(authOIDC)));
|
48
|
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
|
49
|
String regex = "^([A-Za-z0-9-_=]+)\\.([A-Za-z0-9-_=]+)\\.?([A-Za-z0-9-_.+=]*)$";
|
50
|
Matcher matcher = Pattern.compile(regex).matcher(authOIDC.getAccessTokenValue());
|
51
|
if (matcher.find()) {
|
52
|
long exp = new JsonParser().parse(new String(Base64.getDecoder().decode(matcher.group(2)))).getAsJsonObject().get("exp").getAsLong();
|
53
|
accessToken.setMaxAge((int) (exp - (new Date().getTime()/1000)));
|
54
|
} else {
|
55
|
accessToken.setMaxAge(3600);
|
56
|
}
|
57
|
logger.debug("access token: " + authOIDC.getAccessTokenValue());
|
58
|
logger.debug("refresh token: " + authOIDC.getRefreshTokenValue());
|
59
|
logger.debug("CREDENTIALS > " + authOIDC.getCredentials());
|
60
|
logger.debug("Authorities > " + authOIDC.getAuthorities());
|
61
|
logger.debug("\n\nPrincipals > " + authOIDC.getPrincipal());
|
62
|
logger.debug("\n\nUser Info > " + authOIDC.getUserInfo());
|
63
|
logger.debug("\n\nopenAIREUser: " + JWTGenerator.generateJsonToken(authOIDC));
|
64
|
logger.debug("\n////////////////////////////////////////////////////////////////////////////////////////////////\n");
|
65
|
accessToken.setPath(frontPath);
|
66
|
|
67
|
if (frontDomain!=null) {
|
68
|
accessToken.setDomain(frontDomain);
|
69
|
}
|
70
|
|
71
|
response.addCookie(accessToken);
|
72
|
response.sendRedirect(frontEndURI);
|
73
|
|
74
|
} catch (IOException e) {
|
75
|
logger.error("IOException in redirection ", e);
|
76
|
throw new IOException(e);
|
77
|
}catch (IllegalArgumentException e) {
|
78
|
logger.error("IllegalArgumentException in redirection ", e);
|
79
|
throw new IllegalArgumentException(e);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
public String getFrontEndURI() {
|
84
|
return frontEndURI;
|
85
|
}
|
86
|
|
87
|
public void setFrontEndURI(String frontEndURI) {
|
88
|
this.frontEndURI = frontEndURI;
|
89
|
}
|
90
|
|
91
|
public String getFrontPath() {
|
92
|
return frontPath;
|
93
|
}
|
94
|
|
95
|
public void setFrontPath(String frontPath) {
|
96
|
this.frontPath = frontPath;
|
97
|
}
|
98
|
|
99
|
public String getFrontDomain() {
|
100
|
return frontDomain;
|
101
|
}
|
102
|
|
103
|
public void setFrontDomain(String frontDomain) {
|
104
|
this.frontDomain = frontDomain;
|
105
|
}
|
106
|
}
|
107
|
|
108
|
|