From 41c5dc482dbc93b0de5653076b95e91fc9e115c2 Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Mon, 15 Apr 2019 17:06:16 +0200 Subject: [PATCH] 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"); + } + } }