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