GPG.pm 4.2 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2 3 4 5 6 7
package Lemonldap::NG::Portal::Auth::GPG;

use strict;
use File::Temp 'tempdir';
use IPC::Run;
use Mouse;
use Lemonldap::NG::Portal::Main::Constants qw(
Christophe Maudoux's avatar
Christophe Maudoux committed
8 9 10 11 12 13
    PE_BADCREDENTIALS
    PE_ERROR
    PE_FIRSTACCESS
    PE_FORMEMPTY
    PE_NOTOKEN
    PE_OK
Xavier Guimard's avatar
Xavier Guimard committed
14 15
);

Xavier Guimard's avatar
Xavier Guimard committed
16
our $VERSION = '2.0.2';
Xavier Guimard's avatar
Xavier Guimard committed
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

extends 'Lemonldap::NG::Portal::Auth::_WebForm';

has db => ( is => 'rw' );
has tmp => (
    is      => 'rw',
    default => sub {
        tempdir( CLEANUP => 1 );
    },
);

sub init {
    my ($self) = @_;
    $self->db( $self->conf->{gpgDb} );
    unless ( $_[0]->{conf}->{requireToken} ) {
        $self->error("requireToken isn't set, unable to use GPG");
    }
    unless ( $self->db ) {
        $self->error("gpgDb not set");
        return 0;
    }
    unless ( -r $self->db ) {
        $self->error( "Unable to read " . $self->db );
        return 0;
    }

    return $self->SUPER::init();
}

sub extractFormInfo {
    my ( $self, $req ) = @_;

    # Keep token data for later use
    my ( $token, $gpgToken );
    if ( $token = $req->param('token') ) {
        $gpgToken = $self->ott->getToken($token);
        $req->data->{tokenVerified} = 1 if ($gpgToken);
    }
    my $res = $self->SUPER::extractFormInfo($req);
    return $res if ($res);
    my $signed = $req->data->{password};
    unless ( $signed =~ /SIGNATURE/s ) {
        $self->userLogger->error("Bad signature content");
        $self->userLogger->debug($signed);
        $self->setSecurity($req);
        return PE_BADCREDENTIALS;
    }
    unless ( $signed =~ /\b\Q$token\E\b/ ) {
        $self->userLogger->error("User replayed a bad token in GPG !");
        $self->setSecurity($req);
        return PE_BADCREDENTIALS;
    }
    my ( $out, $err );
    $self->logger->debug(
Christophe Maudoux's avatar
Christophe Maudoux committed
71
        "Launching:\ngpgv --homedir /dev/null --keyring $self->{db} <<EOF\n$signed\nEOF"
Xavier Guimard's avatar
Xavier Guimard committed
72
    );
Christophe Maudoux's avatar
Christophe Maudoux committed
73 74 75 76
    my ( $lang, $language ) = ( $ENV{LANG}, $ENV{LANGUAGE} );
    $ENV{LANG} = $ENV{LANGUAGE} = 'C';
    IPC::Run::run(
        [ 'gpgv', '--homedir', '/dev/null', '--keyring', $self->db ],
Xavier Guimard's avatar
Xavier Guimard committed
77 78
        \$signed, \$out, \$err, IPC::Run::timeout(10) );
    if ( $? >> 8 != 0 ) {
Christophe Maudoux's avatar
Christophe Maudoux committed
79 80
        $self->userLogger->error(
            "GPG verification fails:\n$out\n# # #\n$err");
Xavier Guimard's avatar
Xavier Guimard committed
81 82 83 84 85
        $self->setSecurity($req);
        return PE_BADCREDENTIALS;
    }
    $self->setSecurity($req);
    $self->userLogger->notice("Good GPG signature");
86 87
    $self->userLogger->debug("GPG out:\n$out\n$err");
    unless ( $err =~ /using .*? key (.*)$/m ) {
Xavier Guimard's avatar
Xavier Guimard committed
88 89 90 91 92 93 94 95
        $self->logger->error("Unable to parse gpgv result:\n$err");
        return PE_ERROR;
    }
    my $key = $1;
    chomp $key;
    $self->logger->debug("GPG full sign key: $key");
    my $in;
    IPC::Run::run(
Christophe Maudoux's avatar
Christophe Maudoux committed
96
        [   'gpg',     '--homedir',  $self->tmp, '--keyring',
Xavier Guimard's avatar
Xavier Guimard committed
97 98 99 100 101 102 103
            $self->db, '--list-key', $key
        ],
        \$in,
        \$out,
        \$err,
        IPC::Run::timeout(10)
    );
Christophe Maudoux's avatar
Christophe Maudoux committed
104
    ( $ENV{LANG}, $ENV{LANGUAGE} ) = ( $lang, $language );
Xavier Guimard's avatar
Xavier Guimard committed
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
    if ( $? >> 8 != 0 ) {
        $self->logger->error("gpg --list-key return an error:\n$err");
        return PE_ERROR;
    }
    unless ( $out =~ /pub [^\n]*\r?\n +([^\n]+)\n/ ) {
        $self->logger->error(
            "Unable to parse gpg --list-key result:\n$out\n$err\n");
        return PE_ERROR;
    }
    $key = $1;
    chomp $key;
    $self->logger->debug("GPG full master key: $key");

    # Keep only gpgKeyLength characters
    my $length = $self->conf->{gpgKeyLength} || 8;
    $length = 8 if ( $length < 8 );
    $key =~ s/.*?(.{8,$length})$/$1/;
    $self->logger->info("User key: $key");

    my @identities = ( $out =~ /uid\s+(.*)$/gm );
    my $mail       = $req->param('user');
    foreach (@identities) {
        if (/^(.*)\s+<\Q$mail\E>/) {
            $req->data->{gpgFullName} = $1;
            $req->data->{gpgMail}     = $mail;
            $req->user($mail);
            $self->userLogger->notice("GPG user $mail authenticated");
            return PE_OK;
        }
    }
    $self->userLogger->warn("Given mail does not match with gpg key");
    $self->setSecurity($req);
    return PE_BADCREDENTIALS;
}

sub authenticate {
    PE_OK;
}

sub setAuthSessionInfo {
    my ( $self, $req ) = @_;
    $req->sessionInfo->{gpgMail} = $req->data->{gpgMail};
    return PE_OK;
}

sub authLogout {
    PE_OK;
}

sub getDisplayType {
    return "gpgform";
}

1;