Hash encrypt512

This commit is contained in:
Valentin CZERYBA 2022-05-01 12:43:41 +02:00
parent 0b4bb54ba6
commit a21210935b
5 changed files with 34 additions and 39 deletions

View File

@ -32,7 +32,9 @@ public class ApplicationLifeCycle {
LOGGER.info("The application has started");
if (schemaCreate){
UsersEntity.deleteAll();
LOGGER.info("Robert80 user is created");
UsersEntity.add("robert80", "robert80@gmail.com", "titi", "robert", LocalDate.of(1990, Month.JANUARY, 23), "toto", "User");
LOGGER.info("Peter93 user is created");
UsersEntity.add("peter93", "peter93gmail.com", "yollo", "peter", LocalDate.of(1993, Month.FEBRUARY, 26), "toto", "Admin");
} else {
LOGGER.info("DB init wassn't created");

View File

@ -6,6 +6,7 @@ import java.security.NoSuchAlgorithmException;
public class Hash {
public static String encryptSHA512(String input)
{
try {

View File

@ -1,30 +1,23 @@
package com.covas.Entity;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.xml.bind.DatatypeConverter;
import com.covas.Classes.Hash;
import org.hibernate.annotations.ColumnDefault;
import org.wildfly.security.password.interfaces.SimpleDigestPassword;
import io.quarkus.elytron.security.common.BcryptUtil;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import io.quarkus.security.jpa.UserDefinition;
import io.quarkus.security.jpa.Username;
import io.quarkus.security.jpa.Password;
import io.quarkus.security.jpa.PasswordProvider;
import io.quarkus.security.jpa.PasswordType;
import io.quarkus.security.jpa.Roles;
@Entity
@Table(name = "users")
@UserDefinition
public class UsersEntity extends PanacheEntity {
@Username
@Column(nullable = false, unique = true)
public String pseudo;
@Column(nullable = false, unique = true)
@ -37,10 +30,8 @@ public class UsersEntity extends PanacheEntity {
public LocalDate birth;
@ColumnDefault("false")
public Boolean status;
@Password(value = PasswordType.CUSTOM, provider = CustomPasswordProvider.class)
@Column(nullable = false)
public String password;
@Roles
public String roles;
public static UsersEntity findByPseudo(String pseudo){
@ -55,18 +46,8 @@ public class UsersEntity extends PanacheEntity {
users.firstName = firstName;
users.birth = birth;
users.status = false;
users.password = BcryptUtil.bcryptHash(password);
users.password = Hash.encryptSHA512(new String(password.getBytes(), StandardCharsets.UTF_8));
users.roles = roles;
users.persist();
}
}
public class CustomPasswordProvider implements PasswordProvider {
@Override
public org.wildfly.security.password.Password getPassword(String pass) {
byte[] digest = DatatypeConverter.parseHexBinary(pass);
return SimpleDigestPassword.createRaw(SimpleDigestPassword.ALGORITHM_SIMPLE_DIGEST_SHA_256, digest);
}
}

View File

@ -1,5 +1,6 @@
package com.covas.Resources;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
@ -17,6 +18,8 @@ import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import com.covas.Classes.Hash;
import com.covas.Entity.UsersEntity;
import com.covas.Json.Jwt2;
import com.covas.Json.Token;
@ -25,11 +28,15 @@ import io.smallrye.jwt.auth.principal.ParseException;
import io.smallrye.jwt.build.Jwt;
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.jboss.resteasy.annotations.jaxrs.HeaderParam;
import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64;
import org.jboss.logging.Logger;
@Path("/token")
public class TokenRessource {
private static final Logger LOGGER = Logger.getLogger(TokenRessource.class);
@Inject
JsonWebToken jwt;
@ -38,22 +45,28 @@ public class TokenRessource {
@GET
@Path("authentificate")
@RolesAllowed("User")
@Produces(MediaType.APPLICATION_JSON)
public Response getUserName(@Context SecurityContext ctx, @CookieParam("jwt") String jwtCookie) {
public Response getUserName(@HeaderParam("Authorization") String auth, @CookieParam("jwt") String jwtCookie) {
String name = "anonymous";
String hash = "";
if(ctx.getUserPrincipal() != null){
name = ctx.getUserPrincipal().getName();
hash = ctx.getUserPrincipal().toString();
}
String password = "";
if (jwtCookie == null) {
String[] hash = new String(Base64.decode(auth.split(" ")[1]), StandardCharsets.UTF_8).split(":");
String pseudo = hash[0];
LOGGER.info(hash[1].length());
password = Hash.encryptSHA512(Hash.encryptSHA512(hash[1]));
UsersEntity users = UsersEntity.findByPseudo(pseudo);
LOGGER.info(users.password);
LOGGER.info(password);
// Create a JWT token signed using the 'HS256' algorithm
String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn("Alice").groups(new HashSet<>(Arrays.asList("User"))).sign();
// or create a JWT token encrypted using the 'A256KW' algorithm
// Jwt.upn("alice").encryptWithSecret(secret);
return Response.status(Response.Status.CREATED).entity(new Jwt2(name, hash)).cookie(new NewCookie("jwt", newJwtCookie)).build();
return Response.status(Response.Status.CREATED).entity(new Jwt2(name, password)).cookie(new NewCookie("jwt", newJwtCookie)).build();
} else {
// All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized.
try {
@ -63,7 +76,7 @@ public class TokenRessource {
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2(name, false, p.getMessage())).build();
}
// or jwt = parser.decrypt(jwtCookie, secret);
return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName(),hash)).build();
return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName(),password)).build();
}
}

View File

@ -8,9 +8,7 @@ quarkus.datasource.db-kind = postgresql
quarkus.datasource.username = toto
quarkus.datasource.password = toto
quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:5432/toto
# drop and create the database at startup (use `update` to only update the schema)
quarkus.hibernate-orm.database.generation = drop-and-create
covas.schema.create = true