| 1 | package Gantry::Plugins::Uaf::Authenticate; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Gantry::Utils::Crypt; | ||
| 8 | use Gantry::Plugins::Uaf::User; | ||
| 9 | use Data::Random qw(:all); | ||
| 10 | |||
| 11 | our $VERSION = '0.03'; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $proto = shift; | ||
| 15 | my $gobj = shift; | ||
| 16 | |||
| 17 | my $self; | ||
| 18 | my $class = ref($proto) || $proto; | ||
| 19 | my $app_rootp = $gobj->app_rootp || ""; | ||
| 20 | |||
| 21 | |||
| 22 | $self->{gantry} = $gobj; | ||
| 23 | $self->{limit} = 3; | ||
| 24 | $self->{timeout} = 3600; | ||
| 25 | $self->{secret} = 'w3s3cR7'; | ||
| 26 | $self->{app_rootp} = $app_rootp || ""; | ||
| 27 | $self->{login_rootp} = $self->{app_rootp} . '/login'; | ||
| 28 | $self->{denied_rootp} = $self->{login_rootp} . '/denied'; | ||
| 29 | $self->{expired_rootp} = $self->{login_rootp} . '/expired'; | ||
| 30 | $self->{filter} = qr/^${app_rootp}\/(login|static|cookiecheck).*/; | ||
| 31 | $self->{crypt} = Gantry::Utils::Crypt->new({'secret' => $self->{secret}}); | ||
| 32 | |||
| 33 | bless($self, $class); | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | sub is_valid($) { | ||
| 40 | my ($self) = @_; | ||
| 41 | |||
| 42 | my $ip; | ||
| 43 | my $old_ip; | ||
| 44 | my $access; | ||
| 45 | my $needed; | ||
| 46 | my $old_token; | ||
| 47 | my $token = ""; | ||
| 48 | my $user = undef; | ||
| 49 | |||
| 50 | $ip = $self->{gantry}->remote_ip; | ||
| 51 | $token = ($self->{gantry}->get_cookies('_token_id_') || '') . '='; | ||
| 52 | |||
| 53 | if (defined($token) and ($token ne '=')) { | ||
| 54 | |||
| 55 | if ($self->{gantry}->session_lock()) { | ||
| 56 | |||
| 57 | $old_ip = $self->{gantry}->session_retrieve('uaf_remote_ip') || ''; | ||
| 58 | $old_token = $self->{gantry}->session_retrieve('uaf_token') || ''; | ||
| 59 | |||
| 60 | # This should work for just about everything except a load | ||
| 61 | # balancing, natted firewall. And yeah, they do exist. | ||
| 62 | |||
| 63 | if (($token eq $old_token) and ($ip eq $old_ip)) { | ||
| 64 | |||
| 65 | $user = $self->{gantry}->session_retrieve('uaf_user'); | ||
| 66 | $access = $user->attribute('last_access'); | ||
| 67 | $user->attribute('last_access', time()); | ||
| 68 | $self->{gantry}->session_update('uaf_user', $user); | ||
| 69 | $user = undef if ($access < (time() - $self->{timeout})); | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | $self->{gantry}->session_unlock(); | ||
| 74 | |||
| 75 | } | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | return $user; | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | sub validate($$$) { | ||
| 84 | my ($self, $username, $password) = @_; | ||
| 85 | |||
| 86 | my $ip = ""; | ||
| 87 | my $salt = ""; | ||
| 88 | my $user = undef; | ||
| 89 | |||
| 90 | $username = lc($username); | ||
| 91 | $password = lc($password); | ||
| 92 | $ip = $self->{gantry}->remote_ip; | ||
| 93 | $salt = rand_chars(set => 'all', min => 5, max => 10); | ||
| 94 | |||
| 95 | if (($username eq 'admin') and ($password eq 'admin')) { | ||
| 96 | |||
| 97 | $user = Gantry::Plugins::Uaf::User->new($username); | ||
| 98 | $user->attribute('login_attempts', 0); | ||
| 99 | $user->attribute('last_access', time()); | ||
| 100 | $user->attribute('salt', $salt); | ||
| 101 | $self->{gantry}->session_store('uaf_remote_ip', $ip); | ||
| 102 | $self->{gantry}->session_update('uaf_user', $user); | ||
| 103 | |||
| 104 | } elsif (($username eq 'demo') and ($password eq 'demo')) { | ||
| 105 | |||
| 106 | $user = Gantry::Plugins::Uaf::User->new($username); | ||
| 107 | $user->attribute('login_attempts', 0); | ||
| 108 | $user->attribute('last_access', time()); | ||
| 109 | $user->attribute('salt', $salt); | ||
| 110 | $self->{gantry}->session_store('uaf_remote_ip', $ip); | ||
| 111 | $self->{gantry}->session_update('uaf_user', $user); | ||
| 112 | |||
| 113 | } | ||
| 114 | |||
| 115 | return $user; | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | sub invalidate($) { | ||
| 120 | my $self = shift; | ||
| 121 | |||
| 122 | $self->{gantry}->session_remove('uaf_user'); | ||
| 123 | $self->{gantry}->session_remove('uaf_token'); | ||
| 124 | $self->{gantry}->session_remove('uaf_remote_ip'); | ||
| 125 | $self->{gantry}->set_cookie( | ||
| 126 | { | ||
| 127 | name => '_token_id_', | ||
| 128 | expires => '0', | ||
| 129 | value => '' | ||
| 130 | } | ||
| 131 | ); | ||
| 132 | |||
| 133 | } | ||
| 134 | |||
| 135 | sub login($$) { | ||
| 136 | my ($self, $action) = @_; | ||
| 137 | |||
| 138 | my $user; | ||
| 139 | my $count; | ||
| 140 | my $limit; | ||
| 141 | my $params; | ||
| 142 | my $failures; | ||
| 143 | my $uaf_title; | ||
| 144 | my $uaf_wrapper; | ||
| 145 | my $uaf_template; | ||
| 146 | my $login_rootp; | ||
| 147 | my $denied_rootp; | ||
| 148 | my $gobj = $self->{gantry}; | ||
| 149 | my $loc = $gobj->location; | ||
| 150 | my $app_rootp = $gobj->app_rootp || "/"; | ||
| 151 | |||
| 152 | |||
| 153 | $loc =~ s!^/$!!; | ||
| 154 | $action = lc($action); | ||
| 155 | $login_rootp = $loc . '/login'; | ||
| 156 | $denied_rootp = $login_rootp . '/denied'; | ||
| 157 | |||
| 158 | if ($action eq 'denied') { | ||
| 159 | |||
| 160 | $uaf_title = $gobj->fish_config('uaf_denied_title') || 'Login Denied'; | ||
| 161 | $uaf_wrapper = $gobj->fish_config('uaf_denied_wrapper') || 'default.tt'; | ||
| 162 | $uaf_template = $gobj->fish_config('uaf_denied_template') || 'uaf_denied.tt'; | ||
| 163 | |||
| 164 | $gobj->template_wrapper($uaf_wrapper); | ||
| 165 | $gobj->stash->view->title($uaf_title); | ||
| 166 | $gobj->stash->view->template($uaf_template); | ||
| 167 | |||
| 168 | } else { | ||
| 169 | |||
| 170 | if ($gobj->method eq 'POST') { | ||
| 171 | |||
| 172 | $limit = $self->{limit}; | ||
| 173 | $params = $gobj->get_param_hash(); | ||
| 174 | $count = $gobj->session_retrieve('uaf_login_attempts'); | ||
| 175 | $count++; | ||
| 176 | $gobj->session_update('uaf_login_attempts', $count); | ||
| 177 | |||
| 178 | $user = $self->validate($params->{username}, | ||
| 179 | $params->{password}); | ||
| 180 | |||
| 181 | if (defined($user)) { | ||
| 182 | |||
| 183 | $gobj->session_update('uaf_user', $user); | ||
| 184 | |||
| 185 | if ($count > $limit) { | ||
| 186 | |||
| 187 | $self->relocate($denied_rootp); | ||
| 188 | return; | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | $gobj->session_update('uaf_login_attempts', 0); | ||
| 193 | $self->set_token($user); | ||
| 194 | $self->relocate($app_rootp); | ||
| 195 | |||
| 196 | } else { | ||
| 197 | |||
| 198 | $self->relocate($login_rootp); | ||
| 199 | |||
| 200 | } | ||
| 201 | |||
| 202 | } else { | ||
| 203 | |||
| 204 | $uaf_title = $gobj->fish_config('uaf_title') || 'Please Login'; | ||
| 205 | $uaf_wrapper = $gobj->fish_config('uaf_wrapper') || 'default.tt'; | ||
| 206 | $uaf_template = $gobj->fish_config('uaf_template') || 'uaf_login.tt'; | ||
| 207 | |||
| 208 | $gobj->template_wrapper($uaf_wrapper); | ||
| 209 | $gobj->stash->view->title($uaf_title); | ||
| 210 | $gobj->stash->view->template($uaf_template); | ||
| 211 | |||
| 212 | } | ||
| 213 | |||
| 214 | } | ||
| 215 | |||
| 216 | } | ||
| 217 | |||
| 218 | sub logout { | ||
| 219 | my ($self) = shift; | ||
| 220 | |||
| 221 | my $app_rootp = $self->{gantry}->app_rootp || "/"; | ||
| 222 | |||
| 223 | $self->invalidate(); | ||
| 224 | $self->relocate($app_rootp); | ||
| 225 | |||
| 226 | } | ||
| 227 | |||
| 228 | sub relocate($$) { | ||
| 229 | my ($self, $url) = @_; | ||
| 230 | |||
| 231 | $self->{gantry}->relocate($url); | ||
| 232 | |||
| 233 | } | ||
| 234 | |||
| 235 | sub set_token($$) { | ||
| 236 | my ($self, $user) = @_; | ||
| 237 | |||
| 238 | my $salt = $user->attribute('salt'); | ||
| 239 | my $path = $self->{gantry}->fish_config('uaf_cookie_path') || '/'; | ||
| 240 | my $domain = $self->{gantry}->fish_config('uaf_cookie_domain') || ""; | ||
| 241 | my $secure = $self->{gantry}->fish_config('uaf_cookie_secure'); | ||
| 242 | my $token = $self->{crypt}->encrypt($user->username, ':', time(), ':', $salt, $$); | ||
| 243 | |||
| 244 | |||
| 245 | $self->{gantry}->set_cookie( | ||
| 246 | { | ||
| 247 | name => '_token_id_', | ||
| 248 | value => $token, | ||
| 249 | path => $path | ||
| 250 | } | ||
| 251 | ); | ||
| 252 | |||
| 253 | $self->{gantry}->session_update('uaf_token', $token); | ||
| 254 | |||
| 255 | } | ||
| 256 | |||
| 257 | sub avoid { | ||
| 258 | my $self = shift; | ||
| 259 | |||
| 260 | return 1; | ||
| 261 | |||
| 262 | } | ||
| 263 | |||
| 264 | sub filter($) { | ||
| 265 | my ($self) = @_; | ||
| 266 | |||
| 267 | return $self->{filter}; | ||
| 268 | |||
| 269 | } | ||
| 270 | |||
| 271 | sub login_rootp($) { | ||
| 272 | my ($self) = @_; | ||
| 273 | |||
| 274 | return $self->{login_rootp}; | ||
| 275 | |||
| 276 | } | ||
| 277 | |||
| 278 | sub expired_rootp($) { | ||
| 279 | my ($self) = @_; | ||
| 280 | |||
| 281 | return $self->{expired_rootp}; | ||
| 282 | |||
| 283 | } | ||
| 284 | |||
| 285 | sub denied_rootp($) { | ||
| 286 | my ($self) = @_; | ||
| 287 | |||
| 288 | return $self->{denied_rootp}; | ||
| 289 | |||
| 290 | } | ||
| 291 | |||
| 292 | sub app_rootp($) { | ||
| 293 | my ($self) = @_; | ||
| 294 | |||
| 295 | return $self->{app_rootp}; | ||
| 296 | |||
| 297 | } | ||
| 298 | |||
| 299 | 1; | ||
| 300 | |||
| 301 | __END__ |