-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex4_session.pl
More file actions
114 lines (82 loc) · 2.08 KB
/
Copy pathex4_session.pl
File metadata and controls
114 lines (82 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use Mojolicious::Lite;
# Landing page
get '/' => sub {
my $c = shift;
return $c->redirect_to("/time") if $c->session("username");
return $c->redirect_to("/login");
};
# Show login
get '/login' => 'login';
# Process login
post '/login' => sub {
my $c = shift;
# Authentication
unless ("Bender" eq $c->param("username")) {
return $c->redirect_to("/login");
}
if ("rocks" ne $c->param("password")) {
return $c->redirect_to("/login");
}
### The session persists across requests via cookies
$c->session(username => $c->param("username"));
# Expiration date in seconds from now (persists between requests)
#
# This is how long they are logged in
$c->session(expiration => 604800);
return $c->redirect_to("/time");
};
# Exit member area
get '/logout' => sub {
my $c = shift;
# Delete whole session by setting an expiration date in the past
$c->session(expires => 1);
$c->redirect_to("/login");
};
# Session authentication
under (sub {
my $c = shift;
# Already logged in?
if ($c->session("username")) {
return 1;
}
$c->redirect_to("/login");
return undef;
});
# Super secret member area
get '/time' => sub {
my $c = shift;
$c->stash("whence", scalar(localtime));
return $c->render(template => "time");
};
# Access to private file(s)
get '/passwd' => sub {
my $c = shift;
$c->res->headers->content_type('text/plain');
$c->reply->asset(Mojo::Asset::File->new(path => '/etc/passwd'));
};
app->start;
__DATA__
@@ layouts/main.html.ep
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><%= title %></title>
</head>
<body>
%= content
</body>
</html>
@@ login.html.ep
% layout 'main', title => 'Login';
<form role="form" method="post" action="<%= url_for('/login') %>">
<input type="text" placeholder="Username" name=username>
<input type="password" placeholder="Password" name=password>
<button type="submit">Submit</button>
</form>
@@ time.html.ep
% layout 'main', title => 'Time';
<%= stash('whence') %>
<br>
<%= link_to Passwd => "passwd" %> <br>
<%= link_to Logout => "logout" %>