Implemented public key fetching
This commit is contained in:
parent
d718446f36
commit
43caf76471
@ -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)
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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");
|
||||
}
|
||||
|
||||
|
||||
@ -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() {
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user