fixed a token encoding issue #70
@ -1,8 +1,11 @@
 | 
				
			|||||||
package ovh.herisson.Clyde.EndPoints;
 | 
					package ovh.herisson.Clyde.EndPoints;
 | 
				
			||||||
 | 
					import com.fasterxml.jackson.annotation.JsonFormat;
 | 
				
			||||||
 | 
					import org.springframework.http.HttpHeaders;
 | 
				
			||||||
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.AuthenticatorService;
 | 
					import ovh.herisson.Clyde.Services.AuthenticatorService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.Date;
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@ -10,16 +13,33 @@ import java.util.Date;
 | 
				
			|||||||
public class LoginController {
 | 
					public class LoginController {
 | 
				
			||||||
    private final AuthenticatorService authServ;
 | 
					    private final AuthenticatorService authServ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public LoginController(AuthenticatorService authServ) {
 | 
					    static public class RequestLogin{
 | 
				
			||||||
        this.authServ = authServ;
 | 
					        private final String identifier;
 | 
				
			||||||
 | 
					        private final String password;
 | 
				
			||||||
 | 
					        @JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
 | 
				
			||||||
 | 
					        private final Date expirationDate;
 | 
				
			||||||
 | 
					        public RequestLogin(String identifier, String password, Date expirationDate){
 | 
				
			||||||
 | 
					            this.identifier = identifier;
 | 
				
			||||||
 | 
					            this.password = password;
 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 
					
					Maxime marked this conversation as resolved
					
						
						
							Outdated
						
					
				 
				 | 
				|||||||
 | 
					            this.expirationDate = expirationDate;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @PostMapping("/login")
 | 
					    public LoginController(AuthenticatorService authServ){
 | 
				
			||||||
    public ResponseEntity<String> login(@RequestParam String identifier, String password, Date expirationDate) {
 | 
					        this.authServ = authServ;
 | 
				
			||||||
        String sessionToken = authServ.login(identifier, password, expirationDate);
 | 
					    }
 | 
				
			||||||
        if (sessionToken == null) {
 | 
					    @PostMapping(value = "/login")
 | 
				
			||||||
 | 
					    public ResponseEntity<String> login(@RequestBody RequestLogin requestLogin){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String sessionToken = authServ.login(requestLogin.identifier,requestLogin.password,requestLogin.expirationDate);
 | 
				
			||||||
 | 
					        if (sessionToken == null){
 | 
				
			||||||
            return new UnauthorizedResponse<>("Identifier or Password incorrect");
 | 
					            return new UnauthorizedResponse<>("Identifier or Password incorrect");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return ResponseEntity.ok().header("Set-Cookie", String.format("session_token=%s", sessionToken)).build();
 | 
					
 | 
				
			||||||
 | 
					        HttpHeaders responseHeaders = new HttpHeaders();
 | 
				
			||||||
 | 
					        responseHeaders.set("Set-Cookie",String.format("session_token=%s",sessionToken));
 | 
				
			||||||
 | 
					        return ResponseEntity.ok().headers(responseHeaders).build();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -24,6 +24,13 @@ public class TokenService {
 | 
				
			|||||||
    public String generateNewToken(){
 | 
					    public String generateNewToken(){
 | 
				
			||||||
        byte[] bytes = new byte[64];
 | 
					        byte[] bytes = new byte[64];
 | 
				
			||||||
        new SecureRandom().nextBytes(bytes);
 | 
					        new SecureRandom().nextBytes(bytes);
 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 
					
					Maxime marked this conversation as resolved
					
				 
				
				
					
						tonitch
						commented  
			
		je suis un peu curieux de la différence avec Random() je suis un peu curieux de la différence avec Random()  
			
			
		
				
					
						Maxime
						commented  
			
		random est moins random et plus prévisible donc plus de conflits (on m'a juste conseillé SecureRandom donc sur un site donc voila random est moins random et plus prévisible donc plus de conflits (on m'a juste conseillé SecureRandom donc sur un site donc voila
) 
			
			
		 | 
				|||||||
 | 
					        for (int i = 0; i < bytes.length; i++) {
 | 
				
			||||||
 | 
					            while (bytes[i] == 0){
 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 
				
					
						tonitch
						commented  
			
		tu peux aussi ( x / 4 (division entière) + 64) et ainsi tu est dans une range affichable (lazy) tu peux aussi ( x / 4 (division entière) + 64) et ainsi tu est dans une range affichable (lazy) 
			
			
		
				
					
						Maxime
						commented  
			
		smart je vais try smart je vais try 
			
			
		
				
					
						Maxime
						commented  
			
		Permet d'avoir des caractères affichable Permet d'avoir des caractères affichable  
			
			
		 | 
				|||||||
 | 
					                byte[] temp = new byte[1];
 | 
				
			||||||
 | 
					                new SecureRandom().nextBytes(temp);
 | 
				
			||||||
 | 
					                bytes[i] = temp[0];
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 
					
					tonitch marked this conversation as resolved
					
						
						
							Outdated
						
					
				 
				
				
					
						tonitch
						commented  
			
		tu viens pas de dupliquer le  tu viens pas de dupliquer le `new SecureRandom().nextBytes(bytes);` ? 
			
			
		
				
					
						tonitch
						commented  
			
		https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/SecureRandom.html 
			
			
		 | 
				|||||||
        // will never end up in the catch because of the way that SecureRandom.nextBytes is implemented
 | 
					        // will never end up in the catch because of the way that SecureRandom.nextBytes is implemented
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            return new String(bytes,"ISO_8859_1");
 | 
					            return new String(bytes,"ISO_8859_1");
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user
	
resembler pour mieux brainfuck ^^
osef