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

View File

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

View File

@ -1,30 +1,23 @@
package com.covas.Entity; package com.covas.Entity;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate; import java.time.LocalDate;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table; import javax.persistence.Table;
import javax.xml.bind.DatatypeConverter;
import com.covas.Classes.Hash;
import org.hibernate.annotations.ColumnDefault; 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.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 @Entity
@Table(name = "users") @Table(name = "users")
@UserDefinition
public class UsersEntity extends PanacheEntity { public class UsersEntity extends PanacheEntity {
@Username
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
public String pseudo; public String pseudo;
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
@ -37,10 +30,8 @@ public class UsersEntity extends PanacheEntity {
public LocalDate birth; public LocalDate birth;
@ColumnDefault("false") @ColumnDefault("false")
public Boolean status; public Boolean status;
@Password(value = PasswordType.CUSTOM, provider = CustomPasswordProvider.class)
@Column(nullable = false) @Column(nullable = false)
public String password; public String password;
@Roles
public String roles; public String roles;
public static UsersEntity findByPseudo(String pseudo){ public static UsersEntity findByPseudo(String pseudo){
@ -55,18 +46,8 @@ public class UsersEntity extends PanacheEntity {
users.firstName = firstName; users.firstName = firstName;
users.birth = birth; users.birth = birth;
users.status = false; users.status = false;
users.password = BcryptUtil.bcryptHash(password); users.password = Hash.encryptSHA512(new String(password.getBytes(), StandardCharsets.UTF_8));
users.roles = roles; users.roles = roles;
users.persist(); 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; package com.covas.Resources;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; 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.Response;
import javax.ws.rs.core.SecurityContext; 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.Jwt2;
import com.covas.Json.Token; import com.covas.Json.Token;
@ -25,11 +28,15 @@ import io.smallrye.jwt.auth.principal.ParseException;
import io.smallrye.jwt.build.Jwt; import io.smallrye.jwt.build.Jwt;
import org.eclipse.microprofile.jwt.JsonWebToken; 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") @Path("/token")
public class TokenRessource { public class TokenRessource {
private static final Logger LOGGER = Logger.getLogger(TokenRessource.class);
@Inject @Inject
JsonWebToken jwt; JsonWebToken jwt;
@ -38,22 +45,28 @@ public class TokenRessource {
@GET @GET
@Path("authentificate") @Path("authentificate")
@RolesAllowed("User")
@Produces(MediaType.APPLICATION_JSON) @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 name = "anonymous";
String hash = ""; String password = "";
if(ctx.getUserPrincipal() != null){
name = ctx.getUserPrincipal().getName();
hash = ctx.getUserPrincipal().toString();
}
if (jwtCookie == null) { 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 // 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(); 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 // or create a JWT token encrypted using the 'A256KW' algorithm
// Jwt.upn("alice").encryptWithSecret(secret); // 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 { } else {
// All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized. // All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized.
try { try {
@ -63,7 +76,7 @@ public class TokenRessource {
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2(name, false, p.getMessage())).build(); return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2(name, false, p.getMessage())).build();
} }
// or jwt = parser.decrypt(jwtCookie, secret); // 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.username = toto
quarkus.datasource.password = toto quarkus.datasource.password = toto
quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:5432/toto quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:5432/toto
# drop and create the database at startup (use `update` to only update the schema) # drop and create the database at startup (use `update` to only update the schema)
quarkus.hibernate-orm.database.generation = drop-and-create quarkus.hibernate-orm.database.generation = drop-and-create
covas.schema.create = true covas.schema.create = true