From 8b19ce815fab0a4bc59a3938c7c98e9d2159bb43 Mon Sep 17 00:00:00 2001 From: Katerina Iatropoulou Date: Mon, 2 Nov 2020 12:48:41 +0000 Subject: [PATCH] overview authentication fixed. Edit and Register common form improved --- .../usermanagement/OverviewServlet.java | 17 +- .../RegisterServiceServlet.java | 148 ++++++++++++++---- .../RegisteredServicesServlet.java | 45 +++--- src/main/webapp/registerService.jsp | 39 ++++- src/main/webapp/registeredServices.jsp | 9 +- 5 files changed, 189 insertions(+), 69 deletions(-) diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/OverviewServlet.java b/src/main/java/eu/dnetlib/openaire/usermanagement/OverviewServlet.java index 6af9973..0cc8e01 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/OverviewServlet.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/OverviewServlet.java @@ -14,13 +14,16 @@ public class OverviewServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); - request.getSession().setAttribute("authenticated", - !authentication.getPrincipal().toString() - .equals("anonymousUser")); - StringBuilder name = new StringBuilder().append(authentication.getUserInfo().getGivenName().charAt(0)); - name.append(authentication.getUserInfo().getFamilyName().charAt(0)); - request.getSession().setAttribute("name", name.toString()); + boolean isAuthenticated = !SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString() + .equals("anonymousUser"); + + if (isAuthenticated) { + OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); + StringBuilder name = new StringBuilder().append(authentication.getUserInfo().getGivenName().charAt(0)); + name.append(authentication.getUserInfo().getFamilyName().charAt(0)); + request.getSession().setAttribute("name", name.toString()); + } + response.setContentType("text/html"); request.getRequestDispatcher("./overview.jsp").include(request, response); } diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java index 7f4f445..dfeb539 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServiceServlet.java @@ -7,6 +7,7 @@ import eu.dnetlib.openaire.user.pojos.RegisteredService; import eu.dnetlib.openaire.usermanagement.utils.RegisteredServicesUtils; import eu.dnetlib.openaire.usermanagement.utils.TokenUtils; import org.apache.commons.validator.routines.UrlValidator; +import org.apache.http.HttpResponse; import org.apache.log4j.Logger; import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.springframework.beans.factory.annotation.Autowired; @@ -78,7 +79,7 @@ public class RegisterServiceServlet extends HttpServlet { } else { System.out.println("Not authorized"); - request.getSession().setAttribute("message", "Not authorized to edit the registered service with " + id + "id."); + request.getSession().setAttribute("message", "Not authorized to edit the registered service with id " + id + "."); response.sendRedirect("./registeredServices"); logger.warn("Not authorized to edit the service with " + id + "id."); } @@ -107,7 +108,6 @@ public class RegisterServiceServlet extends HttpServlet { } private void updateFormFields(HttpServletRequest request, String serviceName, String description, String keyType, ServiceResponse serviceResponse) { - String jwksUri; if (serviceName ==null || serviceName.trim().isEmpty()) { request.getSession().setAttribute("first_name", serviceResponse.getClientName()); } @@ -117,13 +117,15 @@ public class RegisterServiceServlet extends HttpServlet { } if (keyType == null || keyType.trim().isEmpty()) { - if (serviceResponse.getJwksUri()!=null){ - keyType = "uri"; - jwksUri = serviceResponse.getJwksUri(); + System.out.println("Service response URI " + serviceResponse.getJwksUri()); + if (serviceResponse.getJwksUri() != null) { + request.getSession().setAttribute("key_radio","uri"); + request.getSession().setAttribute("jwksUri", serviceResponse.getJwksUri()); } else { - keyType = "value"; + request.getSession().setAttribute("key_radio", "value"); Key key = serviceResponse.getJwks().keys[0]; + System.out.println("Service response keys " + serviceResponse.getJwksUri()); Gson gson = new GsonBuilder().setPrettyPrinting().create(); request.getSession().setAttribute("value", gson.toJson(key)); } @@ -138,8 +140,10 @@ public class RegisterServiceServlet extends HttpServlet { boolean canProceed = true; - //Careful! Redirects in method - checkNumberOfRegisteredServices(request, response, authentication); + String mode = request.getParameter("mode").trim(); + System.out.println("Mode was " + mode); + checkmode(mode); + System.out.println("Mode is " + mode); String name = request.getParameter("first_name").trim(); String description = request.getParameter("description").trim(); @@ -148,7 +152,6 @@ public class RegisterServiceServlet extends HttpServlet { String jwksString = null; Jwks jwks = null; - if (keyType.equals("uri")) { jwksUri = request.getParameter("uri"); String[] schemes = {"https"}; @@ -184,33 +187,106 @@ public class RegisterServiceServlet extends HttpServlet { String serverRequestJSON; String serverMessage; - if (keyType.equals("uri")){ - serverRequestJSON = createServiceJson(name, description, email, jwksUri); + if(mode.equals("create")) { + System.out.println("In create..."); + //Careful! Redirects in method + checkNumberOfRegisteredServices(request, response, authentication); + + if (keyType.equals("uri")){ + serverRequestJSON = createServiceJson(name, description, email, jwksUri); + } else { + serverRequestJSON = createServiceJson(name, description, email, jwks); + } + + serverMessage = tokenUtils.registerService(serverRequestJSON, accessToken); + + if (serverMessage == null) { + request.getSession().setAttribute("message", "There was an error registering your service. Please try again later."); + response.sendRedirect("./registeredServices"); + return; + } + serviceResponse = new Gson().fromJson(serverMessage, ServiceResponse.class); + String aai_id = serviceResponse.getId(); + String owner = userid; + + RegisteredService registeredService = new RegisteredService(aai_id, owner, name); + + try { + registeredServicesUtils.addRegistedService(registeredService); + request.getSession().setAttribute("success", "Your service has been successfully registered!"); + + } catch (SQLException sqle) { + logger.error("Fail to save service.", sqle); + request.getSession().setAttribute("message", "There was an error registering your service. Please try again later."); + response.sendRedirect("./registeredServices"); + return; + } } else { - serverRequestJSON = createServiceJson(name, description, email, jwks); - } + String serviceId = request.getParameter("id"); + int serviceIdInt = 0; - serverMessage = tokenUtils.registerService(serverRequestJSON, accessToken); + if (serviceId == null || serviceId.isEmpty()) { //TODO WRONG MESSAGE + request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist."); + response.sendRedirect("./registeredServices"); - if (serverMessage == null) { - request.getSession().setAttribute("message", "There was an error registering your service. Please try again later."); - response.sendRedirect("./registeredServices"); - } - serviceResponse = new Gson().fromJson(serverMessage, ServiceResponse.class); - String aai_id = serviceResponse.getId(); - String owner = userid; + } else { + System.out.println("In edit..."); + try { + serviceIdInt = Integer.parseInt(serviceId); + if (!registeredServicesUtils.isAuthorized(authentication.getSub(), serviceIdInt)) { + request.getSession().setAttribute("message", "You have no permission to edit the service."); + response.sendRedirect("./registeredServices"); + } - RegisteredService registeredService = new RegisteredService(aai_id, owner, name); + RegisteredService registeredService = null; + registeredService = registeredServicesUtils.getRegisteredServiceDao().fetchRegisteredServiceById(serviceIdInt); - try { - registeredServicesUtils.addRegistedService(registeredService); - request.getSession().setAttribute("success", "Your service has been successfully registered!"); + if (registeredService != null && registeredService.getAai_id() != null) { + serviceResponse = tokenUtils.getRegisteredService(registeredService.getAai_id(), accessToken); - } catch (SQLException sqle) { - logger.error("Fail to save service.", sqle); - request.getSession().setAttribute("message", "There was an error registering your service. Please try again later."); - response.sendRedirect("./registeredServices"); - return; + request.getSession().setAttribute("serviceId", serviceResponse.getId()); + System.out.println("service client name " + serviceResponse.getClientName()); + request.getSession().setAttribute("first_name", serviceResponse.getClientName()); + System.out.println("service client description " + serviceResponse.getClientDescription()); + request.getSession().setAttribute("description", serviceResponse.getClientDescription()); + + HttpResponse resp = tokenUtils.updateService(serviceId, authentication.getSub(), name, description, authentication.getUserInfo().getEmail(), accessToken); + + if (resp.getStatusLine().getStatusCode()==200) { + + 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); + return; + } + + request.getSession().setAttribute("success", "Your service with name '" + name + "' was successfully updated"); + } + + } else { + request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist."); + response.sendRedirect("./registeredServices"); + return; + } + + } catch (SQLException sqle) { + logger.error("Unable to access service with id " + serviceId, sqle); + request.getSession().setAttribute("message", "There was an error accessing your service."); + response.sendRedirect("./registeredServices"); + + } catch (NumberFormatException nfe) { + logger.error("Unable to access service with id " + serviceId, nfe); + request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist."); + response.sendRedirect("./registeredServices"); + } + } } } else { @@ -221,13 +297,23 @@ public class RegisterServiceServlet extends HttpServlet { request.getSession().setAttribute("uri", jwksUri); request.getSession().setAttribute("value", jwksString); - response.sendRedirect("./registerService"); + response.sendRedirect("./registeredServices"); return; } response.sendRedirect("./registeredServices"); } + private void checkmode(String mode) { + if (mode!=null && !mode.isEmpty()) { + if(!mode.equals("edit")||mode.equals("create")) { + mode = "create"; + } + } else { + mode = "create"; + } + } + private boolean keyIsValid(String keyType, String jwksUri, String jwksString) { return (keyType.equals("uri") && jwksUri != null && !jwksUri.isEmpty()) || keyType.equals("value") && jwksString != null && !jwksString.isEmpty(); diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisteredServicesServlet.java b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisteredServicesServlet.java index e1f7435..f661e62 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/RegisteredServicesServlet.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/RegisteredServicesServlet.java @@ -57,6 +57,30 @@ public class RegisteredServicesServlet extends HttpServlet { getRegisteredServiceDao().fetchAllRegisteredServicesByOwner(userId); System.out.println("LOAD REGISTERED SERVICES. " + registeredServices.size()); + if (registeredServices== null || registeredServices.isEmpty()) { + request.getSession().setAttribute("showEmptyList", true); + + } else { + Map serviceResponses = new HashMap<>(); + Map serviceKey = new HashMap<>(); + + for (RegisteredService registeredService:registeredServices) { + ServiceResponse serviceResponse = tokenUtils.getRegisteredService(registeredService.getAai_id(),authentication.getAccessTokenValue()); + serviceResponses.put(registeredService.getId(), serviceResponse); + serviceKey.put(registeredService.getId(), extractPublicKeySet(serviceResponse)); + } + + boolean reachedLimit = reachedMaximumNumberOfServices(registeredServices); + StringBuilder name = new StringBuilder().append(authentication.getUserInfo().getGivenName().charAt(0)); + name.append(authentication.getUserInfo().getFamilyName().charAt(0)); + request.getSession().setAttribute("name", name.toString()); + request.getSession().setAttribute("reachedLimit", reachedLimit); + System.out.println("REACHED LIMIT??? " + reachedLimit); + + request.getSession().setAttribute("services", serviceResponses); + request.getSession().setAttribute("keys", serviceKey); + request.getSession().setAttribute("registeredServices", registeredServices); + } } catch (SQLException sqle) { logger.error("Error fetching registered services for user " + userId , sqle); @@ -66,27 +90,6 @@ public class RegisteredServicesServlet extends HttpServlet { request.getRequestDispatcher("./registeredServices.jsp").include(request, response); } - Map serviceResponses = new HashMap<>(); - Map serviceKey = new HashMap<>(); - - for (RegisteredService registeredService:registeredServices) { - ServiceResponse serviceResponse = tokenUtils.getRegisteredService(registeredService.getAai_id(),authentication.getAccessTokenValue()); - serviceResponses.put(registeredService.getId(), serviceResponse); - serviceKey.put(registeredService.getId(), extractPublicKeySet(serviceResponse)); - } - - boolean reachedLimit = reachedMaximumNumberOfServices(registeredServices); - StringBuilder name = new StringBuilder().append(authentication.getUserInfo().getGivenName().charAt(0)); - name.append(authentication.getUserInfo().getFamilyName().charAt(0)); - request.getSession().setAttribute("name", name.toString()); - request.getSession().setAttribute("reachedLimit", reachedLimit); - request.getSession().setAttribute("test", "TEST"); - System.out.println("REACHED LIMIT??? " + reachedLimit); - - - request.getSession().setAttribute("services", serviceResponses); - request.getSession().setAttribute("keys", serviceKey); - request.getSession().setAttribute("registeredServices", registeredServices); response.setContentType("text/html"); request.getRequestDispatcher("./registeredServices.jsp").include(request, response); } diff --git a/src/main/webapp/registerService.jsp b/src/main/webapp/registerService.jsp index 472a8f6..f3d42e2 100644 --- a/src/main/webapp/registerService.jsp +++ b/src/main/webapp/registerService.jsp @@ -43,14 +43,29 @@
-

Add a new service

- - PPPPPPPPP - ${registeredService} + >>>>>> ${param.id} + + +

Edit service

+
+ +

Add a new service

+
+
+
+ + + + + + + + + @@ -58,7 +73,7 @@
+ class="uk-input" onkeyup="validate()" onfocusout="nameTouched = true;validate()" value="${first_name}"> diff --git a/src/main/webapp/registeredServices.jsp b/src/main/webapp/registeredServices.jsp index eb05224..21133bd 100644 --- a/src/main/webapp/registeredServices.jsp +++ b/src/main/webapp/registeredServices.jsp @@ -85,8 +85,9 @@
  • -
    +
    Name
    +
    id
    Creation Date
    Actions
    @@ -94,13 +95,16 @@
  • -
    +
    +
    + ${services[key].clientId} +
    @@ -141,6 +145,7 @@

    Name: ${services[key].clientName}

    Description: ${services[key].clientDescription}

    +

    Service Id: ${services[key].clientId}

    Scope: openid

    Grant type: client credentials

    Authentication Method Asymmetrically-signed JWT assertion