34-Auth-Proxy-and-REST-Server.t 7.33 KB
Newer Older
1
use lib 'inc';
Xavier Guimard's avatar
Xavier Guimard committed
2 3 4
use Test::More;
use strict;
use IO::String;
5
use LWP::UserAgent;
6
use LWP::Protocol::PSGI;
Xavier Guimard's avatar
Xavier Guimard committed
7 8 9 10 11 12

BEGIN {
    require 't/test-lib.pm';
}

my $debug = 'error';
13
my ( $issuer, $sp, $res, $spId, $idpId );
Xavier Guimard's avatar
Xavier Guimard committed
14 15
my %handlerOR = ( issuer => [], sp => [] );

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
LWP::Protocol::PSGI->register(
    sub {
        my $req = Plack::Request->new(@_);
        ok(
            $req->uri =~ m#http://auth.idp.com(.*)#,
            ' @ REST request (' . $req->method . " $1)"
        );
        count(1);
        my $url = $1;
        my $res;
        my $s = $req->content;
        if ( $req->method =~ /^(post|put)$/i ) {
            my $mth = '_' . lc($1);
            my $s   = $req->content;
            ok(
                $res = $issuer->$mth(
                    $url,
                    IO::String->new($s),
                    length => length($s),
                    type   => $req->header('Content-Type'),
                ),
                ' Post request'
            );
            count(1);
            expectOK($res);
            $idpId = expectCookie($res);
        }
        elsif ( $req->method =~ /^(get|delete)$/i ) {
            my $mth = '_' . lc($1);
            ok(
                $res = $issuer->$mth(
                    $url,
                    accept => $req->header('Accept'),
                    cookie => $req->header('Cookie')
                ),
                ' Execute request'
            );
            count(1);
            expectOK($res);
        }
        ok(
            getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/json#,
            'Content is JSON'
        ) or explain( $res->[1], 'Content-Type => application/json' );
        count(1);
        return $res;
    }
);

Xavier Guimard's avatar
Xavier Guimard committed
65
ok( $issuer = issuer(), 'Issuer portal' );
66
$handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload;
Xavier Guimard's avatar
Xavier Guimard committed
67
switch ('sp');
Xavier Guimard's avatar
Tidy  
Xavier Guimard committed
68
&Lemonldap::NG::Handler::Main::cfgNum( 0, 0 );
Xavier Guimard's avatar
Xavier Guimard committed
69 70

ok( $sp = sp(), 'SP portal' );
71
$handlerOR{sp} = \@Lemonldap::NG::Handler::Main::_onReload;
Xavier Guimard's avatar
Xavier Guimard committed
72
count(2);
Xavier Guimard's avatar
Xavier Guimard committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

# Simple SP access
ok(
    $res = $sp->_get(
        '/', accept => 'text/html',
    ),
    'Unauth SP request'
);
expectOK($res);

# Try to auth
ok(
    $res = $sp->_post(
        '/', IO::String->new('user=dwho&password=dwho'),
        length => 23,
        accept => 'text/html'
    ),
    'Post user/password'
);
Xavier Guimard's avatar
Xavier Guimard committed
92
count(2);
Xavier Guimard's avatar
Xavier Guimard committed
93
expectRedirection( $res, 'http://auth.sp.com' );
94 95 96 97 98 99 100 101 102 103 104 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
$spId = expectCookie($res);

# Test other REST queries
switch ('issuer');

# Session content
ok( $res = $issuer->_get("/sessions/global/$idpId"), 'Session content' );
expectOK($res);
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
  or print STDERR $@;
ok( $res->{_session_id} eq $idpId, ' Good ID' )
  or explain( $res, "_session_id => $idpId" );
count(3);

# Session key
ok( $res = $issuer->_get("/sessions/global/$idpId/[_session_id,uid]"),
    'Some session keys' );
expectOK($res);
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
  or print STDERR $@;
ok( $res->{_session_id} eq $idpId, ' Good ID' )
  or explain( $res, "_session_id => $idpId" );
ok( $res->{uid} eq 'dwho', ' Uid is dwho' ) or explain( $res, 'uid => dwho' );
count(4);

# New session
ok(
    $res = $issuer->_post(
        '/sessions/global', IO::String->new('{"uid":"zz","_whatToTrace":"zz"}'),
        length => 32,
        type   => 'application/json'
    ),
    'Create session'
);
expectOK($res);
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
  or print STDERR $@;
131
my $newId = $res->{session}->{_session_id};
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

# Verify a key
ok( $res = $issuer->_get("/sessions/global/$newId/uid"), 'Verify uid' );
ok( $res->[2]->[0] eq 'zz', ' Uid is good' );
count(4);

# Update a key
ok(
    $res = $issuer->app->(
        {
            HTTP_ACCEPT            => 'application/json',
            HTTP_ACCEPT_LANGUAGE   => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
            HTTP_HOST              => 'auth.idp.com',
            PATH_INFO              => "/sessions/global/$newId",
            REMOTE_ADDR            => '127.0.0.1',
            REQUEST_METHOD         => 'PUT',
            REQUEST_URI            => "/sessions/global/$newId",
            SCRIPT_NAME            => '',
            SERVER_NAME            => 'auth.example.com',
            SERVER_PORT            => '80',
            SERVER_PROTOCOL        => 'HTTP/1.1',
            'psgix.input.buffered' => 0,
            'psgi.input'           => IO::String->new('{"cn":"CN"}'),
            CONTENT_TYPE           => 'application/json',
            CONTENT_LENGTH         => 11,
        }
    ),
    'Put a new key'
);
expectOK($res);
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
  or print STDERR $@;
ok( $res->{result} == 1, ' Result is 1' );
count(3);

# Verify new key
ok( $res = $issuer->_get("/sessions/global/$newId/cn"), 'Verify cn' );
ok( $res->[2]->[0] eq 'CN', ' CN is good' );
count(2);

# Del new session
ok(
    $res = $issuer->app->(
        {
            HTTP_ACCEPT          => 'application/json',
            HTTP_ACCEPT_LANGUAGE => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
            HTTP_HOST            => 'auth.idp.com',
            PATH_INFO            => "/sessions/global/$newId",
            REMOTE_ADDR          => '127.0.0.1',
            REQUEST_METHOD       => 'DELETE',
            REQUEST_URI          => "/sessions/global/$newId",
            SCRIPT_NAME          => '',
            SERVER_NAME          => 'auth.example.com',
            SERVER_PORT          => '80',
            SERVER_PROTOCOL      => 'HTTP/1.1',
        }
    ),
    'Delete new session'
);
expectOK($res);
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
  or print STDERR $@;
ok( $res->{result} == 1, ' Result is 1' );
count(3);

# Verify that session is deleted
ok( $res = $issuer->_get("/sessions/global/$newId/cn"),
    'New session is deleted' );
ok( $res->[0] == 400, ' Session does not exist' );
count(2);
Xavier Guimard's avatar
Xavier Guimard committed
202 203

# Logout
204
switch ('sp');
Xavier Guimard's avatar
Xavier Guimard committed
205 206 207 208 209 210 211 212 213 214 215 216
ok(
    $res = $sp->_get(
        '/',
        query  => 'logout',
        accept => 'text/html',
        cookie => "lemonldap=$spId"
    ),
    'Ask for logout'
);
count(1);
expectOK($res);

Xavier Guimard's avatar
Xavier Guimard committed
217
# Test if user is reject on IdP
Xavier Guimard's avatar
Xavier Guimard committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
ok(
    $res = $sp->_get(
        '/', cookie => "lemonldap=$spId",
    ),
    'Test if user is reject on IdP'
);
count(1);
expectReject($res);

clean_sessions();
done_testing( count() );

# Redefine LWP methods for tests
sub switch {
    my $type = shift;
233
    @Lemonldap::NG::Handler::Main::_onReload = @{
Xavier Guimard's avatar
Xavier Guimard committed
234 235 236 237 238 239 240 241 242 243 244 245 246
        $handlerOR{$type};
    };
}

sub issuer {
    return LLNG::Manager::Test->new(
        {
            ini => {
                logLevel          => $debug,
                templatesDir      => 'site/htdocs/static',
                domain            => 'idp.com',
                portal            => 'http://auth.idp.com',
                authentication    => 'Demo',
247
                userDB            => 'Same',
Xavier Guimard's avatar
Xavier Guimard committed
248
                restSessionServer => 1,
249
                restConfigServer  => 1,
Xavier Guimard's avatar
Xavier Guimard committed
250 251 252 253 254 255 256 257 258 259 260 261 262
            }
        }
    );
}

sub sp {
    return LLNG::Manager::Test->new(
        {
            ini => {
                logLevel         => $debug,
                domain           => 'sp.com',
                portal           => 'http://auth.sp.com',
                authentication   => 'Proxy',
263
                userDB           => 'Same',
Xavier Guimard's avatar
Xavier Guimard committed
264 265
                proxyAuthService => 'http://auth.idp.com',
                proxyUseSoap     => 0,
266
                whatToTrace      => '_whatToTrace',
Xavier Guimard's avatar
Xavier Guimard committed
267 268 269 270
            },
        }
    );
}