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

use common::sense;
#use Modern::Perl 2011;
use Modern::Perl;

use JSON;

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

use Mail::IMAPTalk ;
use Net::Google::FederatedLogin;
#use IO::Socket::SSL;

do "common.pl";

sub check_imap_password($$) {
  my ($user, $password) = @_;

  #my $socket = IO::Socket::SSL->new('imap.googlemail.com:imaps');
  my $imap = Mail::IMAPTalk->new(
  #  Socket   => $socket,
    Server   => $::MULKONF->{imap_server},
    Port     => $::MULKONF->{imap_port},
    Username => $user,
    Password => $password,
    Uid      => 1
  );
  if ($imap) {
    $imap->logout;
    return 1;
  } else {
    return 0;
  }
}


while (my $cgi = new CGI::Fast) {
  load_config;

  my $cookie = $cgi->cookie('mulkid_session');
  my $session;
  if ($cookie) {
    $session = new CGI::Session("driver:File", $cookie, {Directory=>"/tmp"});
    print $cgi->header(-content_type => 'application/json; charset=UTF-8');
  } else {
    $session = new CGI::Session("driver:File", undef, {Directory=>"/tmp"});
    my $cookie = $cgi->cookie(-name     => 'mulkid_session',
                              -value    => $session->id,
                              -expires  => '+1d',
                              -secure   => 1,
                              -httponly => 1,
                              #-domain   => '.mulk.eu'
                             );
    print $cgi->header(-content_type => 'application/json; charset=UTF-8',
                       -cookie       => $cookie);
  }

  my $email = $cgi->param('email') or die "No email address provided";

  for ($::MULKONF->{auth_type}) {
    when ('imap') {
      my $password = $cgi->param('password') or die "Empty password";
      for my $user (email_users($email)) {
        #say STDERR "Trying user: $user";
        if (check_imap_password($user, $password)) {
          $session->param('user', $user);
          #say encode_json({user => $user});
          print $cgi->redirect(-url => reluri($cgi, 'successful-login.html'));
          exit 0;
        }
      }
    }
    when ('google') {
      my $g = Net::Google::FederatedLogin->new(
        cgi => $cgi,
        return_to => $cgi->url()
      );
      $g->verify_auth or die "Could not verify the OpenID assertion!";
      my $ext = $g->get_extension('http://openid.net/srv/ax/1.0');
      my $verified_email = $ext->get_parameter('value.email');
      $session->param('user', $verified_email);
      #say encode_json({user => $user});
      print $cgi->redirect(-url => reluri($cgi, 'successful-login.html'));
      exit 0;
    }
    default {
      die "Invalid auth_type.  Check MulkyID configuration!";
    }
  }

  die "Could not authenticate.";
}