| 1 | package MVC::Neaf::X::Session::Cookie; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 4 | our $VERSION = '0.2901'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | MVC::Neaf::X::Session::Cookie - Stateless cookie-based session for Neaf | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | Use this module as a session handler in a Neaf app. | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | The session data is stored within user's cookies without encryption. | ||||||||||||||||||||
| 16 | However, it is signed with a key only known to the application owner. | ||||||||||||||||||||
| 17 | So the session can be read, but not tampered with. | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | Please take these concern into account, or better use server-side storage. | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 METHODS | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 4 | ▶ | use MVC::Neaf::Util qw( encode_b64 decode_b64 ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 4 | ▶ | use Digest::SHA qw( hmac_sha224 ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 4 | ▶ | use parent qw( MVC::Neaf::X::Session::Base ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head2 new( %options ) | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | %options may include: | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =over | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =item * key (required) - a secret text string used to sign session data. | ||||||||||||||||||||
| 37 | This should be the same throughout the application. | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | =item * hmac_function - HMAC to be used, default is hmac_sha224_base64 | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =back | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 3 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||
| 46 | 3 | my ($class, %opt) = @_; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 3 | ▶ | $opt{key} or $class->my_croak( "key option is required" ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 3 | ▶ | $opt{hmac_function} ||= sub { encode_b64( hmac_sha224( @_ ) ) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$opt{"hmac_function"} ||= sub {
encode_b64(&hmac_sha224(@_));
}
MC/DC0%0/2
$opt{"hmac_function"} ||= sub {
encode_b64(&hmac_sha224(@_));
}
$opt{"hmac_function"}
sub {
encode_b64(&hmac_sha224(@_));
}
Truth table33%1/3
A ||= B
A= $opt{"hmac_function"}B= sub {
encode_b64(&hmac_sha224(@_));
}
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 3 | return $class->SUPER::new( %opt ); | |||||||||||||||||||
| 53 | }; | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head2 store( $id, $data ) | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | Create a cookie from $data hash. Given $id is ignored. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 3 | ▶ | sub store { | ||||||||||||||||||
|
Subroutine
store: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 62 | 3 | my ($self, $id, $data) = @_; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # TODO 0.90 Make universal HMAC mechanism for ALL cookies | ||||||||||||||||||||
| 65 | 3 | my $str = encode_b64($data); | |||||||||||||||||||
| 66 | 3 | $str =~ s/\s//gs; | |||||||||||||||||||
| 67 | 3 | $str .= "~".$self->get_expire; | |||||||||||||||||||
| 68 | 3 | my $sum = $self->{hmac_function}->( $str, $self->{key} ); | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 3 | return { id => "$str~$sum" }; | |||||||||||||||||||
| 71 | }; | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head2 fetch | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | Restore session data from cookie. | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 5 | ▶ | sub fetch { | ||||||||||||||||||
|
Subroutine
fetch: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 80 | 5 | my ($self, $id) = @_; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 5 | my ($str, $time, $key) = split /~/, $id, 3; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 5 | ▶ | return unless $key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 4 | ▶ | return unless $self->{hmac_function}->( "$str~$time", $self->{key} ) eq $key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 3 | return { strfy => decode_b64($str), expire => $time }; | |||||||||||||||||||
| 88 | }; | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =head2 get_session_id | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | Replaced by a stub - we'll generate ID from data anyway. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 2 | ▶ | sub get_session_id { return 'Cookie Session Need No Id' }; | ||||||||||||||||||
|
Subroutine
get_session_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | This module is part of L<MVC::Neaf> suite. | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>. | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 105 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||
| 106 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 4 | 1; | |||||||||||||||||||