uoa-repository-manager-service/src/main/java/eu/dnetlib/repo/manager/config/FrontEndLinkURIAuthenticati...

99 lines
3.6 KiB
Java

package eu.dnetlib.repo.manager.config;
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;
import com.google.gson.*;
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 aaiMode;
private final static String ENCODING = "UTF-8";
@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(), ENCODING));
if(authOIDC.getUserInfo().getName() != null)
userInfo.addProperty("fullname", URLEncoder.encode(authOIDC.getUserInfo().getName(), ENCODING));
if (authOIDC.getUserInfo().getGivenName() == null)
userInfo.addProperty("firstname", "");
else
userInfo.addProperty("firstname", URLEncoder.encode(authOIDC.getUserInfo().getGivenName(), ENCODING) + "");
if (authOIDC.getUserInfo().getFamilyName() == null)
userInfo.addProperty("lastname", "");
else
userInfo.addProperty("lastname", URLEncoder.encode(authOIDC.getUserInfo().getFamilyName(), ENCODING) + "");
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(), ENCODING) + "");
Cookie openAIREUser = new Cookie("openAIREUser", new Gson().toJson(userInfo) );
openAIREUser.setMaxAge(14400);
openAIREUser.setPath("/");
openAIREUser.setHttpOnly(true);
if(aaiMode.equalsIgnoreCase("production") || aaiMode.equalsIgnoreCase("beta"))
openAIREUser .setDomain(".openaire.eu");
response.addCookie(openAIREUser);
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
accessToken.setMaxAge(14400);
accessToken.setHttpOnly(true);
if(aaiMode.equalsIgnoreCase("production") || aaiMode.equalsIgnoreCase("beta"))
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;
}
}