From a21210935bd2bb401e58ca00af54adb4876c8ebc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 1 May 2022 12:43:41 +0200 Subject: [PATCH] Hash encrypt512 --- .../ApplicationLifeCycle.java | 2 ++ src/main/java/com/covas/Classes/Hash.java | 1 + .../java/com/covas/Entity/UsersEntity.java | 33 ++++--------------- .../com/covas/Resources/TokenRessource.java | 33 +++++++++++++------ src/main/resources/application.properties | 4 +-- 5 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java index 7e7be39..ecd1721 100644 --- a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java +++ b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java @@ -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"); diff --git a/src/main/java/com/covas/Classes/Hash.java b/src/main/java/com/covas/Classes/Hash.java index ea9d82b..a6a1a29 100644 --- a/src/main/java/com/covas/Classes/Hash.java +++ b/src/main/java/com/covas/Classes/Hash.java @@ -6,6 +6,7 @@ import java.security.NoSuchAlgorithmException; public class Hash { + public static String encryptSHA512(String input) { try { diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index 2735072..e7be035 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -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); - } - -} + } +} \ No newline at end of file diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index b9bb5c6..4f895c2 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -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(); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index cf310af..2d02a15 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 - +covas.schema.create = true \ No newline at end of file