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