overview authentication fixed. Edit and Register common form improved

This commit is contained in:
Katerina Iatropoulou 2020-11-02 12:48:41 +00:00
parent 9ba8cb3d7d
commit 8b19ce815f
5 changed files with 189 additions and 69 deletions

View File

@ -14,13 +14,16 @@ public class OverviewServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean isAuthenticated = !SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()
.equals("anonymousUser");
if (isAuthenticated) {
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());
}
response.setContentType("text/html");
request.getRequestDispatcher("./overview.jsp").include(request, response);
}

View File

@ -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,6 +187,11 @@ public class RegisterServiceServlet extends HttpServlet {
String serverRequestJSON;
String serverMessage;
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 {
@ -195,6 +203,7 @@ public class RegisterServiceServlet extends HttpServlet {
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();
@ -212,6 +221,73 @@ public class RegisterServiceServlet extends HttpServlet {
response.sendRedirect("./registeredServices");
return;
}
} else {
String serviceId = request.getParameter("id");
int serviceIdInt = 0;
if (serviceId == null || serviceId.isEmpty()) { //TODO WRONG MESSAGE
request.getSession().setAttribute("message", "Service with id " + serviceId + " does not exist.");
response.sendRedirect("./registeredServices");
} 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 = null;
registeredService = registeredServicesUtils.getRegisteredServiceDao().fetchRegisteredServiceById(serviceIdInt);
if (registeredService != null && registeredService.getAai_id() != null) {
serviceResponse = tokenUtils.getRegisteredService(registeredService.getAai_id(), accessToken);
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 {
//something is wrong with the form and the error messages will appear
@ -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();

View File

@ -57,15 +57,10 @@ 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);
} catch (SQLException sqle) {
logger.error("Error fetching registered services for user " + userId , sqle);
request.getSession().setAttribute("message", "Error fetching registered services. " +
"Please try again later.");
request.getSession().setAttribute("showEmptyList", false);
request.getRequestDispatcher("./registeredServices.jsp").include(request, response);
}
} else {
Map<String, ServiceResponse> serviceResponses = new HashMap<>();
Map<String, String> serviceKey = new HashMap<>();
@ -80,13 +75,21 @@ public class RegisteredServicesServlet extends HttpServlet {
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);
}
} catch (SQLException sqle) {
logger.error("Error fetching registered services for user " + userId , sqle);
request.getSession().setAttribute("message", "Error fetching registered services. " +
"Please try again later.");
request.getSession().setAttribute("showEmptyList", false);
request.getRequestDispatcher("./registeredServices.jsp").include(request, response);
}
response.setContentType("text/html");
request.getRequestDispatcher("./registeredServices.jsp").include(request, response);
}

View File

@ -43,14 +43,29 @@
</div>
<!-- CENTER SIDE -->
<div class="uk-width-2-3@l uk-width-2-3@m">
>>>>>> ${param.id}
<c:choose>
<c:when test="${not empty param.id}">
<h4 class="uk-margin-remove-top uk-text-bold uk-text-primary">Edit service</h4>
</c:when>
<c:otherwise>
<h4 class="uk-margin-remove-top uk-text-bold uk-text-primary">Add a new service</h4>
<!-- REGISTER FORM -->
PPPPPPPPP
${registeredService}
</c:otherwise>
</c:choose>
<!-- REGISTER FORM -->
<div id="registerForm">
<form action="registerService" method="POST" role="form" class="m-t uk-form-horizontal"
id="register_form">
<input type="hidden" name="id" value="${param.id}"/>
<c:choose>
<c:when test = "${not empty param.id}">
<input type="hidden" name="mode" value="edit"/>
</c:when>
<c:otherwise>
<input type="hidden" name="mode" value="create"/>
</c:otherwise>
</c:choose>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<div class="alert alert-success" aria-hidden="true" style="display: none;"></div>
<div class="alert alert-danger" aria-hidden="true" style="display: none;"></div>
@ -58,7 +73,7 @@
<div class="uk-margin-medium-top">
<label class="uk-form-label uk-text-bold" for="first_name">Name*</label>
<input id="first_name" name="first_name" type="text" placeholder="Name (*)"
class="uk-input" onkeyup="validate()" onfocusout="nameTouched = true;validate()" value=${first_name}>
class="uk-input" onkeyup="validate()" onfocusout="nameTouched = true;validate()" value="${first_name}">
<c:remove var="first_name" scope="session"/>
<div id="first_name_error" style="display:none;" class="uk-text-danger uk-text-small">Please enter a name for your
service.
@ -67,7 +82,7 @@
<div class="uk-margin-medium-top">
<label class="uk-form-label uk-text-bold" for="description">Description</label>
<textarea id="description" name="description" type="textarea" placeholder="Description:"
class="uk-textarea" rows="3" value=${description}></textarea>
class="uk-textarea" rows="3" value="${description}"></textarea>
<c:remove var="description" scope="session"/>
</div>
<h4 class="uk-text-bold uk-text-center">Access</h4>
@ -117,7 +132,8 @@
<c:remove var="value_error" scope="session"/>
</c:when>
<c:otherwise>
<div id="value_error" style="display:none;" class="uk-text-danger uk-text-small">Please provide a valid JSON.</div>
<div id="value_error" style="display:none;" class="uk-text-danger uk-text-small">Please provide a valid JSON. The format should be
{"kty": ..., "e": ... , "use": ... , "kid": ..., "alg": ... , "n": ...} </div>
</c:otherwise>
</c:choose>
<c:remove var="value" scope="session"/>
@ -146,7 +162,14 @@
<a type="submit" class="uk-button uk-button-default uk-margin-small-right"
href="./registeredServices">Cancel</a>
<button id="create" type="submit" class="uk-button uk-button-primary">
<c:choose>
<c:when test="${not empty param.id}">
Edit service
</c:when>
<c:otherwise>
Add new service
</c:otherwise>
</c:choose>
</button>
</div>
</form>

View File

@ -85,8 +85,9 @@
<c:if test="${registeredServices.size() > 0}">
<ul class="uk-list uk-list-divider">
<li>
<div class="uk-grid uk-child-width-1-3 uk-text-muted" uk-grid>
<div class="uk-grid uk-child-width-1-4 uk-text-muted" uk-grid>
<div>Name</div>
<div>id</div>
<div>Creation Date</div>
<div>Actions</div>
</div>
@ -94,13 +95,16 @@
<c:forEach items="${registeredServices}" var="registeredService" varStatus="loop">
<c:set var="key" value="${registeredService.id}"/>
<li>
<div class="uk-grid uk-child-width-1-3" uk-grid>
<div class="uk-grid uk-child-width-1-4" uk-grid>
<div>
<a uk-toggle="target: #details${registeredService.id}; animation: uk-animation-fade">
<span>${registeredService.name}</span>
<span class="space" uk-icon="icon:info;ratio:0.7"></span>
</a>
</div>
<div>
<span>${services[key].clientId}</span>
</div>
<div><fmt:formatDate value="${registeredService.date}"
pattern="dd-MM-yyyy HH:mm"/>
</div>
@ -141,6 +145,7 @@
<div class="uk-alert">
<p><span class="uk-text-primary">Name:</span> ${services[key].clientName}</p>
<p><span class="uk-text-primary">Description:</span> ${services[key].clientDescription}</p>
<p><span class="uk-text-primary">Service Id:</span> ${services[key].clientId}</p>
<p><span class="uk-text-primary">Scope:</span> openid</p>
<p><span class="uk-text-primary">Grant type:</span> client credentials</p>
<p><span class="uk-text-primary">Authentication Method</span> Asymmetrically-signed JWT assertion</p>