Merge pull request 'fixed a token encoding issue' (#70) from Max/backend/loginFix into master
Reviewed-on: PGL/Clyde#70 Reviewed-by: Wal <karpinskiwal@gmail.com> Reviewed-by: Debucquoy Anthony <d.tonitch@gmail.com> Reviewed-by: LeoMoulin <leomoulin125@gmail.com>
This commit is contained in:
		@ -1,4 +1,5 @@
 | 
				
			|||||||
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.HttpHeaders;
 | 
				
			||||||
import org.springframework.http.ResponseEntity;
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
import org.springframework.web.bind.annotation.*;
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
@ -11,13 +12,26 @@ import java.util.Date;
 | 
				
			|||||||
@CrossOrigin(origins = "http://localhost:5173")
 | 
					@CrossOrigin(origins = "http://localhost:5173")
 | 
				
			||||||
public class LoginController {
 | 
					public class LoginController {
 | 
				
			||||||
    private final AuthenticatorService authServ;
 | 
					    private final AuthenticatorService authServ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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;
 | 
				
			||||||
 | 
					            this.expirationDate = expirationDate;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public LoginController(AuthenticatorService authServ){
 | 
					    public LoginController(AuthenticatorService authServ){
 | 
				
			||||||
        this.authServ = authServ;
 | 
					        this.authServ = authServ;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    @PostMapping("/login")
 | 
					    @PostMapping(value = "/login")
 | 
				
			||||||
    public ResponseEntity<String> login(@RequestParam String identifier, String password, Date expirationDate){
 | 
					    public ResponseEntity<String> login(@RequestBody RequestLogin requestLogin){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        String sessionToken = authServ.login(identifier,password,expirationDate);
 | 
					        String sessionToken = authServ.login(requestLogin.identifier,requestLogin.password,requestLogin.expirationDate);
 | 
				
			||||||
        if (sessionToken == null){
 | 
					        if (sessionToken == null){
 | 
				
			||||||
            return new UnauthorizedResponse<>("Identifier or Password incorrect");
 | 
					            return new UnauthorizedResponse<>("Identifier or Password incorrect");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -1,10 +1,12 @@
 | 
				
			|||||||
package ovh.herisson.Clyde.Services;
 | 
					package ovh.herisson.Clyde.Services;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.springframework.stereotype.Service;
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
import ovh.herisson.Clyde.Repositories.TokenRepository;
 | 
					import ovh.herisson.Clyde.Repositories.TokenRepository;
 | 
				
			||||||
import ovh.herisson.Clyde.Tables.Token;
 | 
					import ovh.herisson.Clyde.Tables.Token;
 | 
				
			||||||
import ovh.herisson.Clyde.Tables.User;
 | 
					import ovh.herisson.Clyde.Tables.User;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.UnsupportedEncodingException;
 | 
				
			||||||
import java.nio.charset.StandardCharsets;
 | 
					import java.nio.charset.StandardCharsets;
 | 
				
			||||||
import java.security.SecureRandom;
 | 
					import java.security.SecureRandom;
 | 
				
			||||||
import java.util.Date;
 | 
					import java.util.Date;
 | 
				
			||||||
@ -22,9 +24,15 @@ 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);
 | 
				
			||||||
        String token = new String(bytes, StandardCharsets.US_ASCII);
 | 
					        for (int i = 0; i < bytes.length; i++) {
 | 
				
			||||||
        System.out.println(token);
 | 
					            bytes[i] = (byte) (((bytes[i]+256)%256  %95+ 32));
 | 
				
			||||||
        return token;
 | 
					        }
 | 
				
			||||||
 | 
					        // 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) {
 | 
				
			||||||
 | 
					            throw new RuntimeException(e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public User getUserFromToken(String token){
 | 
					    public User getUserFromToken(String token){
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user