1
|
package eu.dnetlib.repo.manager.service.config;
|
2
|
|
3
|
import com.google.gson.JsonObject;
|
4
|
import org.apache.log4j.Logger;
|
5
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
6
|
import org.springframework.security.core.Authentication;
|
7
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
8
|
|
9
|
import javax.servlet.ServletException;
|
10
|
import javax.servlet.http.Cookie;
|
11
|
import javax.servlet.http.HttpServletRequest;
|
12
|
import javax.servlet.http.HttpServletResponse;
|
13
|
import java.io.IOException;
|
14
|
import java.net.URLEncoder;
|
15
|
import com.google.gson.*;
|
16
|
|
17
|
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
18
|
|
19
|
private String frontEndURI;
|
20
|
|
21
|
private static final Logger LOGGER = Logger
|
22
|
.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
|
23
|
|
24
|
public void init(){
|
25
|
LOGGER.debug("Front end uri : " + frontEndURI);
|
26
|
}
|
27
|
|
28
|
@Override
|
29
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
|
30
|
|
31
|
OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
|
32
|
JsonObject userInfo = new JsonObject();
|
33
|
|
34
|
if (authOIDC.getUserInfo().getSub() == null)
|
35
|
userInfo.addProperty("sub", "");
|
36
|
else
|
37
|
userInfo.addProperty("sub", URLEncoder.encode(authOIDC.getUserInfo().getSub(), "UTF-8"));
|
38
|
|
39
|
userInfo.addProperty("fullname", URLEncoder.encode(authOIDC.getUserInfo().getName(), "UTF-8"));
|
40
|
|
41
|
if (authOIDC.getUserInfo().getGivenName() == null)
|
42
|
userInfo.addProperty("firstname", "");
|
43
|
else
|
44
|
userInfo.addProperty("firstname", URLEncoder.encode(authOIDC.getUserInfo().getGivenName(), "UTF-8") + "");
|
45
|
|
46
|
if (authOIDC.getUserInfo().getFamilyName() == null)
|
47
|
userInfo.addProperty("lastname", "");
|
48
|
else
|
49
|
userInfo.addProperty("lastname", URLEncoder.encode(authOIDC.getUserInfo().getFamilyName(), "UTF-8") + "");
|
50
|
|
51
|
userInfo.addProperty("email", authOIDC.getUserInfo().getEmail() + "");
|
52
|
if (authOIDC.getUserInfo().getSource().getAsJsonArray("edu_person_entitlements") == null)
|
53
|
userInfo.addProperty("role", "");
|
54
|
else
|
55
|
userInfo.addProperty("role", URLEncoder.encode(authOIDC.getUserInfo()
|
56
|
.getSource().getAsJsonArray("edu_person_entitlements").toString(), "UTF-8") + "");
|
57
|
|
58
|
|
59
|
Cookie openAIREUser = new Cookie("openAIREUser", new Gson().toJson(userInfo) );
|
60
|
openAIREUser.setMaxAge(14400);
|
61
|
openAIREUser.setPath("/");
|
62
|
// openAIREUser .setDomain(".openaire.eu");
|
63
|
// openAIREUser.setDomain(".athenarc.gr");
|
64
|
response.addCookie(openAIREUser);
|
65
|
|
66
|
|
67
|
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
|
68
|
accessToken.setMaxAge(14400);
|
69
|
// accessToken.setDomain(".openaire.eu");
|
70
|
accessToken.setPath("/");
|
71
|
|
72
|
// accessToken.setDomain(".athenarc.gr");
|
73
|
response.addCookie(accessToken);
|
74
|
response.sendRedirect(frontEndURI);
|
75
|
}
|
76
|
|
77
|
public String getFrontEndURI() {
|
78
|
return frontEndURI;
|
79
|
}
|
80
|
|
81
|
public void setFrontEndURI(String frontEndURI) {
|
82
|
this.frontEndURI = frontEndURI;
|
83
|
}
|
84
|
}
|
85
|
|