summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Model/ReadMails/MockMaildir.pm
blob: de5c745c40191dfb8a090d88cb251f1a844fe377 (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
package JWebmail::Model::ReadMails::MockMaildir;

use Mojo::Base 'JWebmail::Model::ReadMails::QMailAuthuser';

use Mojo::JSON 'decode_json';

use Digest::HMAC_MD5 'hmac_md5_hex';


use constant {
    VALID_USER => 'mockmaildir@example.org',
    VALID_PW   => '12345',
};

has user      => sub { $ENV{USER} };
has maildir   => 't/';
has extractor => 'python';

our %EXTRACTORS = (
    perl   => 'script/qmauth.pl',
    python => 'script/qmauth.py',
    rust   => 'bin/jwebmail-extract',
);


sub new {
    my $cls = shift;
    my %args = @_ == 1 ? %$_[0] : @_;

    my $self = bless {%args}, ref $cls || $cls;
    $self->user;
    $self->maildir;

    $self->next::method(prog => $EXTRACTORS{$self->extractor});
    return $self;
}


sub verify_user {
    my $self = shift;
    my $auth = shift;

    my $passwd = $auth->{password}->show_password;

    if ($auth->{challenge}) {
        return $auth->{user} eq VALID_USER &&
            $passwd eq hmac_md5_hex($auth->{challenge}, VALID_PW);
    }
    else {
        return $auth->{user} eq VALID_USER && $passwd eq VALID_PW;
    }
}

sub start_qmauth {
    my $self = shift;
    my ($auth, $mode, $args) = @_;

    my $mail_user = 'maildir';
    my @exec = ($EXTRACTORS{$self->extractor}, $self->maildir, $self->user, $mail_user, $mode, @$args);

    my $pid = open(my $reader, '-|', @exec)
        or die "failed to create subprocess: $!";

    return $pid, $reader;
}


1