Compare commits
	
		
			2 Commits
		
	
	
		
			e1d8e37c52
			...
			6b58c852a2
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 6b58c852a2 | |||
| 8b35b3dc01 | 
@ -1,44 +1,29 @@
 | 
				
			|||||||
package ovh.herisson.Clyde.EndPoints;
 | 
					package ovh.herisson.Clyde.EndPoints;
 | 
				
			||||||
import org.springframework.http.HttpHeaders;
 | 
					import org.springframework.http.HttpHeaders;
 | 
				
			||||||
import org.springframework.http.HttpStatus;
 | 
					 | 
				
			||||||
import org.springframework.http.ResponseEntity;
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
import org.springframework.web.bind.annotation.*;
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
import ovh.herisson.Clyde.Services.TokenService;
 | 
					import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
 | 
				
			||||||
import ovh.herisson.Clyde.Services.UserService;
 | 
					import ovh.herisson.Clyde.Services.AuthenticatorService;
 | 
				
			||||||
import ovh.herisson.Clyde.Tables.User;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.Date;
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@CrossOrigin(origins = "http://localhost:5173")
 | 
					@CrossOrigin(origins = "http://localhost:5173")
 | 
				
			||||||
public class LoginController {
 | 
					public class LoginController {
 | 
				
			||||||
    private final UserService userService;
 | 
					    private final AuthenticatorService authServ;
 | 
				
			||||||
    private final TokenService tokenService;
 | 
					    public LoginController(AuthenticatorService authServ){
 | 
				
			||||||
 | 
					       this.authServ = authServ;
 | 
				
			||||||
    public LoginController(UserService userService, TokenService tokenService){
 | 
					 | 
				
			||||||
        this.userService =userService;
 | 
					 | 
				
			||||||
        this.tokenService = tokenService;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    @PostMapping("/login")
 | 
					    @PostMapping("/login")
 | 
				
			||||||
    public ResponseEntity<String> login(@RequestParam String identifier, String password, Date expirationDate){
 | 
					    public ResponseEntity<String> login(@RequestParam String identifier, String password, Date expirationDate){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        User user = userService.getUser(identifier);
 | 
					        String sessionToken = authServ.login(identifier,password,expirationDate);
 | 
				
			||||||
        if (user == null){
 | 
					        if (sessionToken == null){
 | 
				
			||||||
            return new ResponseEntity<String>("wrong ID or Email", HttpStatus.BAD_REQUEST);
 | 
					            return new UnauthorizedResponse<>("Identifier or Password incorrect");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        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();
 | 
					        HttpHeaders responseHeaders = new HttpHeaders();
 | 
				
			||||||
        responseHeaders.set("Set-Cookie",String.format("session_token=%s",token));
 | 
					        responseHeaders.set("Set-Cookie",String.format("session_token=%s",sessionToken));
 | 
				
			||||||
        return ResponseEntity.ok().headers(responseHeaders).build();
 | 
					        return ResponseEntity.ok().headers(responseHeaders).build();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,55 @@
 | 
				
			|||||||
 | 
					package ovh.herisson.Clyde.EndPoints;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.CrossOrigin;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.PostMapping;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.RestController;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Repositories.TokenRepository;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Repositories.UserRepository;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Tables.Role;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Tables.Token;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Tables.User;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.Arrays;
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@RestController
 | 
				
			||||||
 | 
					@CrossOrigin(origins = "http://localhost:5173")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class MockController {
 | 
				
			||||||
 | 
					    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public final UserRepository userRepo;
 | 
				
			||||||
 | 
					    public final TokenRepository tokenRepo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public MockController(UserRepository userRepo, TokenRepository tokenRepo){
 | 
				
			||||||
 | 
					        this.tokenRepo = tokenRepo;
 | 
				
			||||||
 | 
					        this.userRepo = userRepo;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /** Saves an example of each user type by :
 | 
				
			||||||
 | 
					     * email : FooRole@FooRole.com, password : FooRole and token : FooRole
 | 
				
			||||||
 | 
					     * For example the admin as "admin@admin.com" as email and "admin" as both password and token
 | 
				
			||||||
 | 
					     * They all have silly names
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @PostMapping("/generateMock")
 | 
				
			||||||
 | 
					    public void postMock(){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), Role.Admin,passwordEncoder.encode("admin"));
 | 
				
			||||||
 | 
					        User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), Role.Student,passwordEncoder.encode("student"));
 | 
				
			||||||
 | 
					        User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), Role.Teacher,passwordEncoder.encode("secretary"));
 | 
				
			||||||
 | 
					        User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), Role.Teacher,passwordEncoder.encode("teacher"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ArrayList<User> users = new ArrayList<User>(Arrays.asList(herobrine,joe,meh,joke));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        userRepo.saveAll(users);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (User user: users){
 | 
				
			||||||
 | 
					            tokenRepo.save(new Token(user,user.getPassword()));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -6,7 +6,7 @@ import org.springframework.http.HttpStatus;
 | 
				
			|||||||
import org.springframework.http.ResponseEntity;
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
import org.springframework.web.bind.annotation.*;
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
 | 
					import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
 | 
				
			||||||
import ovh.herisson.Clyde.Services.TokenService;
 | 
					import ovh.herisson.Clyde.Services.AuthenticatorService;
 | 
				
			||||||
import ovh.herisson.Clyde.Services.UserService;
 | 
					import ovh.herisson.Clyde.Services.UserService;
 | 
				
			||||||
import ovh.herisson.Clyde.Tables.User;
 | 
					import ovh.herisson.Clyde.Tables.User;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -16,24 +16,19 @@ import ovh.herisson.Clyde.Tables.User;
 | 
				
			|||||||
public class UserController {
 | 
					public class UserController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final UserService userService;
 | 
					    private final UserService userService;
 | 
				
			||||||
 | 
					    private final AuthenticatorService authServ;
 | 
				
			||||||
    private final TokenService tokenService;
 | 
					    public UserController(UserService userService, AuthenticatorService authServ){
 | 
				
			||||||
    public UserController(UserService userService, TokenService tokenService){
 | 
					 | 
				
			||||||
        this.userService = userService;
 | 
					        this.userService = userService;
 | 
				
			||||||
        this.tokenService = tokenService; // todo find a way to be clearer
 | 
					        this.authServ = authServ;
 | 
				
			||||||
 | 
					 | 
				
			||||||
        tokenService.postMockToken(userService.postMockUsers());// todo find a better place to put that
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @GetMapping("/user")
 | 
					    @GetMapping("/user")
 | 
				
			||||||
    public ResponseEntity<User> getUsers(@RequestHeader("Authorization") String token){
 | 
					    public ResponseEntity<User> getUser(@RequestHeader("Authorization") String token){
 | 
				
			||||||
 | 
					        User user = authServ.getUserFromToken(token);
 | 
				
			||||||
        User user = tokenService.getUserFromToken(token);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (user == null) {
 | 
					        if (user == null) {
 | 
				
			||||||
            return new UnauthorizedResponse<User>(null);
 | 
					            return new UnauthorizedResponse<>(null);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return new ResponseEntity<User>(user, HttpStatus.OK);
 | 
					        return new ResponseEntity<>(user, HttpStatus.OK);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @PostMapping("/user")
 | 
					    @PostMapping("/user")
 | 
				
			||||||
@ -43,7 +38,7 @@ public class UserController {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @GetMapping("/users")
 | 
					    @GetMapping("/users")
 | 
				
			||||||
    public Iterable<User> getAllUsers(){//TODO ne l'accepter que si c'est le secrétariat
 | 
					    public Iterable<User> getAllUsers(){
 | 
				
			||||||
        return userService.getAll();
 | 
					        return userService.getAll();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					package ovh.herisson.Clyde.Services;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Tables.User;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Service
 | 
				
			||||||
 | 
					public class AuthenticatorService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private final TokenService tokenService;
 | 
				
			||||||
 | 
					    private final UserService userService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public AuthenticatorService(TokenService tokenService, UserService userService){
 | 
				
			||||||
 | 
					        this.tokenService = tokenService;
 | 
				
			||||||
 | 
					        this.userService = userService;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public User getUserFromToken(String token){
 | 
				
			||||||
 | 
					        return tokenService.getUserFromToken(token);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public String login(String identifier, String password, Date expirationDate){
 | 
				
			||||||
 | 
					        User user = userService.getUser(identifier);
 | 
				
			||||||
 | 
					        if (user == null){return null;}
 | 
				
			||||||
 | 
					        if (!userService.checkPassword(user,password)){return null;}
 | 
				
			||||||
 | 
					        String token = tokenService.generateNewToken();
 | 
				
			||||||
 | 
					        tokenService.saveToken(token,user,expirationDate);
 | 
				
			||||||
 | 
					        return token;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -27,25 +27,11 @@ public class TokenService {
 | 
				
			|||||||
        return 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));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public User getUserFromToken(String token){
 | 
					    public User getUserFromToken(String token){
 | 
				
			||||||
        return tokenRepo.getByToken(token).getUser();
 | 
					        return tokenRepo.getByToken(token).getUser();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /** Take the list of mock user to save them in the Token DB
 | 
					    public void saveToken(String token, User user, Date expirationDate){// todo faire qlq chose de l'expDate
 | 
				
			||||||
     * With token being the password of the user (also his role)
 | 
					        tokenRepo.save(new Token(user,token));
 | 
				
			||||||
     * @param users an
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public void postMockToken(Iterable<User> users){
 | 
					 | 
				
			||||||
        for (User user: users){
 | 
					 | 
				
			||||||
            tokenRepo.save(new Token(user,user.getPassword()));
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -39,27 +39,6 @@ public class UserService {
 | 
				
			|||||||
        return passwordEncoder.matches(tryingPassword,  user.getPassword());
 | 
					        return passwordEncoder.matches(tryingPassword,  user.getPassword());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /** Saves an example of :
 | 
					 | 
				
			||||||
     * an Admin with id 1, email : admin@admin.com and password: admin
 | 
					 | 
				
			||||||
     * a Student with id 2, email: student@student.com and password: student (no cursus yet)
 | 
					 | 
				
			||||||
     * a Secretary with id 3, email: secretary@secretary.com and password: secretary
 | 
					 | 
				
			||||||
     * a Teacher (same)
 | 
					 | 
				
			||||||
     * and they all have silly names (hihi)
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public Iterable<User> postMockUsers(){
 | 
					 | 
				
			||||||
        User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), Role.Admin,passwordEncoder.encode("admin"));
 | 
					 | 
				
			||||||
        User Joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), Role.Student,passwordEncoder.encode("student"));
 | 
					 | 
				
			||||||
        User Meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0), Role.Teacher,passwordEncoder.encode("secretary"));
 | 
					 | 
				
			||||||
        User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), Role.Teacher,passwordEncoder.encode("teacher"));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        userRepo.save(herobrine);
 | 
					 | 
				
			||||||
        userRepo.save(Joe);
 | 
					 | 
				
			||||||
        userRepo.save(Meh);
 | 
					 | 
				
			||||||
        userRepo.save(joke);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return new ArrayList<User>(Arrays.asList(herobrine,Joe,Meh,joke));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void save(User  user){
 | 
					    public void save(User  user){
 | 
				
			||||||
        userRepo.save(user);
 | 
					        userRepo.save(user);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user