-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use captcha in register & reset password endpoints
- Loading branch information
Showing
8 changed files
with
98 additions
and
32 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
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
10 changes: 10 additions & 0 deletions
10
iam/src/main/java/io/corbel/iam/service/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,10 @@ | ||
package io.corbel.iam.service; | ||
|
||
/** | ||
* @author Alberto J. Rubios | ||
*/ | ||
public interface CaptchaService { | ||
|
||
boolean verifyRequestCaptcha(String domainId, String captcha); | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
iam/src/main/java/io/corbel/iam/service/DefaultCaptchaService.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,41 @@ | ||
package io.corbel.iam.service; | ||
|
||
import ch.compile.recaptcha.ReCaptchaVerify; | ||
import ch.compile.recaptcha.model.SiteVerifyResponse; | ||
import io.corbel.iam.api.IamErrorResponseFactory; | ||
|
||
import javax.ws.rs.NotFoundException; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author Alberto J. Rubio | ||
*/ | ||
public class DefaultCaptchaService implements CaptchaService { | ||
|
||
private static final String CAPTCHA_SECRET_KEY = "secret_key"; | ||
private static final String CAPTCHA_KEY = "captcha"; | ||
|
||
private final DomainService domainService; | ||
|
||
public DefaultCaptchaService(DomainService domainService) { | ||
this.domainService = domainService; | ||
} | ||
|
||
@Override | ||
public boolean verifyRequestCaptcha(String domainId, String captcha) { | ||
return domainService.getDomain(domainId).map(domain -> { | ||
Map<String, String> configuration = domain.getAuthConfigurations().get(CAPTCHA_KEY); | ||
if(configuration != null) { | ||
ReCaptchaVerify reCaptchaVerify = new ReCaptchaVerify(configuration.get(CAPTCHA_SECRET_KEY)); | ||
try { | ||
SiteVerifyResponse siteVerifyResponse = reCaptchaVerify.verify(captcha, null); | ||
return siteVerifyResponse.isSuccess(); | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} else return true; | ||
}).orElse(false); | ||
} | ||
} |
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