Compare commits

...

7 Commits

10 changed files with 150 additions and 10 deletions

View File

@ -21,6 +21,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-mail")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-security")
// implementation("org.springframework.session:spring-session-jdbc")
developmentOnly("org.springframework.boot:spring-boot-devtools")
developmentOnly("org.springframework.boot:spring-boot-docker-compose")

View File

@ -2,8 +2,9 @@ package ovh.herisson.Clyde;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
@SpringBootApplication
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
public class ClydeApplication {
public static void main(String[] args) {

View File

@ -0,0 +1,45 @@
package ovh.herisson.Clyde.EndPoints;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Services.TokenService;
import ovh.herisson.Clyde.Services.UserService;
import ovh.herisson.Clyde.Tables.User;
import java.util.Date;
@RestController
@CrossOrigin(origins = "http://localhost:5173")
public class LoginController {
private final UserService userService;
private final TokenService tokenService;
public LoginController(UserService userService, TokenService tokenService){
this.userService =userService;
this.tokenService = tokenService;
}
@PostMapping("/login")
public ResponseEntity<String> login(@RequestParam String identifier, String password, Date expirationDate){
User user = userService.getUser(identifier);
if (user == null){
return new ResponseEntity<String>("wrong ID or Email", HttpStatus.BAD_REQUEST);
}
if (!userService.checkPassword(user,password)){
return new ResponseEntity<String>("wrong Password",HttpStatus.BAD_REQUEST);
}
String token = tokenService.generateNewToken();
tokenService.saveToken(token,user,expirationDate);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Set-Cookie",String.format("session_token=%s",token));
return ResponseEntity.ok().headers(responseHeaders).build();
}
}

View File

@ -2,7 +2,7 @@ package ovh.herisson.Clyde.EndPoints;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Repositories.UserRepository;

View File

@ -0,0 +1,7 @@
package ovh.herisson.Clyde.Repositories;
import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.Token;
public interface TokenRepository extends CrudRepository<Token,Long> {
}

View File

@ -10,6 +10,8 @@ public interface UserRepository extends CrudRepository<User, Long> {
User findById(long id);
User findByEmail(String email);
/**
@Query(value = "select a.* from Users a ",nativeQuery = true)
Iterable<User> findAllUsers();**/

View File

@ -0,0 +1,12 @@
package ovh.herisson.Clyde.Responses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
public class UnauthorizedResponse extends ResponseEntity<String> {
public UnauthorizedResponse(String message) {
super(message,HttpStatus.UNAUTHORIZED);
}
}

View File

@ -0,0 +1,36 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.TokenRepository;
import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Date;
@Service
public class TokenService {
TokenRepository tokenRepo;
public TokenService(TokenRepository tokenRepo){
this.tokenRepo = tokenRepo;
}
public String generateNewToken(){
byte[] bytes = new byte[64];
new SecureRandom().nextBytes(bytes);
String token = new String(bytes, StandardCharsets.US_ASCII);
System.out.println(token);
return token;
}
//todo potentiellement return bool pour savoir si token bien add
public void saveToken(String token, User user, Date expirationDate){
tokenRepo.save(new Token(user,token));
}
}

View File

@ -0,0 +1,36 @@
package ovh.herisson.Clyde.Services;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Tables.User;
@Service
public class UserService {
private final UserRepository userRepo;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public UserService(UserRepository userRepo){
this.userRepo = userRepo;
}
public User getUser(String identifier){
if (identifier == null) return null;
try {
int id = Integer.parseInt(identifier);
return userRepo.findById(id);
}
catch (NumberFormatException nfe){
return userRepo.findByEmail(identifier);
}
}
public boolean checkPassword(User user, String tryingPassword){
return passwordEncoder.matches(tryingPassword, user.getPassword());
}
}

View File

@ -8,12 +8,13 @@ public class Token {
@Id
private int id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name ="Users")
private int regNo;
private User user;
private String token;
public Token(int regNo, String token){
this.regNo = regNo;
public Token(User user, String token){
this.user = user;
this.token = token;
}
@ -21,13 +22,12 @@ public class Token {
public int getId() {
return id;
}
public int getRegNo() {
return regNo;
public User getUser() {
return user;
}
public void setRegNo(int regNo) {
this.regNo = regNo;
public void setUser(User regNo) {
this.user = regNo;
}
public String getToken(){