In Configuration of Session cookie get domain through properties file | add a getUser method that uses the OIDCAuthenticationToken to get userInfo without request to AAI

This commit is contained in:
Argiro Kokogiannaki 2018-05-08 09:39:37 +00:00
parent ae84629a2e
commit 8fa3d0f6c4
3 changed files with 57 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package eu.dnetlib.openaire.usermanagement.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.unboundid.ldap.sdk.LDAPException;
@ -15,22 +16,17 @@ import eu.dnetlib.openaire.user.ldap.MUserActionsLDAP;
import eu.dnetlib.openaire.user.store.DataSourceConnector;
import eu.dnetlib.openaire.usermanagement.security.JWTGenerator;
import org.apache.log4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.mitre.openid.connect.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.http.HttpMethod;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@ -246,6 +242,42 @@ public class Test3Service {
}
@GET
@Path("/getUser")
@Produces(MediaType.APPLICATION_JSON)
public Response getUser() {
JsonObject userInfoJson = new JsonObject();
try {
OIDCAuthenticationToken authentication = null;
try {
authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
}catch (Exception e){
logger.info("Try to get User info - no context found ");
return Response.status(404).entity(compose404Message("Get User info: no context found ")).type(MediaType.APPLICATION_JSON).build();
}
UserInfo userInfo = authentication.getUserInfo();
if(userInfo == null){
return Response.status(404).entity(compose404Message("Get User info: user info is null ")).type(MediaType.APPLICATION_JSON).build();
}
logger.info("Get User:\n name: " + authentication.getUserInfo().getGivenName() + " " + authentication.getUserInfo().getFamilyName());
userInfoJson.addProperty("sub", userInfo.getSub());
userInfoJson.addProperty("name", userInfo.getName());
userInfoJson.addProperty("given_name", userInfo.getGivenName());
userInfoJson.addProperty("family_name", userInfo.getFamilyName());
userInfoJson.addProperty("email", userInfo.getEmail());
JsonArray roles = new JsonArray();
JsonObject source = authentication.getUserInfo().getSource();
roles = source.getAsJsonArray("edu_person_entitlements");
userInfoJson.add("edu_person_entitlements", roles);
}catch (Exception e){
logger.error("Get User info: An error occured ",e);
return Response.status(500).entity(compose500Message("Get User info: An error occured ",e)).type(MediaType.APPLICATION_JSON).build();
}
return Response.status(200).entity(userInfoJson.toString()).type(MediaType.APPLICATION_JSON).build();
}
/* JSON Utility Methods */
private String compose401Message(String message) {

View File

@ -4,6 +4,7 @@ import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
@ -12,7 +13,7 @@ import org.springframework.session.web.http.DefaultCookieSerializer;
/**
* Created by stefanos on 14/6/2017.
*/
@PropertySource(value = { "classpath:eu/dnet/openaire/usermanagement/redis.properties", "classpath:eu/dnet/openaire/usermanagement/springContext-dnetOpenaireUsersService.properties"} )
@Configuration
@EnableRedisHttpSession
public class Config {
@ -28,9 +29,12 @@ public class Config {
@Value("${redis.password:#{null}}")
private String password;
@Value("${webbapp.front.domain:.openaire.eu}")
private String domain;
@Bean
public LettuceConnectionFactory connectionFactory() {
logger.info(String.format("Redis connection listens to %s:%s",host,port));
logger.info(String.format("Redis connection listens to %s:%s ",host,port));
LettuceConnectionFactory factory = new LettuceConnectionFactory(host,Integer.parseInt(port));
if(password != null) factory.setPassword(password);
return factory;
@ -38,10 +42,12 @@ public class Config {
@Bean
public CookieSerializer cookieSerializer() {
logger.info("Cookie Serializer: Domain is "+domain);
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("SESSION"); // <1>
serializer.setCookiePath("/"); // <2>
serializer.setDomainNamePattern("");
// serializer.setDomainNamePattern(""); //with value "" set's the domain of the service e.g scoobydoo.di.uoa.gr
serializer.setDomainName(domain);
return serializer;
}
}

View File

@ -2,12 +2,16 @@ oidc.id=767422b9-5461-4807-a80a-f9a2072d3a7d
oidc.secret=AMQtGlbTXNjwjhF0st28LmM6V0XypMdaVS7tJmGuYFlmH36iIv4t7tVqYuLYrNPkhnZ_GPUJvhymBhFupdgb6aU
oidc.issuer = https://aai.openaire.eu/oidc/
oidc.home = http://rudie.di.uoa.gr:8080/dnet-openaire-users-1.0.0-SNAPSHOT/openid_connect_login
#oidc.home = https://beta.services.openaire.eu/uoa-user-management/openid_connect_login
#webbapp.front = https://beta.explore.openaire.eu/
webbapp.front = http://scoobydoo.di.uoa.gr:4200
#oidc.home = https://beta.services.openaire.eu/admin-user-management/openid_connect_login
#webbapp.front = https://beta.admin.connect.openaire.eu/reload
#webbapp.front.path = /
#webbapp.front.domain = .openaire.eu
#testing
oidc.home = http://scoobydoo.di.uoa.gr:8080/dnet-openaire-users-1.0.0-SNAPSHOT/openid_connect_login
webbapp.front = https://scoobydoo.di.uoa.gr:4200/reload
webbapp.front.path = /
webbapp.front.domain = .di.uoa.gr
webbapp.front.domain =.di.uoa.gr
google.recaptcha.secret = 6LfYrU8UAAAAADwrbImPvDo_XcxEZvrkkgMy9yU0
google.recaptcha.key = 6LfYrU8UAAAAAFsl3m2YhP1uavdmAdFEXBkoY_vd