From fe0dd478cd3d665fb0a30053a42089e049eff818 Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Sat, 13 Apr 2019 13:52:42 +0200 Subject: [PATCH 01/29] Started reorganising backend code --- .../bugageocaching/controller/Controller.java | 115 +----------------- .../bugageocaching/entities/Station.java | 11 ++ .../util/CacheConstructionUtil.java | 115 ++++++++++++++++++ 3 files changed, 129 insertions(+), 112 deletions(-) create mode 100644 src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 73daf44..91e0c3f 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -9,20 +9,17 @@ import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; -import io.jsonwebtoken.security.Keys; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.crypto.bcrypt.BCrypt; import org.springframework.web.bind.annotation.*; import javax.annotation.PostConstruct; -import javax.xml.bind.DatatypeConverter; -import java.security.Key; import java.security.SecureRandom; import java.util.*; import java.util.concurrent.atomic.AtomicLong; -import java.util.logging.Logger; + +import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.*; @RestController public class Controller { @@ -103,14 +100,6 @@ public class Controller { return ResponseEntity.status(200).body(token); } - /*if (BCrypt.checkpw(user.getPassword(), userRepository.findByUsername(user.getUsername()).getPassword())) { - String token = user.getUsername() + BCrypt.hashpw(String.valueOf(System.currentTimeMillis() + counter.incrementAndGet()), BCrypt.gensalt()); - String hashedToken = BCrypt.hashpw(token, BCrypt.gensalt()); - userRepository.findByUsername(user.getUsername()).setToken(hashedToken); - userRepository.save(userRepository.findByUsername(user.getUsername())); - //return ResponseEntity.ok(new Gson().toJson(token)); - return ResponseEntity.status(200).body(token); - }*/ return ResponseEntity.status(400).body("Es ist ein Fehler aufgetreten"); } @@ -176,113 +165,15 @@ public class Controller { } } - //Eigentlich brauchen wir mit JWT keine Logout Methode mehr. - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose - @RequestMapping("/api/logout") - @ResponseBody - public ResponseEntity logout(@RequestParam String token) { -// System.out.println("logout"); - /*User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); -// System.out.println(token); -// System.out.println(user.getToken()); - if (user == null || user.getToken().isEmpty()) { - return ResponseEntity.status(404).body("User was not found"); - } - user.setToken(null); - userRepository.save(user);*/ - return ResponseEntity.status(200).body("Token was deleted"); - } - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/createCache") @ResponseBody public ResponseEntity createCache(@RequestBody Cache cache) { - - // Stationen werden in die Datenbank eingetragen - for (Station station : cache.getStationen()) { - ResponseEntity response = createStation(station); - if (response.getStatusCodeValue() == 400) { - deleteStationen(cache); - return response; - } - } - - // Caches werden in die Datenbank eingetragen - if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || cache.getRankingPoints() == 0.0 || cache.getStationen().size() == 0) { - deleteStationen(cache); - return ResponseEntity.status(400).body("cache fields can´t be empty"); - } - - for (Cache cache1 : cacheRepository.findAll()) { - if (cache1.getName().equals(cache.getName())) { - deleteStationen(cache); - return ResponseEntity.status(400).body("name is already taken"); - } - } - - if (cache.getRankingPoints() < 0) { - deleteStationen(cache); - return ResponseEntity.status(400).body("Ranking points has to be a positive number"); - } - - cacheRepository.save(cache); - - for (int i = 0; i + 1 < cache.getStationen().size(); i++) { - StationReihenfolge stationReihenfolge = new StationReihenfolge(); - stationReihenfolge.setCache(cache); - stationReihenfolge.setStation(cache.getStationen().get(i)); - stationReihenfolge.setNachfolgeStation(cache.getStationen().get(i + 1)); - stationReihenfolgeRepository.save(stationReihenfolge); - } - - return ResponseEntity.status(200).body(new Gson().toJson(cache)); + return createCacheUtil(cache); } - public ResponseEntity createStation(Station station) { - if (station.getDescription().length() == 0 || station.getLattitude() == 0.0 || station.getLongitude() == 0.0 || station.getSolution().length() == 0) { - return ResponseEntity.status(400).body("station fields can´t be empty"); - } - if (station.getLattitude() < -90 || station.getLattitude() > 90) { - return ResponseEntity.status(400).body("Lattitude has to be between -90 and 90 Degree"); - } - - if (station.getLongitude() < -180 || station.getLongitude() > 180) { - return ResponseEntity.status(400).body("Longitude has to be in the range of -180 to 180 degrees"); - } - - Random r = new Random(); - int low = 100000; - int high = 1000000; - int code = 0; - boolean unique = false; - while (!unique) { - code = r.nextInt(high - low) + low; - unique = true; - for (Station station1 : stationRepository.findAll()) { - if (station1.getCode() == code) { - unique = false; - } - } - } - - station.setCode(code); - - stationRepository.save(station); - - return ResponseEntity.status(200).body(new Gson().toJson(station)); - } - - public void deleteStationen(Cache cache) { - for (Station station : cache.getStationen()) { - try { - stationRepository.delete(station); - } catch (IllegalArgumentException e) { // station is null - // do nothing - } - } - } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/Station.java b/src/main/java/hhn/labsw/bugageocaching/entities/Station.java index fe1344f..31bc0eb 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/Station.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/Station.java @@ -2,6 +2,7 @@ package hhn.labsw.bugageocaching.entities; import javax.persistence.*; +import javax.validation.constraints.NotNull; @Entity @Table @@ -11,10 +12,20 @@ public class Station { @GeneratedValue private int id; + @NotNull private String description; + + @NotNull private double longitude; + + @NotNull private double lattitude; + + @Column(unique = true) + @NotNull private int code; + + @NotNull private String solution; public Station() { diff --git a/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java new file mode 100644 index 0000000..d7594c7 --- /dev/null +++ b/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java @@ -0,0 +1,115 @@ +package hhn.labsw.bugageocaching.util; + +import com.google.gson.Gson; +import hhn.labsw.bugageocaching.entities.Cache; +import hhn.labsw.bugageocaching.entities.Station; +import hhn.labsw.bugageocaching.entities.StationReihenfolge; +import hhn.labsw.bugageocaching.repositories.CacheRepository; +import hhn.labsw.bugageocaching.repositories.StationRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; + +import javax.persistence.RollbackException; +import java.util.Random; + +public class CacheConstructionUtil { + + @Autowired + static StationRepository stationRepository; + + @Autowired + static CacheRepository cacheRepository; + + public static ResponseEntity createCacheUtil(Cache cache){ + + // Stationen werden in die Datenbank eingetragen + for (Station station : cache.getStationen()) { + ResponseEntity response = createStationUtil(station); + if (response.getStatusCodeValue() == 400) { + deleteStationenUtil(cache); + return response; + } + } + + // Caches werden in die Datenbank eingetragen + if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || cache.getRankingPoints() == 0.0 || cache.getStationen().size() == 0) { + deleteStationenUtil(cache); + return ResponseEntity.status(400).body("cache fields can´t be empty"); + } + + for (Cache cache1 : cacheRepository.findAll()) { + if (cache1.getName().equals(cache.getName())) { + deleteStationenUtil(cache); + return ResponseEntity.status(400).body("name is already taken"); + } + } + + if (cache.getRankingPoints() < 0) { + deleteStationenUtil(cache); + return ResponseEntity.status(400).body("Ranking points has to be a positive number"); + } + + cacheRepository.save(cache); + + /*for (int i = 0; i + 1 < cache.getStationen().size(); i++) { + StationReihenfolge stationReihenfolge = new StationReihenfolge(); + stationReihenfolge.setCache(cache); + stationReihenfolge.setStation(cache.getStationen().get(i)); + stationReihenfolge.setNachfolgeStation(cache.getStationen().get(i + 1)); + stationReihenfolgeRepository.save(stationReihenfolge); + }*/ + + return ResponseEntity.status(200).body(new Gson().toJson(cache)); + } + + public static ResponseEntity createStationUtil(Station station) { + + if (station.getDescription().length() == 0 || station.getLattitude() == 0.0 || station.getLongitude() == 0.0 || station.getSolution().length() == 0) { + return ResponseEntity.status(400).body("station fields can´t be empty"); + } + + if (station.getLattitude() < -90 || station.getLattitude() > 90) { + return ResponseEntity.status(400).body("Lattitude has to be between -90 and 90 Degree"); + } + + if (station.getLongitude() < -180 || station.getLongitude() > 180) { + return ResponseEntity.status(400).body("Longitude has to be in the range of -180 to 180 degrees"); + } + + Random r = new Random(); + int low = 100000; + int high = 1000000; + int code = 0; + boolean unique = false; + + while (!unique) { + code = r.nextInt(high - low) + low; + unique = true; + for (Station station1 : stationRepository.findAll()) { + if (station1.getCode() == code) { + unique = false; + } + } + } + + try { + station.setCode(code); + }catch (RollbackException e){ + return ResponseEntity.status(400).body("There was an error generating the unique code"); + } + + stationRepository.save(station); + + return ResponseEntity.status(200).body(new Gson().toJson(station)); + } + + public static void deleteStationenUtil(Cache cache) { + for (Station station : cache.getStationen()) { + try { + stationRepository.delete(station); + } catch (IllegalArgumentException e) { // station is null + // do nothing + } + } + } +} From d718446f369bc694006aa1ae0bc535ea6a4ed16a Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Sat, 13 Apr 2019 14:35:08 +0200 Subject: [PATCH 02/29] More code cleanup --- .../bugageocaching/controller/Controller.java | 53 ++----------------- .../util/CacheConstructionUtil.java | 37 ++++++++++++- 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 95b376a..f2451f3 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -1,7 +1,6 @@ package hhn.labsw.bugageocaching.controller; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import hhn.labsw.bugageocaching.entities.*; import hhn.labsw.bugageocaching.exceptions.IllegalParameterException; import hhn.labsw.bugageocaching.repositories.*; @@ -9,22 +8,18 @@ import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; -import io.jsonwebtoken.security.Keys; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.crypto.bcrypt.BCrypt; import org.springframework.web.bind.annotation.*; import javax.annotation.PostConstruct; -import javax.xml.bind.DatatypeConverter; -import java.lang.reflect.Array; -import java.security.Key; import java.security.SecureRandom; import java.util.*; import java.util.concurrent.atomic.AtomicLong; -import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.*; +import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.createCacheUtil; +import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.deleteCacheUtil; @RestController public class Controller { @@ -49,9 +44,8 @@ public class Controller { @Autowired UserRepository userRepository; - - private AtomicLong counter = new AtomicLong(); byte[] key = new byte[64]; + private AtomicLong counter = new AtomicLong(); @PostConstruct public void init() { @@ -180,9 +174,6 @@ public class Controller { } - - - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkAdmin") @ResponseBody @@ -199,17 +190,6 @@ public class Controller { } catch (Exception e) { return ResponseEntity.status(400).body("JWT Token invalid"); } - - /*User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); - if (user == null) { - return ResponseEntity.status(404).body("User was not found"); - } - for (Role role : user.getRoles()) { - if (role.getId() == 0) { // is admin - return ResponseEntity.status(200).body("User is Admin"); - } - } - return ResponseEntity.status(401).body("User is no Admin");*/ } //Bis hier @@ -224,32 +204,7 @@ public class Controller { @RequestMapping("/api/deleteCache") @ResponseBody public ResponseEntity deleteCache(@RequestParam String cacheID) { - Optional optionalCache = cacheRepository.findById(Integer.valueOf(cacheID)); - if (!optionalCache.isPresent()) { - return ResponseEntity.status(404).body(new Gson().toJson("There is no cache with the ID " + cacheID)); - } - - Cache cache = optionalCache.get(); - - for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) { - if (bearbeitet.getCache().getId() == cache.getId()) { - bearbeitetRepository.delete(bearbeitet); - } - } - - ArrayList stationen = new ArrayList<>(); - for (Station station : cache.getStationen()) { - stationen.add(stationRepository.findById(station.getId()).get()); - } - - cacheRepository.delete(cache); - - for (Station station : stationen) { - stationRepository.delete(station); - } - - - return ResponseEntity.status(200).body(new Gson().toJson(true)); + return deleteCacheUtil(cacheID); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose diff --git a/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java index d7594c7..308e263 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java @@ -1,15 +1,19 @@ package hhn.labsw.bugageocaching.util; import com.google.gson.Gson; +import hhn.labsw.bugageocaching.entities.Bearbeitet; import hhn.labsw.bugageocaching.entities.Cache; import hhn.labsw.bugageocaching.entities.Station; -import hhn.labsw.bugageocaching.entities.StationReihenfolge; +import hhn.labsw.bugageocaching.repositories.BearbeitetRepository; import hhn.labsw.bugageocaching.repositories.CacheRepository; import hhn.labsw.bugageocaching.repositories.StationRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestParam; import javax.persistence.RollbackException; +import java.util.ArrayList; +import java.util.Optional; import java.util.Random; public class CacheConstructionUtil { @@ -20,6 +24,9 @@ public class CacheConstructionUtil { @Autowired static CacheRepository cacheRepository; + @Autowired + static BearbeitetRepository bearbeitetRepository; + public static ResponseEntity createCacheUtil(Cache cache){ // Stationen werden in die Datenbank eingetragen @@ -112,4 +119,32 @@ public class CacheConstructionUtil { } } } + + public static ResponseEntity deleteCacheUtil(String cacheID) { + Optional optionalCache = cacheRepository.findById(Integer.valueOf(cacheID)); + if (!optionalCache.isPresent()) { + return ResponseEntity.status(404).body(new Gson().toJson("There is no cache with the ID " + cacheID)); + } + + Cache cache = optionalCache.get(); + + for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) { + if (bearbeitet.getCache().getId() == cache.getId()) { + bearbeitetRepository.delete(bearbeitet); + } + } + + ArrayList stationen = new ArrayList<>(); + for (Station station : cache.getStationen()) { + stationen.add(stationRepository.findById(station.getId()).get()); + } + + cacheRepository.delete(cache); + + for (Station station : stationen) { + stationRepository.delete(station); + } + + return ResponseEntity.status(200).body(new Gson().toJson(true)); + } } From 43caf76471bd19f7efffaf730c3e760487008bf0 Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Sat, 13 Apr 2019 18:08:11 +0200 Subject: [PATCH 03/29] Implemented public key fetching --- .../bugageocaching/controller/Controller.java | 14 ++++---- .../fetchObjects/PublicKey.java | 13 +++++++ .../util/CacheConstructionUtil.java | 13 ++----- .../bugageocaching/util/VerificationUtil.java | 34 +++++++++++++++++++ 4 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 src/main/java/hhn/labsw/bugageocaching/fetchObjects/PublicKey.java create mode 100644 src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index f2451f3..0bd9fc0 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -2,7 +2,6 @@ package hhn.labsw.bugageocaching.controller; import com.google.gson.Gson; import hhn.labsw.bugageocaching.entities.*; -import hhn.labsw.bugageocaching.exceptions.IllegalParameterException; import hhn.labsw.bugageocaching.repositories.*; import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; @@ -14,12 +13,12 @@ import org.springframework.security.crypto.bcrypt.BCrypt; import org.springframework.web.bind.annotation.*; import javax.annotation.PostConstruct; -import java.security.SecureRandom; +import java.security.Key; import java.util.*; -import java.util.concurrent.atomic.AtomicLong; import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.createCacheUtil; import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.deleteCacheUtil; +import static hhn.labsw.bugageocaching.util.VerificationUtil.fetchPublicKey; @RestController public class Controller { @@ -44,13 +43,12 @@ public class Controller { @Autowired UserRepository userRepository; - byte[] key = new byte[64]; - private AtomicLong counter = new AtomicLong(); + + Key key; @PostConstruct public void init() { - new SecureRandom().nextBytes(key); - System.out.println(Arrays.toString(key)); + key = fetchPublicKey(); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @@ -103,7 +101,7 @@ public class Controller { @RequestMapping("/api/startCache") @ResponseBody public ResponseEntity startCache(@RequestParam(value = "token", defaultValue = "-1") String token, - @RequestParam String cacheID) throws IllegalParameterException { + @RequestParam String cacheID) { if (!token.equals("-1")) { // ein angemeldeter user startet den cache(es werden zwei parameter übergeben) diff --git a/src/main/java/hhn/labsw/bugageocaching/fetchObjects/PublicKey.java b/src/main/java/hhn/labsw/bugageocaching/fetchObjects/PublicKey.java new file mode 100644 index 0000000..198ccca --- /dev/null +++ b/src/main/java/hhn/labsw/bugageocaching/fetchObjects/PublicKey.java @@ -0,0 +1,13 @@ +package hhn.labsw.bugageocaching.fetchObjects; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class PublicKey { + + private String message; + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java index 308e263..ee2fd03 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java @@ -9,7 +9,6 @@ import hhn.labsw.bugageocaching.repositories.CacheRepository; import hhn.labsw.bugageocaching.repositories.StationRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestParam; import javax.persistence.RollbackException; import java.util.ArrayList; @@ -27,7 +26,7 @@ public class CacheConstructionUtil { @Autowired static BearbeitetRepository bearbeitetRepository; - public static ResponseEntity createCacheUtil(Cache cache){ + public static ResponseEntity createCacheUtil(Cache cache) { // Stationen werden in die Datenbank eingetragen for (Station station : cache.getStationen()) { @@ -58,14 +57,6 @@ public class CacheConstructionUtil { cacheRepository.save(cache); - /*for (int i = 0; i + 1 < cache.getStationen().size(); i++) { - StationReihenfolge stationReihenfolge = new StationReihenfolge(); - stationReihenfolge.setCache(cache); - stationReihenfolge.setStation(cache.getStationen().get(i)); - stationReihenfolge.setNachfolgeStation(cache.getStationen().get(i + 1)); - stationReihenfolgeRepository.save(stationReihenfolge); - }*/ - return ResponseEntity.status(200).body(new Gson().toJson(cache)); } @@ -101,7 +92,7 @@ public class CacheConstructionUtil { try { station.setCode(code); - }catch (RollbackException e){ + } catch (RollbackException e) { return ResponseEntity.status(400).body("There was an error generating the unique code"); } diff --git a/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java new file mode 100644 index 0000000..40db8fe --- /dev/null +++ b/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java @@ -0,0 +1,34 @@ +package hhn.labsw.bugageocaching.util; + +import hhn.labsw.bugageocaching.fetchObjects.PublicKey; +import org.springframework.web.client.RestTemplate; + +import java.security.Key; +import java.security.KeyFactory; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; + +public class VerificationUtil { + + public static Key fetchPublicKey() { + RestTemplate restTemplate = new RestTemplate(); + try { + PublicKey response = restTemplate.getForObject("http://seserver.se.hs-heilbronn.de:8090/buga19usermanagement/token/publickey", PublicKey.class); + byte[] decodedKey = Base64.getDecoder().decode(response.getMessage()); + KeyFactory factory = KeyFactory.getInstance("RSA"); + X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodedKey); + Key key = factory.generatePublic(publicKeySpec); + + return key; + } catch (Exception e) { + e.printStackTrace(); + } + + //Fehler muss zurückgegeben werden + return null; + } + + public void verifyToken() { + + } +} From 0f856b1ec2bed6fc55a0490806a615a746bda87f Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Apr 2019 19:44:47 +0200 Subject: [PATCH 04/29] =?UTF-8?q?first=20implementation=20for=20checkStati?= =?UTF-8?q?on(API=20method=20for=20cache=20durchf=C3=BChren)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bugageocaching/controller/Controller.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 0bd9fc0..5951034 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -164,6 +164,81 @@ public class Controller { } } + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/checkStation") + @ResponseBody + public ResponseEntity checkStation(@RequestParam String token, + @RequestParam String cacheID, + @RequestParam String stationID, + @RequestParam String durchgefuehrterCacheID) { + try { + Claims claims = Jwts.parser() //Parse JWT + .setSigningKey(key) + .parseClaimsJws(token).getBody(); + + User user = userRepository.findByUsername(claims.getSubject()); + if (user == null) { + return ResponseEntity.status(404).body("User was not found"); + } + + Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); + Cache cache; + if (cacheOptional.isPresent()) { + cache = cacheOptional.get(); + } else { + return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); + } + + Optional durchgefuehrterCacheIDOptional = cacheRepository.findById(Integer.valueOf(durchgefuehrterCacheID)); + Cache durchgefuehrterCache; + if (durchgefuehrterCacheIDOptional.isPresent()) { + durchgefuehrterCache = cacheOptional.get(); + } else { + return ResponseEntity.status(404).body("Couldnt find Cache " + durchgefuehrterCacheID); + } + + Optional stationOptional = stationRepository.findById(Integer.valueOf(stationID)); + Station station; + if (stationOptional.isPresent()) { + station = stationOptional.get(); + } else { + return ResponseEntity.status(404).body("Couldnt find Station " + stationID); + } + + if (cache != durchgefuehrterCache) { + return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); + } + + Bearbeitet bearbeitet; + if (bearbeitetRepository.findByUserAndCache(user, cache) != null) { + bearbeitet = bearbeitetRepository.findByUserAndCache(user, cache); + } else { + return ResponseEntity.status(400).body("The user has not started this cache yet"); + } + + Station aktuelleStation = bearbeitet.getAktuelleStation(); + + int i = 0; + for (Station station1 : cache.getStationen()) { + if (station1.equals(station)) { + break; + } + i++; + } + + if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { + return ResponseEntity.status(200).body("OK"); + } else { + return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); + } + + } catch (ExpiredJwtException e) { + return ResponseEntity.status(400).body("JWT Token expired"); + } catch (Exception e) { + return ResponseEntity.status(400).body("JWT Token invalid"); + } + } + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/createCache") @ResponseBody From 2a5dbd9e7275df6383a8c4ed66b4455173ae1c22 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Apr 2019 21:18:20 +0200 Subject: [PATCH 05/29] fixed a bug --- .../java/hhn/labsw/bugageocaching/controller/Controller.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 5951034..7177887 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -218,6 +218,10 @@ public class Controller { Station aktuelleStation = bearbeitet.getAktuelleStation(); + if(!cache.getStationen().contains(station)) { + return ResponseEntity.status(400).body("The scanned station isnt a part of the cache"); + } + int i = 0; for (Station station1 : cache.getStationen()) { if (station1.equals(station)) { From 749fcd119a2633b5391dbdfde527c996fede7390 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Apr 2019 21:24:14 +0200 Subject: [PATCH 06/29] fixed more bugs --- .../java/hhn/labsw/bugageocaching/controller/Controller.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 7177887..083b8ff 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -218,7 +218,7 @@ public class Controller { Station aktuelleStation = bearbeitet.getAktuelleStation(); - if(!cache.getStationen().contains(station)) { + if (!cache.getStationen().contains(station)) { return ResponseEntity.status(400).body("The scanned station isnt a part of the cache"); } @@ -231,6 +231,7 @@ public class Controller { } if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { + bearbeitet.setAktuelleStation(station); return ResponseEntity.status(200).body("OK"); } else { return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); From ff7ddde5eef68a5a1f14969d9e0d0125bebebff0 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Apr 2019 21:34:57 +0200 Subject: [PATCH 07/29] returns the station as JSON now if everything is OK --- .../java/hhn/labsw/bugageocaching/controller/Controller.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 083b8ff..c6dd76f 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -232,7 +232,7 @@ public class Controller { if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { bearbeitet.setAktuelleStation(station); - return ResponseEntity.status(200).body("OK"); + return ResponseEntity.status(200).body(new Gson().toJson(station)); } else { return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); } From d14a4b5d86ad7f92d14ca056169ed188e3bd6995 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Apr 2019 21:41:24 +0200 Subject: [PATCH 08/29] modified checkStation --- .../labsw/bugageocaching/controller/Controller.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index c6dd76f..e31b56b 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -232,6 +232,16 @@ public class Controller { if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { bearbeitet.setAktuelleStation(station); + if (i == cache.getStationen().size() - 1) { // letze Station erreicht + Optional cacheAccesDefinitionOptional = + cacheAccesDefinitionRepository.findById(1); // abgeschlossen + if (cacheAccesDefinitionOptional.isPresent()) { + CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); + bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); + } else { + return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 1); + } + } return ResponseEntity.status(200).body(new Gson().toJson(station)); } else { return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); From 4f7b8bd4f657fb2c6f4bbfc36778d18c90a9bd2a Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Apr 2019 22:01:18 +0200 Subject: [PATCH 09/29] added comments --- .../java/hhn/labsw/bugageocaching/controller/Controller.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index e31b56b..9f440bc 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -164,6 +164,10 @@ public class Controller { } } + /** + * Checkt, ob die eingescannte Station die Nachfolgestation der zuletzt + * besuchten Stationen des aktuell durchgeführten Caches ist + */ @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkStation") @ResponseBody From b82bab1f8e3dbd82d708603e12696740c0948aa1 Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Mon, 15 Apr 2019 13:33:29 +0200 Subject: [PATCH 10/29] Stash --- .../bugageocaching/controller/Controller.java | 19 ++++---- .../labsw/bugageocaching/entities/Role.java | 9 ++++ .../labsw/bugageocaching/entities/User.java | 40 +--------------- .../bugageocaching/entities/User_Info.java | 46 +++++++++++++++++++ .../bugageocaching/util/VerificationUtil.java | 13 +++--- 5 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 0bd9fc0..25c4db2 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -3,6 +3,7 @@ package hhn.labsw.bugageocaching.controller; import com.google.gson.Gson; import hhn.labsw.bugageocaching.entities.*; import hhn.labsw.bugageocaching.repositories.*; +import hhn.labsw.bugageocaching.util.VerificationUtil; import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jwts; @@ -13,7 +14,6 @@ import org.springframework.security.crypto.bcrypt.BCrypt; import org.springframework.web.bind.annotation.*; import javax.annotation.PostConstruct; -import java.security.Key; import java.util.*; import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.createCacheUtil; @@ -44,11 +44,10 @@ public class Controller { @Autowired UserRepository userRepository; - Key key; @PostConstruct public void init() { - key = fetchPublicKey(); + fetchPublicKey(); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @@ -78,12 +77,12 @@ public class Controller { .setSubject(user.getUsername()) .claim("admin", userRepository.findByUsername(user.getUsername()).getRoles().stream().anyMatch(x -> x.getId() == 0)) //True if user is admin .setExpiration(new Date(new Date().getTime() + (1000 * 60 * 60 * 24))) //One day expiration - .signWith(signatureAlgorithm, key) + .signWith(signatureAlgorithm, VerificationUtil.publicKey) .compact(); System.out.println(token); Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(key) + .setSigningKey(VerificationUtil.publicKey) .parseClaimsJws(token).getBody(); System.out.println("ID: " + claims.getId()); System.out.println("Subject: " + claims.getSubject()); @@ -109,7 +108,7 @@ public class Controller { try { Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(key) + .setSigningKey(VerificationUtil.publicKey) .parseClaimsJws(token).getBody(); User user = userRepository.findByUsername(claims.getSubject()); @@ -179,7 +178,7 @@ public class Controller { try { Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(key) + .setSigningKey(VerificationUtil.publicKey) .parseClaimsJws(token).getBody(); return ResponseEntity.status(200).body(claims.get("admin")); @@ -212,7 +211,7 @@ public class Controller { try { Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(key) + .setSigningKey(VerificationUtil.publicKey) .parseClaimsJws(token).getBody(); @@ -248,7 +247,7 @@ public class Controller { User u = new User(); u.setId((int) obj[0]); u.setUsername((String) obj[1]); - u.setRankingPointsSum((int) obj[2]); + //u.setRankingPointsSum((int) obj[2]); sendBackUsers.add(u); } @@ -261,7 +260,7 @@ public class Controller { public ResponseEntity getUser(@RequestParam String token) { try { Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(key) + .setSigningKey(VerificationUtil.publicKey) .parseClaimsJws(token).getBody(); diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/Role.java b/src/main/java/hhn/labsw/bugageocaching/entities/Role.java index b5612bc..3e4a9c5 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/Role.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/Role.java @@ -11,6 +11,7 @@ public class Role { @GeneratedValue private int id; private String name; + private String domain; public Role() { @@ -32,6 +33,14 @@ public class Role { this.name = name; } + public String getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } + @Override public String toString() { return name; diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/User.java b/src/main/java/hhn/labsw/bugageocaching/entities/User.java index 0b546f6..81f2e12 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/User.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/User.java @@ -15,10 +15,8 @@ public class User { @GeneratedValue private int id; - private String firstname; - private String lastname; private String username; - private int rankingPointsSum; + private String email; private String password; @@ -26,10 +24,6 @@ public class User { @ManyToMany private List roles; - @ManyToOne - private Team team; - - @Transient private String passwordConfirm; @@ -41,22 +35,6 @@ public class User { this.id = id; } - public String getFirstname() { - return firstname; - } - - public void setFirstname(String firstname) { - this.firstname = firstname; - } - - public String getLastname() { - return lastname; - } - - public void setLastname(String lastname) { - this.lastname = lastname; - } - public String getUsername() { return username; } @@ -65,14 +43,6 @@ public class User { this.username = username; } - public int getRankingPointsSum() { - return rankingPointsSum; - } - - public void setRankingPointsSum(int rankingPointsSum) { - this.rankingPointsSum = rankingPointsSum; - } - public String getEmail() { return email; } @@ -89,14 +59,6 @@ public class User { this.password = password; } - public Team getTeam() { - return team; - } - - public void setTeam(Team team) { - this.team = team; - } - public List getRoles() { return roles; } diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java b/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java new file mode 100644 index 0000000..08c43fd --- /dev/null +++ b/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java @@ -0,0 +1,46 @@ +package hhn.labsw.bugageocaching.entities; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +@Table +public class User_Info implements Serializable { + + @OneToOne + @Id + private User userID; + + private int rankingPointsSum; + + @ManyToOne + private Team teamID; + + + public User_Info() { + } + + public User getUserID() { + return userID; + } + + public void setUserID(User userID) { + this.userID = userID; + } + + public int getRankingPointsSum() { + return rankingPointsSum; + } + + public void setRankingPointsSum(int rankingPointsSum) { + this.rankingPointsSum = rankingPointsSum; + } + + public Team getTeamID() { + return teamID; + } + + public void setTeamID(Team teamID) { + this.teamID = teamID; + } +} diff --git a/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java index 40db8fe..26648f0 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java @@ -1,6 +1,8 @@ package hhn.labsw.bugageocaching.util; import hhn.labsw.bugageocaching.fetchObjects.PublicKey; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import java.security.Key; @@ -10,7 +12,9 @@ import java.util.Base64; public class VerificationUtil { - public static Key fetchPublicKey() { + public static Key publicKey; + + public static void fetchPublicKey() { RestTemplate restTemplate = new RestTemplate(); try { PublicKey response = restTemplate.getForObject("http://seserver.se.hs-heilbronn.de:8090/buga19usermanagement/token/publickey", PublicKey.class); @@ -19,16 +23,13 @@ public class VerificationUtil { X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodedKey); Key key = factory.generatePublic(publicKeySpec); - return key; + publicKey = key; } catch (Exception e) { e.printStackTrace(); } //Fehler muss zurückgegeben werden - return null; } - public void verifyToken() { - - } + //Verify methode } From 41c5dc482dbc93b0de5653076b95e91fc9e115c2 Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Mon, 15 Apr 2019 17:06:16 +0200 Subject: [PATCH 11/29] Added FinderUtil --- .../bugageocaching/controller/Controller.java | 178 ++++++++++-------- .../bugageocaching/entities/Bearbeitet.java | 1 + .../labsw/bugageocaching/util/FinderUtil.java | 67 +++++++ .../bugageocaching/util/VerificationUtil.java | 18 +- 4 files changed, 184 insertions(+), 80 deletions(-) create mode 100644 src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 21ee6b3..2f14845 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -3,6 +3,7 @@ package hhn.labsw.bugageocaching.controller; import com.google.gson.Gson; import hhn.labsw.bugageocaching.entities.*; import hhn.labsw.bugageocaching.repositories.*; +import hhn.labsw.bugageocaching.util.FinderUtil; import hhn.labsw.bugageocaching.util.VerificationUtil; import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; @@ -174,87 +175,106 @@ public class Controller { @RequestParam String cacheID, @RequestParam String stationID, @RequestParam String durchgefuehrterCacheID) { - try { - Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(key) - .parseClaimsJws(token).getBody(); + //---------------------- + //Verify token + ResponseEntity tokenVerification = VerificationUtil.verifyToken(token); - User user = userRepository.findByUsername(claims.getSubject()); - if (user == null) { - return ResponseEntity.status(404).body("User was not found"); - } - - Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); - Cache cache; - if (cacheOptional.isPresent()) { - cache = cacheOptional.get(); - } else { - return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); - } - - Optional durchgefuehrterCacheIDOptional = cacheRepository.findById(Integer.valueOf(durchgefuehrterCacheID)); - Cache durchgefuehrterCache; - if (durchgefuehrterCacheIDOptional.isPresent()) { - durchgefuehrterCache = cacheOptional.get(); - } else { - return ResponseEntity.status(404).body("Couldnt find Cache " + durchgefuehrterCacheID); - } - - Optional stationOptional = stationRepository.findById(Integer.valueOf(stationID)); - Station station; - if (stationOptional.isPresent()) { - station = stationOptional.get(); - } else { - return ResponseEntity.status(404).body("Couldnt find Station " + stationID); - } - - if (cache != durchgefuehrterCache) { - return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); - } - - Bearbeitet bearbeitet; - if (bearbeitetRepository.findByUserAndCache(user, cache) != null) { - bearbeitet = bearbeitetRepository.findByUserAndCache(user, cache); - } else { - return ResponseEntity.status(400).body("The user has not started this cache yet"); - } - - Station aktuelleStation = bearbeitet.getAktuelleStation(); - - if (!cache.getStationen().contains(station)) { - return ResponseEntity.status(400).body("The scanned station isnt a part of the cache"); - } - - int i = 0; - for (Station station1 : cache.getStationen()) { - if (station1.equals(station)) { - break; - } - i++; - } - - if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { - bearbeitet.setAktuelleStation(station); - if (i == cache.getStationen().size() - 1) { // letze Station erreicht - Optional cacheAccesDefinitionOptional = - cacheAccesDefinitionRepository.findById(1); // abgeschlossen - if (cacheAccesDefinitionOptional.isPresent()) { - CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); - bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); - } else { - return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 1); - } - } - return ResponseEntity.status(200).body(new Gson().toJson(station)); - } else { - return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); - } - - } catch (ExpiredJwtException e) { - return ResponseEntity.status(400).body("JWT Token expired"); - } catch (Exception e) { - return ResponseEntity.status(400).body("JWT Token invalid"); + //Error in token verification + if (tokenVerification.getStatusCodeValue() != 200) { + return tokenVerification; } + + Claims claims = (Claims) tokenVerification.getBody(); + + User user = userRepository.findByUsername(claims.getSubject()); + if (user == null) { + return ResponseEntity.status(404).body("User was not found"); + } + //---------------------- + + //---------------------- + //Get Cache + ResponseEntity getCache = FinderUtil.findCacheById(cacheID); + + if(getCache.getStatusCodeValue() != 200){ + return getCache; + } + + Cache cache = (Cache) getCache.getBody(); + //---------------------- + + //---------------------- + //Get durchgeführter Cache + ResponseEntity getDurchgefuehrterCache = FinderUtil.findCacheById(durchgefuehrterCacheID); + + if(getDurchgefuehrterCache.getStatusCodeValue() != 200){ + return getDurchgefuehrterCache; + } + + Cache durchgefuehrterCache = (Cache) getDurchgefuehrterCache.getBody(); + //---------------------- + + //---------------------- + //Get Station + ResponseEntity getStation = FinderUtil.findStationById(stationID); + + if(getStation.getStatusCodeValue() != 200){ + return getStation; + } + + Station station = (Station) getStation.getBody(); + //---------------------- + + if (cache != durchgefuehrterCache) { + return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); + } + + //---------------------- + //Get Bearbeitet entry + ResponseEntity getBearbeitet = FinderUtil.findBearbeitetByUserAndCache(user, cache); + + if(getBearbeitet.getStatusCodeValue() != 200){ + return getBearbeitet; + } + + Bearbeitet bearbeitet = (Bearbeitet) getBearbeitet.getBody(); + //---------------------- + + + Station aktuelleStation = bearbeitet.getAktuelleStation(); + if(aktuelleStation == null){ + return ResponseEntity.status(400).body("Database Error"); + } + + if (!cache.getStationen().contains(station)) { + return ResponseEntity.status(400).body("The scanned station isnt a part of the cache"); + } + + int i = 0; + for (Station station1 : cache.getStationen()) { + if (station1.equals(station)) { + break; + } + i++; + } + + if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { + bearbeitet.setAktuelleStation(station); + if (i == cache.getStationen().size() - 1) { // letze Station erreicht + Optional cacheAccesDefinitionOptional = + cacheAccesDefinitionRepository.findById(1); // abgeschlossen + if (cacheAccesDefinitionOptional.isPresent()) { + CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); + bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); + } else { + return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 1); + } + } + return ResponseEntity.status(200).body(new Gson().toJson(station)); + } else { + return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); + } + } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/Bearbeitet.java b/src/main/java/hhn/labsw/bugageocaching/entities/Bearbeitet.java index f046d84..8b1f3dc 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/Bearbeitet.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/Bearbeitet.java @@ -2,6 +2,7 @@ package hhn.labsw.bugageocaching.entities; import javax.persistence.*; +import javax.validation.constraints.NotNull; @Entity @Table diff --git a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java new file mode 100644 index 0000000..d3590ae --- /dev/null +++ b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java @@ -0,0 +1,67 @@ +package hhn.labsw.bugageocaching.util; + +import hhn.labsw.bugageocaching.entities.Bearbeitet; +import hhn.labsw.bugageocaching.entities.Cache; +import hhn.labsw.bugageocaching.entities.Station; +import hhn.labsw.bugageocaching.entities.User; +import hhn.labsw.bugageocaching.repositories.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; + +import java.util.Optional; + +public class FinderUtil { + + @Autowired + static CacheRepository cacheRepository; + + @Autowired + static RewardRepository rewardRepository; + + @Autowired + static StationRepository stationRepository; + + @Autowired + static BearbeitetRepository bearbeitetRepository; + + @Autowired + static CacheAccesDefinitionRepository cacheAccesDefinitionRepository; + + @Autowired + static TeamRepository teamRepository; + + @Autowired + static UserRepository userRepository; + + public static ResponseEntity findCacheById(String cacheID) { + + Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); + if (cacheOptional.isPresent()) { + return ResponseEntity.status(200).body(cacheOptional.get()); + } else { + return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); + } + } + + public static ResponseEntity findStationById(String stationID){ + + Optional stationOptional = stationRepository.findById(Integer.valueOf(stationID)); + if (stationOptional.isPresent()) { + return ResponseEntity.status(200).body(stationOptional.get()); + } else { + return ResponseEntity.status(404).body("Couldnt find Station " + stationID); + } + } + + public static ResponseEntity findBearbeitetByUserAndCache(User user, Cache cache){ + + Bearbeitet bearbeitet = bearbeitetRepository.findByUserAndCache(user, cache); + + if(bearbeitet != null){ + return ResponseEntity.status(200).body(bearbeitet); + } + + return ResponseEntity.status(404).body("The user has not started this cache yet"); + } + +} diff --git a/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java index 26648f0..b29ae41 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/VerificationUtil.java @@ -1,6 +1,9 @@ package hhn.labsw.bugageocaching.util; import hhn.labsw.bugageocaching.fetchObjects.PublicKey; +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.ExpiredJwtException; +import io.jsonwebtoken.Jwts; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; @@ -31,5 +34,18 @@ public class VerificationUtil { //Fehler muss zurückgegeben werden } - //Verify methode + public static ResponseEntity verifyToken(String token){ + + try{ + Claims claims = Jwts.parser() //Parse JWT + .setSigningKey(VerificationUtil.publicKey) + .parseClaimsJws(token).getBody(); + + return ResponseEntity.status(200).body(claims); + } catch (ExpiredJwtException e){ + return ResponseEntity.status(401).body("JWT Token expired"); + } catch (Exception e){ + return ResponseEntity.status(400).body("Something went wrong"); + } + } } From 663d3482ee1a0679254dbfbfb5990d9ec34ea20c Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Mon, 15 Apr 2019 17:23:34 +0200 Subject: [PATCH 12/29] Changed Methods to user FinderUtil --- .../bugageocaching/controller/Controller.java | 224 ++++++++++-------- .../labsw/bugageocaching/util/FinderUtil.java | 10 + 2 files changed, 135 insertions(+), 99 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 2f14845..66e1f5f 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -6,7 +6,6 @@ import hhn.labsw.bugageocaching.repositories.*; import hhn.labsw.bugageocaching.util.FinderUtil; import hhn.labsw.bugageocaching.util.VerificationUtil; import io.jsonwebtoken.Claims; -import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.springframework.beans.factory.annotation.Autowired; @@ -107,60 +106,75 @@ public class Controller { Bearbeitet bearbeitet = new Bearbeitet(); - try { - Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(VerificationUtil.publicKey) - .parseClaimsJws(token).getBody(); - User user = userRepository.findByUsername(claims.getSubject()); - if (user == null) { - return ResponseEntity.status(404).body("User was not found"); - } - bearbeitet.setUser(user); + //---------------------- + //Verify token + ResponseEntity tokenVerification = VerificationUtil.verifyToken(token); - Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); - if (cacheOptional.isPresent()) { - Cache cache = cacheOptional.get(); - - if (bearbeitetRepository.findByUserAndCache(user, cache) != null) { - Bearbeitet bearbeitet1 = bearbeitetRepository.findByUserAndCache(user, cache); - return ResponseEntity.status(200).body(bearbeitet1); - } - - bearbeitet.setCache(cache); - - Station startStation = cache.getStationen().get(0); - bearbeitet.setAktuelleStation(startStation); - } else { - return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); - } - - Optional cacheAccesDefinitionOptional = - cacheAccesDefinitionRepository.findById(0); // angefangen - if (cacheAccesDefinitionOptional.isPresent()) { - CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); - bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); - } else { - return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 0); - } - - bearbeitetRepository.save(bearbeitet); - - return ResponseEntity.status(201).body(new Gson().toJson(bearbeitet)); - } catch (ExpiredJwtException e) { - return ResponseEntity.status(400).body("JWT Token expired"); - } catch (Exception e) { - return ResponseEntity.status(400).body("JWT Token invalid"); + //Error in token verification + if (tokenVerification.getStatusCodeValue() != 200) { + return tokenVerification; } + Claims claims = (Claims) tokenVerification.getBody(); + + + ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject()); + + if (getUser.getStatusCodeValue() != 200) { + return getUser; + } + + User user = (User) getUser.getBody(); + + bearbeitet.setUser(user); + + //---------------------- + //Get Cache + ResponseEntity getCache = FinderUtil.findCacheById(cacheID); + + if (getCache.getStatusCodeValue() != 200) { + return getCache; + } + + Cache cache = (Cache) getCache.getBody(); + //---------------------- + + if (bearbeitetRepository.findByUserAndCache(user, cache) != null) { + Bearbeitet bearbeitet1 = bearbeitetRepository.findByUserAndCache(user, cache); + return ResponseEntity.status(200).body(bearbeitet1); + } + + bearbeitet.setCache(cache); + + Station startStation = cache.getStationen().get(0); + bearbeitet.setAktuelleStation(startStation); + + + Optional cacheAccesDefinitionOptional = + cacheAccesDefinitionRepository.findById(0); // angefangen + if (cacheAccesDefinitionOptional.isPresent()) { + CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); + bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); + } else { + return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 0); + } + + bearbeitetRepository.save(bearbeitet); + + return ResponseEntity.status(201).body(new Gson().toJson(bearbeitet)); + } else { // kein angemeldeter User startet den cache(es wird nur der cache als parameter übergeben) - Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); - if (cacheOptional.isPresent()) { - Cache cache = cacheOptional.get(); - return ResponseEntity.status(200).body(new Gson().toJson(cache)); - } else { - return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); + + ResponseEntity getCache = FinderUtil.findCacheById(cacheID); + + if (getCache.getStatusCodeValue() != 200) { + return getCache; } + + Cache cache = (Cache) getCache.getBody(); + + return ResponseEntity.status(200).body(new Gson().toJson(cache)); } } @@ -186,17 +200,21 @@ public class Controller { Claims claims = (Claims) tokenVerification.getBody(); - User user = userRepository.findByUsername(claims.getSubject()); - if (user == null) { - return ResponseEntity.status(404).body("User was not found"); + + ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject()); + + if (getUser.getStatusCodeValue() != 200) { + return getUser; } + + User user = (User) getUser.getBody(); //---------------------- //---------------------- //Get Cache ResponseEntity getCache = FinderUtil.findCacheById(cacheID); - if(getCache.getStatusCodeValue() != 200){ + if (getCache.getStatusCodeValue() != 200) { return getCache; } @@ -207,7 +225,7 @@ public class Controller { //Get durchgeführter Cache ResponseEntity getDurchgefuehrterCache = FinderUtil.findCacheById(durchgefuehrterCacheID); - if(getDurchgefuehrterCache.getStatusCodeValue() != 200){ + if (getDurchgefuehrterCache.getStatusCodeValue() != 200) { return getDurchgefuehrterCache; } @@ -218,7 +236,7 @@ public class Controller { //Get Station ResponseEntity getStation = FinderUtil.findStationById(stationID); - if(getStation.getStatusCodeValue() != 200){ + if (getStation.getStatusCodeValue() != 200) { return getStation; } @@ -233,7 +251,7 @@ public class Controller { //Get Bearbeitet entry ResponseEntity getBearbeitet = FinderUtil.findBearbeitetByUserAndCache(user, cache); - if(getBearbeitet.getStatusCodeValue() != 200){ + if (getBearbeitet.getStatusCodeValue() != 200) { return getBearbeitet; } @@ -242,7 +260,7 @@ public class Controller { Station aktuelleStation = bearbeitet.getAktuelleStation(); - if(aktuelleStation == null){ + if (aktuelleStation == null) { return ResponseEntity.status(400).body("Database Error"); } @@ -290,17 +308,15 @@ public class Controller { @ResponseBody public ResponseEntity checkAdmin(@RequestParam String token) { - try { - Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(VerificationUtil.publicKey) - .parseClaimsJws(token).getBody(); + ResponseEntity verifyToken = VerificationUtil.verifyToken(token); - return ResponseEntity.status(200).body(claims.get("admin")); - } catch (ExpiredJwtException e) { - return ResponseEntity.status(400).body("JWT Token expired"); - } catch (Exception e) { - return ResponseEntity.status(400).body("JWT Token invalid"); + if (verifyToken.getStatusCodeValue() != 200) { + return verifyToken; } + + Claims claims = (Claims) verifyToken.getBody(); + + return ResponseEntity.status(200).body(claims.get("admin")); } //Bis hier @@ -322,31 +338,35 @@ public class Controller { @RequestMapping("/api/getMyCaches") @ResponseBody public ResponseEntity getMyCaches(@RequestParam String token) { - try { - - Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(VerificationUtil.publicKey) - .parseClaimsJws(token).getBody(); - User user = userRepository.findByUsername(claims.getSubject()); + ResponseEntity verifyToken = VerificationUtil.verifyToken(token); - if (user != null) { - ArrayList bearbeitetList = new ArrayList<>(); + if (verifyToken.getStatusCodeValue() != 200) { + return verifyToken; + } - for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) { - if (bearbeitet.getUser().getId() == user.getId()) { - bearbeitetList.add(bearbeitet); - } + Claims claims = (Claims) verifyToken.getBody(); + + ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject()); + + if (getUser.getStatusCodeValue() != 200) { + return getUser; + } + + User user = (User) getUser.getBody(); + + if (user != null) { + ArrayList bearbeitetList = new ArrayList<>(); + + for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) { + if (bearbeitet.getUser().getId() == user.getId()) { + bearbeitetList.add(bearbeitet); } - return ResponseEntity.status(200).body(new Gson().toJson(bearbeitetList)); - } else { - return ResponseEntity.status(404).body("User was not found in the database"); } - } catch (ExpiredJwtException e) { - return ResponseEntity.status(400).body("JWT Token expired"); - } catch (Exception e) { - return ResponseEntity.status(400).body("JWT Token invalid"); + return ResponseEntity.status(200).body(new Gson().toJson(bearbeitetList)); + } else { + return ResponseEntity.status(404).body("User was not found in the database"); } } @@ -372,23 +392,29 @@ public class Controller { @RequestMapping("/api/getUser") @ResponseBody public ResponseEntity getUser(@RequestParam String token) { - try { - Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(VerificationUtil.publicKey) - .parseClaimsJws(token).getBody(); + ResponseEntity verifyToken = VerificationUtil.verifyToken(token); - User user = userRepository.findByUsername(claims.getSubject()); - if (user != null) { - return ResponseEntity.status(200).body(new Gson().toJson(user)); - } else { - return ResponseEntity.status(404).body("User was not found in the database"); - } - } catch (ExpiredJwtException e) { - return ResponseEntity.status(400).body("JWT Token expired"); - } catch (Exception e) { - return ResponseEntity.status(400).body("JWT Token invalid"); + if (verifyToken.getStatusCodeValue() != 200) { + return verifyToken; + } + + Claims claims = (Claims) verifyToken.getBody(); + + ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject()); + + if (getUser.getStatusCodeValue() != 200) { + return getUser; + } + + User user = (User) getUser.getBody(); + + if (user != null) { + return ResponseEntity.status(200).body(new Gson().toJson(user)); + } else { + return ResponseEntity.status(404).body("User was not found in the database"); } } } + diff --git a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java index d3590ae..e128a12 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java @@ -64,4 +64,14 @@ public class FinderUtil { return ResponseEntity.status(404).body("The user has not started this cache yet"); } + public static ResponseEntity findUserByUsername(String username){ + + User user = userRepository.findByUsername(username); + if(user != null){ + return ResponseEntity.status(200).body(user); + } + + return ResponseEntity.status(404).body("Couldnt find user with username " + username); + } + } From 4b16b6a2b1a4ba66998aee11918e0bacb8417633 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 16 Apr 2019 10:17:36 +0200 Subject: [PATCH 13/29] returns bearbeitet instead of the station now so the frontend is able to see whether the cache is finished or not added a FinderUtil method for cacheAccesDefinition --- .../bugageocaching/controller/Controller.java | 18 ++++++++------- .../labsw/bugageocaching/util/FinderUtil.java | 23 +++++++++++-------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 66e1f5f..16ed376 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -279,16 +279,18 @@ public class Controller { if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { bearbeitet.setAktuelleStation(station); if (i == cache.getStationen().size() - 1) { // letze Station erreicht - Optional cacheAccesDefinitionOptional = - cacheAccesDefinitionRepository.findById(1); // abgeschlossen - if (cacheAccesDefinitionOptional.isPresent()) { - CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); - bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); - } else { - return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 1); + //---------------------- + //Get CacheAccesDefinition + ResponseEntity getCacheAccesDefinition = FinderUtil.findCacheAccesDefinitionById("1"); + + if (getCacheAccesDefinition.getStatusCodeValue() != 200) { + return getCacheAccesDefinition; } + + CacheAccesDefinition cacheAccesDefinition = (CacheAccesDefinition) getCacheAccesDefinition.getBody(); + //---------------------- } - return ResponseEntity.status(200).body(new Gson().toJson(station)); + return ResponseEntity.status(200).body(new Gson().toJson(bearbeitet)); } else { return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); } diff --git a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java index e128a12..21f4770 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java @@ -1,9 +1,6 @@ package hhn.labsw.bugageocaching.util; -import hhn.labsw.bugageocaching.entities.Bearbeitet; -import hhn.labsw.bugageocaching.entities.Cache; -import hhn.labsw.bugageocaching.entities.Station; -import hhn.labsw.bugageocaching.entities.User; +import hhn.labsw.bugageocaching.entities.*; import hhn.labsw.bugageocaching.repositories.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -43,7 +40,7 @@ public class FinderUtil { } } - public static ResponseEntity findStationById(String stationID){ + public static ResponseEntity findStationById(String stationID) { Optional stationOptional = stationRepository.findById(Integer.valueOf(stationID)); if (stationOptional.isPresent()) { @@ -53,25 +50,33 @@ public class FinderUtil { } } - public static ResponseEntity findBearbeitetByUserAndCache(User user, Cache cache){ + public static ResponseEntity findBearbeitetByUserAndCache(User user, Cache cache) { Bearbeitet bearbeitet = bearbeitetRepository.findByUserAndCache(user, cache); - if(bearbeitet != null){ + if (bearbeitet != null) { return ResponseEntity.status(200).body(bearbeitet); } return ResponseEntity.status(404).body("The user has not started this cache yet"); } - public static ResponseEntity findUserByUsername(String username){ + public static ResponseEntity findUserByUsername(String username) { User user = userRepository.findByUsername(username); - if(user != null){ + if (user != null) { return ResponseEntity.status(200).body(user); } return ResponseEntity.status(404).body("Couldnt find user with username " + username); } + public static ResponseEntity findCacheAccesDefinitionById(String cacheAccesDefinitionID) { + Optional cacheAccesDefinitionOptional = cacheAccesDefinitionRepository.findById(Integer.valueOf(cacheAccesDefinitionID)); + if (cacheAccesDefinitionOptional.isPresent()) { + return ResponseEntity.status(200).body(cacheAccesDefinitionOptional.get()); + } else { + return ResponseEntity.status(404).body("Couldnt find CacheAccesDefinition " + cacheAccesDefinitionID); + } + } } From de852f08f412a0990cf1d673103560debfb3672e Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 16 Apr 2019 10:21:15 +0200 Subject: [PATCH 14/29] fixed a bug --- .../java/hhn/labsw/bugageocaching/controller/Controller.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 16ed376..685d553 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -288,6 +288,7 @@ public class Controller { } CacheAccesDefinition cacheAccesDefinition = (CacheAccesDefinition) getCacheAccesDefinition.getBody(); + bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); //---------------------- } return ResponseEntity.status(200).body(new Gson().toJson(bearbeitet)); From 01b1ca190d195115ea3d074861680f8c23df6611 Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Tue, 16 Apr 2019 18:11:35 +0200 Subject: [PATCH 15/29] Made User ID not autogenerated bcs. the database wouldnt work otherwise --- src/main/java/hhn/labsw/bugageocaching/entities/User.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/User.java b/src/main/java/hhn/labsw/bugageocaching/entities/User.java index 81f2e12..656a7f3 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/User.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/User.java @@ -12,7 +12,6 @@ import java.util.List; public class User { @Id - @GeneratedValue private int id; private String username; From cd6cd061cd037b3414957a39d1869d32535bea82 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 16 Apr 2019 22:49:51 +0200 Subject: [PATCH 16/29] - user info uses foreign keys again - user is the primary key - added trigger so user_info is automatically generated whenever a user is created/deleted --- .../bugageocaching/entities/User_Info.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java b/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java index 08c43fd..5cd5025 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java @@ -1,5 +1,7 @@ package hhn.labsw.bugageocaching.entities; +import org.springframework.context.annotation.Primary; + import javax.persistence.*; import java.io.Serializable; @@ -7,25 +9,25 @@ import java.io.Serializable; @Table public class User_Info implements Serializable { - @OneToOne @Id - private User userID; + @OneToOne + private User user; private int rankingPointsSum; @ManyToOne - private Team teamID; + private Team team; public User_Info() { } - public User getUserID() { - return userID; + public User getUser() { + return user; } - public void setUserID(User userID) { - this.userID = userID; + public void setUser(User user) { + this.user = user; } public int getRankingPointsSum() { @@ -36,11 +38,11 @@ public class User_Info implements Serializable { this.rankingPointsSum = rankingPointsSum; } - public Team getTeamID() { - return teamID; + public Team getTeam() { + return team; } - public void setTeamID(Team teamID) { - this.teamID = teamID; + public void setTeam(Team team) { + this.team = team; } } From 3ec8ac93e94e94d3e03eb6052cd9ee55b446185e Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 01:06:58 +0200 Subject: [PATCH 17/29] added User_InfoRepository --- .../bugageocaching/repositories/User_InfoRepository.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/java/hhn/labsw/bugageocaching/repositories/User_InfoRepository.java diff --git a/src/main/java/hhn/labsw/bugageocaching/repositories/User_InfoRepository.java b/src/main/java/hhn/labsw/bugageocaching/repositories/User_InfoRepository.java new file mode 100644 index 0000000..a96b12d --- /dev/null +++ b/src/main/java/hhn/labsw/bugageocaching/repositories/User_InfoRepository.java @@ -0,0 +1,8 @@ +package hhn.labsw.bugageocaching.repositories; + +import hhn.labsw.bugageocaching.entities.User_Info; +import org.springframework.data.repository.CrudRepository; + +public interface User_InfoRepository extends CrudRepository { + +} From e235a72c088dd8feb01815ac7d182523d71f205e Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 01:07:19 +0200 Subject: [PATCH 18/29] added a method to find a user_info by id --- .../hhn/labsw/bugageocaching/util/FinderUtil.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java index 21f4770..1b3672a 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java @@ -30,6 +30,9 @@ public class FinderUtil { @Autowired static UserRepository userRepository; + @Autowired + static User_InfoRepository user_infoRepository; + public static ResponseEntity findCacheById(String cacheID) { Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); @@ -79,4 +82,13 @@ public class FinderUtil { return ResponseEntity.status(404).body("Couldnt find CacheAccesDefinition " + cacheAccesDefinitionID); } } + + public static ResponseEntity findUser_InfoByID(String infoID) { + Optional user_InfoOptional = user_infoRepository.findById(Integer.valueOf(infoID)); + if (user_InfoOptional.isPresent()) { + return ResponseEntity.status(200).body(user_InfoOptional.get()); + } else { + return ResponseEntity.status(404).body("Couldnt find User_Info " + infoID); + } + } } From 5845f85c65005a0e9f283c709042f839d88e3746 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 01:09:19 +0200 Subject: [PATCH 19/29] checkStation also adds the rankingPoints to the users current score after the cache is done and saves it in user_info --- .../bugageocaching/controller/Controller.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 685d553..7010828 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -44,6 +44,9 @@ public class Controller { @Autowired UserRepository userRepository; + @Autowired + User_InfoRepository user_infoRepository; + @PostConstruct public void init() { @@ -288,8 +291,20 @@ public class Controller { } CacheAccesDefinition cacheAccesDefinition = (CacheAccesDefinition) getCacheAccesDefinition.getBody(); - bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); //---------------------- + bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); + //Get User_Info + ResponseEntity getUser_Info = FinderUtil.findUser_InfoByID(String.valueOf(user.getId())); + + if (getUser_Info.getStatusCodeValue() != 200) { + return getUser_Info; + } + + User_Info user_info = (User_Info) getUser_Info.getBody(); + //---------------------- + user_info.setRankingPointsSum(user_info.getRankingPointsSum() + cache.getRankingPoints()); + user_infoRepository.save(user_info); + bearbeitetRepository.save(bearbeitet); } return ResponseEntity.status(200).body(new Gson().toJson(bearbeitet)); } else { From 363640d5b7e62b6ae88b99941843abc010435611 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 01:11:09 +0200 Subject: [PATCH 20/29] deleted login, login is done through the userManagement now --- .../bugageocaching/controller/Controller.java | 39 ------------------- 1 file changed, 39 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 7010828..eab8d0f 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -60,45 +60,6 @@ public class Controller { return ResponseEntity.status(200).body(new Gson().toJson(cacheRepository.findAll())); } - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose - @RequestMapping("/api/login") - @ResponseBody - public ResponseEntity login(@RequestBody User user) { - if (user.getUsername() == null || user.getPassword() == null) { - System.out.println(user.getUsername()); - System.out.println(user.getPassword()); - return ResponseEntity.status(400).body("Username or password cant be null"); - } - if (userRepository.findByUsername(user.getUsername()) == null) { - return ResponseEntity.status(404).body("User was not found"); - } - - SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; - - if (BCrypt.checkpw(user.getPassword(), userRepository.findByUsername(user.getUsername()).getPassword())) { - String token = Jwts.builder() - .setSubject(user.getUsername()) - .claim("admin", userRepository.findByUsername(user.getUsername()).getRoles().stream().anyMatch(x -> x.getId() == 0)) //True if user is admin - .setExpiration(new Date(new Date().getTime() + (1000 * 60 * 60 * 24))) //One day expiration - .signWith(signatureAlgorithm, VerificationUtil.publicKey) - .compact(); - System.out.println(token); - - Claims claims = Jwts.parser() //Parse JWT - .setSigningKey(VerificationUtil.publicKey) - .parseClaimsJws(token).getBody(); - System.out.println("ID: " + claims.getId()); - System.out.println("Subject: " + claims.getSubject()); - System.out.println("Issuer: " + claims.getIssuer()); - System.out.println("Admin: " + claims.get("admin")); - System.out.println("Expiration: " + claims.getExpiration()); - - return ResponseEntity.status(200).body(token); - } - - return ResponseEntity.status(400).body("Es ist ein Fehler aufgetreten"); - } - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/startCache") @ResponseBody From 07e88255545747ffb4a821278446ed72e5f38e94 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 02:40:15 +0200 Subject: [PATCH 21/29] userInfo primamry key consists or user/rankingPointsSum now --- .../java/hhn/labsw/bugageocaching/entities/User_Info.java | 2 ++ .../hhn/labsw/bugageocaching/entities/User_InfoID.java | 8 ++++++++ 2 files changed, 10 insertions(+) create mode 100644 src/main/java/hhn/labsw/bugageocaching/entities/User_InfoID.java diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java b/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java index 5cd5025..74ff0a3 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java @@ -7,12 +7,14 @@ import java.io.Serializable; @Entity @Table +@IdClass(User_InfoID.class) public class User_Info implements Serializable { @Id @OneToOne private User user; + @Id private int rankingPointsSum; @ManyToOne diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/User_InfoID.java b/src/main/java/hhn/labsw/bugageocaching/entities/User_InfoID.java new file mode 100644 index 0000000..4d63e41 --- /dev/null +++ b/src/main/java/hhn/labsw/bugageocaching/entities/User_InfoID.java @@ -0,0 +1,8 @@ +package hhn.labsw.bugageocaching.entities; + +import java.io.Serializable; + +public class User_InfoID implements Serializable { + private int user; + private int rankingPointsSum; +} From ca618619615089b98ed8915871496dff733648c4 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 07:54:00 +0200 Subject: [PATCH 22/29] rewrote sql query cause of new database --- .../labsw/bugageocaching/repositories/UserRepository.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java b/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java index 8ab2115..d502743 100644 --- a/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java +++ b/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java @@ -14,7 +14,10 @@ import java.util.List; public interface UserRepository extends CrudRepository { User findByUsername(String username); - @Query(value = "SELECT u.id, u.username, u.ranking_points_sum from user u order by ranking_points_sum DESC", nativeQuery = true) + @Query(value = "SELECT u.id AS ID, SUBSTRING_INDEX(u.email, '@', 1) AS Name, ui.ranking_points_sum AS Ranglistenpunkte\n" + + "FROM user u, user_info ui\n" + + "WHERE u.id = ui.user_id\n" + + "order by ranking_points_sum DESC", nativeQuery = true) List getRankingList(); } From 3d10c6a906f985b83cb51e624447d6137e8aa1b3 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 07:54:56 +0200 Subject: [PATCH 23/29] added some TODO comments/thoughts to methods --- .../bugageocaching/controller/Controller.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index eab8d0f..70710bd 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -60,6 +60,8 @@ public class Controller { return ResponseEntity.status(200).body(new Gson().toJson(cacheRepository.findAll())); } + // TODO user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist + // TODO (über der checkAdmin methode steht ein möglicher lösungsvorschlag dafür) @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/startCache") @ResponseBody @@ -142,10 +144,7 @@ public class Controller { } } - /** - * Checkt, ob die eingescannte Station die Nachfolgestation der zuletzt - * besuchten Stationen des aktuell durchgeführten Caches ist - */ + // TODO user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkStation") @ResponseBody @@ -281,7 +280,8 @@ public class Controller { return createCacheUtil(cache); } - + // TODO claims.get funktioniert denk ich jetzt nurnoch mit claims.get("user") und liefert glaub ein userEntity objekt als JSON zurück + // TODO wir könnten doch dann die email aus dem JSON auslesen und mit Hilfe einer findByEmail methode dann den user aus unserer DB finden oder? @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkAdmin") @ResponseBody @@ -298,7 +298,6 @@ public class Controller { return ResponseEntity.status(200).body(claims.get("admin")); } - //Bis hier @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/getAllStations") @ResponseBody @@ -313,6 +312,7 @@ public class Controller { return deleteCacheUtil(cacheID); } + // TODO @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/getMyCaches") @ResponseBody @@ -349,6 +349,9 @@ public class Controller { } } + // TODO SQL-Anfrage geschrieben, müssen uns nurnoch überlegen wie wir das ganze zurückgeben. + // TODO Da die Daten jetzt über mehrere Klassen verteilt sind, können wir nicht nur ein einzelnes Objekt zurückliefern. + // TODO Wäre eigentlich am Besten, wenn wir einfach das array zurückgeben, das frontend sollte ja ohne probleme daraus dann die Daten rausholen können. @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/getRankingList") @ResponseBody @@ -364,9 +367,13 @@ public class Controller { sendBackUsers.add(u); } + /** + * return ResponseEntity.status(200).body(new Gson().toJson(userRepository.getRankingList())); + */ return ResponseEntity.status(200).body(new Gson().toJson(sendBackUsers)); } + // TODO ebenfalls userfindung ändern @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/getUser") @ResponseBody From 57f7cd4164bc104f2f5d9c80e75ff26c54f9a009 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 08:07:44 +0200 Subject: [PATCH 24/29] simplified code --- .../hhn/labsw/bugageocaching/controller/Controller.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 70710bd..d1106d2 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -231,13 +231,8 @@ public class Controller { return ResponseEntity.status(400).body("The scanned station isnt a part of the cache"); } - int i = 0; - for (Station station1 : cache.getStationen()) { - if (station1.equals(station)) { - break; - } - i++; - } + int i = cache.getStationen().indexOf(station); + if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { bearbeitet.setAktuelleStation(station); From 26e98f3db4e73525ca901f4758b1b6ad675cda10 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 08:30:22 +0200 Subject: [PATCH 25/29] added a possible way to get the user with a given token --- build.gradle | 1 + .../bugageocaching/controller/Controller.java | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 58e161e..1701aa8 100644 --- a/build.gradle +++ b/build.gradle @@ -34,6 +34,7 @@ dependencies { //JSON Parser implementation 'com.google.code.gson:gson:2.8.5' + compile 'com.googlecode.json-simple:json-simple:1.1.1' //compile 'org.springframework.boot:spring-boot-starter-tomcat' diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index d1106d2..83b4d5c 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -1,6 +1,7 @@ package hhn.labsw.bugageocaching.controller; import com.google.gson.Gson; +import com.google.gson.JsonObject; import hhn.labsw.bugageocaching.entities.*; import hhn.labsw.bugageocaching.repositories.*; import hhn.labsw.bugageocaching.util.FinderUtil; @@ -8,6 +9,9 @@ import hhn.labsw.bugageocaching.util.VerificationUtil; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.crypto.bcrypt.BCrypt; @@ -118,7 +122,7 @@ public class Controller { Optional cacheAccesDefinitionOptional = - cacheAccesDefinitionRepository.findById(0); // angefangen + cacheAccesDefinitionRepository.findById(0); // angefangen if (cacheAccesDefinitionOptional.isPresent()) { CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); @@ -276,7 +280,8 @@ public class Controller { } // TODO claims.get funktioniert denk ich jetzt nurnoch mit claims.get("user") und liefert glaub ein userEntity objekt als JSON zurück - // TODO wir könnten doch dann die email aus dem JSON auslesen und mit Hilfe einer findByEmail methode dann den user aus unserer DB finden oder? + // TODO wir könnten doch dann die userID aus dem JSON auslesen und mit Hilfe einer findByID methode dann den user aus unserer DB finden oder? + // TODO unten sieht man auskommentiert, wie das eventuell funktionieren könnte @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkAdmin") @ResponseBody @@ -290,6 +295,22 @@ public class Controller { Claims claims = (Claims) verifyToken.getBody(); + +// try { +// JSONParser parser = new JSONParser(); +// JSONObject userObject = (JSONObject) parser.parse(token); +// int userID = (Integer) userObject.get("userID"); +// Optional userOptional = userRepository.findById(userID); +// if (userOptional.isPresent()) { +// User user = userOptional.get(); +// // hier dann admin check etc... +// } else { +// return ResponseEntity.status(404).body("Couldnt find User " + userID); +// } +// } catch (ParseException e) { +// e.printStackTrace(); +// } + return ResponseEntity.status(200).body(claims.get("admin")); } From da75fbb922e478efee536562885ddcb3f3cb9b66 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Apr 2019 08:36:52 +0200 Subject: [PATCH 26/29] fixed a mistake --- .../java/hhn/labsw/bugageocaching/controller/Controller.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 83b4d5c..1038a0a 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -297,8 +297,9 @@ public class Controller { // try { +// String userString = (String) claims.get("user"); // JSONParser parser = new JSONParser(); -// JSONObject userObject = (JSONObject) parser.parse(token); +// JSONObject userObject = (JSONObject) parser.parse(userString); // int userID = (Integer) userObject.get("userID"); // Optional userOptional = userRepository.findById(userID); // if (userOptional.isPresent()) { From b9a975c10195a30e00980a2ade18b9c44301f1f7 Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Wed, 17 Apr 2019 11:12:13 +0200 Subject: [PATCH 27/29] Added helper class for rankinglist --- .../bugageocaching/controller/Controller.java | 27 ++++++++---------- .../helper/RankingListHelper.java | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 src/main/java/hhn/labsw/bugageocaching/helper/RankingListHelper.java diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 1038a0a..8aee304 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -1,24 +1,21 @@ package hhn.labsw.bugageocaching.controller; import com.google.gson.Gson; -import com.google.gson.JsonObject; import hhn.labsw.bugageocaching.entities.*; +import hhn.labsw.bugageocaching.helper.RankingListHelper; import hhn.labsw.bugageocaching.repositories.*; import hhn.labsw.bugageocaching.util.FinderUtil; import hhn.labsw.bugageocaching.util.VerificationUtil; import io.jsonwebtoken.Claims; -import io.jsonwebtoken.Jwts; -import io.jsonwebtoken.SignatureAlgorithm; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; -import org.springframework.security.crypto.bcrypt.BCrypt; import org.springframework.web.bind.annotation.*; import javax.annotation.PostConstruct; -import java.util.*; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.createCacheUtil; import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.deleteCacheUtil; @@ -122,7 +119,7 @@ public class Controller { Optional cacheAccesDefinitionOptional = - cacheAccesDefinitionRepository.findById(0); // angefangen + cacheAccesDefinitionRepository.findById(0); // angefangen if (cacheAccesDefinitionOptional.isPresent()) { CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); @@ -369,19 +366,19 @@ public class Controller { // TODO SQL-Anfrage geschrieben, müssen uns nurnoch überlegen wie wir das ganze zurückgeben. // TODO Da die Daten jetzt über mehrere Klassen verteilt sind, können wir nicht nur ein einzelnes Objekt zurückliefern. // TODO Wäre eigentlich am Besten, wenn wir einfach das array zurückgeben, das frontend sollte ja ohne probleme daraus dann die Daten rausholen können. + + // Ich hab mal eine Hilfsklasse erstellt, damit die Daten in einer schöneren Form ins Frontend kommen und da quasi nichts geändert + // werden muss. Ich konnte es noch nicht ausprobieren, da die se server down sind (11:05 Uhr) @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/getRankingList") @ResponseBody public ResponseEntity getRankingList() { - List sendBackUsers = new LinkedList<>(); + List sendBackUsers = new LinkedList<>(); List rankingUsers = userRepository.getRankingList(); for (Object[] obj : rankingUsers) { - User u = new User(); - u.setId((int) obj[0]); - u.setUsername((String) obj[1]); - //u.setRankingPointsSum((int) obj[2]); - sendBackUsers.add(u); + RankingListHelper tmp = new RankingListHelper((String) obj[1], (int) obj[2]); + sendBackUsers.add(tmp); } /** diff --git a/src/main/java/hhn/labsw/bugageocaching/helper/RankingListHelper.java b/src/main/java/hhn/labsw/bugageocaching/helper/RankingListHelper.java new file mode 100644 index 0000000..13f474a --- /dev/null +++ b/src/main/java/hhn/labsw/bugageocaching/helper/RankingListHelper.java @@ -0,0 +1,28 @@ +package hhn.labsw.bugageocaching.helper; + +public class RankingListHelper { + + private String username; + private int ranking_Points; + + public RankingListHelper(String username, int ranking_Points) { + this.username = username; + this.ranking_Points = ranking_Points; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public int getRanking_Points() { + return ranking_Points; + } + + public void setRanking_Points(int ranking_Points) { + this.ranking_Points = ranking_Points; + } +} From 4b039cb198ffa13e3415e0686d754eac2f80a59e Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Wed, 17 Apr 2019 14:47:12 +0200 Subject: [PATCH 28/29] Changed format of rankinglist --- .../labsw/bugageocaching/controller/Controller.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 8aee304..84bf3d9 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -293,6 +293,9 @@ public class Controller { Claims claims = (Claims) verifyToken.getBody(); + // TODO sieht nach einer lösung aus, sollte aber denk ich in FinderUtil oder so ausgelagert + // TODO werden. Dann haben wir eine Methode, die hierfür aufgerufen wird und ein 'Claim' + // TODO entgegennimmt und dann eine ResponseEntity zurückgibt // try { // String userString = (String) claims.get("user"); // JSONParser parser = new JSONParser(); @@ -363,10 +366,6 @@ public class Controller { } } - // TODO SQL-Anfrage geschrieben, müssen uns nurnoch überlegen wie wir das ganze zurückgeben. - // TODO Da die Daten jetzt über mehrere Klassen verteilt sind, können wir nicht nur ein einzelnes Objekt zurückliefern. - // TODO Wäre eigentlich am Besten, wenn wir einfach das array zurückgeben, das frontend sollte ja ohne probleme daraus dann die Daten rausholen können. - // Ich hab mal eine Hilfsklasse erstellt, damit die Daten in einer schöneren Form ins Frontend kommen und da quasi nichts geändert // werden muss. Ich konnte es noch nicht ausprobieren, da die se server down sind (11:05 Uhr) @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @@ -380,10 +379,6 @@ public class Controller { RankingListHelper tmp = new RankingListHelper((String) obj[1], (int) obj[2]); sendBackUsers.add(tmp); } - - /** - * return ResponseEntity.status(200).body(new Gson().toJson(userRepository.getRankingList())); - */ return ResponseEntity.status(200).body(new Gson().toJson(sendBackUsers)); } From 0bc18552846c8fe04d5540617ecf894732a90804 Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Wed, 17 Apr 2019 15:02:42 +0200 Subject: [PATCH 29/29] Changed Find User Methods so its matching to the user management --- .../bugageocaching/controller/Controller.java | 47 +++++++------------ .../labsw/bugageocaching/util/FinderUtil.java | 27 ++++++++++- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 84bf3d9..3efc682 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -61,8 +61,8 @@ public class Controller { return ResponseEntity.status(200).body(new Gson().toJson(cacheRepository.findAll())); } - // TODO user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist - // TODO (über der checkAdmin methode steht ein möglicher lösungsvorschlag dafür) + // user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist + // (über der checkAdmin methode steht ein möglicher lösungsvorschlag dafür) @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/startCache") @ResponseBody @@ -86,7 +86,8 @@ public class Controller { Claims claims = (Claims) tokenVerification.getBody(); - ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject()); + //Sollte jetzt eigentlich funktionieren...hoffe ich + ResponseEntity getUser = FinderUtil.findUserFromClaim(claims); if (getUser.getStatusCodeValue() != 200) { return getUser; @@ -145,7 +146,7 @@ public class Controller { } } - // TODO user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist + //user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkStation") @ResponseBody @@ -165,7 +166,7 @@ public class Controller { Claims claims = (Claims) tokenVerification.getBody(); - ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject()); + ResponseEntity getUser = FinderUtil.findUserFromClaim(claims); if (getUser.getStatusCodeValue() != 200) { return getUser; @@ -276,9 +277,6 @@ public class Controller { return createCacheUtil(cache); } - // TODO claims.get funktioniert denk ich jetzt nurnoch mit claims.get("user") und liefert glaub ein userEntity objekt als JSON zurück - // TODO wir könnten doch dann die userID aus dem JSON auslesen und mit Hilfe einer findByID methode dann den user aus unserer DB finden oder? - // TODO unten sieht man auskommentiert, wie das eventuell funktionieren könnte @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkAdmin") @ResponseBody @@ -292,25 +290,15 @@ public class Controller { Claims claims = (Claims) verifyToken.getBody(); + ResponseEntity userResponse = FinderUtil.findUserFromClaim(claims); - // TODO sieht nach einer lösung aus, sollte aber denk ich in FinderUtil oder so ausgelagert - // TODO werden. Dann haben wir eine Methode, die hierfür aufgerufen wird und ein 'Claim' - // TODO entgegennimmt und dann eine ResponseEntity zurückgibt -// try { -// String userString = (String) claims.get("user"); -// JSONParser parser = new JSONParser(); -// JSONObject userObject = (JSONObject) parser.parse(userString); -// int userID = (Integer) userObject.get("userID"); -// Optional userOptional = userRepository.findById(userID); -// if (userOptional.isPresent()) { -// User user = userOptional.get(); -// // hier dann admin check etc... -// } else { -// return ResponseEntity.status(404).body("Couldnt find User " + userID); -// } -// } catch (ParseException e) { -// e.printStackTrace(); -// } + if(userResponse.getStatusCodeValue() != 200){ + return userResponse; + } + + User user = (User) userResponse.getBody(); + + //TODO Hier Admin Check einfügen return ResponseEntity.status(200).body(claims.get("admin")); } @@ -329,7 +317,7 @@ public class Controller { return deleteCacheUtil(cacheID); } - // TODO + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/getMyCaches") @ResponseBody @@ -344,7 +332,7 @@ public class Controller { Claims claims = (Claims) verifyToken.getBody(); - ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject()); + ResponseEntity getUser = FinderUtil.findUserFromClaim(claims); if (getUser.getStatusCodeValue() != 200) { return getUser; @@ -382,7 +370,6 @@ public class Controller { return ResponseEntity.status(200).body(new Gson().toJson(sendBackUsers)); } - // TODO ebenfalls userfindung ändern @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/getUser") @ResponseBody @@ -396,7 +383,7 @@ public class Controller { Claims claims = (Claims) verifyToken.getBody(); - ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject()); + ResponseEntity getUser = FinderUtil.findUserFromClaim(claims); if (getUser.getStatusCodeValue() != 200) { return getUser; diff --git a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java index 1b3672a..26aa0a7 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java @@ -2,6 +2,10 @@ package hhn.labsw.bugageocaching.util; import hhn.labsw.bugageocaching.entities.*; import hhn.labsw.bugageocaching.repositories.*; +import io.jsonwebtoken.Claims; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -91,4 +95,25 @@ public class FinderUtil { return ResponseEntity.status(404).body("Couldnt find User_Info " + infoID); } } -} + + public static ResponseEntity findUserFromClaim(Claims claims) { + + try { + String userString = (String) claims.get("user"); + JSONParser parser = new JSONParser(); + JSONObject userObject = (JSONObject) parser.parse(userString); + int userID = (Integer) userObject.get("userID"); + Optional userOptional = userRepository.findById(userID); + if (userOptional.isPresent()) { + User user = userOptional.get(); + return ResponseEntity.status(200).body(user); + } else { + return ResponseEntity.status(404).body("Couldnt find User " + userID); + } + } catch (ParseException e) { + e.printStackTrace(); + return ResponseEntity.status(404).body("String format was corrupt"); + } + } + + }