Added FinderUtil

This commit is contained in:
Maximilian Leopold 2019-04-15 17:06:16 +02:00
parent 81c2b19f09
commit 41c5dc482d
4 changed files with 184 additions and 80 deletions

View File

@ -3,6 +3,7 @@ package hhn.labsw.bugageocaching.controller;
import com.google.gson.Gson; import com.google.gson.Gson;
import hhn.labsw.bugageocaching.entities.*; import hhn.labsw.bugageocaching.entities.*;
import hhn.labsw.bugageocaching.repositories.*; import hhn.labsw.bugageocaching.repositories.*;
import hhn.labsw.bugageocaching.util.FinderUtil;
import hhn.labsw.bugageocaching.util.VerificationUtil; import hhn.labsw.bugageocaching.util.VerificationUtil;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.ExpiredJwtException;
@ -174,52 +175,76 @@ public class Controller {
@RequestParam String cacheID, @RequestParam String cacheID,
@RequestParam String stationID, @RequestParam String stationID,
@RequestParam String durchgefuehrterCacheID) { @RequestParam String durchgefuehrterCacheID) {
try { //----------------------
Claims claims = Jwts.parser() //Parse JWT //Verify token
.setSigningKey(key) ResponseEntity tokenVerification = VerificationUtil.verifyToken(token);
.parseClaimsJws(token).getBody();
//Error in token verification
if (tokenVerification.getStatusCodeValue() != 200) {
return tokenVerification;
}
Claims claims = (Claims) tokenVerification.getBody();
User user = userRepository.findByUsername(claims.getSubject()); User user = userRepository.findByUsername(claims.getSubject());
if (user == null) { if (user == null) {
return ResponseEntity.status(404).body("User was not found"); return ResponseEntity.status(404).body("User was not found");
} }
//----------------------
Optional<Cache> cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); //----------------------
Cache cache; //Get Cache
if (cacheOptional.isPresent()) { ResponseEntity getCache = FinderUtil.findCacheById(cacheID);
cache = cacheOptional.get();
} else { if(getCache.getStatusCodeValue() != 200){
return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); return getCache;
} }
Optional<Cache> durchgefuehrterCacheIDOptional = cacheRepository.findById(Integer.valueOf(durchgefuehrterCacheID)); Cache cache = (Cache) getCache.getBody();
Cache durchgefuehrterCache; //----------------------
if (durchgefuehrterCacheIDOptional.isPresent()) {
durchgefuehrterCache = cacheOptional.get(); //----------------------
} else { //Get durchgeführter Cache
return ResponseEntity.status(404).body("Couldnt find Cache " + durchgefuehrterCacheID); ResponseEntity getDurchgefuehrterCache = FinderUtil.findCacheById(durchgefuehrterCacheID);
if(getDurchgefuehrterCache.getStatusCodeValue() != 200){
return getDurchgefuehrterCache;
} }
Optional<Station> stationOptional = stationRepository.findById(Integer.valueOf(stationID)); Cache durchgefuehrterCache = (Cache) getDurchgefuehrterCache.getBody();
Station station; //----------------------
if (stationOptional.isPresent()) {
station = stationOptional.get(); //----------------------
} else { //Get Station
return ResponseEntity.status(404).body("Couldnt find Station " + stationID); ResponseEntity getStation = FinderUtil.findStationById(stationID);
if(getStation.getStatusCodeValue() != 200){
return getStation;
} }
Station station = (Station) getStation.getBody();
//----------------------
if (cache != durchgefuehrterCache) { if (cache != durchgefuehrterCache) {
return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); return ResponseEntity.status(400).body("The scanned station isn´t the correct following station");
} }
Bearbeitet bearbeitet; //----------------------
if (bearbeitetRepository.findByUserAndCache(user, cache) != null) { //Get Bearbeitet entry
bearbeitet = bearbeitetRepository.findByUserAndCache(user, cache); ResponseEntity getBearbeitet = FinderUtil.findBearbeitetByUserAndCache(user, cache);
} else {
return ResponseEntity.status(400).body("The user has not started this cache yet"); if(getBearbeitet.getStatusCodeValue() != 200){
return getBearbeitet;
} }
Bearbeitet bearbeitet = (Bearbeitet) getBearbeitet.getBody();
//----------------------
Station aktuelleStation = bearbeitet.getAktuelleStation(); Station aktuelleStation = bearbeitet.getAktuelleStation();
if(aktuelleStation == null){
return ResponseEntity.status(400).body("Database Error");
}
if (!cache.getStationen().contains(station)) { if (!cache.getStationen().contains(station)) {
return ResponseEntity.status(400).body("The scanned station isnt a part of the cache"); return ResponseEntity.status(400).body("The scanned station isnt a part of the cache");
@ -250,11 +275,6 @@ public class Controller {
return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); 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 @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose

View File

@ -2,6 +2,7 @@ package hhn.labsw.bugageocaching.entities;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity @Entity
@Table @Table

View File

@ -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<Cache> 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<Station> 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");
}
}

View File

@ -1,6 +1,9 @@
package hhn.labsw.bugageocaching.util; package hhn.labsw.bugageocaching.util;
import hhn.labsw.bugageocaching.fetchObjects.PublicKey; 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.HttpHeaders;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -31,5 +34,18 @@ public class VerificationUtil {
//Fehler muss zurückgegeben werden //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");
}
}
} }