7 use Authen::Passphrase::SaltedDigest;
8 with 'Web::Simple::Application::Role::Authentication';
12 has 'schema' => (is => 'lazy');
13 has 'deployed' => (is => 'rw');
15 sub dispatch_request {
19 $self->check_authenticated($user);
24 return [ 200, [ 'Content-type', 'text/html' ], [ $self->main_page($user) ]];
27 sub (POST + /login + %username=&password=) {
28 my ($self, $usern, $passw) = @_;
30 my $user = $self->get_check_user($usern, $passw);
33 return ($self->set_authenticated($user),
34 [ 303, [ 'Content-type', 'text/html',
36 [ 'Login succeeded, back to <a href="/"></a>' ]]);
38 return [ 200, [ 'Content-type', 'text/html' ], [ 'Login failed' ]];
41 sub (POST + /register + %username=&password=) {
42 my ($self, $username, $password) = @_;
44 ## FIXME: Check length of inputs!
45 my $newuser = $self->create_user($username, $password);
49 [ 303, [ 'Content-type', 'text/html',
51 [ 'Registration succeeded, back to <a href="/"></a>' ]];
53 return [ 200, [ 'Content-type', 'text/html' ], [ 'Registration failed' ]];
63 return ($self->logout,
64 [ 303, [ 'Content-type', 'text/html',
66 [ 'Logout succeeded, back to <a href="/"></a>' ]]);
70 ## Implement these two (examples based on DBIx::Class):
72 ## _ident_from_user, return a unique way of identifying a user, this
73 ## will be stored in the session
74 sub _ident_from_user {
75 my ($self, $user) = @_;
76 return $user->ident_condition;
79 ## _user_from_ident, return a user object, given the unique user identifier
80 sub _user_from_ident {
81 my ($self, $ident) = @_;
82 return $self->users_rs->find($ident);
88 my $schema = AuthApp::Schema->connect("dbi:SQLite:auth.db");
89 if(!$self->deployed) {
98 my ($self, $username, $password) = @_;
100 my $user = $self->schema->resultset('User')->find({ username => $username });
101 if($user && $user->password->match($password)) {
109 my ($self, $username, $password) = @_;
111 my $user = $self->schema->resultset('User')->find({ username => $username });
113 warn "Cowardly refusing to re-create an existing user $username";
117 $user = $self->schema->resultset('User')->create({
118 username => $username,
119 password => Authen::Passphrase::SaltedDigest->new(algorithm => "SHA-1", salt_random => 20, passphrase=>$password),
126 my ($self, $user) = @_;
128 my $is_user = $user ? $user->username . ' is logged in. <a href="logout">Logout</a>' : <<FORM;
130 <form action="login" method="post">
131 Username: <input type="text" name="username"><br>
132 Password: <input type="password" name="password"><br>
133 <input type="submit">
136 <form action="register" method="post">
137 Username: <input type="text" name="username"><br>
138 Password: <input type="password" name="password"><br>
139 <input type="submit">
145 <head><title>Auth App</title></head>
154 AuthApp->run_if_script;