ldap_escaping.t 2.91 KB
Newer Older
1
2
3
use strict;
use warnings;

Ruslan Zakirov's avatar
Ruslan Zakirov committed
4
use RT::Authen::ExternalAuth::Test tests => undef, ldap => 1;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

my $ldap_port = 1024 + int rand(10000) + $$ % 1024;
ok( my $server = Net::LDAP::Server::Test->new( $ldap_port, auto_schema => 1 ),
    "spawned test LDAP server on port $ldap_port" );

my $ldap = Net::LDAP->new("localhost:$ldap_port");
$ldap->bind();

my $users_dn = "ou=users,dc=bestpractical,dc=com";
my $group_dn = "cn=test group,ou=groups,dc=bestpractical,dc=com";

$ldap->add(
    "cn=Smith\\, John,$users_dn",
    attr => [
        cn           => 'Smith\\, John',
        mail         => 'jsmith@example.com',
        uid          => 'jsmith',
        objectClass  => 'User',
        userPassword => 'password',
    ]
);
26
27
28
29
30
31
32
33
34
35
$ldap->add(
    "cn=John Doe,$users_dn",
    attr => [
        cn           => 'John Doe',
        mail         => 'jdoe@example.com',
        uid          => 'j(doe',
        objectClass  => 'User',
        userPassword => 'password',
    ]
);
36
37
38
39
$ldap->add(
    $group_dn,
    attr => [
        cn          => "test group",
40
        memberDN    => [ "cn=Smith\\, John,$users_dn", "cn=John Doe,$users_dn" ],
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
71
72
73
74
75
76
77
78
79
80
81
82
        objectClass => 'Group',
    ],
);

RT->Config->Set( ExternalAuthPriority        => ['My_LDAP'] );
RT->Config->Set( ExternalInfoPriority        => ['My_LDAP'] );
RT->Config->Set( ExternalServiceUsesSSLorTLS => 0 );
RT->Config->Set( AutoCreateNonExternalUsers  => 0 );
RT->Config->Set( AutoCreate  => undef );
RT->Config->Set(
    ExternalSettings => {
        'My_LDAP' => {
            'type'            => 'ldap',
            'server'          => "127.0.0.1:$ldap_port",
            'base'            => $users_dn,
            'filter'          => '(objectClass=*)',
            'd_filter'        => '()',
            'group'           => $group_dn,
            'group_attr'      => 'memberDN',
            'tls'             => 0,
            'net_ldap_args'   => [ version => 3 ],
            'attr_match_list' => [ 'Name', 'EmailAddress' ],
            'attr_map'        => {
                'Name'         => 'uid',
                'EmailAddress' => 'mail',
            }
        },
    }
);

my ( $baseurl, $m ) = RT::Test->started_ok();

diag "comma in the DN";
{
    ok( $m->login( 'jsmith', 'password' ), 'logged in' );

    my $testuser = RT::User->new($RT::SystemUser);
    my ($ok,$msg) = $testuser->Load( 'jsmith' );
    ok($ok,$msg);
    is($testuser->EmailAddress,'jsmith@example.com');
}

83
84
85
86
87
88
89
90
91
92
93
94
95
96
diag "paren in the username";
{
    ok( $m->logout, 'logged out' );
    # $m->login chokes on ( in 4.0.5
    $m->get_ok($m->rt_base_url . "?user=j(doe;pass=password");
    $m->content_like(qr/Logout/i, 'contains logout link');
    $m->content_contains('<span class="current-user">j&#40;doe</span>', 'contains logged in user name');

    my $testuser = RT::User->new($RT::SystemUser);
    my ($ok,$msg) = $testuser->Load( 'j(doe' );
    ok($ok,$msg);
    is($testuser->EmailAddress,'jdoe@example.com');
}

97
98
99
100
$ldap->unbind();

undef $m;
done_testing;