-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
243 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
client/src/main/java/com/gdevxy/blog/client/google/GoogleClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.gdevxy.blog.client.google; | ||
|
||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
|
||
import com.gdevxy.blog.client.google.model.CaptchaVerifyRequest; | ||
import com.gdevxy.blog.client.google.model.CaptchaVerifyResponse; | ||
import io.smallrye.mutiny.Uni; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
@RegisterRestClient(configKey = "google") | ||
public interface GoogleClient { | ||
|
||
@POST | ||
@Path("/recaptcha/api/siteverify") | ||
Uni<CaptchaVerifyResponse> verifyCaptcha(CaptchaVerifyRequest req); | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
client/src/main/java/com/gdevxy/blog/client/google/model/CaptchaVerifyRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.gdevxy.blog.client.google.model; | ||
|
||
public record CaptchaVerifyRequest(String secret, String response) { | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
client/src/main/java/com/gdevxy/blog/client/google/model/CaptchaVerifyResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.gdevxy.blog.client.google.model; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@Getter | ||
@Builder | ||
@Jacksonized | ||
@ToString | ||
public class CaptchaVerifyResponse { | ||
|
||
@Builder.Default | ||
private final Boolean success = false; | ||
@JsonProperty("error-codes") | ||
private final List<String> errorCodes; | ||
private final String hostname; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
model/src/main/java/com/gdevxy/blog/model/BlogPostRateReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.gdevxy.blog.model; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
|
||
public record BlogPostRateReq(@NotEmpty String captcha) { | ||
} |
36 changes: 36 additions & 0 deletions
36
service/src/main/java/com/gdevxy/blog/service/captcha/CaptchaService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.gdevxy.blog.service.captcha; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.ws.rs.ForbiddenException; | ||
|
||
import com.gdevxy.blog.client.google.GoogleClient; | ||
import com.gdevxy.blog.client.google.model.CaptchaVerifyRequest; | ||
import io.smallrye.mutiny.Uni; | ||
import io.smallrye.mutiny.unchecked.Unchecked; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
|
||
@Slf4j | ||
@ApplicationScoped | ||
public class CaptchaService { | ||
|
||
@RestClient | ||
GoogleClient googleClient; | ||
|
||
@ConfigProperty(name = "google.captcha.secret") | ||
String secret; | ||
|
||
public Uni<Void> verify(String captcha) { | ||
|
||
return googleClient.verifyCaptcha(new CaptchaVerifyRequest(secret, captcha)) | ||
.invoke(Unchecked.consumer(res -> { | ||
if (!res.getSuccess()){ | ||
log.info("Captcha validation failed {}", res); | ||
throw new ForbiddenException(); | ||
} | ||
})) | ||
.replaceWithVoid(); | ||
} | ||
|
||
} |
Oops, something went wrong.