From 2202700bc73206d4f35a1def8b0395420e850356 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 11 Apr 2022 22:47:13 +0200 Subject: [PATCH 01/21] add extension jdbc postgresql and orm --- pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pom.xml b/pom.xml index e7fe8c7..6c9dbf3 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,14 @@ io.quarkus quarkus-smallrye-jwt + + io.quarkus + quarkus-jdbc-postgresql + + + io.quarkus + quarkus-hibernate-orm-panache + io.quarkus quarkus-junit5 From 0b241bd8b53e51cb8e8b8d875bcb0dec07fba6ff Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 12 Apr 2022 17:51:34 +0200 Subject: [PATCH 02/21] Test users databse --- src/main/java/com/covas/TokenRessource.java | 9 +++++++-- src/main/java/com/covas/Users.java | 20 ++++++++++++++++++++ src/main/resources/application.properties | 11 +++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/covas/Users.java diff --git a/src/main/java/com/covas/TokenRessource.java b/src/main/java/com/covas/TokenRessource.java index 2ecd335..833d033 100644 --- a/src/main/java/com/covas/TokenRessource.java +++ b/src/main/java/com/covas/TokenRessource.java @@ -1,16 +1,20 @@ package com.covas; +import java.time.LocalDate; +import java.time.Month; import java.util.Arrays; import java.util.HashSet; +import javax.annotation.PostConstruct; import javax.annotation.security.PermitAll; import javax.annotation.security.RolesAllowed; import javax.inject.Inject; +import javax.transaction.TransactionScoped; +import javax.transaction.Transactional; import javax.ws.rs.CookieParam; import javax.ws.rs.GET; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; @@ -32,8 +36,9 @@ public class TokenRessource { @Inject JsonWebToken jwt; + @Inject JWTParser parser; - + @GET @Path("authentificate") diff --git a/src/main/java/com/covas/Users.java b/src/main/java/com/covas/Users.java new file mode 100644 index 0000000..d6fcee2 --- /dev/null +++ b/src/main/java/com/covas/Users.java @@ -0,0 +1,20 @@ +package com.covas; + +import java.time.LocalDate; + +import javax.persistence.Entity; + +import io.quarkus.hibernate.orm.panache.PanacheEntity; + + +@Entity +public class Users extends PanacheEntity { + public String name; + public LocalDate birth; + public Boolean status; + + public static Users findByName(String name){ + return find("name", name).firstResult(); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3e147b7..3af2412 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,13 @@ smallrye.jwt.sign.key.location=privateKey.pem - mp.jwt.verify.publickey.location=publicKey.pem -mp.jwt.verify.issuer=https://example.com/issuer \ No newline at end of file +mp.jwt.verify.issuer=https://example.com/issuer + +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 \ No newline at end of file From 7b5772b49f7cda5493667e233e46ea304d4b0261 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 13 Apr 2022 23:08:22 +0200 Subject: [PATCH 03/21] ressource put init --- src/main/java/com/covas/TokenRessource.java | 24 +++++++++++++++++++++ src/main/resources/application.properties | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/covas/TokenRessource.java b/src/main/java/com/covas/TokenRessource.java index 833d033..8e7b05d 100644 --- a/src/main/java/com/covas/TokenRessource.java +++ b/src/main/java/com/covas/TokenRessource.java @@ -14,6 +14,7 @@ import javax.transaction.Transactional; import javax.ws.rs.CookieParam; import javax.ws.rs.GET; import javax.ws.rs.InternalServerErrorException; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; @@ -26,18 +27,41 @@ import io.smallrye.jwt.auth.principal.JWTParser; import io.smallrye.jwt.auth.principal.ParseException; import io.smallrye.jwt.build.Jwt; +import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.jwt.JsonWebToken; @Path("/token") public class TokenRessource { + @Inject + @ConfigProperty(name = "covas.schema.create", defaultValue = "true") + boolean schemaCreate; @Inject JsonWebToken jwt; @Inject JWTParser parser; + + @PUT + @Path("init") + @Transactional + public Response init(){ + if (schemaCreate){ + Users users = new Users(); + if(users.findByName("Peter") == null){ + users.name = "Peter"; + users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); + users.status = true; + users.persist(); + return Response.status(Response.Status.CREATED).build(); + } + } + return Response.status(Response.Status.NOT_MODIFIED).build(); + + + } @GET diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3af2412..8e2db47 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -10,4 +10,6 @@ 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 \ No newline at end of file +quarkus.hibernate-orm.database.generation = drop-and-create + +covas.schema.create = true \ No newline at end of file From ca2ae584bcfef52f9622f189f23d24969d136782 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 14 Apr 2022 22:03:42 +0200 Subject: [PATCH 04/21] =?UTF-8?q?DB=20init=20au=20d=C3=A9marrage=20de=20l'?= =?UTF-8?q?application?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/covas/ApplicationLifeCycle.java | 50 +++++++++++++++++++ src/main/java/com/covas/TokenRessource.java | 25 ---------- 2 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/covas/ApplicationLifeCycle.java diff --git a/src/main/java/com/covas/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationLifeCycle.java new file mode 100644 index 0000000..1c6edea --- /dev/null +++ b/src/main/java/com/covas/ApplicationLifeCycle.java @@ -0,0 +1,50 @@ +package com.covas; + + +import org.jboss.logging.Logger; + +import io.quarkus.runtime.ShutdownEvent; +import io.quarkus.runtime.StartupEvent; + +import java.time.LocalDate; +import java.time.Month; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.transaction.Transactional; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + + +@ApplicationScoped +public class ApplicationLifeCycle { + @Inject + @ConfigProperty(name = "covas.schema.create", defaultValue = "true") + boolean schemaCreate; + + private static final Logger LOGGER = Logger.getLogger(ApplicationLifeCycle.class); + + @Transactional + void onStart(@Observes StartupEvent ev) { + LOGGER.info("The application has started"); + if (schemaCreate){ + Users users = new Users(); + if(users.findByName("Peter") == null){ + users.name = "Peter"; + users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); + users.status = true; + users.persist(); + LOGGER.info("Users test was created"); + } else { + LOGGER.info("User test wasn't created"); + } + } else { + LOGGER.info("DB init wassn't created"); + } + } + void onStop(@Observes ShutdownEvent ev) { + LOGGER.info("The application is stopping..."); + } + +} diff --git a/src/main/java/com/covas/TokenRessource.java b/src/main/java/com/covas/TokenRessource.java index 8e7b05d..eee5144 100644 --- a/src/main/java/com/covas/TokenRessource.java +++ b/src/main/java/com/covas/TokenRessource.java @@ -27,43 +27,18 @@ import io.smallrye.jwt.auth.principal.JWTParser; import io.smallrye.jwt.auth.principal.ParseException; import io.smallrye.jwt.build.Jwt; -import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.jwt.JsonWebToken; @Path("/token") public class TokenRessource { - @Inject - @ConfigProperty(name = "covas.schema.create", defaultValue = "true") - boolean schemaCreate; - @Inject JsonWebToken jwt; @Inject JWTParser parser; - @PUT - @Path("init") - @Transactional - public Response init(){ - if (schemaCreate){ - Users users = new Users(); - if(users.findByName("Peter") == null){ - users.name = "Peter"; - users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); - users.status = true; - users.persist(); - return Response.status(Response.Status.CREATED).build(); - } - } - return Response.status(Response.Status.NOT_MODIFIED).build(); - - - } - - @GET @Path("authentificate") @Produces(MediaType.APPLICATION_JSON) From cac880df01c7b9b5fe623fae6180849df7858026 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 14 Apr 2022 22:26:12 +0200 Subject: [PATCH 05/21] Modifiy table users --- .../java/com/covas/ApplicationLifeCycle.java | 8 ++++++-- src/main/java/com/covas/Users.java | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/covas/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationLifeCycle.java index 1c6edea..ac04296 100644 --- a/src/main/java/com/covas/ApplicationLifeCycle.java +++ b/src/main/java/com/covas/ApplicationLifeCycle.java @@ -30,10 +30,14 @@ public class ApplicationLifeCycle { LOGGER.info("The application has started"); if (schemaCreate){ Users users = new Users(); - if(users.findByName("Peter") == null){ - users.name = "Peter"; + if(users.findByPseudo("Peter") == null){ + users.pseudo = "Peter"; + users.email = "peter@email.com"; + users.name = "Toto"; + users.firstName = "Peter"; users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); users.status = true; + users.password = "toto"; users.persist(); LOGGER.info("Users test was created"); } else { diff --git a/src/main/java/com/covas/Users.java b/src/main/java/com/covas/Users.java index d6fcee2..14f413e 100644 --- a/src/main/java/com/covas/Users.java +++ b/src/main/java/com/covas/Users.java @@ -2,19 +2,34 @@ package com.covas; import java.time.LocalDate; +import javax.persistence.Column; import javax.persistence.Entity; +import org.hibernate.annotations.ColumnDefault; + import io.quarkus.hibernate.orm.panache.PanacheEntity; @Entity public class Users extends PanacheEntity { + + @Column(nullable = false, unique = true) + public String pseudo; + @Column(nullable = false, unique = true) + public String email; + @Column(nullable = false) public String name; + @Column(nullable = false) + public String firstName; + @Column(nullable = false) public LocalDate birth; + @ColumnDefault("false") public Boolean status; + @Column(nullable = false) + public String password; - public static Users findByName(String name){ - return find("name", name).firstResult(); + public static Users findByPseudo(String pseudo){ + return find("pseudo", pseudo).firstResult(); } } From 268ee6e2775ac33eb81eaceec7efd4ac1a03ef8f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 15 Apr 2022 20:19:06 +0200 Subject: [PATCH 06/21] Add another user and enum for roles --- .../java/com/covas/ApplicationLifeCycle.java | 22 +++++++++++++++++-- src/main/java/com/covas/TokenRessource.java | 1 - src/main/java/com/covas/Users.java | 6 +++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/covas/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationLifeCycle.java index ac04296..ac09d0e 100644 --- a/src/main/java/com/covas/ApplicationLifeCycle.java +++ b/src/main/java/com/covas/ApplicationLifeCycle.java @@ -30,6 +30,9 @@ public class ApplicationLifeCycle { LOGGER.info("The application has started"); if (schemaCreate){ Users users = new Users(); + Users users2 = new Users(); + + if(users.findByPseudo("Peter") == null){ users.pseudo = "Peter"; users.email = "peter@email.com"; @@ -38,10 +41,25 @@ public class ApplicationLifeCycle { users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); users.status = true; users.password = "toto"; + users.roles = Roles.User; users.persist(); - LOGGER.info("Users test was created"); + LOGGER.info("Peter test was created"); } else { - LOGGER.info("User test wasn't created"); + LOGGER.info("Peter's user test wasn't created"); + } + if(users2.findByPseudo("Robert") == null){ + users2.pseudo = "Robert"; + users2.email = "robert@email.com"; + users2.name = "Toto"; + users2.firstName = "Peter"; + users2.birth = LocalDate.of(1993, Month.FEBRUARY, 23); + users2.status = true; + users2.password = "toto"; + users2.roles = Roles.Admin; + users2.persist(); + LOGGER.info("Robert test was created"); + } else { + LOGGER.info("Robert's user test wasn't created"); } } else { LOGGER.info("DB init wassn't created"); diff --git a/src/main/java/com/covas/TokenRessource.java b/src/main/java/com/covas/TokenRessource.java index eee5144..e4142bc 100644 --- a/src/main/java/com/covas/TokenRessource.java +++ b/src/main/java/com/covas/TokenRessource.java @@ -14,7 +14,6 @@ import javax.transaction.Transactional; import javax.ws.rs.CookieParam; import javax.ws.rs.GET; import javax.ws.rs.InternalServerErrorException; -import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; diff --git a/src/main/java/com/covas/Users.java b/src/main/java/com/covas/Users.java index 14f413e..ab12b47 100644 --- a/src/main/java/com/covas/Users.java +++ b/src/main/java/com/covas/Users.java @@ -9,6 +9,10 @@ import org.hibernate.annotations.ColumnDefault; import io.quarkus.hibernate.orm.panache.PanacheEntity; +enum Roles { + User, + Admin +} @Entity public class Users extends PanacheEntity { @@ -27,6 +31,8 @@ public class Users extends PanacheEntity { public Boolean status; @Column(nullable = false) public String password; + + public Roles roles; public static Users findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); From 205f8471bfb56f41acf90b814ae4fcb842bbf37f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 15 Apr 2022 21:34:45 +0200 Subject: [PATCH 07/21] Re-organisation des packages --- .../ApplicationLifeCycle.java | 9 ++++++--- .../covas/{Users.java => Entity/UsersEntity.java} | 15 +++++++-------- src/main/java/com/covas/Enums/Roles.java | 9 +++++++++ src/main/java/com/covas/{ => Json}/Hello.java | 2 +- src/main/java/com/covas/{ => Json}/Jwt2.java | 2 +- src/main/java/com/covas/{ => Json}/Token.java | 2 +- .../covas/{ => Resources}/GreetingResource.java | 2 +- .../com/covas/{ => Resources}/HelloRessource.java | 4 +++- .../com/covas/{ => Resources}/TokenRessource.java | 10 ++++------ 9 files changed, 33 insertions(+), 22 deletions(-) rename src/main/java/com/covas/{ => ApplicationScoped}/ApplicationLifeCycle.java (91%) rename src/main/java/com/covas/{Users.java => Entity/UsersEntity.java} (77%) create mode 100644 src/main/java/com/covas/Enums/Roles.java rename src/main/java/com/covas/{ => Json}/Hello.java (91%) rename src/main/java/com/covas/{ => Json}/Jwt2.java (96%) rename src/main/java/com/covas/{ => Json}/Token.java (96%) rename src/main/java/com/covas/{ => Resources}/GreetingResource.java (90%) rename src/main/java/com/covas/{ => Resources}/HelloRessource.java (91%) rename src/main/java/com/covas/{ => Resources}/TokenRessource.java (94%) diff --git a/src/main/java/com/covas/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java similarity index 91% rename from src/main/java/com/covas/ApplicationLifeCycle.java rename to src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java index ac09d0e..adac74c 100644 --- a/src/main/java/com/covas/ApplicationLifeCycle.java +++ b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java @@ -1,4 +1,4 @@ -package com.covas; +package com.covas.ApplicationScoped; import org.jboss.logging.Logger; @@ -14,6 +14,9 @@ import javax.enterprise.event.Observes; import javax.inject.Inject; import javax.transaction.Transactional; +import com.covas.Entity.UsersEntity; +import com.covas.Enums.Roles; + import org.eclipse.microprofile.config.inject.ConfigProperty; @@ -29,8 +32,8 @@ public class ApplicationLifeCycle { void onStart(@Observes StartupEvent ev) { LOGGER.info("The application has started"); if (schemaCreate){ - Users users = new Users(); - Users users2 = new Users(); + UsersEntity users = new UsersEntity(); + UsersEntity users2 = new UsersEntity(); if(users.findByPseudo("Peter") == null){ diff --git a/src/main/java/com/covas/Users.java b/src/main/java/com/covas/Entity/UsersEntity.java similarity index 77% rename from src/main/java/com/covas/Users.java rename to src/main/java/com/covas/Entity/UsersEntity.java index ab12b47..3f01c85 100644 --- a/src/main/java/com/covas/Users.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -1,21 +1,20 @@ -package com.covas; +package com.covas.Entity; import java.time.LocalDate; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.Table; + +import com.covas.Enums.Roles; import org.hibernate.annotations.ColumnDefault; import io.quarkus.hibernate.orm.panache.PanacheEntity; -enum Roles { - User, - Admin -} - @Entity -public class Users extends PanacheEntity { +@Table(name = "users") +public class UsersEntity extends PanacheEntity { @Column(nullable = false, unique = true) public String pseudo; @@ -34,7 +33,7 @@ public class Users extends PanacheEntity { public Roles roles; - public static Users findByPseudo(String pseudo){ + public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); } diff --git a/src/main/java/com/covas/Enums/Roles.java b/src/main/java/com/covas/Enums/Roles.java new file mode 100644 index 0000000..8185ec4 --- /dev/null +++ b/src/main/java/com/covas/Enums/Roles.java @@ -0,0 +1,9 @@ +package com.covas; + +public enum Roles { + User, + Admin; + Roles(){ + + } +} \ No newline at end of file diff --git a/src/main/java/com/covas/Hello.java b/src/main/java/com/covas/Json/Hello.java similarity index 91% rename from src/main/java/com/covas/Hello.java rename to src/main/java/com/covas/Json/Hello.java index 1250c08..6a5a749 100644 --- a/src/main/java/com/covas/Hello.java +++ b/src/main/java/com/covas/Json/Hello.java @@ -1,4 +1,4 @@ -package com.covas; +package com.covas.Json; import io.quarkus.runtime.annotations.RegisterForReflection; diff --git a/src/main/java/com/covas/Jwt2.java b/src/main/java/com/covas/Json/Jwt2.java similarity index 96% rename from src/main/java/com/covas/Jwt2.java rename to src/main/java/com/covas/Json/Jwt2.java index 7e98745..d20a02d 100644 --- a/src/main/java/com/covas/Jwt2.java +++ b/src/main/java/com/covas/Json/Jwt2.java @@ -1,4 +1,4 @@ -package com.covas; +package com.covas.Json; import io.quarkus.runtime.annotations.RegisterForReflection; diff --git a/src/main/java/com/covas/Token.java b/src/main/java/com/covas/Json/Token.java similarity index 96% rename from src/main/java/com/covas/Token.java rename to src/main/java/com/covas/Json/Token.java index fe6eec7..3efa5c2 100644 --- a/src/main/java/com/covas/Token.java +++ b/src/main/java/com/covas/Json/Token.java @@ -1,4 +1,4 @@ -package com.covas; +package com.covas.Json; import io.quarkus.runtime.annotations.RegisterForReflection; diff --git a/src/main/java/com/covas/GreetingResource.java b/src/main/java/com/covas/Resources/GreetingResource.java similarity index 90% rename from src/main/java/com/covas/GreetingResource.java rename to src/main/java/com/covas/Resources/GreetingResource.java index e40ba8f..312ddc1 100644 --- a/src/main/java/com/covas/GreetingResource.java +++ b/src/main/java/com/covas/Resources/GreetingResource.java @@ -1,4 +1,4 @@ -package com.covas; +package com.covas.Resources; import javax.ws.rs.GET; import javax.ws.rs.Path; diff --git a/src/main/java/com/covas/HelloRessource.java b/src/main/java/com/covas/Resources/HelloRessource.java similarity index 91% rename from src/main/java/com/covas/HelloRessource.java rename to src/main/java/com/covas/Resources/HelloRessource.java index 4b7e99b..87f637e 100644 --- a/src/main/java/com/covas/HelloRessource.java +++ b/src/main/java/com/covas/Resources/HelloRessource.java @@ -1,4 +1,4 @@ -package com.covas; +package com.covas.Resources; import java.util.Collections; import java.util.LinkedHashSet; @@ -11,6 +11,8 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import com.covas.Json.Hello; + @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/json") diff --git a/src/main/java/com/covas/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java similarity index 94% rename from src/main/java/com/covas/TokenRessource.java rename to src/main/java/com/covas/Resources/TokenRessource.java index e4142bc..84acce6 100644 --- a/src/main/java/com/covas/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -1,16 +1,11 @@ -package com.covas; +package com.covas.Resources; -import java.time.LocalDate; -import java.time.Month; import java.util.Arrays; import java.util.HashSet; -import javax.annotation.PostConstruct; import javax.annotation.security.PermitAll; import javax.annotation.security.RolesAllowed; import javax.inject.Inject; -import javax.transaction.TransactionScoped; -import javax.transaction.Transactional; import javax.ws.rs.CookieParam; import javax.ws.rs.GET; import javax.ws.rs.InternalServerErrorException; @@ -22,6 +17,9 @@ import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; +import com.covas.Json.Jwt2; +import com.covas.Json.Token; + import io.smallrye.jwt.auth.principal.JWTParser; import io.smallrye.jwt.auth.principal.ParseException; import io.smallrye.jwt.build.Jwt; From 33471ad1ca9e90afca418742c25b210a49f0b643 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 15 Apr 2022 21:53:49 +0200 Subject: [PATCH 08/21] Add Hash class --- .../ApplicationLifeCycle.java | 8 ++-- src/main/java/com/covas/Classes/Hash.java | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/covas/Classes/Hash.java diff --git a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java index adac74c..54df434 100644 --- a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java +++ b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java @@ -14,6 +14,7 @@ import javax.enterprise.event.Observes; import javax.inject.Inject; import javax.transaction.Transactional; +import com.covas.Classes.Hash; import com.covas.Entity.UsersEntity; import com.covas.Enums.Roles; @@ -34,8 +35,7 @@ public class ApplicationLifeCycle { if (schemaCreate){ UsersEntity users = new UsersEntity(); UsersEntity users2 = new UsersEntity(); - - + Hash hash = new Hash(); if(users.findByPseudo("Peter") == null){ users.pseudo = "Peter"; users.email = "peter@email.com"; @@ -43,7 +43,7 @@ public class ApplicationLifeCycle { users.firstName = "Peter"; users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); users.status = true; - users.password = "toto"; + users.password = hash.encryptSHA512("toto"); users.roles = Roles.User; users.persist(); LOGGER.info("Peter test was created"); @@ -57,7 +57,7 @@ public class ApplicationLifeCycle { users2.firstName = "Peter"; users2.birth = LocalDate.of(1993, Month.FEBRUARY, 23); users2.status = true; - users2.password = "toto"; + users2.password = hash.encryptSHA512("toto"); users2.roles = Roles.Admin; users2.persist(); LOGGER.info("Robert test was created"); diff --git a/src/main/java/com/covas/Classes/Hash.java b/src/main/java/com/covas/Classes/Hash.java new file mode 100644 index 0000000..b59abb4 --- /dev/null +++ b/src/main/java/com/covas/Classes/Hash.java @@ -0,0 +1,41 @@ +package com.covas.Classes; + +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class Hash { + + public static String encryptPassword(String input) + { + try { + // getInstance() method is called with algorithm SHA-512 + MessageDigest md = MessageDigest.getInstance("SHA-512"); + + // digest() method is called + // to calculate message digest of the input string + // returned as array of byte + byte[] messageDigest = md.digest(input.getBytes()); + + // Convert byte array into signum representation + BigInteger no = new BigInteger(1, messageDigest); + + // Convert message digest into hex value + String hashtext = no.toString(16); + + // Add preceding 0s to make it 32 bit + while (hashtext.length() < 32) { + hashtext = "0" + hashtext; + } + + // return the HashText + return hashtext; + } + + // For specifying wrong message digest algorithms + catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + +} From ff25580bee2a29f018b4b5f0cf02315cd763853d Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 15 Apr 2022 22:38:11 +0200 Subject: [PATCH 09/21] Change name for method --- src/main/java/com/covas/Classes/Hash.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/covas/Classes/Hash.java b/src/main/java/com/covas/Classes/Hash.java index b59abb4..ea9d82b 100644 --- a/src/main/java/com/covas/Classes/Hash.java +++ b/src/main/java/com/covas/Classes/Hash.java @@ -6,7 +6,7 @@ import java.security.NoSuchAlgorithmException; public class Hash { - public static String encryptPassword(String input) + public static String encryptSHA512(String input) { try { // getInstance() method is called with algorithm SHA-512 From 9e325414da0d44bc7790e4defa095bd31fd557ce Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 15 Apr 2022 22:38:24 +0200 Subject: [PATCH 10/21] Remove page index html --- .../resources/META-INF/resources/index.html | 282 ------------------ 1 file changed, 282 deletions(-) delete mode 100644 src/main/resources/META-INF/resources/index.html diff --git a/src/main/resources/META-INF/resources/index.html b/src/main/resources/META-INF/resources/index.html deleted file mode 100644 index 781d7d2..0000000 --- a/src/main/resources/META-INF/resources/index.html +++ /dev/null @@ -1,282 +0,0 @@ - - - - - covas-quarkus - 1.0 - - - -
-
-
- - - - - quarkus_logo_horizontal_rgb_1280px_reverse - - - - - - - - - - - - - - - - - - -
-
-
- -
-
-
-

You just made a Quarkus application.

-

This page is served by Quarkus.

- Visit the Dev UI -

This page: src/main/resources/META-INF/resources/index.html

-

App configuration: src/main/resources/application.properties

-

Static assets: src/main/resources/META-INF/resources/

-

Code: src/main/java

-

Generated starter code:

-
    -
  • - RESTEasy JAX-RS Easily start your RESTful Web Services -
    @Path: /hello -
    Related guide -
  • - -
-
-
-

Selected extensions

-
    -
  • RESTEasy Jackson
  • -
-
Documentation
-

Practical step-by-step guides to help you achieve a specific goal. Use them to help get your work - done.

-
Set up your IDE
-

Everyone has a favorite IDE they like to use to code. Learn how to configure yours to maximize your - Quarkus productivity.

-
-
-
- - From 0b4bb54ba6e6c03c58636debaead158fd84d6333 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 23 Apr 2022 10:52:27 +0200 Subject: [PATCH 11/21] Password WIP --- pom.xml | 4 ++ .../ApplicationLifeCycle.java | 36 ++------------- .../java/com/covas/Entity/UsersEntity.java | 44 ++++++++++++++++--- src/main/java/com/covas/Enums/Roles.java | 9 ---- src/main/java/com/covas/Json/Jwt2.java | 11 ++++- .../com/covas/Resources/TokenRessource.java | 17 ++++--- src/main/resources/application.properties | 3 +- 7 files changed, 69 insertions(+), 55 deletions(-) delete mode 100644 src/main/java/com/covas/Enums/Roles.java diff --git a/pom.xml b/pom.xml index 6c9dbf3..b188d1d 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,10 @@ io.quarkus quarkus-hibernate-orm-panache
+ + io.quarkus + quarkus-security-jpa + io.quarkus quarkus-junit5 diff --git a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java index 54df434..7e7be39 100644 --- a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java +++ b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java @@ -14,9 +14,7 @@ import javax.enterprise.event.Observes; import javax.inject.Inject; import javax.transaction.Transactional; -import com.covas.Classes.Hash; import com.covas.Entity.UsersEntity; -import com.covas.Enums.Roles; import org.eclipse.microprofile.config.inject.ConfigProperty; @@ -33,37 +31,9 @@ public class ApplicationLifeCycle { void onStart(@Observes StartupEvent ev) { LOGGER.info("The application has started"); if (schemaCreate){ - UsersEntity users = new UsersEntity(); - UsersEntity users2 = new UsersEntity(); - Hash hash = new Hash(); - if(users.findByPseudo("Peter") == null){ - users.pseudo = "Peter"; - users.email = "peter@email.com"; - users.name = "Toto"; - users.firstName = "Peter"; - users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); - users.status = true; - users.password = hash.encryptSHA512("toto"); - users.roles = Roles.User; - users.persist(); - LOGGER.info("Peter test was created"); - } else { - LOGGER.info("Peter's user test wasn't created"); - } - if(users2.findByPseudo("Robert") == null){ - users2.pseudo = "Robert"; - users2.email = "robert@email.com"; - users2.name = "Toto"; - users2.firstName = "Peter"; - users2.birth = LocalDate.of(1993, Month.FEBRUARY, 23); - users2.status = true; - users2.password = hash.encryptSHA512("toto"); - users2.roles = Roles.Admin; - users2.persist(); - LOGGER.info("Robert test was created"); - } else { - LOGGER.info("Robert's user test wasn't created"); - } + UsersEntity.deleteAll(); + UsersEntity.add("robert80", "robert80@gmail.com", "titi", "robert", LocalDate.of(1990, Month.JANUARY, 23), "toto", "User"); + 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/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index 3f01c85..2735072 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -5,17 +5,26 @@ import java.time.LocalDate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; - -import com.covas.Enums.Roles; +import javax.xml.bind.DatatypeConverter; 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) @@ -28,13 +37,36 @@ 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; - - public Roles roles; + @Roles + public String roles; public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); } - + + public static void add(String pseudo, String email, String name, String firstName, LocalDate birth, String password, String roles){ + UsersEntity users = new UsersEntity(); + users.pseudo = pseudo; + users.email = email; + users.name = name; + users.firstName = firstName; + users.birth = birth; + users.status = false; + users.password = BcryptUtil.bcryptHash(password); + 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); + } + } diff --git a/src/main/java/com/covas/Enums/Roles.java b/src/main/java/com/covas/Enums/Roles.java deleted file mode 100644 index 8185ec4..0000000 --- a/src/main/java/com/covas/Enums/Roles.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.covas; - -public enum Roles { - User, - Admin; - Roles(){ - - } -} \ No newline at end of file diff --git a/src/main/java/com/covas/Json/Jwt2.java b/src/main/java/com/covas/Json/Jwt2.java index d20a02d..ac6fbd9 100644 --- a/src/main/java/com/covas/Json/Jwt2.java +++ b/src/main/java/com/covas/Json/Jwt2.java @@ -8,6 +8,7 @@ public class Jwt2 { public String name; public Boolean status; public String message; + public String password; public Jwt2(){ name = ""; @@ -21,10 +22,18 @@ public class Jwt2 { message = ""; } - public Jwt2(String name, String message){ + public Jwt2(String name, String password){ + this.name = name; + status = true; + this.password = password; + message = ""; + } + + public Jwt2(String name, String password, String message){ this.name = name; this.message = message; status = true; + this.password = password; } public Jwt2(String name, Boolean status, String message){ diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index 84acce6..b9bb5c6 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -38,25 +38,32 @@ public class TokenRessource { @GET @Path("authentificate") + @RolesAllowed("User") @Produces(MediaType.APPLICATION_JSON) - public Response getUserName(@CookieParam("jwt") String jwtCookie) { - if (jwtCookie == null) { + public Response getUserName(@Context SecurityContext ctx, @CookieParam("jwt") String jwtCookie) { + String name = "anonymous"; + String hash = ""; + if(ctx.getUserPrincipal() != null){ + name = ctx.getUserPrincipal().getName(); + hash = ctx.getUserPrincipal().toString(); + } + if (jwtCookie == null) { // 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("Alice")).cookie(new NewCookie("jwt", newJwtCookie)).build(); + return Response.status(Response.Status.CREATED).entity(new Jwt2(name, hash)).cookie(new NewCookie("jwt", newJwtCookie)).build(); } else { // All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized. try { jwt = parser.parse(jwtCookie); } catch(ParseException p){ - return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2("Alice", 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); - return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName())).build(); + return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName(),hash)).build(); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8e2db47..cf310af 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -12,4 +12,5 @@ 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 \ No newline at end of file +covas.schema.create = true + From a21210935bd2bb401e58ca00af54adb4876c8ebc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 1 May 2022 12:43:41 +0200 Subject: [PATCH 12/21] 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 From 77e473442e8f896bb492e9d4b675003b369669c7 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 3 May 2022 22:46:54 +0200 Subject: [PATCH 13/21] fix hashing 512 --- src/main/java/com/covas/Entity/UsersEntity.java | 4 ++-- src/main/java/com/covas/Resources/TokenRessource.java | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index e7be035..ce0b8d7 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -1,6 +1,5 @@ package com.covas.Entity; -import java.nio.charset.StandardCharsets; import java.time.LocalDate; import javax.persistence.Column; @@ -39,6 +38,7 @@ public class UsersEntity extends PanacheEntity { } public static void add(String pseudo, String email, String name, String firstName, LocalDate birth, String password, String roles){ + UsersEntity users = new UsersEntity(); users.pseudo = pseudo; users.email = email; @@ -46,7 +46,7 @@ public class UsersEntity extends PanacheEntity { users.firstName = firstName; users.birth = birth; users.status = false; - users.password = Hash.encryptSHA512(new String(password.getBytes(), StandardCharsets.UTF_8)); + users.password = Hash.encryptSHA512(password); users.roles = roles; users.persist(); } diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index 4f895c2..1b36835 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -53,14 +53,14 @@ public class TokenRessource { 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])); + password = Hash.encryptSHA512(hash[1]); UsersEntity users = UsersEntity.findByPseudo(pseudo); + if(users != null){ + + } - 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 From 047a32a67783c34d5e1bdf066a1b4829f8e7fe70 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 4 May 2022 22:42:35 +0200 Subject: [PATCH 14/21] Function generate token finish --- src/main/java/com/covas/Json/Jwt2.java | 11 ++----- .../com/covas/Resources/TokenRessource.java | 33 ++++++++++--------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/covas/Json/Jwt2.java b/src/main/java/com/covas/Json/Jwt2.java index ac6fbd9..7032135 100644 --- a/src/main/java/com/covas/Json/Jwt2.java +++ b/src/main/java/com/covas/Json/Jwt2.java @@ -8,7 +8,6 @@ public class Jwt2 { public String name; public Boolean status; public String message; - public String password; public Jwt2(){ name = ""; @@ -22,18 +21,12 @@ public class Jwt2 { message = ""; } - public Jwt2(String name, String password){ - this.name = name; - status = true; - this.password = password; - message = ""; - } + - public Jwt2(String name, String password, String message){ + public Jwt2(String name, String message){ this.name = name; this.message = message; status = true; - this.password = password; } public Jwt2(String name, Boolean status, String message){ diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index 1b36835..ea0554e 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -30,13 +30,9 @@ 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; @@ -52,21 +48,26 @@ public class TokenRessource { if (jwtCookie == null) { String[] hash = new String(Base64.decode(auth.split(" ")[1]), StandardCharsets.UTF_8).split(":"); - String pseudo = hash[0]; + name = hash[0]; password = Hash.encryptSHA512(hash[1]); - UsersEntity users = UsersEntity.findByPseudo(pseudo); + UsersEntity users = UsersEntity.findByPseudo(name); if(users != null){ - - } - - // 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); + if(password.equals(users.password)){ + // Create a JWT token signed using the 'HS256' algorithm + String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn(name).groups(new HashSet<>(Arrays.asList(users.roles))).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, "Token is generated")).cookie(new NewCookie("jwt", newJwtCookie)).build(); + } else { + return Response.status(Response.Status.FORBIDDEN).entity(new Jwt2(name, false, "Password is incorrect")).build(); + } + + + } + return Response.status(Response.Status.NOT_FOUND).entity(new Jwt2(name, false, "User not found")).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 { @@ -76,7 +77,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(),password)).build(); + return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName(),"Token is still valid")).build(); } } @@ -101,7 +102,7 @@ public class TokenRessource { @GET @Path("roles-user") - @RolesAllowed({"Toto"}) + @RolesAllowed({"User"}) @Produces(MediaType.APPLICATION_JSON) public Token helloRolesUser(@Context SecurityContext ctx) { Token token = getResponseString(ctx); From d00aacbadad64add533c2f72e08aa77cc56c7dc4 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 4 May 2022 23:07:47 +0200 Subject: [PATCH 15/21] change class name and moving functions --- .../covas/Json/{Jwt2.java => Message.java} | 10 +-- .../com/covas/Resources/HelloRessource.java | 26 ++++++++ .../com/covas/Resources/TokenRessource.java | 63 ++----------------- 3 files changed, 37 insertions(+), 62 deletions(-) rename src/main/java/com/covas/Json/{Jwt2.java => Message.java} (73%) diff --git a/src/main/java/com/covas/Json/Jwt2.java b/src/main/java/com/covas/Json/Message.java similarity index 73% rename from src/main/java/com/covas/Json/Jwt2.java rename to src/main/java/com/covas/Json/Message.java index 7032135..57f5d85 100644 --- a/src/main/java/com/covas/Json/Jwt2.java +++ b/src/main/java/com/covas/Json/Message.java @@ -3,19 +3,19 @@ package com.covas.Json; import io.quarkus.runtime.annotations.RegisterForReflection; @RegisterForReflection -public class Jwt2 { +public class Message { public String name; public Boolean status; public String message; - public Jwt2(){ + public Message(){ name = ""; status = true; message = ""; } - public Jwt2(String name){ + public Message(String name){ this.name = name; status = true; message = ""; @@ -23,13 +23,13 @@ public class Jwt2 { - public Jwt2(String name, String message){ + public Message(String name, String message){ this.name = name; this.message = message; status = true; } - public Jwt2(String name, Boolean status, String message){ + public Message(String name, Boolean status, String message){ this.name = name; this.status = status; this.message = message; diff --git a/src/main/java/com/covas/Resources/HelloRessource.java b/src/main/java/com/covas/Resources/HelloRessource.java index 87f637e..fe4bc5d 100644 --- a/src/main/java/com/covas/Resources/HelloRessource.java +++ b/src/main/java/com/covas/Resources/HelloRessource.java @@ -4,6 +4,9 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; +import javax.annotation.security.PermitAll; +import javax.annotation.security.RolesAllowed; +import javax.inject.Inject; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -13,10 +16,15 @@ import javax.ws.rs.core.Response; import com.covas.Json.Hello; +import org.eclipse.microprofile.jwt.JsonWebToken; + @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/json") public class HelloRessource { + @Inject + JsonWebToken jwt; + Set hello = Collections.synchronizedSet(new LinkedHashSet<>()); public HelloRessource(){ @@ -24,7 +32,25 @@ public class HelloRessource { } @GET + @PermitAll + @Produces(MediaType.APPLICATION_JSON) public Response hello_json(){ return Response.ok(this.hello).build(); } + + @GET + @Path("/user") + @RolesAllowed({"User"}) + @Produces(MediaType.APPLICATION_JSON) + public Response hello_user(){ + return Response.ok(new Hello(String.format("Hello %s", jwt.getName()))).build(); + } + + @GET + @Path("/admin") + @RolesAllowed({"Admin"}) + @Produces(MediaType.APPLICATION_JSON) + public Response hello_admin(){ + return Response.ok(new Hello(String.format("Hello admin %s", jwt.getName()))).build(); + } } diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index ea0554e..e017aed 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -16,12 +16,10 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; 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; +import com.covas.Json.Message; import io.smallrye.jwt.auth.principal.JWTParser; import io.smallrye.jwt.auth.principal.ParseException; @@ -40,7 +38,6 @@ public class TokenRessource { @Inject JWTParser parser; @GET - @Path("authentificate") @Produces(MediaType.APPLICATION_JSON) public Response getUserName(@HeaderParam("Authorization") String auth, @CookieParam("jwt") String jwtCookie) { String name = "anonymous"; @@ -59,14 +56,14 @@ public class TokenRessource { String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn(name).groups(new HashSet<>(Arrays.asList(users.roles))).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, "Token is generated")).cookie(new NewCookie("jwt", newJwtCookie)).build(); + return Response.status(Response.Status.CREATED).entity(new Message(name, "Token is generated")).cookie(new NewCookie("jwt", newJwtCookie)).build(); } else { - return Response.status(Response.Status.FORBIDDEN).entity(new Jwt2(name, false, "Password is incorrect")).build(); + return Response.status(Response.Status.FORBIDDEN).entity(new Message(name, false, "Password is incorrect")).build(); } } - return Response.status(Response.Status.NOT_FOUND).entity(new Jwt2(name, false, "User not found")).build(); + return Response.status(Response.Status.NOT_FOUND).entity(new Message(name, false, "User not found")).build(); } else { // All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized. @@ -74,58 +71,10 @@ public class TokenRessource { jwt = parser.parse(jwtCookie); } catch(ParseException p){ - return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2(name, false, p.getMessage())).build(); + return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Message(name, false, p.getMessage())).build(); } // or jwt = parser.decrypt(jwtCookie, secret); - return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName(),"Token is still valid")).build(); + return Response.status(Response.Status.OK).entity(new Message(jwt.getName(),"Token is still valid")).build(); } } - - @GET - @Path("permit-all") - @PermitAll - @Produces(MediaType.APPLICATION_JSON) - public Token hello(@Context SecurityContext ctx) { - return getResponseString(ctx); - } - - @GET - @Path("roles-allowed") - @RolesAllowed({"Admin" }) - @Produces(MediaType.APPLICATION_JSON) - public Token helloRolesAllowed(@Context SecurityContext ctx) { - Token token = getResponseString(ctx); - token.name = jwt.getName().toString(); - token.role = "Admin"; - return token; - } - - @GET - @Path("roles-user") - @RolesAllowed({"User"}) - @Produces(MediaType.APPLICATION_JSON) - public Token helloRolesUser(@Context SecurityContext ctx) { - Token token = getResponseString(ctx); - token.name = jwt.getName().toString(); - token.role = "User"; - return token; - } - - - private Token getResponseString(SecurityContext ctx) { - String name; - if (ctx.getUserPrincipal() == null) { - name = "anonymous"; - } else if (!ctx.getUserPrincipal().getName().equals(jwt.getName())) { - throw new InternalServerErrorException("Principal and JsonWebToken names do not match"); - } else { - name = ctx.getUserPrincipal().getName(); - } - return new Token(name, ctx.isSecure(), ctx.getAuthenticationScheme(), hasJwt()); - } - - private boolean hasJwt() { - return jwt.getClaimNames() != null; - } - } From 5c83694bce2e618ad7c363110b3fadc9608156f5 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 6 May 2022 23:36:08 +0200 Subject: [PATCH 16/21] remove class useless --- src/main/java/com/covas/Json/Token.java | 36 ------------------------- 1 file changed, 36 deletions(-) delete mode 100644 src/main/java/com/covas/Json/Token.java diff --git a/src/main/java/com/covas/Json/Token.java b/src/main/java/com/covas/Json/Token.java deleted file mode 100644 index 3efa5c2..0000000 --- a/src/main/java/com/covas/Json/Token.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.covas.Json; - -import io.quarkus.runtime.annotations.RegisterForReflection; - -@RegisterForReflection -public class Token { - - public String name; - public Boolean isHttps; - public String authScheme; - public Boolean hasJwt; - public String birthday; - public String role = ""; - - public Token(){ - this.name = "anonymous"; - this.isHttps = false; - this.authScheme = ""; - this.hasJwt = false; - this.birthday = ""; - this.role = ""; - } - - public Token(String name, Boolean isHttps, String authScheme, Boolean hasJwt){ - this.name = name; - this.isHttps = isHttps; - this.authScheme = authScheme; - this.hasJwt = hasJwt; - this.birthday = ""; - this.role = ""; - } - - - - -} From 0adf2a3e31e72445fa3fbb53c0158fa338de7466 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 7 May 2022 00:03:43 +0200 Subject: [PATCH 17/21] Add users ressources --- .../com/covas/Resources/TokenRessource.java | 8 ++--- .../com/covas/Resources/UsersRessources.java | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/covas/Resources/UsersRessources.java diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index e017aed..883cc74 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -4,15 +4,12 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashSet; -import javax.annotation.security.PermitAll; -import javax.annotation.security.RolesAllowed; + import javax.inject.Inject; import javax.ws.rs.CookieParam; import javax.ws.rs.GET; -import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; @@ -29,7 +26,7 @@ import org.eclipse.microprofile.jwt.JsonWebToken; import org.jboss.resteasy.annotations.jaxrs.HeaderParam; import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64; -@Path("/token") +@Path("/api") public class TokenRessource { @Inject JsonWebToken jwt; @@ -39,6 +36,7 @@ public class TokenRessource { @GET @Produces(MediaType.APPLICATION_JSON) + @Path("token") public Response getUserName(@HeaderParam("Authorization") String auth, @CookieParam("jwt") String jwtCookie) { String name = "anonymous"; String password = ""; diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java new file mode 100644 index 0000000..097d96f --- /dev/null +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -0,0 +1,35 @@ +package com.covas.Resources; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.covas.Entity.UsersEntity; + + +@Consumes(MediaType.APPLICATION_JSON) +@Produces(MediaType.APPLICATION_JSON) +@Path("/api") +public class UsersRessources { + + @GET + @Path("users") + public Response getUsers(){ + return Response.ok(UsersEntity.listAll()).build(); + } + + @GET + @Path("user/{id}") + public Response getSingleUser(Long id){ + UsersEntity users = UsersEntity.findById(id); + if(users == null){ + return Response.status(Response.Status.NOT_FOUND).build(); + } + return Response.ok(users).build(); + } + + +} From 34f38a81aedbcfe646bc95416ee4798efc1b54de Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 11 May 2022 22:32:23 +0200 Subject: [PATCH 18/21] getSingleUser --- .../java/com/covas/Resources/UsersRessources.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 097d96f..d41be90 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -1,29 +1,30 @@ package com.covas.Resources; -import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.covas.Entity.UsersEntity; +import org.jboss.logging.Logger; + -@Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) -@Path("/api") +@Path("/api/users") public class UsersRessources { - + private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); @GET - @Path("users") public Response getUsers(){ return Response.ok(UsersEntity.listAll()).build(); } @GET - @Path("user/{id}") - public Response getSingleUser(Long id){ + @Path("{id}") + public Response getSingleUser(@PathParam("id") Long id){ + LOGGER.info(id); UsersEntity users = UsersEntity.findById(id); if(users == null){ return Response.status(Response.Status.NOT_FOUND).build(); From dabcd87485f959676f19127099a293bcd6f5f692 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 11 May 2022 22:40:24 +0200 Subject: [PATCH 19/21] add root path --- .../com/covas/ApplicationScoped/ApplicationRoot.java | 9 +++++++++ src/main/java/com/covas/Resources/UsersRessources.java | 2 +- src/test/java/com/covas/GreetingResourceTest.java | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/covas/ApplicationScoped/ApplicationRoot.java diff --git a/src/main/java/com/covas/ApplicationScoped/ApplicationRoot.java b/src/main/java/com/covas/ApplicationScoped/ApplicationRoot.java new file mode 100644 index 0000000..732f414 --- /dev/null +++ b/src/main/java/com/covas/ApplicationScoped/ApplicationRoot.java @@ -0,0 +1,9 @@ +package com.covas.ApplicationScoped; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/api") +public class ApplicationRoot extends Application { + +} diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index d41be90..9c4f889 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -13,7 +13,7 @@ import org.jboss.logging.Logger; @Produces(MediaType.APPLICATION_JSON) -@Path("/api/users") +@Path("users") public class UsersRessources { private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); @GET diff --git a/src/test/java/com/covas/GreetingResourceTest.java b/src/test/java/com/covas/GreetingResourceTest.java index 7fb3ed3..6ee0982 100644 --- a/src/test/java/com/covas/GreetingResourceTest.java +++ b/src/test/java/com/covas/GreetingResourceTest.java @@ -12,7 +12,7 @@ public class GreetingResourceTest { @Test public void testHelloEndpoint() { given() - .when().get("/hello") + .when().get("/api/hello") .then() .statusCode(200) .body(is("Hello RESTEasy")); From 4ea644e1d41eb7a206a006905c2c67d0c5a144b6 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 11 May 2022 23:01:27 +0200 Subject: [PATCH 20/21] get id users with uuid --- src/main/java/com/covas/Entity/UsersEntity.java | 15 +++++++++++++-- .../java/com/covas/Resources/UsersRessources.java | 8 +++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index ce0b8d7..0d17bc8 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -1,21 +1,32 @@ package com.covas.Entity; import java.time.LocalDate; +import java.util.UUID; +import javax.annotation.Generated; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; import javax.persistence.Table; import com.covas.Classes.Hash; import org.hibernate.annotations.ColumnDefault; +import org.hibernate.annotations.GenericGenerator; -import io.quarkus.hibernate.orm.panache.PanacheEntity; +import io.quarkus.hibernate.orm.panache.PanacheEntityBase; @Entity @Table(name = "users") -public class UsersEntity extends PanacheEntity { +public class UsersEntity extends PanacheEntityBase { + @Id + @Column(name = "id") + @GeneratedValue(generator = "UUID") + @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator") + public UUID id; @Column(nullable = false, unique = true) public String pseudo; diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 9c4f889..39ac060 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -1,5 +1,7 @@ package com.covas.Resources; +import java.util.UUID; + import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -23,9 +25,9 @@ public class UsersRessources { @GET @Path("{id}") - public Response getSingleUser(@PathParam("id") Long id){ - LOGGER.info(id); - UsersEntity users = UsersEntity.findById(id); + public Response getSingleUser(@PathParam("id") String id){ + UUID uid = UUID.fromString(id); + UsersEntity users = UsersEntity.findById(uid); if(users == null){ return Response.status(Response.Status.NOT_FOUND).build(); } From 77aa772a84ddead493bd561ef77eed2ff643684f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 12 May 2022 22:45:54 +0200 Subject: [PATCH 21/21] remove class useless and reduce reponse http for token class --- src/main/java/com/covas/Json/Message.java | 38 ------------------- .../com/covas/Resources/TokenRessource.java | 11 +++--- .../com/covas/Resources/UsersRessources.java | 10 +++++ 3 files changed, 15 insertions(+), 44 deletions(-) delete mode 100644 src/main/java/com/covas/Json/Message.java diff --git a/src/main/java/com/covas/Json/Message.java b/src/main/java/com/covas/Json/Message.java deleted file mode 100644 index 57f5d85..0000000 --- a/src/main/java/com/covas/Json/Message.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.covas.Json; - -import io.quarkus.runtime.annotations.RegisterForReflection; - -@RegisterForReflection -public class Message { - - public String name; - public Boolean status; - public String message; - - public Message(){ - name = ""; - status = true; - message = ""; - } - - public Message(String name){ - this.name = name; - status = true; - message = ""; - } - - - - public Message(String name, String message){ - this.name = name; - this.message = message; - status = true; - } - - public Message(String name, Boolean status, String message){ - this.name = name; - this.status = status; - this.message = message; - } - -} diff --git a/src/main/java/com/covas/Resources/TokenRessource.java b/src/main/java/com/covas/Resources/TokenRessource.java index 883cc74..f1864f4 100644 --- a/src/main/java/com/covas/Resources/TokenRessource.java +++ b/src/main/java/com/covas/Resources/TokenRessource.java @@ -16,7 +16,6 @@ import javax.ws.rs.core.Response; import com.covas.Classes.Hash; import com.covas.Entity.UsersEntity; -import com.covas.Json.Message; import io.smallrye.jwt.auth.principal.JWTParser; import io.smallrye.jwt.auth.principal.ParseException; @@ -54,14 +53,14 @@ public class TokenRessource { String newJwtCookie = Jwt.issuer("https://example.com/issuer").upn(name).groups(new HashSet<>(Arrays.asList(users.roles))).sign(); // or create a JWT token encrypted using the 'A256KW' algorithm // Jwt.upn("alice").encryptWithSecret(secret); - return Response.status(Response.Status.CREATED).entity(new Message(name, "Token is generated")).cookie(new NewCookie("jwt", newJwtCookie)).build(); + return Response.status(Response.Status.CREATED).cookie(new NewCookie("jwt", newJwtCookie)).build(); } else { - return Response.status(Response.Status.FORBIDDEN).entity(new Message(name, false, "Password is incorrect")).build(); + return Response.status(Response.Status.FORBIDDEN).build(); } } - return Response.status(Response.Status.NOT_FOUND).entity(new Message(name, false, "User not found")).build(); + return Response.status(Response.Status.NOT_FOUND).build(); } else { // All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized. @@ -69,10 +68,10 @@ public class TokenRessource { jwt = parser.parse(jwtCookie); } catch(ParseException p){ - return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Message(name, false, p.getMessage())).build(); + return Response.status(Response.Status.UNAUTHORIZED).build(); } // or jwt = parser.decrypt(jwtCookie, secret); - return Response.status(Response.Status.OK).entity(new Message(jwt.getName(),"Token is still valid")).build(); + return Response.status(Response.Status.OK).build(); } } } diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 39ac060..ade1086 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -2,6 +2,7 @@ package com.covas.Resources; import java.util.UUID; +import javax.annotation.security.RolesAllowed; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -19,11 +20,13 @@ import org.jboss.logging.Logger; public class UsersRessources { private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); @GET + @RolesAllowed({"Admin"}) public Response getUsers(){ return Response.ok(UsersEntity.listAll()).build(); } @GET + @RolesAllowed({"Admin"}) @Path("{id}") public Response getSingleUser(@PathParam("id") String id){ UUID uid = UUID.fromString(id); @@ -34,5 +37,12 @@ public class UsersRessources { return Response.ok(users).build(); } + @GET + @RolesAllowed({"User"}) + @Path("info") + public Response getInfoUser(){ + return Response.ok().build(); + } + }