Merge branch 'master' into Leo/Backend
All checks were successful
Build and test backend / Build-backend (pull_request) Successful in 2m1s
Build and test backend / Test-backend (pull_request) Successful in 1m59s
Build and test FrontEnd / Build-frontend (pull_request) Successful in 23s

This commit is contained in:
2024-03-11 16:04:06 +01:00
18 changed files with 58 additions and 302 deletions

View File

@ -9,7 +9,7 @@ import ovh.herisson.Clyde.Services.AuthenticatorService;
import java.util.Date;
@RestController
@CrossOrigin(origins = "http://localhost:5173")
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class LoginController {
private final AuthenticatorService authServ;

View File

@ -52,10 +52,6 @@ public class MockController {
mockUsers = new ArrayList<User>(Arrays.asList(herobrine,joe,meh,joke));
userRepo.saveAll(mockUsers);
for (User user: mockUsers){
tokenService.saveToken(new Token(user,user.getPassword(), new Date()));
}
}
@DeleteMapping("/mock")

View File

@ -0,0 +1,26 @@
package ovh.herisson.Clyde.EndPoints;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import ovh.herisson.Clyde.Services.TokenService;
import ovh.herisson.Clyde.Tables.Token;
@RestController
@CrossOrigin(origins = "http://localhost:5173")
public class TokenController {
private final TokenService tokenServ;
public TokenController(TokenService tokenServ){
this.tokenServ = tokenServ;
}
@GetMapping("/tokens")
public Iterable<Token> getTokens(){
return tokenServ.getAllTokens();
}
}

View File

@ -23,18 +23,18 @@ public class UserController {
}
@GetMapping("/user")
public ResponseEntity<User> getUser(@RequestHeader("Authorization") String token){
User user = authServ.getUserFromToken(token);
if (user == null) {
return new UnauthorizedResponse<>(null);
}
public ResponseEntity<User> getUser(@RequestHeader("Cookie") String authorization){
if (authorization == null) return new UnauthorizedResponse<>(null);
User user = authServ.getUserFromToken(authorization);
if (user == null) return new UnauthorizedResponse<>(null);
return new ResponseEntity<>(user, HttpStatus.OK);
}
@PostMapping("/user")
public ResponseEntity<String> postUser(@RequestBody User user){
userService.save(user);
return new ResponseEntity<String>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED);
return new ResponseEntity<>(String.format("Account created with ID:%s",user.getRegNo()),HttpStatus.CREATED);
}
@GetMapping("/users")

View File

@ -20,12 +20,19 @@ public class TokenService {
this.tokenRepo = tokenRepo;
}
public Iterable<Token> getAllTokens() {
return tokenRepo.findAll();
}
public String generateNewToken(){
byte[] bytes = new byte[64];
new SecureRandom().nextBytes(bytes);
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (((bytes[i]+256)%256 %95+ 32));
while ((char)bytes[i] == ';'){
bytes[i] = new SecureRandom().generateSeed(1)[0];
}
}
// will never end up in the catch because of the way that SecureRandom.nextBytes is implemented
try {
@ -35,8 +42,10 @@ public class TokenService {
}
}
public User getUserFromToken(String token){
return tokenRepo.getByToken(token).getUser();
public User getUserFromToken(String token) {
Token tokenRep = tokenRepo.getByToken(token);
if (tokenRep == null) return null;
return tokenRep.getUser();
}
public void saveToken(Token token){

View File

@ -12,7 +12,7 @@ public class Token {
@Id
private int id;
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name ="Users")
private User user;
private String token;