1
|
package eu.dnetlib.repo.manager.config;
|
2
|
|
3
|
import com.google.gson.JsonParser;
|
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
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
10
|
|
11
|
import javax.servlet.ServletException;
|
12
|
import javax.servlet.http.Cookie;
|
13
|
import javax.servlet.http.HttpServletRequest;
|
14
|
import javax.servlet.http.HttpServletResponse;
|
15
|
import java.io.IOException;
|
16
|
import java.util.Base64;
|
17
|
import java.util.Date;
|
18
|
import java.util.regex.Matcher;
|
19
|
import java.util.regex.Pattern;
|
20
|
|
21
|
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
22
|
|
23
|
private String frontEndURI;
|
24
|
|
25
|
private static final Logger LOGGER = Logger
|
26
|
.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
|
27
|
|
28
|
public void init() {
|
29
|
LOGGER.debug("Front end uri : " + frontEndURI);
|
30
|
}
|
31
|
|
32
|
|
33
|
@Value("${aai.mode}")
|
34
|
private String aai_mode;
|
35
|
|
36
|
@Override
|
37
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
|
38
|
OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
|
39
|
request.getSession().setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, authOIDC.getUserInfo().getEmail());
|
40
|
|
41
|
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
|
42
|
String regex = "^([A-Za-z0-9-_=]+)\\.([A-Za-z0-9-_=]+)\\.?([A-Za-z0-9-_.+=]*)$";
|
43
|
Matcher matcher = Pattern.compile(regex).matcher(authOIDC.getAccessTokenValue());
|
44
|
if (matcher.find()) {
|
45
|
long exp = new JsonParser().parse(new String(Base64.getDecoder().decode(matcher.group(2)))).getAsJsonObject().get("exp").getAsLong();
|
46
|
accessToken.setMaxAge((int) (exp - (new Date().getTime() / 1000)));
|
47
|
} else {
|
48
|
accessToken.setMaxAge(3600);
|
49
|
}
|
50
|
if (aai_mode.equalsIgnoreCase("production") || aai_mode.equalsIgnoreCase("beta"))
|
51
|
accessToken.setDomain(".openaire.eu");
|
52
|
accessToken.setPath("/");
|
53
|
|
54
|
response.addCookie(accessToken);
|
55
|
response.sendRedirect(frontEndURI);
|
56
|
}
|
57
|
|
58
|
public String getFrontEndURI() {
|
59
|
return frontEndURI;
|
60
|
}
|
61
|
|
62
|
public void setFrontEndURI(String frontEndURI) {
|
63
|
this.frontEndURI = frontEndURI;
|
64
|
}
|
65
|
}
|
66
|
|