From dce878dc04fadcd99e6256dfc4bae9051601dc39 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 15 Jun 2022 23:25:13 +0200 Subject: [PATCH] mail resources with redis --- src/main/java/com/covas/Json/RedisMail.java | 15 +++++++++++++++ .../java/com/covas/Resources/MailRessource.java | 12 ++++++++++-- src/main/resources/templates/mailer.txt | 4 +++- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/covas/Json/RedisMail.java diff --git a/src/main/java/com/covas/Json/RedisMail.java b/src/main/java/com/covas/Json/RedisMail.java new file mode 100644 index 0000000..e5792b0 --- /dev/null +++ b/src/main/java/com/covas/Json/RedisMail.java @@ -0,0 +1,15 @@ +package com.covas.Json; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public class RedisMail { + public String key = ""; + public RedisMail(){ + key = ""; + } + + public RedisMail(String key){ + this.key = key; + } +} diff --git a/src/main/java/com/covas/Resources/MailRessource.java b/src/main/java/com/covas/Resources/MailRessource.java index a157725..9205b2a 100644 --- a/src/main/java/com/covas/Resources/MailRessource.java +++ b/src/main/java/com/covas/Resources/MailRessource.java @@ -10,6 +10,7 @@ import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; import com.covas.Entity.UsersEntity; +import com.covas.Json.RedisMail; import io.quarkus.mailer.MailTemplate; import io.quarkus.redis.client.RedisClient; @@ -29,16 +30,23 @@ public class MailRessource { if(UsersEntity.findByPseudo(users.pseudo) == null){ return Response.status(Response.Status.NOT_FOUND).build(); } - mailer.to("valcze808@gmail.com").subject("test").data("name", "Robert").send().subscribeAsCompletionStage(); + String randomKey=UUID.randomUUID().toString(); + redisClient.set(Arrays.asList(users.id.toString(),randomKey)); + mailer.to(users.email).subject(String.format("Compte %s créée", users.email)).data("name", users.pseudo).data("id",users.id).data("key",randomKey).send().subscribeAsCompletionStage(); return Response.ok().build(); } @GET @Path("{id}") - public Response activateUsers(@PathParam("id") String id){ + public Response activateUsers(@PathParam("id") String id, RedisMail redisMail){ if(UsersEntity.findById(UUID.fromString(id)) == null){ return Response.status(Response.Status.NOT_FOUND).build(); } + String redisKey = redisClient.get(id).toString(); + + if(!redisKey.equals(redisMail.key)){ + return Response.status(Response.Status.NOT_ACCEPTABLE).build(); + } return Response.ok().build(); } diff --git a/src/main/resources/templates/mailer.txt b/src/main/resources/templates/mailer.txt index 9a61208..c1c4474 100644 --- a/src/main/resources/templates/mailer.txt +++ b/src/main/resources/templates/mailer.txt @@ -1 +1,3 @@ -Coucou {name} ! \ No newline at end of file +Coucou {name} + +Voici un lien https://localhost:8080/api/mail/{id}?key={key}