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

do "common.pl";

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

  my $fakedomain = $::MULKONF->{fake_domain};
  my $realdomain = $::MULKONF->{real_domain};

  my $claimed_email = $cgi->param('email');
  $claimed_email =~ s/\@$fakedomain/\@$realdomain/ if $fakedomain;

  given (my $_ = $::MULKONF->{auth_type}) {
    when ('imap') {
      print $cgi->redirect(reluri($cgi, "authenticate-with-password.html?email=$claimed_email"));
    }
    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'}}}]
      );
      print $cgi->redirect($g->get_auth_url());
    }
    default {
      die "Invalid auth_type! " . $::MULKONF->{auth_type};
    }
  }
}