Password WIP
This commit is contained in:
parent
9e325414da
commit
0b4bb54ba6
4
pom.xml
4
pom.xml
@ -56,6 +56,10 @@
|
|||||||
<groupId>io.quarkus</groupId>
|
<groupId>io.quarkus</groupId>
|
||||||
<artifactId>quarkus-hibernate-orm-panache</artifactId>
|
<artifactId>quarkus-hibernate-orm-panache</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.quarkus</groupId>
|
||||||
|
<artifactId>quarkus-security-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.quarkus</groupId>
|
<groupId>io.quarkus</groupId>
|
||||||
<artifactId>quarkus-junit5</artifactId>
|
<artifactId>quarkus-junit5</artifactId>
|
||||||
|
@ -14,9 +14,7 @@ import javax.enterprise.event.Observes;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
import com.covas.Classes.Hash;
|
|
||||||
import com.covas.Entity.UsersEntity;
|
import com.covas.Entity.UsersEntity;
|
||||||
import com.covas.Enums.Roles;
|
|
||||||
|
|
||||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||||
|
|
||||||
@ -33,37 +31,9 @@ public class ApplicationLifeCycle {
|
|||||||
void onStart(@Observes StartupEvent ev) {
|
void onStart(@Observes StartupEvent ev) {
|
||||||
LOGGER.info("The application has started");
|
LOGGER.info("The application has started");
|
||||||
if (schemaCreate){
|
if (schemaCreate){
|
||||||
UsersEntity users = new UsersEntity();
|
UsersEntity.deleteAll();
|
||||||
UsersEntity users2 = new UsersEntity();
|
UsersEntity.add("robert80", "robert80@gmail.com", "titi", "robert", LocalDate.of(1990, Month.JANUARY, 23), "toto", "User");
|
||||||
Hash hash = new Hash();
|
UsersEntity.add("peter93", "peter93gmail.com", "yollo", "peter", LocalDate.of(1993, Month.FEBRUARY, 26), "toto", "Admin");
|
||||||
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");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
LOGGER.info("DB init wassn't created");
|
LOGGER.info("DB init wassn't created");
|
||||||
}
|
}
|
||||||
|
@ -5,17 +5,26 @@ 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.Enums.Roles;
|
|
||||||
|
|
||||||
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)
|
||||||
@ -28,13 +37,36 @@ 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 Roles roles;
|
public String roles;
|
||||||
|
|
||||||
public static UsersEntity findByPseudo(String pseudo){
|
public static UsersEntity findByPseudo(String pseudo){
|
||||||
return find("pseudo", pseudo).firstResult();
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package com.covas;
|
|
||||||
|
|
||||||
public enum Roles {
|
|
||||||
User,
|
|
||||||
Admin;
|
|
||||||
Roles(){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,6 +8,7 @@ public class Jwt2 {
|
|||||||
public String name;
|
public String name;
|
||||||
public Boolean status;
|
public Boolean status;
|
||||||
public String message;
|
public String message;
|
||||||
|
public String password;
|
||||||
|
|
||||||
public Jwt2(){
|
public Jwt2(){
|
||||||
name = "";
|
name = "";
|
||||||
@ -21,10 +22,18 @@ public class Jwt2 {
|
|||||||
message = "";
|
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.name = name;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
status = true;
|
status = true;
|
||||||
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Jwt2(String name, Boolean status, String message){
|
public Jwt2(String name, Boolean status, String message){
|
||||||
|
@ -38,25 +38,32 @@ public class TokenRessource {
|
|||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("authentificate")
|
@Path("authentificate")
|
||||||
|
@RolesAllowed("User")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response getUserName(@CookieParam("jwt") String jwtCookie) {
|
public Response getUserName(@Context SecurityContext ctx, @CookieParam("jwt") String jwtCookie) {
|
||||||
if (jwtCookie == null) {
|
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
|
// 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("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 {
|
} 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 {
|
||||||
jwt = parser.parse(jwtCookie);
|
jwt = parser.parse(jwtCookie);
|
||||||
}
|
}
|
||||||
catch(ParseException p){
|
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);
|
// 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
# 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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user