Fail on empty token
[mod_auth_kerb.cvs/.git] / test / gssweb_client.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use WWW::Mechanize;
5     use LWP::ConnCache;
6 use GSSAPI;
7 use GSSAPI::OID;
8 use MIME::Base64;
9 use JSON;
10 use URI::Encode qw(uri_encode);
11
12 sub parse_token($) {
13     my ($json) = @_;
14     print $json;
15     my $ref = decode_json($json);
16     return decode_base64($ref->{'gssweb'}{'token'}) or die "No token\n";
17                 }
18 ;
19
20 sub token_body($$) {
21     my ($target_server, $itoken) = @_;
22     my $status;
23     my $otoken;
24     my $target;
25   try: {
26      $status = GSSAPI::Name->import( $target,
27                                        $target_server,
28                                      GSSAPI::OID::gss_nt_hostbased_service) or last;
29      our  $ctx = GSSAPI::Context->new() unless $ctx;
30      my $mech;
31      $status = GSSAPI::OID->from_str($mech, '{ 1.3.6.1.5.5.15.1.1.17              }') or last;
32      my $iflags = GSSAPI::GSS_C_MUTUAL_FLAG | GSSAPI::GSS_C_SEQUENCE_FLAG | GSSAPI::GSS_C_REPLAY_FLAG;
33      my $bindings = GSS_C_NO_CHANNEL_BINDINGS;
34      my $creds = GSS_C_NO_CREDENTIAL;
35      my $itime = 0;
36
37              $status = $ctx->init($creds,$target,
38                                   $mech,$iflags,$itime,$bindings,$itoken,
39                                   undef, $otoken,undef,undef);
40     }
41     print "$status\n";
42     return undef unless $otoken;
43     print "Pre-encoding token: $otoken\n";
44     my $encoded_token = encode_base64($otoken, '');
45     chomp($encoded_token);
46     my $out =  "token=" . uri_encode($encoded_token, {encode_reserved => 1}) ."&nonce=42";
47     print "$out\n";
48     return $out;
49 }
50
51 my ($url, $gssname) = @ARGV;
52 my $www = WWW::Mechanize->new('autocheck' => 0);
53 $www->conn_cache(LWP::ConnCache->new);
54 my $done = 0;
55 my $response_token = undef;
56    while (!$done) {
57
58     $www->post($url, 'Content' => token_body($gssname, $response_token));
59     my $status = $www->status();
60     if ($status == 200) {
61         $done = 1;
62         print "authenticated: response is ".$www->content()."\n";
63         if (token_body($gssname, parse_token($www->content()))) {
64             print "Expecting gss success but did not get it!\n";
65         }
66     } elsif ($status == 401) {
67         print "Continuing\n";
68         $response_token = parse_token($www->content());
69         die "Empty token supplied\n" if $response_token eq "";
70     } else {
71         print "Unexpected response status: $status\n";
72         print $www->content();
73               $done = 1;
74     }
75 }