1db407f9d16345e19fbe25d2a2beb81ba37e3269
[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     print $json;
14     my $ref = decode_json($json);
15     return decode_base64($ref->{'gssweb'}{'token'});
16                 }
17 ;
18
19 sub token_body($$) {
20     my ($target_server, $itoken) = @_;
21     my $status;
22     my $otoken;
23     my $target;
24   try: {
25      $status = GSSAPI::Name->import( $target,
26                                        $target_server,
27                                      GSSAPI::OID::gss_nt_hostbased_service) or last;
28      our  $ctx = GSSAPI::Context->new() unless $ctx;
29      my $mech;
30      $status = GSSAPI::OID->from_str($mech, '{ 1.3.6.1.5.5.15.1.1.17              }') or last;
31      my $iflags = GSSAPI::GSS_C_MUTUAL_FLAG | GSSAPI::GSS_C_SEQUENCE_FLAG | GSSAPI::GSS_C_REPLAY_FLAG;
32      my $bindings = GSS_C_NO_CHANNEL_BINDINGS;
33      my $creds = GSS_C_NO_CREDENTIAL;
34      my $itime = 0;
35
36              $status = $ctx->init($creds,$target,
37                                   $mech,$iflags,$itime,$bindings,$itoken,
38                                   undef, $otoken,undef,undef);
39     }
40     print "$status\n";
41     return undef unless $otoken;
42     print "Pre-encoding token: $otoken\n";
43     my $encoded_token = encode_base64($otoken);
44     chomp($encoded_token);
45     my $out =  "token=" . uri_encode($encoded_token, {encode_reserved => 1}) ."&nonce=42";
46     print "$out\n";
47     return $out;
48 }
49
50 my ($url, $gssname) = @ARGV;
51 my $www = WWW::Mechanize->new('autocheck' => 0);
52 my $done = 0;
53 my $response_token = undef;
54    while (!$done) {
55
56     $www->post($url, 'Content' => token_body($gssname, $response_token));
57     my $status = $www->status();
58     if ($status == 200) {
59         $done = 1;
60         print "authenticated: response is ".$www->content()."\n";
61         if (token_body($gssname, parse_token($www->content()))) {
62             print "Expecting gss success but did not get it!\n";
63         }
64     } elsif ($status == 401) {
65         print "Continuing\n";
66         $response_token = parse_token($www->content());
67     } else {
68         print "Unexpected response status: $status\n";
69         print $www->content();
70               $done = 1;
71     }
72 }