blib/lib/Lemonldap/NG/Portal/Captcha/SecurityImage.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Lemonldap/NG/Portal/Auth/Custom.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Main.pm »
1 package Lemonldap::NG::Portal::Captcha::SecurityImage;
2  
3 340 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 340 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 340 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 340 ▶use GD::SecurityImage use_magick => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '2.0.15';
9  
10 extends 'Lemonldap::NG::Portal::Main::Plugin';
11  
12 has width => (
13 is => 'rw',
14 lazy => 1,
15 default => sub { $_[0]->{conf}->{captchaWidth} || 220 }
16 );
17 has height => (
18 is => 'rw',
19 lazy => 1,
20 default => sub { $_[0]->{conf}->{captchaHeight} || 60 }
21 );
22 has lines => (
23 is => 'rw',
24 lazy => 1,
25 default => sub { $_[0]->{conf}->{captchaLines} || 4 }
26 );
27 has scramble => (
28 is => 'rw',
29 lazy => 1,
30 default => sub { $_[0]->{conf}->{captchaScramble} || 1 }
31 );
32 has fgColor => (
33 is => 'rw',
34 lazy => 1,
35 default => sub { $_[0]->{conf}->{captchaFg} || '#403030' }
36 );
37 has bgColor => (
38 is => 'rw',
39 lazy => 1,
40 default => sub { $_[0]->{conf}->{captchaBg} || '#FF644B' }
41 );
42 has rndmax => (
43 is => 'rw',
44 lazy => 1,
45 default => sub { $_[0]->{conf}->{captcha_size} || 6 }
46 );
47 has timeout => (
48 is => 'rw',
49 lazy => 1,
50 default => sub { $_[0]->{conf}->{formTimeout} }
51 );
52  
53 has ott => (
54 is => 'rw',
55 lazy => 1,
56 default => sub {
57 my $ott = $_[0]->{p}->loadModule('::Lib::OneTimeToken');
58 $ott->timeout( $_[0]->timeout );
59 return $ott;
60 }
61 );
62  
63 sub init {
64 my ($self) = @_;
65 $self->addUnauthRoute( renewcaptcha => '_sendCaptcha', ['GET'] );
66 return 1;
67 }
68  
69 # Internal methods
70 sub _getCaptcha {
71 my ($self) = @_;
72 my $image = GD::SecurityImage->new(
73 width => $self->width,
74 height => $self->height,
75 lines => $self->lines,
76 gd_font => 'Giant',
77 scramble => $self->scramble,
78 rndmax => $self->rndmax,
79 );
80 $image->random;
81 $image->create( 'normal', 'default', $self->fgColor, $self->bgColor );
82 my ( $imageData, $mimeType, $rdm ) = $image->out( force => 'png' );
83 my $img = 'data:image/png;base64,' . encode_base64( $imageData, '' );
84 my $token = $self->ott->createToken( { captcha => $rdm } );
85 return ( $token, $img );
86 }
87  
88 sub _sendCaptcha {
89 my ( $self, $req ) = @_;
90 $self->logger->info("User request for captcha renew");
91 my ( $token, $image ) = $self->_getCaptcha($req);
92  
93 return $self->p->sendJSONresponse( $req,
94 { newtoken => $token, newimage => $image } );
95 }
96  
97 sub _validate_captcha_token {
98 my ( $self, $token, $value ) = @_;
99 my $s = $self->ott->getToken($token);
100 unless ($s) {
101 $self->logger->warn("Captcha token $token isn't valid");
102 return 0;
103 }
104 unless ( $s->{captcha} eq $value ) {
105 $self->logger->notice('Bad captcha response');
106 return 0;
107 }
108 $self->logger->debug('Good captcha response');
109 return 1;
110 }
111  
112 sub _get_captcha_html {
113 my ( $self, $req, $src ) = @_;
114  
115 my $sp = $self->p->staticPrefix;
116 $sp =~ s/\/*$/\//;
117  
118 return $self->loadTemplate(
119 $req,
120 'captcha',
121 params => {
122 STATIC_PREFIX => $sp,
123 CAPTCHA_SRC => $src,
124 CAPTCHA_SIZE => $self->rndmax,
125 }
126 );
127 }
128  
129 # New API
130 sub check_captcha {
131 my ( $self, $req ) = @_;
132 my $token = $req->param('token');
133 unless ($token) {
134 $self->logger->warn("No token provided for Captcha::SecurityImage");
135 return 0;
136 }
137  
138 my $value = $req->param('captcha');
139 unless ($value) {
140 $self->logger->warn("No response provided for Captcha::SecurityImage");
141 return 0;
142 }
143  
144 return $self->_validate_captcha_token( $token, $value );
145 }
146  
147 sub init_captcha {
148 my ( $self, $req ) = @_;
149  
150 my ( $token, $image ) = $self->_getCaptcha;
151 $self->logger->debug('Prepare captcha');
152 $req->token($token);
153 $req->captchaHtml( $self->_get_captcha_html( $req, $image ) );
154  
155 # DEPRECATED: Compatibility with old templates
156 $req->captcha($image);
157 }
158  
159 # #######
160 # Old API
161 # TODO: Remove this in 3.0
162 # #######
163  
164 sub validateCaptcha {
165 my ( $self, $token, $value ) = @_;
166 return $self->_validate_captcha_token( $token, $value );
167 }
168  
169 sub setCaptcha {
170 my ( $self, $req ) = @_;
171 $self->init_captcha($req);
172 }
173  
174 sub sendCaptcha {
175 my ( $self, $req ) = @_;
176 return $self->_sendCaptcha($req);
177 }
178  
179 sub getCaptcha {
180 my ( $self, $req ) = @_;
181 return $self->_getCaptcha($req);
182 }
183  
184 1;
« blib/lib/Lemonldap/NG/Portal/Auth/Custom.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Main.pm »