Authorization.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 122 CC 7 SCAR 16.7
CC
7
Cov
0%
check_auth
12.0
check_session
12.0
registration
6.0
CRAP 5.3
1 package Exception::Authorization;
2 $Exception::Authorization::VERSION = '0.005';
3 use base qw(Exception);
4  
5 package Exception::Authorization::NotFound;
6 $Exception::Authorization::NotFound::VERSION = '0.005';
7 use base qw(Exception::Authorization);
8  
9 package Exception::Authorization::BadPassword;
10 $Exception::Authorization::BadPassword::VERSION = '0.005';
11 use base qw(Exception::Authorization);
12  
13 package Exception::Authorization::BadSession;
14 $Exception::Authorization::BadSession::VERSION = '0.005';
15 use base qw(Exception::Authorization);
16  
17 package QBit::Application::Model::Authorization;
18 $QBit::Application::Model::Authorization::VERSION = '0.005';
19 use qbit;
20  
21 use base qw(QBit::Application::Model);
22  
23 use Digest::SHA qw(sha512_hex);
24 use Crypt::CBC;
25  
26 my $SALT;
27  
28 __PACKAGE__->model_accessors(db => 'QBit::Application::Model::DB::Authorization');
29  
30 sub init {
31 my ($self) = @_;
32  
33 $SALT = $self->get_option('salt') // throw Exception::Authorization gettext('Add salt in config');
34 }
35  
36 sub registration {
37 my ($self, $keys, $password) = @_;
38  
39 $keys = [$keys] unless ref($keys) eq 'ARRAY';
40  
41 my $session;
42 $self->db->transaction(
43 sub {
44 foreach my $key (@$keys) {
45 my $password_hash = $self->_password_hash($key, $password);
46  
47 $self->db->authorization->add({key => $key, password_hash => $password_hash});
48  
49 $session = $self->_get_session($key, $password_hash);
50 }
51 }
52 );
53  
54 return $session;
55 }
56  
57 sub delete {
58 my ($self, $key) = @_;
59  
60 $self->db->authorization->delete($key);
61 }
62  
63 sub _get_session {
64 my ($self, $key, $password_hash) = @_;
65  
66 my $session = {key => $key, session_hash => $self->_session_hash($key, $password_hash)};
67  
68 my $cipher = Crypt::CBC->new(
69 -key => $SALT,
70 -cipher => 'Blowfish'
71 );
72  
73 return $cipher->encrypt(to_json($session));
74 }
75  
76 sub _password_hash {
77 my ($self, $key, $password) = @_;
78  
79 utf8::encode($key);
80 utf8::encode($password);
81  
82 return sha512_hex($password . sha512_hex($key . $SALT));
83 }
84  
85 sub _session_hash {
86 my ($self, $key, $password_hash) = @_;
87  
88 utf8::encode($key);
89 utf8::encode($password_hash);
90  
91 return sha512_hex($key . $password_hash . sha512_hex($SALT));
92 }
93  
94 sub check_auth {
95 my ($self, $key, $password) = @_;
96  
97 my $auth = $self->db->authorization->get($key, fields => ['password_hash']);
98 throw Exception::Authorization::NotFound gettext('"%s" not found', $key) unless defined($auth);
99  
100 return $self->_password_hash($key, $password) eq $auth->{'password_hash'}
101 ? $self->_get_session($key, $auth->{'password_hash'})
102 : throw Exception::Authorization::BadPassword gettext('Invalid password');
103 }
104  
105 sub check_session {
106 my ($self, $session) = @_;
107  
108 my $cipher = Crypt::CBC->new(
109 -key => $SALT,
110 -cipher => 'Blowfish'
111 );
112  
113 try {
114 $session = from_json($cipher->decrypt($session));
115 }
116 catch {
117 throw Exception::Authorization::BadSession gettext('Invalid session');
118 };
119  
120 my $auth = $self->db->authorization->get($session->{'key'}, fields => [qw(password_hash)]);
121 throw Exception::Authorization::NotFound gettext('"%s" not found', $session->{'key'}) unless defined($auth);
122  
123 return $session->{'session_hash'} eq $self->_session_hash($session->{'key'}, $auth->{'password_hash'})
124 ? $session->{'key'}
125 : throw Exception::Authorization::BadSession gettext('Invalid session');
126 }
127  
128 sub process {
129 my ($self, $cookie_name, $interval, $request, $response, $cb) = @_;
130  
131 my $session = $request->cookie($cookie_name);
132  
133 return FALSE unless defined($session);
134  
135 my $key = $self->check_session($session);
136  
137 $cb->($key);
138  
139 $response->add_cookie($cookie_name, $session, expires => $interval,);
140  
141 return TRUE;
142 }
143  
144 TRUE;
145  
146 __END__