aboutsummaryrefslogtreecommitdiff
path: root/www/logged_in_p.pl
blob: 575295987c632ead7aaa8434c0d44846a06468dd (plain)
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
#! /usr/bin/env perl
# Copyright 2012, Matthias Andreas Benkard <code@mail.matthias.benkard.de>.


use common::sense;
use Modern::Perl;
use syntax 'junction';

use JSON;

use File::Slurp;

use Crypt::OpenSSL::RSA;

use CGI;
use CGI::Fast;
use CGI::Session;

do "common.pl";


while (my $cgi = new CGI::Fast) {
  $::MULKONF = {};   # to silence a warning
  load_config();

  print $cgi->header(-content_type => 'application/json; charset=UTF-8');

  my $cookie = $cgi->cookie('mulkyid_session');

  unless ($cookie) {
    say encode_json({logged_in_p => 0});
    exit(0);
  }

  my $key = Crypt::OpenSSL::RSA->new_private_key(scalar read_file($::MULKONF->{pemfile}));
  $key->use_pkcs1_padding();
  $key->use_sha256_hash();

  my $reverse_encrypted_user_session = decode_base64_url($cookie);
  my $plain_user_session = $key->public_decrypt($reverse_encrypted_user_session);
  my ($session_user, $timestamp) = split /#/, $plain_user_session;

  if ($timestamp < time - 24*3600) {
    say encode_json({logged_in_p => 0});
    exit(0);
  }

  my $email = $cgi->param('email')    or die "No email address supplied";
  if (any(email_users($email)) eq $session_user) {
    say encode_json({logged_in_p => 1});
  } else {
    say encode_json({logged_in_p => 0});
  }
}