= 94 = package MyLib::Login; = 95 = = 96 = use strict; = 97 = use base 'CGI::Application'; = 98 = = 99 = use CGI::Application::Plugin::AutoRunmode; = 100 = use CGI::Application::Plugin::DBH(qw/dbh_config dbh/); = 101 = use CGI::Application::Plugin::Session; = 102 = use CGI::Application::Plugin::Authentication; = 103 = use CGI::Application::Plugin::Redirect; = 104 = use CGI::Application::Plugin::ConfigAuto (qw/cfg/); = 105 = use Digest::MD5 qw(md5_hex); = 106 = = 107 = sub setup { = 108 = my $self = shift; = 109 = = 110 = $self->mode_param( = 111 = path_info => 1, = 112 = param => 'rm', = 113 = ); = 114 = } = 115 = = 116 = sub cgiapp_init { = 117 = my $self = shift; = 118 = = 119 = my %CFG = $self->cfg; = 120 = = 121 = $self->tmpl_path(['./templates']); = 122 = = 123 = # open database connection = 124 = $self->dbh_config( = 125 = $CFG{'DB_DSN'}, # "dbi:mysql:database=webapp", = 126 = $CFG{'DB_USER'}, # "webadmin", = 127 = $CFG{'DB_PASS'}, # "" = 128 = ); = 129 = = 130 = $self->session_config( = 131 = CGI_SESSION_OPTIONS => [ = 132 = "driver:mysql;serializer:Storable;id:md5", = 133 = $self->query, {Handle => $self->dbh}, = 134 = ], = 135 = = 136 = DEFAULT_EXPIRY => '+1h', = 137 = # COOKIE_PARAMS => { = 138 = # -name => 'MYCGIAPPSID', = 139 = # -expires => '+24h', = 140 = # -path => '/', = 141 = # }, = 142 = ); = 143 = = 144 = # configure authentication parameters = 145 = $self->authen->config( = 146 = DRIVER => [ 'DBI', = 147 = DBH => $self->dbh, = 148 = TABLE => 'user_info', = 149 = CONSTRAINTS => { = 150 = 'user_info.username' => '__CREDENTIAL_1__', = 151 = 'MD5:user_info.password' => '__CREDENTIAL_2__' = 152 = }, = 153 = ], = 154 = = 155 = STORE => 'Session', = 156 = LOGOUT_RUNMODE => 'logout', = 157 = LOGIN_RUNMODE => 'login', = 158 = POST_LOGIN_RUNMODE => 'okay', = 159 = RENDER_LOGIN => \&my_login_form, = 160 = ); = 161 = = 162 = # define runmodes (pages) that require successful login: = 163 = $self->authen->protected_runmodes( = 164 = 'mustlogin', = 165 = ); = 166 = = 167 = } = 168 = = 169 = sub teardown { = 170 = my $self = shift; = 171 = $self->dbh->disconnect(); # close database connection = 172 = } = 173 = = 174 = sub mustlogin : Runmode { = 175 = my $self = shift; = 176 = my $url = $self->query->url; = 177 = return $self->redirect($url); = 178 = } = 179 = = 180 = sub okay : Runmode { = 181 = my $self = shift; = 182 = = 183 = my $url = $self->query->url; = 184 = # my $user = $self->authen->username; = 185 = my $dest = $self->query->param('destination') || 'index'; = 186 = = 187 = if ($url =~ /^https/) { = 188 = $url =~ s/^https/http/; = 189 = } = 190 = = 191 = return $self->redirect("$url/$dest"); = 192 = } = 193 = = 194 = sub login : Runmode { = 195 = my $self = shift; = 196 = my $url = $self->query->url; = 197 = = 198 = my $user = $self->authen->username; = 199 = if ($user) { = 200 = my $message = "User $user is already logged in!"; = 201 = my $template = $self->load_tmpl('default.html'); = 202 = $template->param(MESSAGE => $message); = 203 = $template->param(MYURL => $url); = 204 = return $template->output; = 205 = } else { = 206 = my $url = $self->query->self_url; = 207 = unless ($url =~ /^https/) { = 208 = $url =~ s/^http/https/; = 209 = return $self->redirect($url); = 210 = } = 211 = return $self->my_login_form; = 212 = } = 213 = } = 214 = = 215 = sub my_login_form { = 216 = my $self = shift; = 217 = my $template = $self->load_tmpl('login_form.html'); = 218 = = 219 = (undef, my $info) = split(/\//, $ENV{'PATH_INFO'}); = 220 = my $url = $self->query->url; = 221 = = 222 = my $destination = $self->query->param('destination'); = 223 = = 224 = unless ($destination) { = 225 = if ($info) { = 226 = $destination = $info; = 227 = } else { = 228 = $destination = "index"; = 229 = } = 230 = } = 231 = = 232 = my $error = $self->authen->login_attempts; = 233 = = 234 = $template->param(MYURL => $url); = 235 = $template->param(ERROR => $error); = 236 = $template->param(DESTINATION => $destination); = 237 = return $template->output; = 238 = } = 239 = = 240 = sub logout : Runmode { = 241 = my $self = shift; = 242 = if ($self->authen->username) { = 243 = $self->authen->logout; = 244 = $self->session->delete; = 245 = } = 246 = return $self->redirect($self->query->url); = 247 = } = 248 = = 249 = sub myerror : ErrorRunmode { = 250 = my $self = shift; = 251 = my $error = shift; = 252 = my $template = $self->load_tmpl("default.html"); = 253 = $template->param(NAME => 'ERROR'); = 254 = $template->param(MESSAGE => $error); = 255 = $template->param(MYURL => $self->query->url); = 256 = return $template->output; = 257 = } = 258 = = 259 = sub AUTOLOAD : Runmode { = 260 = my $self = shift; = 261 = my $rm = shift; = 262 = my $template = $self->load_tmpl("default.html"); = 263 = $template->param(NAME => 'AUTOLOAD'); = 264 = $template->param(MESSAGE => = 265 = "

Error: could not find run mode \'$rm\'
\n"); = 266 = $template->param(MYURL => $self->query->url); = 267 = return $template->output; = 268 = } = 269 = = 270 = = 271 = 1; = 272 =