aboutsummaryrefslogtreecommitdiff
path: root/www/authenticate.pl
blob: d73869d6cd79db5cc566585f0ae7493e61031aea (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
#! /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 Net::Google::FederatedLogin;

do "common.pl";

sub redirect_with_cookie($$$$) {
  my ($cgi, $uri, $session, $cookie) = @_;
  if ($cookie) {
    print $cgi->redirect(-url => $uri);
  } else {
    my $cookie = $cgi->cookie(-name     => 'mulkid_session',
                              -value    => $session->id,
                              -expires  => '+1d',
                              -secure   => 1,
                              -httponly => 1,
                              #-domain   => '.'.$::MULKONF->{realm}
                             );
    print $cgi->redirect(-cookie => $cookie, -url => $uri);
  }
}

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

  my $claimed_email = $cgi->param('email');
  my $cookie = $cgi->cookie('mulkid_session');
  my $session = new CGI::Session("driver:File", $cookie, {Directory=>"/tmp"});

  my $fakedomain = $::MULKONF->{fake_domain};
  my $realdomain = $::MULKONF->{real_domain};
  $claimed_email =~ s/\@$fakedomain/\@$realdomain/ if $fakedomain;

  $session->param('claimed_email', $claimed_email);

  given (my $_ = $::MULKONF->{auth_type}) {
    when ('imap') {
      redirect_with_cookie($cgi, reluri($cgi, "authenticate-with-password.html?email=$claimed_email"), $session, $cookie);
    }
    when ('google') {
      my $g = Net::Google::FederatedLogin->new(
        claimed_id => $claimed_email,
        return_to  => reluri($cgi, 'login.pl'),
        extensions => [{ns          => 'ax',
                        uri         => 'http://openid.net/srv/ax/1.0',
                        attributes  => {mode        => 'fetch_request',
                                        required    => 'email',
                                        type        => {email => 'http://axschema.org/contact/email'}}}]
      );
      redirect_with_cookie($cgi, $g->get_auth_url(), $session, $cookie);
    }
    default {
      die "Invalid auth_type! " . $::MULKONF->{auth_type};
    }
  }
}