« lib/Web/Authenticate/User/Storage/Handler/SQL.pm
↑ Summary
blib/lib/Web/Authenticate/Session/Handler.pm »
| 1 | use strict; | ||
| 2 | package Web::Authenticate::Cookie::Handler; | ||
| 3 | $Web::Authenticate::Cookie::Handler::VERSION = '0.002'; | ||
| 4 | use Mouse; | ||
| 5 | use CGI; | ||
| 6 | use CGI::Cookie; | ||
| 7 | use Carp; | ||
| 8 | #ABSTRACT: The default implementation of Web::Authentication::Cookie::Handler::Role | ||
| 9 | |||
| 10 | with 'Web::Authenticate::Cookie::Handler::Role'; | ||
| 11 | |||
| 12 | |||
| 13 | has cookie_prefix => ( | ||
| 14 | isa => 'Str', | ||
| 15 | is => 'ro', | ||
| 16 | required => 1, | ||
| 17 | default => 'web_authenticate_', | ||
| 18 | ); | ||
| 19 | |||
| 20 | |||
| 21 | has domain => ( | ||
| 22 | is => 'ro', | ||
| 23 | default => undef, | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | has path => ( | ||
| 28 | is => 'ro', | ||
| 29 | default => undef, | ||
| 30 | ); | ||
| 31 | |||
| 32 | |||
| 33 | has secure => ( | ||
| 34 | isa => 'Bool', | ||
| 35 | is => 'ro', | ||
| 36 | default => undef, | ||
| 37 | ); | ||
| 38 | |||
| 39 | |||
| 40 | has httponly => ( | ||
| 41 | isa => 'Bool', | ||
| 42 | is => 'ro', | ||
| 43 | default => undef, | ||
| 44 | ); | ||
| 45 | |||
| 46 | |||
| 47 | has samesite => ( | ||
| 48 | is => 'ro', | ||
| 49 | default => undef, | ||
| 50 | ); | ||
| 51 | |||
| 52 | |||
| 53 | sub set_cookie { | ||
| 54 | my ($self, $name, $value, $expires_in_seconds) = @_; | ||
| 55 | croak "must provide name" unless $name; | ||
| 56 | croak "must provide value" unless $value; | ||
| 57 | croak "expires_in_seconds must be positive" unless $expires_in_seconds > 0; | ||
| 58 | |||
| 59 | $self->_bake_cookie($name, $value, $expires_in_seconds); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub get_cookie { | ||
| 64 | my ($self, $name) = @_; | ||
| 65 | croak "must provide name" unless $name; | ||
| 66 | |||
| 67 | my $q = CGI->new; | ||
| 68 | return scalar $q->cookie($self->_get_cookie_name($name)); | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | sub delete_cookie { | ||
| 73 | my ($self, $name) = @_; | ||
| 74 | croak "must provide name" unless $name; | ||
| 75 | |||
| 76 | $self->_bake_cookie($name, '', -123456789); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _get_cookie_name { shift->cookie_prefix . shift } | ||
| 80 | |||
| 81 | sub _bake_cookie { | ||
| 82 | my ($self, $name, $value, $expires_in_seconds) = @_; | ||
| 83 | |||
| 84 | my $maybe_plus = $expires_in_seconds > 0 ? '+' : ''; | ||
| 85 | my $expires_time = "$maybe_plus$expires_in_seconds" . 's'; | ||
| 86 | my $cookie = CGI::Cookie->new( | ||
| 87 | -name => $self->_get_cookie_name($name), | ||
| 88 | -value => $value, | ||
| 89 | -expires => $expires_time, | ||
| 90 | '-max-age' => $expires_time, | ||
| 91 | -domain => $self->domain, | ||
| 92 | -path => $self->path, | ||
| 93 | -secure => $self->secure, | ||
| 94 | -httponly => $self->httponly, | ||
| 95 | -samesite => $self->samesite, | ||
| 96 | ); | ||
| 97 | print "Set-Cookie: $cookie\n"; | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | __END__ | ||
| 103 | |||
| 104 | =pod | ||
| 105 | |||
| 106 | =encoding UTF-8 | ||
| 107 | |||
| 108 | =head1 NAME | ||
| 109 | |||
| 110 | Web::Authenticate::Cookie::Handler - The default implementation of Web::Authentication::Cookie::Handler::Role | ||
| 111 | |||
| 112 | =head1 VERSION | ||
| 113 | |||
| 114 | version 0.002 | ||
| 115 | |||
| 116 | =head1 METHODS | ||
| 117 | |||
| 118 | =head2 cookie_prefix | ||
| 119 | |||
| 120 | Sets the prefix to go before cookie names. Default is 'web_authenticate_'. | ||
| 121 | |||
| 122 | =head2 domain | ||
| 123 | |||
| 124 | See L<CGI::Cookie>. Default is undef. | ||
| 125 | |||
| 126 | =head2 path | ||
| 127 | |||
| 128 | See L<CGI::Cookie>. Default is undef. | ||
| 129 | |||
| 130 | =head2 secure | ||
| 131 | |||
| 132 | See L<CGI::Cookie>. Default is undef. | ||
| 133 | |||
| 134 | =head2 httponly | ||
| 135 | |||
| 136 | See L<CGI::Cookie>. Default is undef. | ||
| 137 | |||
| 138 | =head2 samesite | ||
| 139 | |||
| 140 | See L<CGI::Cookie>. Default is undef. | ||
| 141 | |||
| 142 | =head2 set_cookie | ||
| 143 | |||
| 144 | Creates a session for user and sets it on the browser. expires must be the number of seconds from now | ||
| 145 | when the cookie should expire. | ||
| 146 | |||
| 147 | $cookie_handler->set_cookie($name, $value, $expires_in_seconds); | ||
| 148 | |||
| 149 | =head2 get_cookie | ||
| 150 | |||
| 151 | Gets the value of the cookie with name. Returns undef if there is no cookie with that name, or it has expired. | ||
| 152 | |||
| 153 | my $cookie_value = $cookie_handler->get_cookie($name); | ||
| 154 | |||
| 155 | =head2 delete_cookie | ||
| 156 | |||
| 157 | Deletes cookie with name if it exists. | ||
| 158 | |||
| 159 | $cookie_handler->delete_cookie($name); | ||
| 160 | |||
| 161 | =head1 AUTHOR | ||
| 162 | |||
| 163 | Adam Hopkins <srchulo@cpan.org> | ||
| 164 | |||
| 165 | =head1 COPYRIGHT AND LICENSE | ||
| 166 | |||
| 167 | This software is copyright (c) 2017 by Adam Hopkins. | ||
| 168 | |||
| 169 | This is free software; you can redistribute it and/or modify it under | ||
| 170 | the same terms as the Perl 5 programming language system itself. | ||
| 171 | |||
| 172 | =cut |