-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth_service.pl
392 lines (286 loc) · 11.5 KB
/
auth_service.pl
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/perl
use warnings;
use strict;
use HTTP::Server::Simple::CGI;
use DBI;
use Data::GUID;
use IO::Socket::INET;
use Data::Dumper;
my $LOGFILE = ">>/tmp/auth_service.log";
# Determine the config base
my $config_dir;
if ( $ENV{'XDG_CONFIG_HOME'} ) {
$config_dir = $ENV{'XDG_CONFIG_HOME'};
} else {
$config_dir = $ENV{"HOME"} . "/.broadway_session_manager";
}
print "Config dir: [$config_dir]\n";
if ( ! -d $config_dir ) {
mkdir( $config_dir )
|| die( "Failed to create config directory [$config_dir]:\n" . $! );
}
# Turn off output buffering.
# This is so stuff gets dumped to the console / app log immediately instead of somewhat after the event.
select STDERR;
$| = 1;
select STDOUT;
$| = 1;
# Connect to the config database
print "Connecting to config database ...\n";
my $dbh = DBI->connect(
"dbi:SQLite:dbname=" . $config_dir . "/broadway_proxy_config.db"
, undef # username
, undef # password
, {} # options hash
) || die( DBI->errstr );
my $auth_service_port;
# Fetch our port
my $sth = $dbh->prepare( "select value from simple_config where key = 'auth_service_port'" )
|| die( DBI->errstr );
$sth->execute()
|| die( $sth->errstr );
if ( my $row = $sth->fetchrow_hashref ) {
$auth_service_port = $row->{value};
} else {
die( "Couldn't find auth service port in simple_config!" );
}
# Fetch the user app service port
my $user_app_service_port;
$sth = $dbh->prepare( "select value from simple_config where key = 'user_app_service_port'" )
|| die( DBI->errstr );
$sth->execute()
|| die( $sth->errstr );
if ( my $row = $sth->fetchrow_hashref ) {
$user_app_service_port = $row->{value};
} else {
die( "Couldn't find auth service port in simple_config!" );
}
########################################################################################################
# This code is based off the following example:
# https://renenyffenegger.ch/notes/development/languages/Perl/modules/HTTP/Server/Simple/CGI/webserver
########################################################################################################
{
package WebServer;
use warnings;
use strict;
use base 'HTTP::Server::Simple::CGI';
use File::Slurp; # import read_file
use Data::Dumper;
use Digest::MD5 qw(md5_hex);
my $nl = "\x0d\x0a";
my $name_provided = '';
my $password = '';
my $message = '';
my $auth_cookie_value = undef;
my $root = 'LoginForm';
chdir( $root );
sub print_header {
my $content_type = shift;
print "HTTP/1.0 200 OK$nl";
print "Content-Type: $content_type; charset=utf-8$nl";
if ( $auth_cookie_value ) {
print "Cookie: auth_key=$auth_cookie_value;$nl";
print "Location: /$nl";
}
print $nl;
}
sub serve_file {
my $path_relative = shift;
my $content_type = shift;
my $cgi = shift;
my $auth_cookie = shift;
print_header($content_type);
if ( $path_relative =~ /\.htm$/ or $path_relative =~ /\.html$/ ) {
#############################################################################################################
# For some reason it needs to print some non white space to STDOUT, but not for images only text based files.
# I would like to get rid of this print statement but when i do all the styling goes out that window.
#############################################################################################################
# 10.11.19/fp - commented out next line for testing
#print STDOUT "<div></div>";
#print STDOUT "<div>$message</div>";
$message = '';
}
if (-e $path_relative) {
print read_file($path_relative, binmode => ":raw");
}
else {
print "file $path_relative not found";
}
}
sub find_other_user_with_port {
my ( $username , $port ) = @_;
print LOG "Checking other users having port $port (not $username)\n";
# ensure not stealing other users session
my $sql = 'select username from users where username != ? and port = ?';
my $sth = $dbh->prepare( $sql )
|| print LOG $dbh->errstr;
#print LOG $sth->{Statement} . "\n";
$sth->execute($username, $port )
|| print LOG $sth->errstr . "\n";
my $row = $sth->fetchrow_hashref;
$sth->finish();
if ( $row ) {
my $other_user = $row->{username};
print LOG "Found user $other_user having same port $port as $username\n";
return $other_user;
}
print LOG "No other users having port $port\n";
return undef;
}
sub handle_request {
my $self = shift;
my $cgi = shift;
my $auth_cookie = $cgi->cookie();
my $path = $cgi->path_info;
open LOG , $LOGFILE
|| die( $! );
select LOG;
$| = 1;
print LOG "Opened log ...\n";
select STDOUT;
if ( $cgi->param( 'email' ) ) {
print LOG "Found param email: [" . $cgi->param( 'email' ) . "]\n";
$name_provided = $cgi->param( 'email' );
$password = $cgi->param( 'pass' );
print LOG "Checkpoint 1\n";
###########################################################################
# Authenticate against users, set last_authenticated, auth_key & port.
###########################################################################
my $sql = "select * from users where username = ? and password = ?";
my $sth = $dbh->prepare( $sql )
|| print LOG $dbh->errstr;
print LOG $sth->{Statement} . "\n";
$sth->execute( $name_provided, md5_hex( $password ) )
|| print LOG $sth->errstr . "\n";
print LOG "Checkpoint 2\n";
my $row = $sth->fetchrow_hashref;
$sth->finish();
if ( $row ) {
print LOG "Got auth row:\n" . Dumper( $row ) . "\n";
$message = "name: $row->{username}\n";
my $auth_token = Data::GUID->new;
my $auth_key = $auth_token->as_string;
print LOG "Checkpoint 3\n";
# update the auth table with last_authenticated, auth_key & port
$sql = <<'END_SQL';
update users
set
last_authenticated = datetime('now', 'localtime'),
auth_key = ?,
port = ?,
display_number = null
where username = ? and password = ?
END_SQL
my $sth = $dbh->prepare( $sql )
|| print LOG "DB error: " . $dbh->errstr . "\n";
my $port = $user_app_service_port;
# reconnect to last saved db session
if ($row->{auth_key} and $row->{port}) {
# ensure not stealing portsd from other users
my $other_user = find_other_user_with_port(
$name_provided, $row->{port});
if ( $other_user ) {
print LOG "Redirect to user_app_service\n";
} else {
print LOG "Reconnect to last saved session\n";
$auth_key = $row->{auth_key};
$port = $row->{port};
print LOG "try connecting to $port";
my $host = 'localhost';
my $socket;
eval {
$socket = IO::Socket::INET->new(
PeerAddr => $host
, PeerPort => $port
) || die "Unable to connect to $host:$port: $!";
};
my $err = $@;
if ( $err ) {
print LOG "Cannot connect to $port, redirect to app chooser\n";
$port = $user_app_service_port;
} else {
$socket->close();
}
}
}
print LOG "Checkpoint 4\n";
$sth->execute(
$auth_key
, $port
, $name_provided
, md5_hex( $password )
) || print LOG "DB error: " . $sth->errst . "\n";
$sth->finish();
print LOG "Checkpoint 5\n";
$auth_cookie = $cgi->cookie(
-name => 'auth_key',
-value => $auth_key
);
print LOG "Checkpoint 6\n";
print "HTTP/1.0 200 OK$nl";
print $cgi->header(
{
-cookie => $auth_cookie
, -refresh => 1
}
);
print LOG "Exiting handle_request()\n";
close LOG;
return;
}
} else {
print LOG "No matching record in users table ...\n";
$name_provided = 'nothing provided';
}
if ( $path eq '/' ) {
if (-e 'index.html') {
serve_file ("index.html", 'text/html', $cgi);
}
else {
print join "\n", glob('*');
}
return;
}
# See http://de.selfhtml.org/diverses/mimetypen.htm for Mime Types.
if ($path =~ /\.htm$/ or $path =~ /\.html$/) {
serve_file (".$path", 'text/html', $cgi, $auth_cookie);
return;
}
elsif ($path =~ /\.js$/ ) {
serve_file (".$path", 'application/javascript', $cgi, $auth_cookie);
return;
}
elsif ($path =~ /\.txt$/) {
serve_file (".$path", 'text/plain', $cgi, $auth_cookie);
return;
}
elsif ($path =~ /\.js$/ ) {
serve_file (".$path", 'application/javascript', $cgi, $auth_cookie);
return;
}
elsif ($path =~ /\.png$/) {
serve_file (".$path", 'image/png', $cgi, $auth_cookie);
return;
}
elsif ($path =~ /\.jpg$/ or $path =~ /\.jpeg/) {
serve_file (".$path", 'image/jpeg', $cgi, $auth_cookie);
return;
}
elsif ($path =~ /\.ico$/) {
serve_file (".$path", 'image/x-icon', $cgi, $auth_cookie);
return;
}
elsif ($path =~ /\.css$/) {
serve_file (".$path", 'text/css', $cgi, $auth_cookie);
return;
}
elsif ($path =~ /\.(ttf|woff|woff2)$/) {
serve_file (".$path", 'application/octet-stream', $cgi, $auth_cookie);
return;
}
print STDERR "Unknown Mime type for $path\n";
# send anyhow
serve_file( ".$path", 'text/plain', $cgi, $auth_cookie);
}
}
my $pid = WebServer->new( $auth_service_port )->run;