-
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.
- Loading branch information
Showing
7 changed files
with
243 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Changes | ||
|
||
* Add ReCaptcha v3 | ||
|
||
## v2.20.0-2 _(2024-10-22)_ | ||
* Fix upstream regression on Nginx handler | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
--- /dev/null | ||
+++ b/usr/share/perl5/Lemonldap/NG/Portal/Captcha/ReCaptcha3.pm | ||
@@ -0,0 +1,100 @@ | ||
+package Lemonldap::NG::Portal::Captcha::ReCaptcha3; | ||
+ | ||
+use strict; | ||
+use Mouse; | ||
+use Lemonldap::NG::Common::UserAgent; | ||
+ | ||
+# Add constants used by this module | ||
+ | ||
+our $VERSION = '2.20.0'; | ||
+ | ||
+extends 'Lemonldap::NG::Portal::Main::Plugin'; | ||
+ | ||
+has ua => ( | ||
+ is => 'rw', | ||
+ lazy => 1, | ||
+ builder => sub { | ||
+ my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} ); | ||
+ $ua->env_proxy(); | ||
+ return $ua; | ||
+ } | ||
+); | ||
+ | ||
+has score => ( | ||
+ is => 'ro', | ||
+ default => sub { | ||
+ return $_[0]->{conf}->{captchaOptions}->{score} || 0.5; | ||
+ } | ||
+); | ||
+ | ||
+sub init { | ||
+ my ($self) = @_; | ||
+ unless ($self->conf->{captchaOptions}->{dataSiteKey} | ||
+ and $self->conf->{captchaOptions}->{secretKey} ) | ||
+ { | ||
+ $self->logger->error('Missing required options for reCaptcha'); | ||
+ return 0; | ||
+ } | ||
+ return 1; | ||
+} | ||
+ | ||
+sub init_captcha { | ||
+ my ( $self, $req ) = @_; | ||
+ | ||
+ $req->data->{customScript} .= | ||
+ '<script src="https://www.google.com/recaptcha/api.js?render=' | ||
+ . $self->conf->{captchaOptions}->{dataSiteKey} | ||
+ . '"></script>' | ||
+ . '<script type="application/init"> | ||
+{ | ||
+"datasitekey": "' . $self->conf->{captchaOptions}->{dataSiteKey} . '" | ||
+} | ||
+</script>' | ||
+ . '<script src="/static/common/js/recaptchav3.js"></script>'; | ||
+ | ||
+ # Read option from the manager configuration | ||
+ my $dataSiteKey = $self->conf->{captchaOptions}->{dataSiteKey}; | ||
+ my $html = | ||
+qq'<div class="g-recaptcha" data-sitekey="$dataSiteKey" data-action="LOGIN"> | ||
+<input type="hidden" id="grr" name="g-recaptcha-response" /> | ||
+</div>'; | ||
+ $req->captchaHtml($html); | ||
+} | ||
+ | ||
+sub check_captcha { | ||
+ my ( $self, $req ) = @_; | ||
+ | ||
+ my $captcha_input = $req->param('g-recaptcha-response'); | ||
+ unless ($captcha_input) { | ||
+ $self->logger->info('No captcha value submitted'); | ||
+ return 0; | ||
+ } | ||
+ my $response = $self->ua->post( | ||
+ 'https://www.google.com/recaptcha/api/siteverify', | ||
+ { | ||
+ secret => $self->conf->{captchaOptions}->{secretKey}, | ||
+ response => $captcha_input, | ||
+ } | ||
+ ); | ||
+ if ( $response->is_success ) { | ||
+ my $res = eval { JSON::from_json( $response->decoded_content ) }; | ||
+ if ($@) { | ||
+ $self->logger->error("reCaptcha: $@"); | ||
+ return 0; | ||
+ } | ||
+ my $success = | ||
+ $res->{success} | ||
+ and $res->{score} | ||
+ and ( $res->{score} >= $self->score ); | ||
+ unless ($success) { | ||
+ $self->logger->info( | ||
+ 'reCaptcha errors:' . $response->decoded_content ); | ||
+ } | ||
+ return $success; | ||
+ } | ||
+ $self->logger->error( 'reCaptcha error: ' . $response->status_line ); | ||
+ return 0; | ||
+} | ||
+ | ||
+1; | ||
+ | ||
--- /dev/null | ||
+++ b/usr/share/lemonldap-ng/portal/htdocs/static/common/js/recaptchav3.js | ||
@@ -0,0 +1,13 @@ | ||
+(function() { | ||
+ $(document).ready(function() { | ||
+ $('form').on('submit', function(e) { | ||
+ e.preventDefault(); | ||
+ grecaptcha.ready(function() { | ||
+ grecaptcha.execute(datas['datasitekey'], {action: 'submit'}).then(function(token) { | ||
+ $('#grr').val(token); | ||
+ $(e.currentTarget).unbind('submit').submit(); | ||
+ }); | ||
+ }) | ||
+ }) | ||
+ }); | ||
+}).call(this); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
--- /dev/null | ||
+++ b/usr/share/perl5/Lemonldap/NG/Portal/Captcha/ReCaptcha3.pm | ||
@@ -0,0 +1,100 @@ | ||
+package Lemonldap::NG::Portal::Captcha::ReCaptcha3; | ||
+ | ||
+use strict; | ||
+use Mouse; | ||
+use Lemonldap::NG::Common::UserAgent; | ||
+ | ||
+# Add constants used by this module | ||
+ | ||
+our $VERSION = '2.20.0'; | ||
+ | ||
+extends 'Lemonldap::NG::Portal::Main::Plugin'; | ||
+ | ||
+has ua => ( | ||
+ is => 'rw', | ||
+ lazy => 1, | ||
+ builder => sub { | ||
+ my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} ); | ||
+ $ua->env_proxy(); | ||
+ return $ua; | ||
+ } | ||
+); | ||
+ | ||
+has score => ( | ||
+ is => 'ro', | ||
+ default => sub { | ||
+ return $_[0]->{conf}->{captchaOptions}->{score} || 0.5; | ||
+ } | ||
+); | ||
+ | ||
+sub init { | ||
+ my ($self) = @_; | ||
+ unless ($self->conf->{captchaOptions}->{dataSiteKey} | ||
+ and $self->conf->{captchaOptions}->{secretKey} ) | ||
+ { | ||
+ $self->logger->error('Missing required options for reCaptcha'); | ||
+ return 0; | ||
+ } | ||
+ return 1; | ||
+} | ||
+ | ||
+sub init_captcha { | ||
+ my ( $self, $req ) = @_; | ||
+ | ||
+ $req->data->{customScript} .= | ||
+ '<script src="https://www.google.com/recaptcha/api.js?render=' | ||
+ . $self->conf->{captchaOptions}->{dataSiteKey} | ||
+ . '"></script>' | ||
+ . '<script type="application/init"> | ||
+{ | ||
+"datasitekey": "' . $self->conf->{captchaOptions}->{dataSiteKey} . '" | ||
+} | ||
+</script>' | ||
+ . '<script src="/static/common/js/recaptchav3.js"></script>'; | ||
+ | ||
+ # Read option from the manager configuration | ||
+ my $dataSiteKey = $self->conf->{captchaOptions}->{dataSiteKey}; | ||
+ my $html = | ||
+qq'<div class="g-recaptcha" data-sitekey="$dataSiteKey" data-action="LOGIN"> | ||
+<input type="hidden" id="grr" name="g-recaptcha-response" /> | ||
+</div>'; | ||
+ $req->captchaHtml($html); | ||
+} | ||
+ | ||
+sub check_captcha { | ||
+ my ( $self, $req ) = @_; | ||
+ | ||
+ my $captcha_input = $req->param('g-recaptcha-response'); | ||
+ unless ($captcha_input) { | ||
+ $self->logger->info('No captcha value submitted'); | ||
+ return 0; | ||
+ } | ||
+ my $response = $self->ua->post( | ||
+ 'https://www.google.com/recaptcha/api/siteverify', | ||
+ { | ||
+ secret => $self->conf->{captchaOptions}->{secretKey}, | ||
+ response => $captcha_input, | ||
+ } | ||
+ ); | ||
+ if ( $response->is_success ) { | ||
+ my $res = eval { JSON::from_json( $response->decoded_content ) }; | ||
+ if ($@) { | ||
+ $self->logger->error("reCaptcha: $@"); | ||
+ return 0; | ||
+ } | ||
+ my $success = | ||
+ $res->{success} | ||
+ and $res->{score} | ||
+ and ( $res->{score} >= $self->score ); | ||
+ unless ($success) { | ||
+ $self->logger->info( | ||
+ 'reCaptcha errors:' . $response->decoded_content ); | ||
+ } | ||
+ return $success; | ||
+ } | ||
+ $self->logger->error( 'reCaptcha error: ' . $response->status_line ); | ||
+ return 0; | ||
+} | ||
+ | ||
+1; | ||
+ | ||
--- /dev/null | ||
+++ b/usr/share/lemonldap-ng/portal/htdocs/static/common/js/recaptchav3.js | ||
@@ -0,0 +1,13 @@ | ||
+(function() { | ||
+ $(document).ready(function() { | ||
+ $('form').on('submit', function(e) { | ||
+ e.preventDefault(); | ||
+ grecaptcha.ready(function() { | ||
+ grecaptcha.execute(datas['datasitekey'], {action: 'submit'}).then(function(token) { | ||
+ $('#grr').val(token); | ||
+ $(e.currentTarget).unbind('submit').submit(); | ||
+ }); | ||
+ }) | ||
+ }) | ||
+ }); | ||
+}).call(this); |