2019-02-16 11:04:41 +01:00
|
|
|
package eu.dnetlib.repo.manager.config;
|
|
|
|
|
2019-12-16 11:54:06 +01:00
|
|
|
import com.google.gson.Gson;
|
2019-02-16 11:04:41 +01:00
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.Cookie;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
|
|
|
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
|
|
|
|
|
|
|
private String frontEndURI;
|
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger
|
|
|
|
.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
|
|
|
|
|
|
|
|
public void init(){
|
|
|
|
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;
|
|
|
|
JsonObject userInfo = new JsonObject();
|
|
|
|
|
|
|
|
if (authOIDC.getUserInfo().getSub() == null)
|
|
|
|
userInfo.addProperty("sub", "");
|
|
|
|
else
|
|
|
|
userInfo.addProperty("sub", URLEncoder.encode(authOIDC.getUserInfo().getSub(), "UTF-8"));
|
|
|
|
|
|
|
|
|
|
|
|
if(authOIDC.getUserInfo().getName() != null)
|
|
|
|
userInfo.addProperty("fullname", URLEncoder.encode(authOIDC.getUserInfo().getName(), "UTF-8"));
|
|
|
|
|
|
|
|
if (authOIDC.getUserInfo().getGivenName() == null)
|
|
|
|
userInfo.addProperty("firstname", "");
|
|
|
|
else
|
|
|
|
userInfo.addProperty("firstname", URLEncoder.encode(authOIDC.getUserInfo().getGivenName(), "UTF-8") + "");
|
|
|
|
|
|
|
|
if (authOIDC.getUserInfo().getFamilyName() == null)
|
|
|
|
userInfo.addProperty("lastname", "");
|
|
|
|
else
|
|
|
|
userInfo.addProperty("lastname", URLEncoder.encode(authOIDC.getUserInfo().getFamilyName(), "UTF-8") + "");
|
|
|
|
|
|
|
|
userInfo.addProperty("email", authOIDC.getUserInfo().getEmail() + "");
|
|
|
|
if (authOIDC.getUserInfo().getSource().getAsJsonArray("edu_person_entitlements") == null)
|
|
|
|
userInfo.addProperty("role", "");
|
|
|
|
else
|
|
|
|
userInfo.addProperty("role", URLEncoder.encode(authOIDC.getUserInfo()
|
|
|
|
.getSource().getAsJsonArray("edu_person_entitlements").toString(), "UTF-8") + "");
|
|
|
|
|
|
|
|
|
|
|
|
Cookie openAIREUser = new Cookie("openAIREUser", new Gson().toJson(userInfo) );
|
|
|
|
openAIREUser.setMaxAge(14400);
|
|
|
|
openAIREUser.setPath("/");
|
|
|
|
if(aai_mode.equalsIgnoreCase("production") || aai_mode.equalsIgnoreCase("beta"))
|
|
|
|
openAIREUser .setDomain(".openaire.eu");
|
|
|
|
// openAIREUser.setDomain(".athenarc.gr");
|
|
|
|
response.addCookie(openAIREUser);
|
|
|
|
|
|
|
|
|
|
|
|
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
|
|
|
|
accessToken.setMaxAge(14400);
|
|
|
|
if(aai_mode.equalsIgnoreCase("production") || aai_mode.equalsIgnoreCase("beta"))
|
|
|
|
accessToken.setDomain(".openaire.eu");
|
|
|
|
accessToken.setPath("/");
|
|
|
|
|
|
|
|
// accessToken.setDomain(".athenarc.gr");
|
|
|
|
response.addCookie(accessToken);
|
|
|
|
response.sendRedirect(frontEndURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFrontEndURI() {
|
|
|
|
return frontEndURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFrontEndURI(String frontEndURI) {
|
|
|
|
this.frontEndURI = frontEndURI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|