| 1 | package Plack::Middleware::Auth::Bitcard; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | $Plack::Middleware::Auth::Bitcard::AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||||||||
| 9 | 1 | $Plack::Middleware::Auth::Bitcard::VERSION = '0.002'; | |||||||||||||||||||||||||||
| 10 | } | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use JSON qw(to_json from_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Plack::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Plack::Util::Accessor qw( bitcard skip_if on_unauth ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Digest::SHA qw(sha1_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use base "Plack::Middleware"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub prepare_app | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
prepare_app: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | croak "Need to provide Authen::Bitcard object" unless ref $self->bitcard; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | $self->bitcard->info_required('username'); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub call | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
call: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 32 | 0 | my $env = $_[0]; | |||||||||||||||||||||||||||
| 33 | 0 | my $req = "Plack::Request"->new($env); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | $env->{BITCARD_URL} = sub | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | unshift @_, 'login_url' if ref $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | my $method = shift; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $env = shift || croak("needs \$env!"); | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || croak('needs $env!')
MC/DC0%0/1
shift() || croak('needs $env!')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $return = shift || $self->_boomerang_uri("Plack::Request"->new($env)); | ||||||||||||||||||||||||||
|
Condition0%0/3
shift() || $self->_boomerang_uri('Plack::Request'->new($env))
MC/DC0%0/2
shift() || $self->_boomerang_uri('Plack::Request'->new($env))
shift()
$self->_boomerang_uri('Plack::Request'->new($env))
Truth table0%0/3
A || B
A= shift()B= $self->_boomerang_uri('Plack::Request'->new($env))
| |||||||||||||||||||||||||||||
| 41 | 0 | $self->bitcard->$method(r => $return); | |||||||||||||||||||||||||||
| 42 | 0 | }; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ($self->_req_is_boomerang($req)) | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||
| 46 | 0 | my $res = $self->_store_cookie_data($req); | |||||||||||||||||||||||||||
| 47 | 0 | return $res->finalize; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | elsif ($self->_fetch_cookie_data($req => $env)) | ||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 0 | return $self->app->($env); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | ▶ | elsif ($self->skip_if and $self->skip_if->($env)) | |||||||||||||||||||||||||||
|
Condition0%0/3
$self->skip_if and $self->skip_if->($env)
MC/DC0%0/2
$self->skip_if and $self->skip_if->($env)
$self->skip_if
$self->skip_if->($env)
Truth table0%0/3
A and B
A= $self->skip_ifB= $self->skip_if->($env)
| |||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 0 | return $self->app->($env); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | elsif (my $on_unauth = $self->on_unauth) | ||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 0 | return $on_unauth->($env); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else | ||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | my $res = $self->_start_boomerang($req); | |||||||||||||||||||||||||||
| 64 | 0 | return $res->finalize; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub _boomerang_uri | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_boomerang_uri: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 0 | my $req = $_[0]; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $base = $req->base; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | $base =~ m{/$} ? "${base}_bitcard_boomerang" : "${base}/_bitcard_boomerang"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub _start_boomerang | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_start_boomerang: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 0 | my $req = $_[0]; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my $res = "Plack::Response"->new; | |||||||||||||||||||||||||||
| 83 | 0 | $res->cookies->{bitcard_return_to} = $req->uri; | |||||||||||||||||||||||||||
| 84 | 0 | $res->redirect( | |||||||||||||||||||||||||||
| 85 | $self->bitcard->login_url( | ||||||||||||||||||||||||||||
| 86 | r => $self->_boomerang_uri($req), | ||||||||||||||||||||||||||||
| 87 | ), | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | 0 | return $res; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub _req_is_boomerang | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_req_is_boomerang: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 94 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 95 | 0 | my $req = $_[0]; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my ($uri) = split /\?/, $req->uri; # ignore query string | |||||||||||||||||||||||||||
| 98 | 0 | return ($uri eq $self->_boomerang_uri($req)); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub _store_cookie_data | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_store_cookie_data: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 103 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 0 | my $req = $_[0]; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | my $res = "Plack::Response"->new; | |||||||||||||||||||||||||||
| 107 | $res->redirect( | ||||||||||||||||||||||||||||
| 108 | defined $req->cookies->{bitcard_return_to} && $req->cookies->{bitcard_return_to} ne '-' | ||||||||||||||||||||||||||||
| 109 | ? $req->cookies->{bitcard_return_to} | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | : $req->base | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $req->cookies->{"bitcard_return_to"} and $req->cookies->{"bitcard_return_to"} ne "-"
MC/DC0%0/2
defined $req->cookies->{"bitcard_return_to"} and $req->cookies->{"bitcard_return_to"} ne "-"
defined $req->cookies->{"bitcard_return_to"}
$req->cookies->{"bitcard_return_to"} ne "-"
Truth table0%0/3
A and B
A= defined $req->cookies->{"bitcard_return_to"}B= $req->cookies->{"bitcard_return_to"} ne "-"
| |||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (my $user = $self->bitcard->verify($req)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 0 | $user->{_checksum} = sha1_hex($self->bitcard->api_secret . $user->{username}); | |||||||||||||||||||||||||||
| 115 | 0 | $res->cookies->{bitcard} = to_json($user); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | else | ||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||
| 119 | 0 | $res->cookies->{bitcard} = to_json({}); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | $req->cookies->{bitcard_return_to} = { value => "-" }; | |||||||||||||||||||||||||||
| 122 | 0 | return $res; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub _fetch_cookie_data | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_fetch_cookie_data: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 127 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 128 | 0 | my ($req, $env) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | return unless $req->cookies->{bitcard}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | my $user = from_json($req->cookies->{bitcard}); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | return unless $user->{username}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | return unless sha1_hex($self->bitcard->api_secret . $user->{username}) eq $user->{_checksum}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | $env->{BITCARD} = +{%$user}; | |||||||||||||||||||||||||||
| 137 | 0 | delete $env->{BITCARD}{_checksum}; | |||||||||||||||||||||||||||
| 138 | 0 | return $env->{BITCARD}{username}; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | 1; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||