blib/lib/Mojolicious/Plugin/Fondation/Auth/Token.pm

filter: Statementstmt 79.0% 68 / 86 Branchbran 76.4% 26 / 34 Conditioncond 46.1% 6 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 78.9% 15 / 19 Podpod 50.0% 1 / 2 totaltotal 72.6% 117 / 161 CC 23 SCAR 16.6
CC
23
Cov
75%
_load_grants
11.4
__ANON__
8.6
__ANON__
8.0
CRAP 5.3
↑ Summary lib/Mojolicious/Plugin/Fondation/Auth/Token/Controller/ApiToken.pm »
1 package Mojolicious::Plugin::Fondation::Auth::Token;
2 3 $Mojolicious::Plugin::Fondation::Auth::Token::VERSION = '0.03';
3 # ABSTRACT: Personal Access Token authentication for Fondation
4  
5 3 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Crypt::URandom qw(urandom);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶sub fondation_meta {
Subroutine fondation_meta: called CC=1 SCAR=0.0
Pod undocumented
11 return {
12 2 dependencies => [
13 'Fondation::Model::DBIx::Async',
14 'Fondation::Auth',
15 'Fondation::Problem',
16 ],
17 defaults => {
18 models => {
19 api_token => {
20 source => 'ApiToken',
21 backend => undef,
22 },
23 },
24 },
25 };
26 }
27  
28 2 ▶sub register ($self, $app, $config) {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
29  
30 # ── around_dispatch: validate Bearer, set current_user, flag stash ──
31  
32 21 ▶ $app->hook(around_dispatch => sub ($next, $c) {
Subroutine __ANON__: called CC=2 SCAR=7.3
33 21 ▶ my $token = _extract_bearer($c) or return $next->();
Branch: 1 branch
BranchTrueFalseTotal
my $token = _extract_bearer($c) or return &$next() 5 16 21
34  
35 5 my $hash = sha256_hex($token);
36  
37 $c->model('api_token')->search(
38 { token_hash => $hash },
39 { rows => 1 },
40 5 ▶ )->first->then(sub ($api_token) {
Subroutine __ANON__: called CC=2 SCAR=7.1
41 5 ▶ unless ($api_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($api_token) 2 3 5
42 2 $c->stash('fondation.bearer_invalid' => 1);
43 2 return $next->();
44 }
45  
46 3 my $user_id = $api_token->user_id;
47  
48 3 ▶ return $c->model('user')->find($user_id)->then(sub ($user) {
Subroutine __ANON__: called CC=2 SCAR=7.8
49 3 ▶ unless ($user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 3 3
50 0 $app->log->error(
51 "[Auth::Token] User $user_id not found for valid token");
52 0 $c->stash('fondation.bearer_invalid' => 1);
53 0 return $next->();
54 }
55 3 $c->current_user({ $user->get_columns });
56 3 $c->stash('fondation._bearer_auth' => 1);
57  
58 return $api_token->update(
59 { last_used_at => \'datetime(\'now\')' }
60 3 ▶ )->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 3 _load_grants($c);
62 3 ▶ })->then(sub { $next->() });
Subroutine __ANON__: called CC=1 SCAR=0.0
63 3 });
64 0 ▶ })->on_fail(sub ($err) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 $app->log->error("[Auth::Token] Token lookup failed: $err");
66 0 $c->stash('fondation.bearer_invalid' => 1);
67 0 $next->();
68 5 })->retain;
69 2 });
70  
71 # ── around_action: enforce Bearer opt-in ──────────────────────────
72 # ── Route is matched, check endpoint requires ────────────────────
73  
74 19 ▶ $app->hook(around_action => sub ($next, $c, $action, $captures) {
Subroutine __ANON__: called CC=8 SCAR=20.8
75 19 ▶ return $next->() unless $c->stash('fondation._bearer_auth');
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash("fondation._bearer_auth") 16 3 19
76  
77 3 my $endpoint = $c->match->endpoint;
78 3 my $allows = 0;
79 3 ▶ if ($endpoint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endpoint) 3 0 3
80 3 ▶ my $requires = $endpoint->{requires} // [];
Condition100%2/2
$endpoint->{"requires"} // []
l!l
21
MC/DC100%1/1
$endpoint->{"requires"} // []
$endpoint->{"requires"}
Truth table100%2/2
A
A= $endpoint->{"requires"}
Aresult
11
00
81 3 my %conds;
82 3 ▶ for (my $i = 0; $i < @$requires; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$requires) 2 3 5
83 2 ▶ $conds{$requires->[$i]} = $requires->[$i+1] // 1;
Condition50%1/2
$$requires[$i + 1] // 1
l!l
02
MC/DC0%0/1
$$requires[$i + 1] // 1
$$requires[$i + 1]
Truth table50%1/2
A
A= $$requires[$i + 1]
Aresult
11
00
84 }
85 3 ▶ $allows = 1 if $conds{'fondation.bearer'};
Branch: 1 branch
BranchTrueFalseTotal
if $conds{"fondation.bearer"} 2 1 3
86 }
87  
88 3 ▶ unless ($allows) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allows) 1 2 3
89 1 delete $c->stash->{'current_user'};
90 1 $c->problem(
91 status => 403,
92 title => 'Access denied',
93 detail => 'Bearer token not allowed on this route',
94 );
95 1 return;
96 }
97  
98 2 return $next->();
99 2 });
100  
101 # ── Route condition: fondation.bearer ────────────────────────────
102  
103 5 ▶ $app->routes->add_condition('fondation.bearer' => sub {
Subroutine __ANON__: called CC=8 SCAR=21.6
104 5 my ($route, $c, $captures) = @_;
105  
106 return 1 unless $route->{requires}
107 5 ▶ && grep { $_ eq 'fondation.bearer' } @{$route->{requires}};
Branch: 1 branch
BranchTrueFalseTotal
unless $route->{"requires"} and grep {$_ eq "fondation.bearer";} @{$$route{"requires"};} 0 5 5
Condition33%1/3
$route->{"requires"} and grep {$_ eq "fondation.bearer";} @{$$route{"requires"};}
!ll&&!rl&&r
005
MC/DC0%0/2
$route->{"requires"} and grep {$_ eq "fondation.bearer";} @{$$route{"requires"};}
$route->{"requires"} grep {$_ eq "fondation.bearer";} @{$$route{"requires"};}
Truth table33%1/3
A and B
A= $route->{"requires"}B= grep {$_ eq "fondation.bearer";} @{$$route{"requires"};}
ABresult
0X0
100
111
108 5 ▶ return 1 unless $route->pattern->match($c->req->url->path->to_string);
Branch: 1 branch
BranchTrueFalseTotal
unless $route->pattern->match($c->req->url->path->to_string) 0 5 5
109  
110 5 ▶ if ($c->stash('fondation.bearer_invalid')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash("fondation.bearer_invalid")) 1 4 5
111 1 $c->res->code(403);
112 1 ▶ $c->stash('fondation.denied' => {
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash("fondation.denied") 1 0 1
113 status => 403,
114 title => 'Access denied',
115 detail => 'Invalid bearer token',
116 }) unless $c->stash('fondation.denied');
117 1 return undef;
118 }
119  
120 4 ▶ return 1 if $c->is_user_authenticated;
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_user_authenticated 3 1 4
121  
122 1 $c->res->code(401);
123 1 ▶ $c->stash('fondation.denied' => {
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash("fondation.denied") 1 0 1
124 status => 401,
125 title => 'Access denied',
126 detail => 'Authentication required',
127 }) unless $c->stash('fondation.denied');
128 1 return undef;
129 2 });
130  
131 # ── Token management API routes ──────────────────────────────────
132  
133 2 $app->routes->get('/api/ApiToken')
134 ->to('ApiToken#list');
135  
136 2 $app->routes->delete('/api/ApiToken/:id')
137 ->to('ApiToken#remove');
138  
139 2 $app->routes->post('/api/token/generate')
140 ->to('ApiToken#generate');
141  
142 2 return $self;
143 }
144  
145 # ── Extract Bearer token from Authorization header ────────────────────
146  
147 21 ▶sub _extract_bearer ($c) {
Subroutine _extract_bearer: called CC=3 SCAR=11.1
148 21 ▶ my $header = $c->req->headers->authorization or return;
Branch: 1 branch
BranchTrueFalseTotal
my $header = $c->req->headers->authorization or return 5 16 21
149 5 ▶ return unless $header =~ /^Bearer\s+(.+)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $header =~ /^Bearer\s+(.+)$/iu 0 5 5
150 5 return $1;
151 }
152  
153 # ── Load grants (permissions + groups) into session ───────────────────
154  
155 3 ▶sub _load_grants ($c) {
Subroutine _load_grants: called CC=4 SCAR=24.3
156 3 my $reg = $c->fondation->registry;
157  
158 return Future->done
159 unless $reg->{'Mojolicious::Plugin::Fondation::Authorization'}
160 && $reg->{'Mojolicious::Plugin::Fondation::Group'}
161 3 ▶ && $reg->{'Mojolicious::Plugin::Fondation::Perm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $reg->{"Mojolicious::Plugin::Fondation::Authorization"} and $reg->{"Mojolicious::Plugin::Fondation::Group"} and $reg->{"Mojolicious::Plugin::Fondation::Perm"} 3 0 3
Condition33%2/6
$reg->{"Mojolicious::Plugin::Fondation::Authorization"} and $reg->{"Mojolicious::Plugin::Fondation::Group"} and $reg->{"Mojolicious::Plugin::Fondation::Perm"}
!ll&&!rl&&r
300
$reg->{"Mojolicious::Plugin::Fondation::Authorization"} and $reg->{"Mojolicious::Plugin::Fondation::Group"}
!ll&&!rl&&r
300
MC/DC0%0/3
$reg->{"Mojolicious::Plugin::Fondation::Authorization"} and $reg->{"Mojolicious::Plugin::Fondation::Group"} and $reg->{"Mojolicious::Plugin::Fondation::Perm"}
$reg->{"Mojolicious::Plugin::Fondation::Authorization"} $reg->{"Mojolicious::Plugin::Fondation::Group"} $reg->{"Mojolicious::Plugin::Fondation::Perm"}
Truth table25%1/4
A and B and C
A= $reg->{"Mojolicious::Plugin::Fondation::Authorization"}B= $reg->{"Mojolicious::Plugin::Fondation::Group"}C= $reg->{"Mojolicious::Plugin::Fondation::Perm"}
ABCresult
0XX0
10X0
1100
1111
162  
163 0 my $uid = $c->current_user->{id};
164  
165 my $groups_f = $c->model('group')->search(
166 { 'user_group.user_id' => $uid },
167 { join => 'user_group' },
168 0 ▶ )->all->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 0 my $rows = shift;
170 0 return [ map { $_->name } @$rows ];
171 0 });
172  
173 my $perms_f = $c->model('perm')->search(
174 { 'user_group.user_id' => $uid },
175 { join => { group_perm => { group => { user_group => undef } } },
176 distinct => 1 },
177 0 ▶ )->all->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 my $rows = shift;
179 0 return [ map { $_->name } @$rows ];
180 0 });
181  
182 0 ▶ return Future->needs_all($groups_f, $perms_f)->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 my ($groups, $perms) = @_;
184 0 $c->session(grants => { permissions => $perms, groups => $groups });
185 0 });
186 }
187  
188 3 1;
189  
190 __END__
↑ Summary lib/Mojolicious/Plugin/Fondation/Auth/Token/Controller/ApiToken.pm »