| 1 | package Exception::Authorization; | ||
| 2 | $Exception::Authorization::VERSION = '0.006'; | ||
| 3 | use base qw(Exception); | ||
| 4 | |||
| 5 | package Exception::Authorization::NotFound; | ||
| 6 | $Exception::Authorization::NotFound::VERSION = '0.006'; | ||
| 7 | use base qw(Exception::Authorization); | ||
| 8 | |||
| 9 | package Exception::Authorization::BadPassword; | ||
| 10 | $Exception::Authorization::BadPassword::VERSION = '0.006'; | ||
| 11 | use base qw(Exception::Authorization); | ||
| 12 | |||
| 13 | package Exception::Authorization::BadSession; | ||
| 14 | $Exception::Authorization::BadSession::VERSION = '0.006'; | ||
| 15 | use base qw(Exception::Authorization); | ||
| 16 | |||
| 17 | package QBit::Application::Model::Authorization; | ||
| 18 | $QBit::Application::Model::Authorization::VERSION = '0.006'; | ||
| 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 | -key => $SALT, | ||
| 71 | -cipher => 'Blowfish' | ||
| 72 | ); | ||
| 73 | |||
| 74 | return $cipher->encrypt(to_json($session)); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _password_hash { | ||
| 78 | my ($self, $key, $password) = @_; | ||
| 79 | |||
| 80 | utf8::encode($key); | ||
| 81 | utf8::encode($password); | ||
| 82 | |||
| 83 | return sha512_hex($password . sha512_hex($key . $SALT)); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _session_hash { | ||
| 87 | my ($self, $key, $password_hash) = @_; | ||
| 88 | |||
| 89 | utf8::encode($key); | ||
| 90 | utf8::encode($password_hash); | ||
| 91 | |||
| 92 | return sha512_hex($key . $password_hash . sha512_hex($SALT)); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub check_auth { | ||
| 96 | my ($self, $key, $password) = @_; | ||
| 97 | |||
| 98 | my $auth = $self->db->authorization->get($key, fields => ['password_hash']); | ||
| 99 | throw Exception::Authorization::NotFound gettext('"%s" not found', $key) unless defined($auth); | ||
| 100 | |||
| 101 | return $self->_password_hash($key, $password) eq $auth->{'password_hash'} | ||
| 102 | ? $self->_get_session($key, $auth->{'password_hash'}) | ||
| 103 | : throw Exception::Authorization::BadPassword gettext('Invalid password'); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub check_session { | ||
| 107 | my ($self, $session) = @_; | ||
| 108 | |||
| 109 | my $cipher = Crypt::CBC->new( | ||
| 110 | -key => $SALT, | ||
| 111 | -cipher => 'Blowfish' | ||
| 112 | ); | ||
| 113 | |||
| 114 | try { | ||
| 115 | $session = from_json($cipher->decrypt($session)); | ||
| 116 | } | ||
| 117 | catch { | ||
| 118 | throw Exception::Authorization::BadSession gettext('Invalid session'); | ||
| 119 | }; | ||
| 120 | |||
| 121 | my $auth = $self->db->authorization->get($session->{'key'}, fields => [qw(password_hash)]); | ||
| 122 | throw Exception::Authorization::NotFound gettext('"%s" not found', $session->{'key'}) unless defined($auth); | ||
| 123 | |||
| 124 | return $session->{'session_hash'} eq $self->_session_hash($session->{'key'}, $auth->{'password_hash'}) | ||
| 125 | ? $session->{'key'} | ||
| 126 | : throw Exception::Authorization::BadSession gettext('Invalid session'); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub process { | ||
| 130 | my ($self, $cookie_name, $interval, $request, $response, $cb) = @_; | ||
| 131 | |||
| 132 | my $session = $request->cookie($cookie_name); | ||
| 133 | |||
| 134 | return FALSE unless defined($session); | ||
| 135 | |||
| 136 | my $key = $self->check_session($session); | ||
| 137 | |||
| 138 | $cb->($key); | ||
| 139 | |||
| 140 | $response->add_cookie($cookie_name, $session, expires => $interval,); | ||
| 141 | |||
| 142 | return TRUE; | ||
| 143 | } | ||
| 144 | |||
| 145 | TRUE; | ||
| 146 | |||
| 147 | __END__ |