lib/Rapi/Blog/DB/ResultSet/PreauthAction.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 154 CC 6 SCAR 15.8
CC
6
Cov
0%
create_auth_key
12.0
request_Actor
12.0
_hash_auth_key
6.0
CRAP 4.8
1 package Rapi::Blog::DB::ResultSet::PreauthAction;
2  
3 use strict;
4 use warnings;
5  
6 use Moo;
7 extends 'DBIx::Class::ResultSet';
8  
9 use RapidApp::Util ':all';
10 use Rapi::Blog::Util;
11 use Scalar::Util 'blessed';
12  
13 use aliased 'Rapi::Blog::PreAuth::Actor::Error::NotFound';
14 use aliased 'Rapi::Blog::PreAuth::Actor::Error::Invalid';
15  
16 use String::Random;
17 use Digest::SHA1;
18  
19 sub unsealed {
20 (shift)
21 ->search_rs({ 'me.sealed' => 0 })
22 }
23  
24 sub _hash_auth_key {
25 my ($self, $key) = @_;
26 die "missing required key argument" unless $key;
27 Digest::SHA1->new->add($key)->hexdigest
28 }
29  
30 sub create_auth_key {
31 my ($self, $type, $user_id, $columns) = @_;
32
33 my $key = String::Random->new->randregex('[a-z0-9A-Z]{15}');
34 my $key_hash = $self->_hash_auth_key($key);
35
36 my $create = {
37 type => $type,
38 user_id => $user_id,
39 auth_key => $key_hash
40 };
41
42 if($columns) {
43 die "Optional 'columns' 3rd argument must be a hashref"
44 unless ((ref($columns)||'') eq 'HASH');
45
46 my @ban_cols = ((keys %$create), 'user', 'type_id');
47 exists $columns->{$_} and die "Custom/override of '$_' is not allowed" for (@ban_cols);
48 %$create = (%$columns,%$create);
49 }
50  
51 $self->create( $create ) and return $key
52 }
53  
54  
55 sub _matches_key {
56 my ($self, $key) = @_;
57 $self->search_rs({ 'me.auth_key' => $self->_hash_auth_key($key) })
58 }
59  
60  
61 sub lookup_key {
62 my ($self, $key) = @_;
63 $self->_matches_key($key)
64 ->unsealed
65 ->first
66 }
67  
68 sub lookup_key_include_sealed {
69 my ($self, $key) = @_;
70 $self->_matches_key($key)
71 ->first
72 }
73  
74  
75 sub _is_actor {
76 my ($self, $obj) = @_;
77 $obj && blessed($obj) && $obj->isa('Rapi::Blog::PreAuth::Actor')
78 }
79  
80  
81 # New: this is the common entrypoint for all client requests to
82 # authenticate and execute a pre-auth action:
83 sub request_Actor {
84 my $self = shift;
85 my $c = shift || RapidApp->active_request_context or die "No active request";
86 my $key = shift || $c->req->params->{key};
87
88 my $Actor;
89
90 try {
91 $Actor = $self->_request_Actor($c,$key);
92 }
93 catch {
94 my $err = shift;
95 $self->_is_actor($err)
96 # If an exception was thrown, but it is an Actor, return it:
97 ? $Actor = $err
98 # otherwise, rethrow:
99 : die $err
100 };
101
102 die "Unknown error occured" unless $self->_is_actor($Actor);
103
104 return $Actor
105 }
106  
107  
108 sub _request_Actor {
109 my ($self, $c, $key) = @_;
110  
111 my $PreauthAction = $self->lookup_key($key) or NotFound->throw;
112
113 my $Hit = $c
114 ->model('DB::Hit')
115 ->create_from_request({}, $c->request );
116
117 # this is just for testing, not planning to stick with this content
118 $PreauthAction->request_validate($Hit) or Invalid->throw(
119 'This pre-authorization is no longer valid',
120 title => 'Permission denied',
121 subtitle => 'Pre-Authorization Invalid'
122 );
123
124 $PreauthAction->_new_actor_instance($c)
125 }
126  
127  
128  
129 1;