diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/EditRegisteredService.java b/src/main/java/eu/dnetlib/openaire/usermanagement/EditRegisteredService.java index 3ee97a8..dd4769a 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/EditRegisteredService.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/EditRegisteredService.java @@ -30,9 +30,9 @@ public class EditRegisteredService extends HttpServlet { SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext()); } + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - request.getSession().setAttribute("authenticated", !SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString() .equals("anonymousUser")); @@ -93,7 +93,6 @@ public class EditRegisteredService extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); String accessToken = authentication.getAccessTokenValue(); @@ -104,6 +103,19 @@ public class EditRegisteredService extends HttpServlet { HttpResponse resp = TokenUtils.updateService(serviceId, authentication.getSub(), name, description, authentication.getUserInfo().getEmail(), accessToken); if (resp.getStatusLine().getStatusCode()==200) { + + RegisteredService registeredService = new RegisteredService(); + registeredService.setName(name); + registeredService.setAai_id(serviceId); + try { + registeredServicesUtils.getRegisteredServiceDao().update(registeredService); + } catch (SQLException sqle) { + logger.error("Unable to contact db.", sqle); + request.getSession().setAttribute("message", "Fail to delete the service. Please try again later."); + response.setContentType("text/html"); + request.getRequestDispatcher("./registeredServices.jsp").include(request, response); + } + request.getSession().setAttribute("success", "Your service with name '" + name + "' was successfully updated"); } diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/PersonalTokenServlet.java b/src/main/java/eu/dnetlib/openaire/usermanagement/PersonalTokenServlet.java index a5895b8..9b3c462 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/PersonalTokenServlet.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/PersonalTokenServlet.java @@ -1,6 +1,19 @@ package eu.dnetlib.openaire.usermanagement; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.log4j.Logger; import org.mitre.openid.connect.model.OIDCAuthenticationToken; +import org.springframework.beans.factory.annotation.Value; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.context.support.SpringBeanAutowiringSupport; @@ -10,14 +23,33 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.core.Response; import java.io.IOException; -import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + public class PersonalTokenServlet extends HttpServlet { + @Value("${oidc.secret}") + private String secret; + + @Value("${oidc.id}") + private String id; + + private Logger logger = Logger.getLogger(PersonalTokenServlet.class); + + public void init(ServletConfig config) throws ServletException { + super.init(config); + SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, + config.getServletContext()); + } + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + System.out.println("IN GET"); response.setContentType("text/html"); OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); @@ -26,4 +58,45 @@ public class PersonalTokenServlet extends HttpServlet { request.getRequestDispatcher("./personal.jsp").include(request, response); } -} + + public void doPost(HttpServletRequest request, HttpServletResponse response) { + System.out.println("IN POST"); + System.out.println(id); + System.out.println(secret); + + OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); + + try { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpPost httppost = new HttpPost("https://openaire-dev.aai-dev.grnet.gr/oidc/revoke"); + httppost.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); + String encoding = Base64.getEncoder().encodeToString((id.concat(":").concat(secret)).getBytes("UTF-8")); + httppost.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + encoding); + + List params = new ArrayList(); + params.add(new BasicNameValuePair("token", authentication.getAccessTokenValue())); + params.add(new BasicNameValuePair("token_type_hint", "access_token")); + httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); + + HttpResponse resp = httpclient.execute(httppost); + System.out.println("status " + resp.getStatusLine().getStatusCode()); + + HttpEntity entity = resp.getEntity(); + + System.out.println("REVOKE " + IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8.name())); + + response.sendRedirect("./personalToken"); + + } catch (UnsupportedEncodingException uee) { + logger.error("Error in Base64 encoding.", uee); + request.getSession().setAttribute("message", "Unable to revoke your token. Please try again later"); + System.out.println("ERROR >>>> " + uee.getMessage()); + + } catch (IOException ioe) { + logger.error("Error in Base64 encoding.", ioe); + request.getSession().setAttribute("message", "Unable to revoke your token. Please try again later"); + System.out.println("ERROR >>>> " + ioe.getMessage()); + + } + } +} \ No newline at end of file diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java index 288f233..6da1e00 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java @@ -78,12 +78,12 @@ public class RegisterServiceServlet extends HttpServlet { System.out.println("SQLE SKATA " + sqle.getMessage()); logger.error("SQLE SKATA", sqle); request.getSession().setAttribute("message", "There was an error registering your service. Please try again later."); - response.sendRedirect("/registeredServices"); + response.sendRedirect("./registeredServices"); } request.getSession().setAttribute("accessToken", authentication.getAccessTokenValue()); request.getSession().setAttribute("refreshToken", authentication.getRefreshTokenValue()); - request.getRequestDispatcher("./registeredServices.jsp").include(request, response); + response.sendRedirect("./registeredServices"); } } diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisteredServicesServlet.java b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisteredServicesServlet.java index 5a748c1..87dcf63 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisteredServicesServlet.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisteredServicesServlet.java @@ -3,6 +3,7 @@ package eu.dnetlib.openaire.usermanagement; import eu.dnetlib.openaire.user.pojos.RegisteredService; import eu.dnetlib.openaire.usermanagement.utils.RegisteredServicesUtils; import eu.dnetlib.openaire.usermanagement.utils.TokenUtils; +import org.apache.http.HttpResponse; import org.apache.log4j.Logger; import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.springframework.beans.factory.annotation.Autowired; @@ -76,6 +77,62 @@ public class RegisteredServicesServlet extends HttpServlet { request.getRequestDispatcher("./registeredServices.jsp").include(request, response); } + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder. + getContext().getAuthentication(); + String id = request.getParameter("id"); + + System.out.println("POST " +id); + + if (id!=null && !id.isEmpty()) { + try { + RegisteredService registeredService = registeredServicesUtils.getRegisteredServiceDao().fetchRegisteredServiceById(Integer.parseInt(id)); + + if (!registeredService.getOwner().equals(authentication.getSub())) { + request.getSession().setAttribute("message", "You are not allowed to delete the service."); + System.out.println("BLOCKED " + registeredService.getOwner() + " >> " + authentication.getSub()); + response.setContentType("text/html"); + request.getRequestDispatcher("./registeredServices.jsp").include(request, response); + } + + System.out.println("HERE"); + String aai_id = registeredService.getAai_id(); + HttpResponse resp = TokenUtils.deleteService(aai_id, authentication.getAccessTokenValue()); + + int statusCode = resp.getStatusLine().getStatusCode(); + System.out.println("STATUS CODE " + statusCode); + + if (statusCode != 200) { + logger.error("Unable to delete the service. Status code was " + statusCode); + request.getSession().setAttribute("message", "Fail to delete the service. Status " + statusCode); + System.out.println("AAI blocked"); + } else { + registeredServicesUtils.getRegisteredServiceDao().delete(Integer.parseInt(id)); + request.getSession().setAttribute("success", "The service was successfully deleted."); + System.out.println("HERE HERE"); + } + + response.setContentType("text/html"); + request.getRequestDispatcher("./registeredServices.jsp").include(request, response); + + } catch (SQLException sqle) { + logger.error("Unable to contact db.", sqle); + request.getSession().setAttribute("message", "Fail to delete the service. Please try again later."); + response.setContentType("text/html"); + request.getRequestDispatcher("./registeredServices.jsp").include(request, response); + } + + } else { + System.out.println("SO WRONG ID..."); + request.getSession().setAttribute("message", "Error selecting service to delete. Please try again."); + } + + System.out.println("FINALLY "); + response.setContentType("text/html"); + request.getRequestDispatcher("./registeredServices.jsp").include(request, response); + } + private boolean reachedMaximumNumberOfServices(List registeredServices) { if (registeredServices.size() == 5) { return true; diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/utils/TokenUtils.java b/src/main/java/eu/dnetlib/openaire/usermanagement/utils/TokenUtils.java index 78119a5..a3ec7b5 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/utils/TokenUtils.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/utils/TokenUtils.java @@ -67,6 +67,17 @@ public class TokenUtils { return httpclient.execute(httpPut); } + public static HttpResponse deleteService(String serviceId, String accessToken) throws IOException { + + System.out.println("DELETE " + "https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients/"+serviceId); + HttpDelete httpDelete = new HttpDelete("https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients/"+serviceId); + httpDelete.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); + httpDelete.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken); + + CloseableHttpClient httpclient = HttpClients.createDefault(); + return httpclient.execute(httpDelete); + } + public static ServiceResponse getRegisteredService(String serviceId, String accessToken) throws IOException { HttpGet httpGet = new HttpGet("https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients/"+serviceId); httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken); @@ -84,15 +95,6 @@ public class TokenUtils { } } - public void deleteService(String serviceId, String accessToken) throws IOException { - HttpDelete httpDelete = new HttpDelete("https://openaire-dev.aai-dev.grnet.gr/oidc/api/clients/"+serviceId); - httpDelete.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); - httpDelete.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken); - - CloseableHttpClient httpclient = HttpClients.createDefault(); - HttpResponse httpResponse = httpclient.execute(httpDelete); - } - private static String createServiceJson(String name, String description) { ServiceRequest serviceJSON = new ServiceRequest(); serviceJSON.setClientName(name); diff --git a/src/main/webapp/personal.jsp b/src/main/webapp/personal.jsp index b85e659..f9d0b6f 100644 --- a/src/main/webapp/personal.jsp +++ b/src/main/webapp/personal.jsp @@ -121,10 +121,14 @@
- - -
Your personal access token is
-
${accessToken}
+ ${message} + +
+ + +
Your personal access token is
+
${accessToken}
+
diff --git a/src/main/webapp/registeredServices.jsp b/src/main/webapp/registeredServices.jsp index e526c31..71a7801 100644 --- a/src/main/webapp/registeredServices.jsp +++ b/src/main/webapp/registeredServices.jsp @@ -87,27 +87,30 @@
-

Registered services

- -

${success}

+

+ ${success} +

-

${message}

+
+ ${message} +
-

You can register up to 5 services.

+

Registered services

+ +

+ You can register up to 5 services. +

-
-

You have reached the maximum size of allowed registered services.

- -
+

+ You have reached the maximum size of allowed registered services. +

+