« blib/lib/Lemonldap/NG/Portal/Lib/Notifications/XML.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Auth/CAS.pm »
| 1 | package Lemonldap::NG::Portal::Auth::GPG; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use File::Temp 'tempdir'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use IPC::Run; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Mouse; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw( | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | PE_BADCREDENTIALS | ||||||||||||
| 9 | PE_ERROR | ||||||||||||
| 10 | PE_FORMEMPTY | ||||||||||||
| 11 | PE_NOTOKEN | ||||||||||||
| 12 | PE_OK | ||||||||||||
| 13 | 1 | ); | |||||||||||
| 14 | |||||||||||||
| 15 | 1 | our $VERSION = '2.0.12'; | |||||||||||
| 16 | |||||||||||||
| 17 | 1 | extends 'Lemonldap::NG::Portal::Auth::_WebForm'; | |||||||||||
| 18 | |||||||||||||
| 19 | 1 | has db => ( is => 'rw' ); | |||||||||||
| 20 | has tmp => ( | ||||||||||||
| 21 | is => 'rw', | ||||||||||||
| 22 | 1 | ▶ | default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 1 | tempdir( CLEANUP => 1 ); | |||||||||||
| 24 | }, | ||||||||||||
| 25 | 1 | ); | |||||||||||
| 26 | |||||||||||||
| 27 | 0 | ▶ | sub init { | ||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 28 | 0 | my $self = shift; | |||||||||||
| 29 | |||||||||||||
| 30 | 0 | $self->db( $self->conf->{gpgDb} ); | |||||||||||
| 31 | 0 | ▶ | unless ( $self->db ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | $self->error("gpgDb not set"); | |||||||||||
| 33 | 0 | return 0; | |||||||||||
| 34 | } | ||||||||||||
| 35 | 0 | ▶ | unless ( -r $self->db ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 0 | $self->error( "Unable to read " . $self->db ); | |||||||||||
| 37 | 0 | return 0; | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | 0 | return $self->SUPER::init(); | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 0 | ▶ | sub extractFormInfo { | ||||||||||
|
Subroutine
extractFormInfo: not called CC=15 SCAR=54.8
Pod
undocumented
| |||||||||||||
| 44 | 0 | my ( $self, $req ) = @_; | |||||||||||
| 45 | |||||||||||||
| 46 | 0 | ▶ | unless ( $self->ottRule->( $req, {} ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 0 | $self->error("OTT isn't set, unable to use GPG"); | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | # Keep token data for later use | ||||||||||||
| 51 | 0 | my ( $token, $gpgToken ); | |||||||||||
| 52 | 0 | ▶ | if ( $token = $req->param('token') ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 0 | $gpgToken = $self->ott->getToken($token); | |||||||||||
| 54 | 0 | ▶ | $req->data->{tokenVerified} = 1 if ($gpgToken); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | } | ||||||||||||
| 56 | 0 | my $res = $self->SUPER::extractFormInfo($req); | |||||||||||
| 57 | 0 | ▶ | return $res if ($res); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | my $signed = $req->data->{password}; | |||||||||||
| 59 | 0 | ▶ | unless ( $signed =~ /SIGNATURE/s ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 0 | $self->userLogger->error("Bad signature content"); | |||||||||||
| 61 | 0 | $self->userLogger->debug($signed); | |||||||||||
| 62 | 0 | $self->setSecurity($req); | |||||||||||
| 63 | 0 | return PE_BADCREDENTIALS; | |||||||||||
| 64 | } | ||||||||||||
| 65 | 0 | ▶ | unless ( $signed =~ /\b\Q$token\E\b/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 0 | $self->userLogger->error("User replayed a bad token in GPG !"); | |||||||||||
| 67 | 0 | $self->setSecurity($req); | |||||||||||
| 68 | 0 | return PE_BADCREDENTIALS; | |||||||||||
| 69 | } | ||||||||||||
| 70 | 0 | my ( $out, $err ); | |||||||||||
| 71 | 0 | $self->logger->debug( | |||||||||||
| 72 | "Launching:\ngpgv --homedir /dev/null --keyring $self->{db} <<EOF\n$signed\nEOF" | ||||||||||||
| 73 | ); | ||||||||||||
| 74 | 0 | my ( $lang, $language ) = ( $ENV{LANG}, $ENV{LANGUAGE} ); | |||||||||||
| 75 | 0 | $ENV{LANG} = $ENV{LANGUAGE} = 'C'; | |||||||||||
| 76 | 0 | IPC::Run::run( [ 'gpgv', '--homedir', '/dev/null', '--keyring', $self->db ], | |||||||||||
| 77 | \$signed, \$out, \$err, IPC::Run::timeout(10) ); | ||||||||||||
| 78 | 0 | ▶ | if ( $? >> 8 != 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 0 | $self->userLogger->error("GPG verification fails:\n$out\n# # #\n$err"); | |||||||||||
| 80 | 0 | $self->setSecurity($req); | |||||||||||
| 81 | 0 | return PE_BADCREDENTIALS; | |||||||||||
| 82 | } | ||||||||||||
| 83 | 0 | $self->setSecurity($req); | |||||||||||
| 84 | 0 | $self->userLogger->notice("Good GPG signature"); | |||||||||||
| 85 | 0 | $self->userLogger->debug("GPG out:\n$out\n$err"); | |||||||||||
| 86 | 0 | my $key; | |||||||||||
| 87 | 0 | ▶ | if ( $err =~ /using .*? key (.*)$/m ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 0 | $key = $1; | |||||||||||
| 89 | 0 | chomp $key; | |||||||||||
| 90 | } | ||||||||||||
| 91 | else { | ||||||||||||
| 92 | 0 | $self->logger->error("Unable to parse gpgv result:\n$err"); | |||||||||||
| 93 | 0 | return PE_ERROR; | |||||||||||
| 94 | } | ||||||||||||
| 95 | 0 | $self->logger->debug("GPG full sign key: $key"); | |||||||||||
| 96 | 0 | my $in; | |||||||||||
| 97 | 0 | IPC::Run::run( [ | |||||||||||
| 98 | 'gpg', '--homedir', $self->tmp, '--keyring', | ||||||||||||
| 99 | $self->db, '--list-key', $key | ||||||||||||
| 100 | ], | ||||||||||||
| 101 | \$in, | ||||||||||||
| 102 | \$out, | ||||||||||||
| 103 | \$err, | ||||||||||||
| 104 | IPC::Run::timeout(10) | ||||||||||||
| 105 | ); | ||||||||||||
| 106 | 0 | ( $ENV{LANG}, $ENV{LANGUAGE} ) = ( $lang, $language ); | |||||||||||
| 107 | 0 | ▶ | if ( $? >> 8 != 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | 0 | $self->logger->error("gpg --list-key return an error:\n$err"); | |||||||||||
| 109 | 0 | return PE_ERROR; | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 0 | ▶ | if ( $out =~ /pub [^\n]*\r?\n +([^\n]+)\n/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 0 | $key = $1; | |||||||||||
| 114 | 0 | chomp $key; | |||||||||||
| 115 | } | ||||||||||||
| 116 | else { | ||||||||||||
| 117 | 0 | $self->logger->error( | |||||||||||
| 118 | "Unable to parse gpg --list-key result:\n$out\n$err\n"); | ||||||||||||
| 119 | 0 | return PE_ERROR; | |||||||||||
| 120 | } | ||||||||||||
| 121 | 0 | $self->logger->debug("GPG full master key: $key"); | |||||||||||
| 122 | |||||||||||||
| 123 | # Keep only gpgKeyLength characters | ||||||||||||
| 124 | 0 | ▶ | my $length = $self->conf->{gpgKeyLength} || 8; | ||||||||||
|
Condition0%0/2
$self->conf->{'gpgKeyLength'} || 8
MC/DC0%0/1
$self->conf->{'gpgKeyLength'} || 8
$self->conf->{'gpgKeyLength'}
Truth table0%0/2
A
A= $self->conf->{'gpgKeyLength'}
| |||||||||||||
| 125 | 0 | ▶ | $length = 8 if ( $length < 8 ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 126 | 0 | $key =~ s/.*?(.{8,$length})$/$1/; | |||||||||||
| 127 | 0 | $self->logger->info("User key: $key"); | |||||||||||
| 128 | |||||||||||||
| 129 | 0 | my @identities = ( $out =~ /uid\s+(.*)$/gm ); | |||||||||||
| 130 | 0 | my $mail = $req->param('user'); | |||||||||||
| 131 | 0 | foreach (@identities) { | |||||||||||
| 132 | 0 | ▶ | if (/^(.*)\s+<\Q$mail\E>/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | 0 | $req->data->{gpgFullName} = $1; | |||||||||||
| 134 | 0 | $req->data->{gpgMail} = $mail; | |||||||||||
| 135 | 0 | $req->user($mail); | |||||||||||
| 136 | 0 | $self->userLogger->notice("GPG user $mail authenticated"); | |||||||||||
| 137 | 0 | return PE_OK; | |||||||||||
| 138 | } | ||||||||||||
| 139 | } | ||||||||||||
| 140 | 0 | $self->userLogger->warn("Given mail does not match with gpg key"); | |||||||||||
| 141 | 0 | $self->setSecurity($req); | |||||||||||
| 142 | 0 | return PE_BADCREDENTIALS; | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | 0 | ▶ | sub authenticate { | ||||||||||
|
Subroutine
authenticate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 146 | 0 | return PE_OK; | |||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | 0 | ▶ | sub setAuthSessionInfo { | ||||||||||
|
Subroutine
setAuthSessionInfo: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 150 | 0 | my ( $self, $req ) = @_; | |||||||||||
| 151 | 0 | $req->sessionInfo->{gpgMail} = $req->data->{gpgMail}; | |||||||||||
| 152 | 0 | $req->sessionInfo->{authenticationLevel} = $self->conf->{gpgAuthnLevel}; | |||||||||||
| 153 | 0 | return PE_OK; | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | 0 | ▶ | sub authLogout { | ||||||||||
|
Subroutine
authLogout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 157 | 0 | return PE_OK; | |||||||||||
| 158 | } | ||||||||||||
| 159 | |||||||||||||
| 160 | 0 | ▶ | sub getDisplayType { | ||||||||||
|
Subroutine
getDisplayType: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 161 | 0 | return "gpgform"; | |||||||||||
| 162 | } | ||||||||||||
| 163 | |||||||||||||
| 164 | 1 | 1; | |||||||||||