diff --git a/dmp-backend/src/main/java/dao/entities/security/UserAuthDao.java b/dmp-backend/src/main/java/dao/entities/security/UserAuthDao.java new file mode 100644 index 000000000..3fee4f35f --- /dev/null +++ b/dmp-backend/src/main/java/dao/entities/security/UserAuthDao.java @@ -0,0 +1,14 @@ +package dao.entities.security; + +import java.util.UUID; + +import dao.Dao; +import entities.security.UserAuth; + +public interface UserAuthDao extends Dao { + + + public String getPasswordHashOfUser(String username); + + +} diff --git a/dmp-backend/src/main/java/dao/entities/security/UserAuthDaoImpl.java b/dmp-backend/src/main/java/dao/entities/security/UserAuthDaoImpl.java new file mode 100644 index 000000000..1fb105de3 --- /dev/null +++ b/dmp-backend/src/main/java/dao/entities/security/UserAuthDaoImpl.java @@ -0,0 +1,30 @@ +package dao.entities.security; + +import java.util.UUID; + +import javax.persistence.TypedQuery; + +import dao.JpaDao; +import entities.security.UserAuth; + +public class UserAuthDaoImpl extends JpaDao implements UserAuthDao { + + @Override + public UserAuth loadDetails(UserAuth t) { + // TODO Auto-generated method stub + return null; + } + + + @Override + public String getPasswordHashOfUser(String username) { + + String queryString = "SELECT userAuth.password FROM UserAuth userAuth where userAuth.username = :username"; + TypedQuery typedQuery = entityManager.createQuery(queryString, String.class); + typedQuery.setParameter("username", username); + return typedQuery.getSingleResult(); + + } + + +} diff --git a/dmp-backend/src/main/java/entities/security/UserAuth.java b/dmp-backend/src/main/java/entities/security/UserAuth.java new file mode 100644 index 000000000..daafca794 --- /dev/null +++ b/dmp-backend/src/main/java/entities/security/UserAuth.java @@ -0,0 +1,57 @@ +package entities.security; + +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.annotations.GenericGenerator; + +@Entity +@Table(name="\"UserAuth\"") +public class UserAuth { + + @Id + @GeneratedValue + @GenericGenerator(name = "uuid2", strategy = "uuid2") + @Column(name = "id", updatable = false, nullable = false, columnDefinition = "BINARY(16)") + private UUID id; + + @Column(name = "username", nullable = false) + private String username; + + @Column(name = "password", nullable = false) + private String password; //hash-encoded password + + + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + + +} diff --git a/dmp-backend/src/main/java/entities/security/UserInfo.java b/dmp-backend/src/main/java/entities/security/UserInfo.java index 1e95a0c21..23710c997 100644 --- a/dmp-backend/src/main/java/entities/security/UserInfo.java +++ b/dmp-backend/src/main/java/entities/security/UserInfo.java @@ -1,13 +1,19 @@ package entities.security; import java.io.Serializable; +import java.util.Date; import java.util.UUID; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.Type; @@ -29,106 +35,97 @@ public class UserInfo implements Serializable{ @Column(name = "autoid", updatable = false, nullable = false, columnDefinition = "BINARY(16)") private UUID autoid; - //required - @Column(name = "id") - String id = null; - @Column(name = "email") - String email = null; - //non required - @Column(name = "\"emailIsVerified\"", nullable = true) - Boolean emailIsVerified = null; + @Column(name = "identification", nullable = false) + private String identification = null; + + @Column(name = "email", nullable = false) + private String email = null; + + @Column(name = "authorization_level", nullable = false) + private Short authorization_level; //0 admin, 1 user + + @Column(name = "usertype", nullable = false) + private Short usertype; // 0 internal, 1 external + + @OneToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "authentication", nullable = true) + private UserAuth authentication; + + @Column(name = "verified_email", nullable = true) + private Boolean verified_email = null; + @Column(name = "name", nullable = true) - String name = null; - @Column(name = "\"pictureUrl\"", nullable = true) - String pictureUrl = null; - @Column(name = "locale", nullable = true) - String locale = null; - @Column(name = "\"familyName\"", nullable = true) - String familyName = null; - @Column(name = "\"givenName\"", nullable = true) - String givenName = null; + private String name = null; + @Column(name = "created", nullable = false) + private Date created = null; + + + @Column(name = "lastloggedin", nullable = true) + private Date lastloggedin = null; + + @Type(type="typedefinition.XMLType") @Column(name = "additionalinfo", columnDefinition = "xml", nullable = true) private String additionalinfo; - - - public UserInfo () {} - - public UserInfo(String id, String email, Boolean emailIsVerified, String name, String pictureUrl, String locale, String familyName, String givenName, String additionalinfo) { - this.id = id; - this.email = email; - this.emailIsVerified = emailIsVerified; - this.name = name; - this.pictureUrl = pictureUrl; - this.locale = locale; - this.familyName = familyName; - this.givenName = givenName; - this.additionalinfo = additionalinfo; + public String getIdentification() { + return identification; } - - - - public String getId() { - return id; - } - public void setId(String id) { - this.id = id; + + public void setIdentification(String identification) { + this.identification = identification; } + public String getEmail() { return email; } + public void setEmail(String email) { this.email = email; } - public boolean isEmailIsVerified() { - return emailIsVerified; + + public Short getAuthorization_level() { + return authorization_level; } - public void setEmailIsVerified(boolean emailIsVerified) { - this.emailIsVerified = emailIsVerified; + + public void setAuthorization_level(Short authorization_level) { + this.authorization_level = authorization_level; } + + public Short getUsertype() { + return usertype; + } + + public void setUsertype(Short usertype) { + this.usertype = usertype; + } + + public UserAuth getAuthentication() { + return authentication; + } + + public void setAuthentication(UserAuth authentication) { + this.authentication = authentication; + } + + public Boolean getVerified_email() { + return verified_email; + } + + public void setVerified_email(Boolean verified_email) { + this.verified_email = verified_email; + } + public String getName() { return name; } + public void setName(String name) { this.name = name; } - public String getPictureUrl() { - return pictureUrl; - } - public void setPictureUrl(String pictureUrl) { - this.pictureUrl = pictureUrl; - } - public String getLocale() { - return locale; - } - public void setLocale(String locale) { - this.locale = locale; - } - public String getFamilyName() { - return familyName; - } - public void setFamilyName(String familyName) { - this.familyName = familyName; - } - public String getGivenName() { - return givenName; - } - public void setGivenName(String givenName) { - this.givenName = givenName; - } - - - public Boolean getEmailIsVerified() { - return emailIsVerified; - } - - public void setEmailIsVerified(Boolean emailIsVerified) { - this.emailIsVerified = emailIsVerified; - } public String getAdditionalinfo() { return additionalinfo; @@ -137,15 +134,7 @@ public class UserInfo implements Serializable{ public void setAdditionalinfo(String additionalinfo) { this.additionalinfo = additionalinfo; } - - @Override - public String toString() { - return "UserInfo [id=" + id + ", email=" + email + ", emailIsVerified=" + emailIsVerified - + ", name=" + name + ", pictureUrl=" + pictureUrl + ", locale=" + locale + ", familyName=" + familyName - + ", givenName=" + givenName + ", additionalinfo=" + additionalinfo + "]"; - } - - + } diff --git a/dmp-backend/src/main/java/rest/DMPs.java b/dmp-backend/src/main/java/rest/entities/DMPs.java similarity index 99% rename from dmp-backend/src/main/java/rest/DMPs.java rename to dmp-backend/src/main/java/rest/entities/DMPs.java index 3fbbf3c0d..bad97c52c 100644 --- a/dmp-backend/src/main/java/rest/DMPs.java +++ b/dmp-backend/src/main/java/rest/entities/DMPs.java @@ -1,4 +1,4 @@ -package rest; +package rest.entities; import java.io.Console; import java.util.List; diff --git a/dmp-backend/src/main/java/rest/DataRepositories.java b/dmp-backend/src/main/java/rest/entities/DataRepositories.java similarity index 99% rename from dmp-backend/src/main/java/rest/DataRepositories.java rename to dmp-backend/src/main/java/rest/entities/DataRepositories.java index c795787d8..940aa35f5 100644 --- a/dmp-backend/src/main/java/rest/DataRepositories.java +++ b/dmp-backend/src/main/java/rest/entities/DataRepositories.java @@ -1,4 +1,4 @@ -package rest; +package rest.entities; import java.util.List; import java.util.UUID; diff --git a/dmp-backend/src/main/java/rest/Datasets.java b/dmp-backend/src/main/java/rest/entities/Datasets.java similarity index 99% rename from dmp-backend/src/main/java/rest/Datasets.java rename to dmp-backend/src/main/java/rest/entities/Datasets.java index f61b5f26f..6b9c8e65c 100644 --- a/dmp-backend/src/main/java/rest/Datasets.java +++ b/dmp-backend/src/main/java/rest/entities/Datasets.java @@ -1,4 +1,4 @@ -package rest; +package rest.entities; import java.util.List; import java.util.UUID; diff --git a/dmp-backend/src/main/java/rest/DmpProfiles.java b/dmp-backend/src/main/java/rest/entities/DmpProfiles.java similarity index 67% rename from dmp-backend/src/main/java/rest/DmpProfiles.java rename to dmp-backend/src/main/java/rest/entities/DmpProfiles.java index 93eb1500d..dba6e2c45 100644 --- a/dmp-backend/src/main/java/rest/DmpProfiles.java +++ b/dmp-backend/src/main/java/rest/entities/DmpProfiles.java @@ -1,7 +1,10 @@ -package rest; +package rest.entities; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; + +import javax.transaction.Transactional; import org.apache.commons.lang3.SerializationUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -66,7 +69,7 @@ public class DmpProfiles { @Autowired private ResearcherDao researcherDao; @Autowired private ServiceDao serviceDao; - + private ObjectMapper objectMapper = new ObjectMapper(); // MANAGE DMPPROFILE(S) @@ -109,31 +112,57 @@ public class DmpProfiles { } - - - @RequestMapping(method = RequestMethod.POST, value = { "/dmpprofile/set" }, consumes = "application/json") - public @ResponseBody ResponseEntity setDmpProfile(@RequestBody DMPProfile dmpProfile) { - String reason = ""; - DMPProfile storedDMPProfile = null; - //try first to create + @RequestMapping(method = RequestMethod.GET, value = { "/dmpprofile/getAll" }, produces="application/json") + public @ResponseBody ResponseEntity getAllDmpProfiles(){ try { - storedDMPProfile = dMPProfileDao.create(dmpProfile); - return ResponseEntity.status(HttpStatus.CREATED).body("Created dmpProfile with id: " + storedDMPProfile.getId()); + List allDmpProfiles = dMPProfileDao.getAll(); + + //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom + List dmpprofileStrL = allDmpProfiles.parallelStream().map((dmpProfileObj) -> { + try { + return objectMapper.writeValueAsString(dmpProfileObj); + } catch (JsonProcessingException e) { + return ""; + } + }).collect(Collectors.toList()); + + return new ResponseEntity("["+String.join(",", dmpprofileStrL)+"]", HttpStatus.OK); + } - catch(Exception e) { - reason += e.getMessage(); - //try updating - try { - storedDMPProfile = dMPProfileDao.update(dmpProfile); - return ResponseEntity.status(HttpStatus.CREATED).body("Updated dmpProfile with id: " + storedDMPProfile.getId()); - } - catch(Exception ex) { - reason += (System.lineSeparator()+e.getMessage()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update dmpProfile! Reason: " + reason); - } + catch(Exception ex) { + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } } + @Transactional + @RequestMapping(method = RequestMethod.POST, value = { "/dmpprofile/create" }, consumes = "application/json", produces="application/json") + public @ResponseBody ResponseEntity setDmpProfile(@RequestBody DMPProfile dmpprofile) { + DMPProfile createdDMPProfile = dMPProfileDao.update(dmpprofile); + try { + return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdDMPProfile)); + } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create DMP Profile!\""); + } + } + + + @RequestMapping(method = RequestMethod.POST, value = { "/dmpprofile/delete" }, consumes = "application/json", produces="text/plain") + public @ResponseBody ResponseEntity delete(@RequestBody DMPProfile dmpprofile) { + + DMPProfile dmpp = new DMPProfile(); + dmpp.setId(dmpprofile.getId()); + try { + dMPProfileDao.delete(dmpp); + return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete DMP Profile!\""); + } + + } + + + + } diff --git a/dmp-backend/src/main/java/rest/Organisations.java b/dmp-backend/src/main/java/rest/entities/Organisations.java similarity index 60% rename from dmp-backend/src/main/java/rest/Organisations.java rename to dmp-backend/src/main/java/rest/entities/Organisations.java index a6e026041..dfe0a9a65 100644 --- a/dmp-backend/src/main/java/rest/Organisations.java +++ b/dmp-backend/src/main/java/rest/entities/Organisations.java @@ -1,7 +1,10 @@ -package rest; +package rest.entities; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; + +import javax.transaction.Transactional; import org.apache.commons.lang3.SerializationUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -65,9 +68,12 @@ public class Organisations { @Autowired private ResearcherDao researcherDao; @Autowired private ServiceDao serviceDao; + + private ObjectMapper objectMapper = new ObjectMapper(); + // MANAGE ORGANISATIONS(S) - @RequestMapping(method = RequestMethod.GET, value = { "/organizations" }) + @RequestMapping(method = RequestMethod.GET, value = { "/organisations" }) public @ResponseBody ResponseEntity listOrganisations(){ try { List allIDs = organisationDao.listAllIDs(); @@ -79,7 +85,7 @@ public class Organisations { } - @RequestMapping(method = RequestMethod.GET, value = { "/organizations/{id}" }) + @RequestMapping(method = RequestMethod.GET, value = { "/organisations/{id}" }) public @ResponseBody ResponseEntity getOrganisations(@PathVariable("id") String id) { try { Organisation organisation = organisationDao.read(UUID.fromString(id)); @@ -91,29 +97,55 @@ public class Organisations { } - @RequestMapping(method = RequestMethod.POST, value = { "/setOrganisation" }, consumes = "application/json") - public @ResponseBody ResponseEntity setOrganisation(@RequestBody Organisation organisation) { - String reason = ""; - Organisation storedOrganisation = null; - //try first to create + @RequestMapping(method = RequestMethod.GET, value = { "/organisation/getAll" }, produces="application/json") + public @ResponseBody ResponseEntity getAllOrganisations(){ try { - storedOrganisation = organisationDao.create(organisation); - return ResponseEntity.status(HttpStatus.CREATED).body("Created organisation with id: " + storedOrganisation.getId()); + List allOrganisations = organisationDao.getAll(); + + //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom + List organisationStrL = allOrganisations.parallelStream().map((organisationObj) -> { + try { + return objectMapper.writeValueAsString(organisationObj); + } catch (JsonProcessingException e) { + return ""; + } + }).collect(Collectors.toList()); + + return new ResponseEntity("["+String.join(",", organisationStrL)+"]", HttpStatus.OK); + } - catch(Exception e) { - reason += e.getMessage(); - //try updating - try { - storedOrganisation = organisationDao.update(organisation); - return ResponseEntity.status(HttpStatus.CREATED).body("Updated organisation with id: " + storedOrganisation.getId()); - } - catch(Exception ex) { - reason += (System.lineSeparator()+e.getMessage()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update organisation! Reason: " + reason); - } + catch(Exception ex) { + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } } + + @Transactional + @RequestMapping(method = RequestMethod.POST, value = { "/organisation/create" }, consumes = "application/json", produces="application/json") + public @ResponseBody ResponseEntity setOrganisation(@RequestBody Organisation organisation) { + Organisation createdOrganisation = organisationDao.update(organisation); + try { + return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdOrganisation)); + } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create organisation!\""); + } + } + + + @RequestMapping(method = RequestMethod.POST, value = { "/organisation/delete" }, consumes = "application/json", produces="text/plain") + public @ResponseBody ResponseEntity delete(@RequestBody Organisation organisation) { + + Organisation org = new Organisation(); + org.setId(organisation.getId()); + try { + organisationDao.delete(org); + return ResponseEntity.status(HttpStatus.OK).body("DELETED!"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not delete organisation!\""); + } + + } + diff --git a/dmp-backend/src/main/java/rest/Projects.java b/dmp-backend/src/main/java/rest/entities/Projects.java similarity index 68% rename from dmp-backend/src/main/java/rest/Projects.java rename to dmp-backend/src/main/java/rest/entities/Projects.java index f8b3c5b22..b642a49c8 100644 --- a/dmp-backend/src/main/java/rest/Projects.java +++ b/dmp-backend/src/main/java/rest/entities/Projects.java @@ -1,7 +1,10 @@ -package rest; +package rest.entities; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; + +import javax.transaction.Transactional; import org.apache.commons.lang3.SerializationUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -67,6 +70,7 @@ public class Projects { @Autowired private ServiceDao serviceDao; + private ObjectMapper objectMapper = new ObjectMapper(); // MANAGE PROJECT(S) @@ -105,29 +109,55 @@ public class Projects { } - @RequestMapping(method = RequestMethod.POST, value = { "/project/set" }, consumes = "application/json") - public @ResponseBody ResponseEntity setProject(@RequestBody Project project) { - String reason = ""; - Project storedProject = null; - //try first to create + @RequestMapping(method = RequestMethod.GET, value = { "/project/getAll" }, produces="application/json") + public @ResponseBody ResponseEntity getAllProjects(){ try { - storedProject = projectDao.create(project); - return ResponseEntity.status(HttpStatus.CREATED).body("Created project with id: " + storedProject.getId()); + List allProjects = projectDao.getAll(); + + //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom + List projectStrL = allProjects.parallelStream().map((projectObj) -> { + try { + return objectMapper.writeValueAsString(projectObj); + } catch (JsonProcessingException e) { + return ""; + } + }).collect(Collectors.toList()); + + return new ResponseEntity("["+String.join(",", projectStrL)+"]", HttpStatus.OK); + } - catch(Exception e) { - reason += e.getMessage(); - //try updating - try { - storedProject = projectDao.update(project); - return ResponseEntity.status(HttpStatus.CREATED).body("Updated project with id: " + storedProject.getId()); - } - catch(Exception ex) { - reason += (System.lineSeparator()+e.getMessage()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update project! Reason: " + reason); - } + catch(Exception ex) { + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } } + + @Transactional + @RequestMapping(method = RequestMethod.POST, value = { "/project/create" }, consumes = "application/json", produces="application/json") + public @ResponseBody ResponseEntity setProject(@RequestBody Project project) { + Project createdProject = projectDao.update(project); + try { + return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdProject)); + } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create Project!\""); + } + } + + + @RequestMapping(method = RequestMethod.POST, value = { "/project/delete" }, consumes = "application/json", produces="text/plain") + public @ResponseBody ResponseEntity delete(@RequestBody Project project) { + + Project p = new Project(); + p.setId(project.getId()); + try { + projectDao.delete(p); + return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete Project!\""); + } + + } + } diff --git a/dmp-backend/src/main/java/rest/Registries.java b/dmp-backend/src/main/java/rest/entities/Registries.java similarity index 58% rename from dmp-backend/src/main/java/rest/Registries.java rename to dmp-backend/src/main/java/rest/entities/Registries.java index a6dbef9c6..54b14b22f 100644 --- a/dmp-backend/src/main/java/rest/Registries.java +++ b/dmp-backend/src/main/java/rest/entities/Registries.java @@ -1,13 +1,15 @@ -package rest; +package rest.entities; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; import org.apache.commons.lang3.SerializationUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PathVariable; @@ -44,6 +46,7 @@ import entities.Project; import entities.Registry; import entities.Researcher; import entities.Service; +import entities.responses.IDLabelPair; import helpers.Transformers; import responses.RestResponse; @@ -65,7 +68,8 @@ public class Registries { @Autowired private ResearcherDao researcherDao; @Autowired private ServiceDao serviceDao; - + + private ObjectMapper objectMapper = new ObjectMapper(); // MANAGE REGISTRY(IES) @@ -91,31 +95,72 @@ public class Registries { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); } } + + - - @RequestMapping(method = RequestMethod.POST, value = { "/setRegistry" }, consumes = "application/json") - public @ResponseBody ResponseEntity setRegistry(@RequestBody Registry registry) { - String reason = ""; - Registry storedRegistry = null; - //try first to create + @RequestMapping(method = RequestMethod.GET, value = { "/registries/listAllLabelIDs" }) + public @ResponseBody ResponseEntity listLabelIds(){ try { - storedRegistry = registryDao.create(registry); - return ResponseEntity.status(HttpStatus.CREATED).body("Created registry with id: " + storedRegistry.getId()); + List allIDs = registryDao.listAllIDsLabels(); + return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); } - catch(Exception e) { - reason += e.getMessage(); - //try updating - try { - storedRegistry = registryDao.update(registry); - return ResponseEntity.status(HttpStatus.CREATED).body("Updated registry with id: " + storedRegistry.getId()); - } - catch(Exception ex) { - reason += (System.lineSeparator()+e.getMessage()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update registry! Reason: " + reason); - } + catch(Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } + + @RequestMapping(method = RequestMethod.GET, value = { "/registry/getAll" }, produces="application/json") + public @ResponseBody ResponseEntity getAllRegistries(){ + try { + List allRegistries = registryDao.getAll(); + + //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom + List registryStrL = allRegistries.parallelStream().map((registryObj) -> { + try { + return objectMapper.writeValueAsString(registryObj); + } catch (JsonProcessingException e) { + return ""; + } + }).collect(Collectors.toList()); + + return new ResponseEntity("["+String.join(",", registryStrL)+"]", HttpStatus.OK); + + } + catch(Exception ex) { + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + + @Transactional + @RequestMapping(method = RequestMethod.POST, value = { "/registry/create" }, consumes = "application/json", produces="application/json") + public @ResponseBody ResponseEntity setRegistry(@RequestBody Registry registry) { + Registry createdRegistry = registryDao.update(registry); + try { + return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdRegistry)); + } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create registry!\""); + } + } + + + @RequestMapping(method = RequestMethod.POST, value = { "/registry/delete" }, consumes = "application/json", produces="text/plain") + public @ResponseBody ResponseEntity delete(@RequestBody Registry registry) { + + Registry r = new Registry(); + r.setId(registry.getId()); + try { + registryDao.delete(r); + return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete registry!\""); + } + + } + + + } diff --git a/dmp-backend/src/main/java/rest/Researchers.java b/dmp-backend/src/main/java/rest/entities/Researchers.java similarity index 99% rename from dmp-backend/src/main/java/rest/Researchers.java rename to dmp-backend/src/main/java/rest/entities/Researchers.java index 472a8d09d..2e5d4827c 100644 --- a/dmp-backend/src/main/java/rest/Researchers.java +++ b/dmp-backend/src/main/java/rest/entities/Researchers.java @@ -1,4 +1,4 @@ -package rest; +package rest.entities; import java.util.List; import java.util.UUID; diff --git a/dmp-backend/src/main/java/rest/Services.java b/dmp-backend/src/main/java/rest/entities/Services.java similarity index 60% rename from dmp-backend/src/main/java/rest/Services.java rename to dmp-backend/src/main/java/rest/entities/Services.java index ef9e8e635..645e95ceb 100644 --- a/dmp-backend/src/main/java/rest/Services.java +++ b/dmp-backend/src/main/java/rest/entities/Services.java @@ -1,7 +1,10 @@ -package rest; +package rest.entities; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; + +import javax.transaction.Transactional; import org.apache.commons.lang3.SerializationUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -66,6 +69,7 @@ public class Services { @Autowired private ServiceDao serviceDao; + private ObjectMapper objectMapper = new ObjectMapper(); // MANAGE SERVICE(S) @@ -73,7 +77,7 @@ public class Services { public @ResponseBody ResponseEntity listServices(){ try { List allIDs = serviceDao.listAllIDs(); - return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(allIDs)); + return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); @@ -85,7 +89,7 @@ public class Services { public @ResponseBody ResponseEntity getServices(@PathVariable("id") String id) { try { Service service = serviceDao.read(UUID.fromString(id)); - return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(service)); + return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(service)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); @@ -93,28 +97,58 @@ public class Services { } - @RequestMapping(method = RequestMethod.POST, value = { "/setService" }, consumes = "application/json") - public @ResponseBody ResponseEntity setService(@RequestBody Service service) { - String reason = ""; - Service storedService = null; - //try first to create + + @RequestMapping(method = RequestMethod.GET, value = { "/service/getAll" }, produces="application/json") + public @ResponseBody ResponseEntity getAllServices(){ try { - storedService = serviceDao.create(service); - return ResponseEntity.status(HttpStatus.CREATED).body("Created service with id: " + storedService.getId()); + List allServices = serviceDao.getAll(); + + //sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom + List serviceStrL = allServices.parallelStream().map((serviceObj) -> { + try { + return objectMapper.writeValueAsString(serviceObj); + } catch (JsonProcessingException e) { + return ""; + } + }).collect(Collectors.toList()); + + return new ResponseEntity("["+String.join(",", serviceStrL)+"]", HttpStatus.OK); + } - catch(Exception e) { - reason += e.getMessage(); - //try updating - try { - storedService = serviceDao.update(service); - return ResponseEntity.status(HttpStatus.CREATED).body("Updated service with id: " + storedService.getId()); - } - catch(Exception ex) { - reason += (System.lineSeparator()+e.getMessage()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update service! Reason: " + reason); - } + catch(Exception ex) { + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } } + + @Transactional + @RequestMapping(method = RequestMethod.POST, value = { "/service/create" }, consumes = "application/json", produces="application/json") + public @ResponseBody ResponseEntity setService(@RequestBody Service service) { + Service createdService = serviceDao.update(service); + try { + return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdService)); + } catch (JsonProcessingException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create service entity!\""); + } + } + + + @RequestMapping(method = RequestMethod.POST, value = { "/service/delete" }, consumes = "application/json", produces="text/plain") + public @ResponseBody ResponseEntity delete(@RequestBody Service service) { + + Service s = new Service(); + s.setId(service.getId()); + try { + serviceDao.delete(s); + return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete Service entity!\""); + } + + } + + + + } diff --git a/dmp-backend/src/main/java/rest/login/Login.java b/dmp-backend/src/main/java/rest/login/Login.java new file mode 100644 index 000000000..4db8ae27a --- /dev/null +++ b/dmp-backend/src/main/java/rest/login/Login.java @@ -0,0 +1,70 @@ +package rest.login; + +import java.io.Serializable; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import dao.entities.DataRepositoryDao; +import dao.entities.security.UserAuthDao; +import dao.entities.security.UserInfoDao; + + +@RestController +@CrossOrigin +public class Login { + + + @Autowired private UserInfoDao userInfoDao; + @Autowired private UserAuthDao userAuthDao; + + + private ObjectMapper objectMapper = new ObjectMapper(); + + + @RequestMapping(method = RequestMethod.POST, value = { "/nativeLogin" }, consumes = "application/json", produces="text/plain") + public @ResponseBody ResponseEntity nativeLogin(@RequestBody Credentials credentials) { + + + System.out.println(userAuthDao.getPasswordHashOfUser("admin")); + + + + return ResponseEntity.status(HttpStatus.OK).body("OUR-GENERATED-TOKEN"); + + } + + +} + +class Credentials implements Serializable{ + + private static final long serialVersionUID = 3519634756673886633L; + + private String username; + private String password; + + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + +} diff --git a/dmp-backend/src/main/java/rest/Proxy.java b/dmp-backend/src/main/java/rest/proxy/Proxy.java similarity index 99% rename from dmp-backend/src/main/java/rest/Proxy.java rename to dmp-backend/src/main/java/rest/proxy/Proxy.java index a8e480d93..4678ee955 100644 --- a/dmp-backend/src/main/java/rest/Proxy.java +++ b/dmp-backend/src/main/java/rest/proxy/Proxy.java @@ -1,4 +1,4 @@ -package rest; +package rest.proxy; import java.io.BufferedReader; import java.io.IOException; diff --git a/dmp-backend/src/main/java/security/TokenAuthenticationFilter.java b/dmp-backend/src/main/java/security/TokenAuthenticationFilter.java index 24c7099eb..dd69e94c6 100644 --- a/dmp-backend/src/main/java/security/TokenAuthenticationFilter.java +++ b/dmp-backend/src/main/java/security/TokenAuthenticationFilter.java @@ -15,22 +15,29 @@ import org.springframework.web.filter.GenericFilterBean; public class TokenAuthenticationFilter extends GenericFilterBean { - private static final String HEADER_TOKEN_FIELD = "oauth2-token"; - + private static final String HEADER_NATIVE_TOKEN_FIELD = "native-token"; + private static final String HEADER_GOOGLE_TOKEN_FIELD = "google-token"; @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpRequest = (HttpServletRequest) request; - String accessToken = httpRequest.getHeader(HEADER_TOKEN_FIELD); - if(accessToken==null) accessToken = ""; + String nativeToken = httpRequest.getHeader(HEADER_NATIVE_TOKEN_FIELD); + String googleToken = httpRequest.getHeader(HEADER_GOOGLE_TOKEN_FIELD); + //just pass the token into the credentials object of the UsernamePasswordAuthenticationToken class - final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("google-user", accessToken); - SecurityContextHolder.getContext().setAuthentication(authentication); - /* - */ - chain.doFilter(request, response); + UsernamePasswordAuthenticationToken authentication = null; + if(nativeToken != null) + authentication = new UsernamePasswordAuthenticationToken("native-user", nativeToken); + if(googleToken != null) + authentication = new UsernamePasswordAuthenticationToken("google-user", nativeToken); + + if(authentication != null) { + SecurityContextHolder.getContext().setAuthentication(authentication); + chain.doFilter(request, response); + } + } } diff --git a/dmp-backend/src/main/java/security/GoogleTokenValidator.java b/dmp-backend/src/main/java/security/validators/GoogleTokenValidator.java similarity index 84% rename from dmp-backend/src/main/java/security/GoogleTokenValidator.java rename to dmp-backend/src/main/java/security/validators/GoogleTokenValidator.java index 8e133d93c..23fecaf41 100644 --- a/dmp-backend/src/main/java/security/GoogleTokenValidator.java +++ b/dmp-backend/src/main/java/security/validators/GoogleTokenValidator.java @@ -1,4 +1,4 @@ -package security; +package security.validators; import java.io.IOException; import java.security.GeneralSecurityException; @@ -54,14 +54,14 @@ public class GoogleTokenValidator { if (idToken != null) { Payload payload = idToken.getPayload(); - UserInfo userInfo = new UserInfo(payload.getSubject(), payload.getEmail(), - payload.getEmailVerified(), (String)payload.get("name"), (String)payload.get("picture"), - (String)payload.get("locale"), (String)payload.get("family_name"), (String)payload.get("given_name"), ""); - +// UserInfo userInfo = new UserInfo(payload.getSubject(), payload.getEmail(), +// payload.getEmailVerified(), (String)payload.get("name"), (String)payload.get("picture"), +// (String)payload.get("locale"), (String)payload.get("family_name"), (String)payload.get("given_name"), ""); // System.out.println(userInfo.toString()); +// return userInfo; + + return null; - return userInfo; - } else { throw new NonValidTokenException("Not a valid token"); } diff --git a/dmp-backend/src/main/webapp/WEB-INF/applicationContext.xml b/dmp-backend/src/main/webapp/WEB-INF/applicationContext.xml index 40ada7523..a8333cb5b 100644 --- a/dmp-backend/src/main/webapp/WEB-INF/applicationContext.xml +++ b/dmp-backend/src/main/webapp/WEB-INF/applicationContext.xml @@ -25,7 +25,7 @@ - + @@ -95,6 +95,7 @@ + diff --git a/dmp-backend/src/main/webapp/WEB-INF/dmp-backend-rest-servlet.xml b/dmp-backend/src/main/webapp/WEB-INF/dmp-backend-rest-servlet.xml index 8c473efe2..1b2b28672 100644 --- a/dmp-backend/src/main/webapp/WEB-INF/dmp-backend-rest-servlet.xml +++ b/dmp-backend/src/main/webapp/WEB-INF/dmp-backend-rest-servlet.xml @@ -16,7 +16,7 @@ - + diff --git a/dmp-db-scema/DataManagementPlanDB.sql b/dmp-db-scema/DataManagementPlanDB.sql index 5d46d5259..9a97eae62 100644 --- a/dmp-db-scema/DataManagementPlanDB.sql +++ b/dmp-db-scema/DataManagementPlanDB.sql @@ -447,20 +447,46 @@ ALTER TABLE ONLY "DatasetService" ADD CONSTRAINT "DatasetServiceServiceReference" FOREIGN KEY ("Service") REFERENCES "Service"("ID"); +DROP table if exists "UserInfo"; + CREATE TABLE "UserInfo" ( "autoid" uuid DEFAULT uuid_generate_v4() NOT NULL, - "id" character varying(500), - "email" character varying(250), - "emailIsVerified" boolean, + "identification" character varying(500) NOT NULL, + "email" character varying(250) NOT NULL, + "authorization_level" smallint NOT NULL, + "usertype" smallint NOT NULL, + "authentication" uuid, + "verified_email" boolean, "name" character varying(250), - "pictureUrl" character varying(500), - "locale" character varying(50), - "familyName" character varying(250), - "givenName" character varying(250), + "created" timestamp, + "lastloggedin" timestamp, "additionalinfo" xml, - PRIMARY KEY (id, email) + PRIMARY KEY (identification, email) ); +COMMENT ON COLUMN "UserInfo"."authorization_level" IS 'This stores the authorization level of the user: 0 admin, 1 user, being able to be extended furthermore'; +COMMENT ON COLUMN "UserInfo"."usertype" IS 'This stores the type of user: 0 -> internal, 1 external'; + + +DROP table if exists "UserAuth"; + +CREATE TABLE "UserAuth" ( + "id" uuid DEFAULT uuid_generate_v4() NOT NULL UNIQUE, + "username" character varying(200) NOT NULL, + "password" character varying(250) NOT NULL, + PRIMARY KEY (username) +); + +CREATE INDEX idx_userauth_username ON "UserAuth"(username); + +ALTER TABLE "UserInfo" ADD CONSTRAINT fkey_userinfo_userauth FOREIGN KEY ("authentication") REFERENCES "UserAuth"(id); + +COMMENT ON COLUMN "UserAuth"."password" IS 'This field stores a password hash'; + +ALTER TABLE "UserInfo" OWNER TO dmptool; +ALTER TABLE "UserAuth" OWNER TO dmptool; + + REVOKE ALL ON SCHEMA public FROM PUBLIC; REVOKE ALL ON SCHEMA public FROM postgres;