2019-02-16 11:04:41 +01:00
|
|
|
package eu.dnetlib.repo.manager.config;
|
|
|
|
|
2021-07-21 13:51:18 +02:00
|
|
|
import com.google.gson.JsonParser;
|
2019-02-16 11:04:41 +01:00
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
2021-07-21 13:51:18 +02:00
|
|
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.Cookie;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.IOException;
|
2021-07-21 13:51:18 +02:00
|
|
|
import java.util.Base64;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
|
|
|
|
|
|
|
private String frontEndURI;
|
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger
|
|
|
|
.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
|
|
|
|
|
2021-07-21 13:51:18 +02:00
|
|
|
public void init() {
|
2019-02-16 11:04:41 +01:00
|
|
|
LOGGER.debug("Front end uri : " + frontEndURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${aai.mode}")
|
|
|
|
private String aai_mode;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
|
|
|
|
OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
|
2021-07-21 13:51:18 +02:00
|
|
|
request.getSession().setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, authOIDC.getUserInfo().getEmail());
|
2019-02-16 11:04:41 +01:00
|
|
|
|
|
|
|
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
|
2021-07-21 13:51:18 +02:00
|
|
|
String regex = "^([A-Za-z0-9-_=]+)\\.([A-Za-z0-9-_=]+)\\.?([A-Za-z0-9-_.+=]*)$";
|
|
|
|
Matcher matcher = Pattern.compile(regex).matcher(authOIDC.getAccessTokenValue());
|
|
|
|
if (matcher.find()) {
|
|
|
|
long exp = new JsonParser().parse(new String(Base64.getDecoder().decode(matcher.group(2)))).getAsJsonObject().get("exp").getAsLong();
|
|
|
|
accessToken.setMaxAge((int) (exp - (new Date().getTime() / 1000)));
|
|
|
|
} else {
|
|
|
|
accessToken.setMaxAge(3600);
|
|
|
|
}
|
|
|
|
if (aai_mode.equalsIgnoreCase("production") || aai_mode.equalsIgnoreCase("beta"))
|
2019-02-16 11:04:41 +01:00
|
|
|
accessToken.setDomain(".openaire.eu");
|
|
|
|
accessToken.setPath("/");
|
|
|
|
|
|
|
|
response.addCookie(accessToken);
|
|
|
|
response.sendRedirect(frontEndURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFrontEndURI() {
|
|
|
|
return frontEndURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFrontEndURI(String frontEndURI) {
|
|
|
|
this.frontEndURI = frontEndURI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|