fixed a token encoding issue #70

Merged
tonitch merged 3 commits from Max/backend/loginFix into master 2024-03-09 09:57:54 +01:00
2 changed files with 31 additions and 9 deletions
@@ -1,4 +1,5 @@
package ovh.herisson.Clyde.EndPoints;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@@ -11,13 +12,26 @@ import java.util.Date;
@CrossOrigin(origins = "http://localhost:5173")
public class LoginController {
private final AuthenticatorService authServ;
public LoginController(AuthenticatorService authServ){
this.authServ = authServ;
}
@PostMapping("/login")
public ResponseEntity<String> login(@RequestParam String identifier, String password, Date expirationDate){
String sessionToken = authServ.login(identifier,password,expirationDate);
static public class RequestLogin{
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
Outdated
Review

resembler pour mieux brainfuck ^^

resembler pour mieux brainfuck ^^
Outdated
Review

osef

osef
this.expirationDate = expirationDate;
}
}
public LoginController(AuthenticatorService authServ){
this.authServ = authServ;
}
@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");
}
@@ -1,10 +1,12 @@
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.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Date;
@@ -22,9 +24,15 @@ public class TokenService {
public String generateNewToken(){
byte[] bytes = new byte[64];
new SecureRandom().nextBytes(bytes);
Maxime marked this conversation as resolved
Review

je suis un peu curieux de la différence avec Random()

je suis un peu curieux de la différence avec Random()
Review

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 )
String token = new String(bytes, StandardCharsets.US_ASCII);
System.out.println(token);
return token;
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (((bytes[i]+256)%256 %95+ 32));
Outdated
Review

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)
Outdated
Review

smart je vais try

smart je vais try
Review

Permet d'avoir des caractères affichable

Permet d'avoir des caractères affichable
}
// will never end up in the catch because of the way that SecureRandom.nextBytes is implemented
try {
return new String(bytes,"ISO_8859_1");
} catch (UnsupportedEncodingException e) {
tonitch marked this conversation as resolved Outdated
Outdated
Review

tu viens pas de dupliquer le new SecureRandom().nextBytes(bytes); ?

tu viens pas de dupliquer le `new SecureRandom().nextBytes(bytes);` ?
Outdated
Review
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/SecureRandom.html
throw new RuntimeException(e);
}
}
public User getUserFromToken(String token){