1
|
package eu.dnetlib.repo.manager.config;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import com.google.gson.JsonObject;
|
5
|
import com.google.gson.JsonParser;
|
6
|
import org.apache.log4j.Logger;
|
7
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
8
|
import org.springframework.beans.factory.annotation.Value;
|
9
|
import org.springframework.security.core.Authentication;
|
10
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
11
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
12
|
|
13
|
import javax.servlet.ServletException;
|
14
|
import javax.servlet.http.Cookie;
|
15
|
import javax.servlet.http.HttpServletRequest;
|
16
|
import javax.servlet.http.HttpServletResponse;
|
17
|
import java.io.IOException;
|
18
|
import java.net.URLEncoder;
|
19
|
import java.util.Base64;
|
20
|
import java.util.Date;
|
21
|
import java.util.regex.Matcher;
|
22
|
import java.util.regex.Pattern;
|
23
|
|
24
|
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
25
|
|
26
|
private String frontEndURI;
|
27
|
|
28
|
private static final Logger LOGGER = Logger
|
29
|
.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class);
|
30
|
|
31
|
public void init() {
|
32
|
LOGGER.debug("Front end uri : " + frontEndURI);
|
33
|
}
|
34
|
|
35
|
|
36
|
@Value("${aai.mode}")
|
37
|
private String aai_mode;
|
38
|
|
39
|
@Override
|
40
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
|
41
|
OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
|
42
|
request.getSession().setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, authOIDC.getUserInfo().getEmail());
|
43
|
|
44
|
JsonObject userInfo = new JsonObject();
|
45
|
|
46
|
if (authOIDC.getUserInfo().getSub() == null)
|
47
|
userInfo.addProperty("sub", "");
|
48
|
else
|
49
|
userInfo.addProperty("sub", URLEncoder.encode(authOIDC.getUserInfo().getSub(), "UTF-8"));
|
50
|
|
51
|
|
52
|
if (authOIDC.getUserInfo().getName() != null)
|
53
|
userInfo.addProperty("fullname", URLEncoder.encode(authOIDC.getUserInfo().getName(), "UTF-8"));
|
54
|
|
55
|
if (authOIDC.getUserInfo().getGivenName() == null)
|
56
|
userInfo.addProperty("firstname", "");
|
57
|
else
|
58
|
userInfo.addProperty("firstname", URLEncoder.encode(authOIDC.getUserInfo().getGivenName(), "UTF-8") + "");
|
59
|
|
60
|
if (authOIDC.getUserInfo().getFamilyName() == null)
|
61
|
userInfo.addProperty("lastname", "");
|
62
|
else
|
63
|
userInfo.addProperty("lastname", URLEncoder.encode(authOIDC.getUserInfo().getFamilyName(), "UTF-8") + "");
|
64
|
|
65
|
userInfo.addProperty("email", authOIDC.getUserInfo().getEmail() + "");
|
66
|
if (authOIDC.getUserInfo().getSource().getAsJsonArray("edu_person_entitlements") == null)
|
67
|
userInfo.addProperty("role", "");
|
68
|
else
|
69
|
userInfo.addProperty("role", URLEncoder.encode(authOIDC.getUserInfo()
|
70
|
.getSource().getAsJsonArray("edu_person_entitlements").toString(), "UTF-8") + "");
|
71
|
|
72
|
|
73
|
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
|
74
|
String regex = "^([A-Za-z0-9-_=]+)\\.([A-Za-z0-9-_=]+)\\.?([A-Za-z0-9-_.+=]*)$";
|
75
|
Matcher matcher = Pattern.compile(regex).matcher(authOIDC.getAccessTokenValue());
|
76
|
if (matcher.find()) {
|
77
|
long exp = new JsonParser().parse(new String(Base64.getDecoder().decode(matcher.group(2)))).getAsJsonObject().get("exp").getAsLong();
|
78
|
accessToken.setMaxAge((int) (exp - (new Date().getTime() / 1000)));
|
79
|
} else {
|
80
|
accessToken.setMaxAge(3600);
|
81
|
}
|
82
|
if (aai_mode.equalsIgnoreCase("production") || aai_mode.equalsIgnoreCase("beta"))
|
83
|
accessToken.setDomain(".openaire.eu");
|
84
|
accessToken.setPath("/");
|
85
|
|
86
|
|
87
|
Cookie openAIREUser = new Cookie("openAIREUser", URLEncoder.encode(new Gson().toJson(userInfo), "UTF-8"));
|
88
|
openAIREUser.setMaxAge(accessToken.getMaxAge());
|
89
|
openAIREUser.setPath("/");
|
90
|
if (aai_mode.equalsIgnoreCase("production") || aai_mode.equalsIgnoreCase("beta"))
|
91
|
openAIREUser.setDomain(".openaire.eu");
|
92
|
|
93
|
// if (frontDomain!=null) {
|
94
|
// accessToken.setDomain(frontDomain);
|
95
|
// // openAIREUser.setDomain(frontDomain);
|
96
|
// }
|
97
|
|
98
|
response.addCookie(openAIREUser);
|
99
|
response.addCookie(accessToken);
|
100
|
response.sendRedirect(frontEndURI);
|
101
|
}
|
102
|
|
103
|
public String getFrontEndURI() {
|
104
|
return frontEndURI;
|
105
|
}
|
106
|
|
107
|
public void setFrontEndURI(String frontEndURI) {
|
108
|
this.frontEndURI = frontEndURI;
|
109
|
}
|
110
|
}
|
111
|
|