Mojolicious/Plugin/Hakkefuin.pm untested

filter: Statementstmt 0.0% 0 / 280 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 89 MC/DCmcdc 0.0% 0 / 51 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 562 CC 44 SCAR 34.3
CC
44
Cov
0%
_unlock
90.0
_lock
72.0
_has_auth
56.0
CRAP 30.9
1 package Mojolicious::Plugin::Hakkefuin;
2 use Mojo::Base 'Mojolicious::Plugin';
3  
4 use CellBIS::Random;
5 use Mojo::Hakkefuin;
6 use Mojo::Hakkefuin::Utils;
7 use Mojo::Hakkefuin::Sessions;
8 use Mojo::Util qw(secure_compare);
9  
10 # ABSTRACT: The Minimalistic Mojolicious Authentication
11 our $VERSION = '0.1.3';
12 our $VERSION_STATE = 'Beta';
13 our $CODENAME = 'YataMirror';
14  
15 has mojo_hf => 'Mojo::Hakkefuin';
16 has utils => sub {
17 state $utils = Mojo::Hakkefuin::Utils->new(random => 'String::Random');
18 };
19 has cookies => sub {
20 state $cookies = Mojolicious::Plugin::Hakkefuin::_cookies->new(
21 utils => shift->utils,
22 random => 'String::Random'
23 );
24 };
25 has cookies_lock => sub {
26 state $cookie = Mojolicious::Plugin::Hakkefuin::_cookiesLock->new(
27 utils => shift->utils,
28 random => 'String::Random'
29 );
30 };
31 has random => 'String::Random';
32 has crand => sub { state $crand = CellBIS::Random->new };
33 has session_manager => sub {undef};
34  
35 sub register {
36 my ($self, $app, $conf) = @_;
37  
38 # Helper for version
39 $app->helper(
40 mojo_version => sub {
41 return {
42 'num' => $VERSION,
43 'state' => $VERSION_STATE,
44 'codename' => $CODENAME
45 };
46 }
47 );
48  
49 # Home Dir
50 my $home = $app->home->detect;
51  
52 # Check Config
53 $conf //= {};
54 $conf->{'helper.prefix'} //= 'mhf';
55 $conf->{'stash.prefix'} //= 'mhf';
56 $conf->{'via'} //= 'sqlite';
57 $conf->{'dir'} //= 'migrations';
58 $conf->{'csrf.name'} //= 'mhf_csrf_token';
59 $conf->{'lock'} //= 1;
60 $conf->{'s.time'} //= '1w';
61 $conf->{'c.time'} //= '1w';
62 $conf->{'cl.time'} //= '60m';
63 $conf->{'callback'} //= {
64 'has_auth' => sub { },
65 'sign_in' => sub { },
66 'sign_out' => sub { },
67 'lock' => sub { },
68 'unlock' => sub { }
69 };
70  
71 my $time_cookies = {
72 session => $self->utils->time_convert($conf->{'s.time'}),
73 cookies => $self->utils->time_convert($conf->{'c.time'}),
74 lock => $self->utils->time_convert($conf->{'cl.time'}),
75 };
76 $conf->{'cookies'}
77 //= {name => 'clg', path => '/', httponly => 1, secure => 0};
78 $conf->{'cookies_lock'}
79 //= {name => 'clglc', path => '/', httponly => 1, secure => 0};
80 $conf->{'session'}
81 //= {cookie_name => '_mhf', cookie_path => '/', secure => 0};
82 $conf->{'session'}->{default_expiration} //= $time_cookies->{session};
83 $conf->{dir} = $home . '/' . $conf->{'dir'};
84  
85 # Build Mojo::Hakkefuin Params
86 my @mhf_params
87 = $conf->{table_config} && $conf->{migration}
88 ? (table_config => $conf->{table_config}, migration => $conf->{migration})
89 : (via => $conf->{via}, dir => $conf->{dir});
90 push @mhf_params, dir => $conf->{dir};
91 push @mhf_params, via => $conf->{via};
92 push @mhf_params, dsn => $conf->{dsn} if $conf->{dsn};
93 my $mhf = $self->mojo_hf->new(@mhf_params);
94  
95 # Build shared sessions manager once
96 my $sessions = $self->session_manager;
97 unless ($sessions) {
98 $sessions = Mojo::Hakkefuin::Sessions->new(%{$conf->{session}});
99 $sessions->max_age(1) if $sessions->can('max_age');
100 $self->session_manager($sessions);
101 }
102  
103 # Check Database Migration
104 $mhf->check_file_migration();
105 $mhf->check_migration();
106  
107 # Helper Prefix
108 my $pre = $conf->{'helper.prefix'};
109  
110 $app->hook(
111 after_build_tx => sub {
112 my ($tx, $c) = @_;
113  
114 # Reuse shared sessions object to avoid per-request allocation
115 $c->sessions($sessions) unless $c->sessions && $c->sessions == $sessions;
116 }
117 );
118  
119 $app->helper($pre . '_lock' => sub { $self->_lock($conf, $mhf, @_) });
120 $app->helper($pre . '_unlock' => sub { $self->_unlock($conf, $mhf, @_) });
121 $app->helper($pre . '_signin' => sub { $self->_sign_in($conf, $mhf, @_) });
122 $app->helper($pre . '_signout' => sub { $self->_sign_out($conf, $mhf, @_) });
123 $app->helper($pre . '_has_auth' => sub { $self->_has_auth($conf, $mhf, @_) });
124 $app->helper(
125 $pre . '_auth_update' => sub { $self->_auth_update($conf, $mhf, @_) });
126  
127 $app->helper($pre . '_csrf' => sub { $self->_csrf($conf, @_) });
128 $app->helper(
129 $pre . '_csrf_regen' => sub { $self->_csrfreset($conf, $mhf, @_) });
130 $app->helper($pre . '_csrf_get' => sub { $self->_csrf_get($conf, @_) });
131 $app->helper($pre . '_csrf_val' => sub { $self->_csrf_val($conf, @_) });
132 $app->helper(mhf_backend => sub { $mhf->backend });
133 }
134  
135 sub _normalize_override {
136 my ($self, $opts) = @_;
137 return {} unless $opts && ref $opts eq 'HASH';
138 return $opts;
139 }
140  
141 sub _timeframe {
142 my ($self, $conf, $opts) = @_;
143  
144 $opts //= {};
145 return {
146 session => $self->utils->time_convert($opts->{s_time} // $conf->{'s.time'}),
147 cookies => $self->utils->time_convert($opts->{c_time} // $conf->{'c.time'}),
148 lock => $self->utils->time_convert($opts->{cl_time} // $conf->{'cl.time'}),
149 };
150 }
151  
152 sub _session_expiration {
153 my ($self, $c, $seconds) = @_;
154 return unless defined $seconds;
155 $c->session(expiration => $seconds);
156 }
157  
158 sub _lock {
159 my ($self, $conf, $mhf, $c, $identify) = @_;
160  
161 return {result => 0, code => 400, data => 'lock disabled'}
162 unless $conf->{lock};
163  
164 my $check_auth = $self->_has_auth($conf, $mhf, $c);
165 return $check_auth
166 if $check_auth->{result} == 0 || $check_auth->{result} == 3;
167 return {result => 2, code => 423, data => $check_auth->{data}}
168 if $check_auth->{result} == 2; # already locked
169  
170 my $backend_id = $c->stash($conf->{'stash.prefix'} . '.backend-id');
171 return {result => 0, code => 404, data => 'missing backend id'}
172 unless $backend_id;
173  
174 my $times = $self->_timeframe($conf);
175 my $seed = $check_auth->{data}->{cookie};
176 my $lock_val = $self->cookies_lock->create($conf, $c, $seed, $times->{lock});
177  
178 my $upd_coolock = $mhf->backend->upd_coolock($backend_id, $lock_val);
179 my $upd_state = $mhf->backend->upd_lckstate($backend_id, 1);
180  
181 unless ($upd_coolock->{code} == 200 && $upd_state->{code} == 200) {
182 $self->cookies_lock->delete($conf, $c);
183 $mhf->backend->upd_coolock($backend_id, $check_auth->{data}->{cookie_lock})
184 if $check_auth->{data}->{cookie_lock};
185 return {result => 0, code => 500, data => 'failed to lock'};
186 }
187  
188 my $result = {result => 1, code => 200, data => {lock => 1}};
189 $conf->{callback}->{lock}->($c, $result)
190 if ref $conf->{callback}->{lock} eq 'CODE';
191 return $result;
192 }
193  
194 sub _unlock {
195 my ($self, $conf, $mhf, $c, $identify) = @_;
196  
197 return {result => 0, code => 400, data => 'lock disabled'}
198 unless $conf->{lock};
199  
200 my $check_auth = $self->_has_auth($conf, $mhf, $c);
201 return $check_auth
202 if $check_auth->{result} == 0 || $check_auth->{result} == 3;
203  
204 my $backend_id = $c->stash($conf->{'stash.prefix'} . '.backend-id');
205 return {result => 0, code => 404, data => 'missing backend id'}
206 unless $backend_id;
207  
208 my $lock_cookie = $self->cookies_lock->check($c, $conf);
209 my $stored_lock = $check_auth->{data}->{cookie_lock};
210  
211 if ($check_auth->{result} == 1) {
212 $self->cookies_lock->delete($conf, $c);
213 return {result => 1, code => 200, data => {lock => 0}};
214 }
215 return {result => 0, code => 401, data => 'lock cookie missing'}
216 unless $lock_cookie && $stored_lock;
217 return {result => 0, code => 401, data => 'lock cookie mismatch'}
218 unless secure_compare $lock_cookie, $stored_lock;
219  
220 my $upd_coolock = $mhf->backend->upd_coolock($backend_id, 'no_lock');
221 my $upd_state = $mhf->backend->upd_lckstate($backend_id, 0);
222 $self->cookies_lock->delete($conf, $c);
223  
224 unless ($upd_coolock->{code} == 200 && $upd_state->{code} == 200) {
225 return {result => 0, code => 500, data => 'failed to unlock'};
226 }
227  
228 my $result = {result => 1, code => 200, data => {lock => 0}};
229 $conf->{callback}->{unlock}->($c, $result)
230 if ref $conf->{callback}->{unlock} eq 'CODE';
231 return $result;
232 }
233  
234 sub _sign_in {
235 my ($self, $conf, $mhf, $c, $identify, $opts) = @_;
236  
237 my $override = $self->_normalize_override($opts);
238 my $times = $self->_timeframe($conf, $override);
239 my $backend = $mhf->backend;
240 $self->_session_expiration($c, $times->{session});
241 $self->cookies_lock->delete($conf, $c);
242 my $cv = $self->cookies->create($conf, $c, $times->{cookies});
243  
244 return $backend->create($identify, $cv->[0], $cv->[1], $times->{cookies});
245 }
246  
247 sub _sign_out {
248 my ($self, $conf, $mhf, $c, $identify) = @_;
249  
250 # Session Destroy :
251 $c->session(expires => 1);
252  
253 $self->cookies_lock->delete($conf, $c);
254 my $cookie = $self->cookies->delete($conf, $c);
255 return $mhf->backend->delete($identify, $cookie);
256 }
257  
258 sub _has_auth {
259 my ($self, $conf, $mhf, $c) = @_;
260  
261 my $result = {result => 0, code => 404, data => 'empty'};
262 my $csrf_get = $conf->{'helper.prefix'} . '_csrf_get';
263 my $coo = $c->cookie($conf->{cookies}->{name});
264  
265 return $result unless $coo;
266  
267 my $auth_check = $mhf->backend->check(1, $coo);
268  
269 if ($auth_check->{result} == 1) {
270 my $csrf_ok = secure_compare($auth_check->{data}->{csrf} // '',
271 $c->$csrf_get() // '');
272 if ($csrf_ok) {
273 my $locked = $conf->{lock} ? $auth_check->{data}->{lock} : 0;
274 if ($locked) {
275 my $lock_cookie = $c->cookie($conf->{cookies_lock}->{name});
276 my $match
277 = $lock_cookie
278 && $auth_check->{data}->{cookie_lock}
279 && secure_compare($lock_cookie, $auth_check->{data}->{cookie_lock});
280 $result = {
281 result => 2,
282 code => 423,
283 data => $auth_check->{data},
284 lock_cookie => $match ? 1 : 0
285 };
286 }
287 else {
288 $result = {result => 1, code => 200, data => $auth_check->{data}};
289 }
290 }
291 else {
292 $result = {result => 3, code => 406, data => ''};
293 }
294 $c->stash(
295 $conf->{'stash.prefix'} . '.backend-id' => $auth_check->{data}->{id});
296 $c->stash(
297 $conf->{'stash.prefix'} . '.identify' => $auth_check->{data}->{identify});
298 $c->stash(
299 $conf->{'stash.prefix'} . '.lock_state' => $auth_check->{data}->{lock});
300 }
301 return $result;
302 }
303  
304 sub _auth_update {
305 my ($self, $conf, $mhf, $c, $identify, $opts) = @_;
306  
307 my $override = $self->_normalize_override($opts);
308 my $times = $self->_timeframe($conf, $override);
309  
310 my $result = {result => 0};
311 my $update = $self->cookies->update($conf, $c, 1, $times->{cookies});
312 return $result unless $update;
313  
314 my $csrf = ref $update->[1] eq 'ARRAY' ? $update->[1]->[1] : $update->[1];
315 $result = $mhf->backend->update($identify, $update->[0], $csrf);
316 $self->_session_expiration($c, $times->{session});
317  
318 return $result;
319 }
320  
321 sub _csrf {
322 my ($self, $conf, $c) = @_;
323  
324 # Generate CSRF Token if not exists
325 unless ($c->session($conf->{'csrf.name'})) {
326 my $cook = $self->utils->gen_cookie(3);
327 my $csrf = $self->crand->random($cook, 2, 3);
328  
329 $c->session($conf->{'csrf.name'} => $csrf);
330 $c->res->headers->append((uc $conf->{'csrf.name'}) => $csrf);
331 }
332 }
333  
334 sub _csrfreset {
335 my ($self, $conf, $mhf, $c, $id) = @_;
336  
337 my $coon = $self->utils->gen_cookie(3);
338 my $csrf = $self->crand->random($coon, 2, 3);
339  
340 my $result = $mhf->backend->update_csrf($id, $csrf) if $id;
341  
342 $c->session($conf->{'csrf.name'} => $csrf);
343 $c->res->headers->header((uc $conf->{'csrf.name'}) => $csrf);
344 return [$result, $csrf];
345 }
346  
347 sub _csrf_get {
348 my ($self, $conf, $c) = @_;
349 return $c->session($conf->{'csrf.name'})
350 || $c->req->headers->header((uc $conf->{'csrf.name'}));
351 }
352  
353 sub _csrf_val {
354 my ($self, $conf, $c) = @_;
355  
356 my $get_csrf = $c->session($conf->{'csrf.name'});
357 my $csrf_header = $c->res->headers->header((uc $conf->{'csrf.name'}));
358 return $csrf_header if $csrf_header eq $get_csrf;
359 }
360  
361 package Mojolicious::Plugin::Hakkefuin::_cookies;
362 use Mojo::Base -base;
363  
364 has 'random';
365 has 'utils';
366  
367 sub _cookie_options {
368 my ($self, $base, $ttl) = @_;
369  
370 my %opts = %{$base || {}};
371 if (defined $ttl) {
372 $opts{expires} = time + $ttl;
373 $opts{max_age} = $ttl;
374 }
375 return \%opts;
376 }
377  
378 sub create {
379 my ($self, $conf, $app, $ttl) = @_;
380  
381 my $csrf_get = $conf->{'helper.prefix'} . '_csrf_get';
382 my $csrf_reg = $conf->{'helper.prefix'} . '_csrf_regen';
383 my $csrf = $app->$csrf_get() || $app->$csrf_reg()->[1];
384  
385 my $cookie_key = $conf->{'cookies'}->{name};
386 my $cookie_val
387 = Mojo::Util::hmac_sha1_sum($self->utils->gen_cookie(5), $csrf);
388  
389 my $duration
390 = defined $ttl ? $ttl : $self->utils->time_convert($conf->{'c.time'});
391 $app->cookie($cookie_key, $cookie_val,
392 $self->_cookie_options($conf->{'cookies'}, $duration));
393 [$cookie_val, $csrf];
394 }
395  
396 sub update {
397 my ($self, $conf, $app, $csrf_reset, $ttl) = @_;
398  
399 if ($self->check($app, $conf)) {
400 my $csrf
401 = $conf->{'helper.prefix'} . ($csrf_reset ? '_csrf_regen' : '_csrf_get');
402 $csrf = $app->$csrf();
403  
404 my $cookie_key = $conf->{'cookies'}->{name};
405 my $cookie_val
406 = Mojo::Util::hmac_sha1_sum($self->utils->gen_cookie(5), $csrf);
407 my $duration
408 = defined $ttl ? $ttl : $self->utils->time_convert($conf->{'c.time'});
409 $app->cookie($cookie_key, $cookie_val,
410 $self->_cookie_options($conf->{'cookies'}, $duration));
411 return [$cookie_val, $csrf];
412 }
413 return undef;
414 }
415  
416 sub delete {
417 my ($self, $conf, $app) = @_;
418  
419 if (my $cookie = $self->check($app, $conf)) {
420 $app->cookie($conf->{'cookies'}->{name} => '', {expires => 1});
421 return $cookie;
422 }
423 return undef;
424 }
425  
426 sub check {
427 my ($self, $app, $conf) = @_;
428 return $app->cookie($conf->{'cookies'}->{name});
429 }
430  
431 package Mojolicious::Plugin::Hakkefuin::_cookiesLock;
432 use Mojo::Base -base;
433  
434 has 'random';
435 has 'utils';
436  
437 sub _cookie_options {
438 my ($self, $base, $ttl) = @_;
439  
440 my %opts = %{$base || {}};
441 if (defined $ttl) {
442 $opts{expires} = time + $ttl;
443 $opts{max_age} = $ttl;
444 }
445 return \%opts;
446 }
447  
448 sub create {
449 my ($self, $conf, $app, $seed, $ttl) = @_;
450  
451 my $base = $seed || $self->utils->gen_cookie(4);
452 my $cookie_val
453 = Mojo::Util::hmac_sha1_sum($self->utils->gen_cookie(6), $base);
454  
455 my $duration
456 = defined $ttl ? $ttl : $self->utils->time_convert($conf->{'cl.time'});
457 $app->cookie($conf->{'cookies_lock'}->{name},
458 $cookie_val, $self->_cookie_options($conf->{'cookies_lock'}, $duration));
459 return $cookie_val;
460 }
461  
462 sub update {
463 my ($self, $conf, $app, $seed, $ttl) = @_;
464  
465 return undef unless $self->check($app, $conf);
466 return $self->create($conf, $app, $seed, $ttl);
467 }
468  
469 sub delete {
470 my ($self, $conf, $app) = @_;
471  
472 if ($self->check($app, $conf)) {
473 $app->cookie($conf->{'cookies_lock'}->{name} => '', {expires => 1});
474 return 1;
475 }
476 return 0;
477 }
478  
479 sub check {
480 my ($self, $app, $conf) = @_;
481 return $app->cookie($conf->{'cookies_lock'}->{name});
482 }
483  
484 1;
485  
486 =encoding utf8
487  
488 =head1 NAME
489  
490 Mojolicious::Plugin::Hakkefuin - Mojolicious Web Authentication.
491  
492 =head1 SYNOPSIS
493  
494 Mojolicious::Lite example (SQLite default):
495  
496 use Mojolicious::Lite;
497  
498 plugin 'Hakkefuin' => {
499 'helper.prefix' => 'fuin',
500 'stash.prefix' => 'fuin',
501 via => 'sqlite', # or mariadb / pg
502 dir => 'migrations',
503 'c.time' => '1w', # auth cookie TTL
504 's.time' => '1w', # session TTL
505 'lock' => 1, # enable lock/unlock helpers
506 };
507  
508 post '/login' => sub {
509 my $c = shift;
510 my $id = $c->param('user');
511 my $res = $c->fuin_signin($id);
512 return $c->render(status => $res->{code}, json => $res);
513 };
514  
515 # Override cookie/session TTL per request
516 post '/login-custom' => sub {
517 my $c = shift;
518 my $res = $c->fuin_signin($c->param('user'), {c_time => '2h', s_time => '30m'});
519 return $c->render(status => $res->{code}, json => $res);
520 };
521  
522 under sub {
523 my $c = shift;
524 my $auth = $c->fuin_has_auth; # checks cookie+csrf, stashes data
525 return $c->render(status => 423, json => $auth) if $auth->{result} == 2;
526 return $c->render(status => 401, text => 'Unauthorized')
527 unless $auth->{result} == 1;
528 $c->fuin_csrf; # ensure CSRF is in session/headers
529 return 1;
530 };
531  
532 get '/me' => sub {
533 my $c = shift;
534 $c->render(json => {user => $c->stash('fuin.identify')});
535 };
536  
537 # Rotate auth with custom TTLs without re-login
538 get '/auth-update-custom' => sub {
539 my $c = shift;
540 my $bid = $c->stash('fuin.backend-id');
541 my $res = $c->fuin_auth_update($bid, {c_time => '45m', s_time => '20m'});
542 $c->render(status => $res->{code}, json => $res);
543 };
544  
545 post '/logout' => sub {
546 my $c = shift;
547 my $res = $c->fuin_signout($c->stash('fuin.identify'));
548 $c->render(status => $res->{code}, json => $res);
549 };
550  
551 app->start;
552  
553 Mojolicious (non-Lite) menambahkan plugin di dalam C<startup>:
554  
555 sub startup {
556 my $self = shift;
557 $self->plugin(Hakkefuin => { via => 'pg', dir => 'migrations/pg' });
558 ...
559 }
560  
561 =head1 DESCRIPTION
562  
563 L<Mojolicious::Plugin::Hakkefuin> is a L<Mojolicious> plugin for
564 Web Authentication. (Minimalistic and Powerful).
565  
566 =head1 OPTIONS
567  
568 =head2 helper.prefix
569  
570 # Mojolicious
571 $self->plugin('Hakkefuin' => {
572 'helper.prefix' => 'your_prefix_here'
573 });
574  
575 # Mojolicious Lite
576 plugin 'Hakkefuin' => {
577 'helper.prefix' => 'your_prefix_here'
578 };
579  
580 To change prefix of all helpers. By default, C<helper.prefix> is C<mhf>.
581  
582 =head2 stash.prefix
583  
584 # Mojolicious
585 $self->plugin('Hakkefuin' => {
586 'stash.prefix' => 'your_stash_prefix_here'
587 });
588  
589 # Mojolicious Lite
590 plugin 'Hakkefuin' => {
591 'stash.prefix' => 'your_stash_prefix_here'
592 };
593  
594 To change prefix of stash. By default, C<stash.prefix> is C<mhf>.
595  
596 =head2 csrf.name
597  
598 # Mojolicious
599 $self->plugin('Hakkefuin' => {
600 'csrf.name' => 'your_csrf_name_here'
601 });
602  
603 # Mojolicious Lite
604 plugin 'Hakkefuin' => {
605 'csrf.name' => 'your_csrf_name_here'
606 };
607  
608 To change csrf name in session and HTTP Headers. By default, C<csrf.name>
609 is C<mhf_csrf_token>.
610  
611 =head2 via
612  
613 # Mojolicious
614 $self->plugin('Hakkefuin' => {
615 via => 'mariadb', # OR
616 via => 'pg'
617 });
618  
619 # Mojolicious Lite
620 plugin 'Hakkefuin' => {
621 via => 'mariadb', # OR
622 via => 'pg'
623 };
624  
625 Use one of C<'mariadb'> or C<'pg'> or C<'sqlite'>. (For C<'sqlite'> option
626 does not need to be specified, as it would by default be using C<'sqlite'>
627 if option C<via> is not specified).
628  
629 =head2 dir
630  
631 # Mojolicious
632 $self->plugin('Hakkefuin' => {
633 dir => 'your-custom-dirname-here'
634 });
635  
636 # Mojolicious Lite
637 plugin 'Hakkefuin' => {
638 dir => 'your-custom-dirname-here'
639 };
640  
641 Specified directory for L<Mojolicious::Plugin::Hakkefuin> configure files.
642  
643 =head2 c.time
644  
645 # Mojolicious
646 $self->plugin('Hakkefuin' => {
647 'c.time' => '1w'
648 });
649  
650 # Mojolicious Lite
651 plugin 'Hakkefuin' => {
652 'c.time' => '1w'
653 };
654  
655 To set a cookie expires time. By default is 1 week.
656  
657 =head2 s.time
658  
659 # Mojolicious
660 $self->plugin('Hakkefuin' => {
661 's.time' => '1w'
662 });
663  
664 # Mojolicious Lite
665 plugin 'Hakkefuin' => {
666 's.time' => '1w'
667 };
668  
669 To set a cookie session expires time. By default is 1 week. For more
670 information of the abbreviation for time C<c.time> and C<s.time> helper,
671 see L<Mojo::Hakkefuin::Utils>.
672  
673 =head2 lock
674  
675 # Mojolicious
676 $self->plugin('Hakkefuin' => {
677 'lock' => 1
678 });
679  
680 # Mojolicious Lite
681 plugin 'Hakkefuin' => {
682 'lock' => 1
683 };
684  
685 To set C<Lock Screen> feature. By default is 1 (enable). If you won't use
686 that feature, you can give 0 (disable). This feature is additional
687 authentication method, beside C<login> and C<logout>. When enabled a
688 dedicated lock cookie is issued and tracked in the backend.
689  
690 =head2 cl.time
691  
692 # Mojolicious
693 $self->plugin('Hakkefuin' => {
694 'cl.time' => '60m'
695 });
696  
697 # Mojolicious Lite
698 plugin 'Hakkefuin' => {
699 'cl.time' => '60m'
700 };
701  
702 To set cookie lock expires time. By default is 60 minutes for the lock
703 cookie used by C<mhf_lock>/C<mhf_unlock>.
704  
705 =head1 HELPERS
706  
707 By default, prefix for all helpers using C<mhf>, but you can do change that
708 with option C<helper.prefix>.
709  
710 =head2 mhf_lock
711  
712 $c->mhf_lock() # In the controllers
713  
714 Helper to lock the current authenticated session; sets lock cookie and
715 marks backend as locked.
716  
717 =head2 mhf_unlock
718  
719 $c->mhf_unlock(); # In the controllers
720  
721 Helper to unlock a locked session; clears lock cookie and unlocks backend.
722  
723 =head2 mhf_signin
724  
725 $c->mhf_signin('login-identify') # In the controllers
726  
727 Helper for action sign-in (login) web application.
728  
729 =head2 mhf_signout
730  
731 $c->mhf_signout('login-identify'); # In the controllers
732  
733 Helper for action sign-out (logout) web application.
734  
735 =head2 mhf_auth_update
736  
737 $c->mhf_auth_update('login-identify'); # In the controllers
738  
739 Helper for rotating authentication cookie and CSRF token.
740  
741 =head2 mhf_has_auth
742  
743 $c->mhf_has_auth; # In the controllers
744  
745 Helper for checking if routes has authenticated.
746  
747 =head2 mhf_csrf
748  
749 $c->mhf_csrf; # In the controllers
750 <%= mhf_csrf %> # In the template.
751  
752 Helper for generate csrf;
753  
754 =head2 mhf_csrf_val
755  
756 $c->mhf_csrf_val; # In the controllers
757  
758 Helper for comparing stored CSRF (session/header) and returning it when it
759 matches.
760  
761 =head2 mhf_csrf_get
762  
763 $c->mhf_csrf_get; # In the controllers
764  
765 Helper for retrieving the stored CSRF token.
766  
767 =head2 mhf_csrf_regen
768  
769 $c->mhf_csrf_regen; # In the controllers
770  
771 Helper for regenerating CSRF token and returning the new value.
772  
773 =head2 mhf_backend
774  
775 $c->mhf_backend; # In the controllers
776  
777 Helper for access to backend.
778  
779 =head1 METHODS
780  
781 L<Mojolicious::Plugin::Hakkefuin> inherits all methods from
782 L<Mojolicious::Plugin> and implements the following new ones.
783  
784 =head2 register
785  
786 $plugin->register(Mojolicious->new);
787  
788 Register plugin in L<Mojolicious> application.
789  
790 =head1 SEE ALSO
791  
792 L<https://github.com/CellBIS/mojo-hakkefuin>,
793 <Mojolicious::Guides>, L<https://mojolicious.org>.
794  
795 =head1 AUTHOR
796  
797 Achmad Yusri Afandi, C<yusrideb@cpan.org>
798  
799 =head1 COPYRIGHT AND LICENSE
800  
801 Copyright (C) 2025 by Achmad Yusri Afandi
802  
803 This program is free software, you can redistribute it and/or modify it
804 under the terms of the Artistic License version 2.0.
805  
806 =cut