| 1 | package LWP::Authen::Gooddata; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | LWP::Authen::Gooddata - Handle GoodData HTTP authentication mechanism | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use WWW::GoodData::Agent; | ||
| 10 | my $agent = new WWW::GoodData::Agent ('https://secure.gooddata.com/gdc'); | ||
| 11 | $agent->post ('/gdc/account/login', ...); | ||
| 12 | # The authentication cookie gets obtained transparently here | ||
| 13 | $agent->get ('/gdc/md'); | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | B<LWP::Authen::Gooddata> gets loaded and invoked by a L<LWP::UserAgent>, | ||
| 18 | or its subclass such as L<WWW::GoodData::Agent>, upon reciept of 401 | ||
| 19 | "Unauthorized" response from the server which indicates use of "GoodData" | ||
| 20 | authentization mechanism in the "WWW-Authenticate" header. | ||
| 21 | |||
| 22 | If the challenge indicates the temporary authentization cookie needs | ||
| 23 | to be refreshed it does so transparently and reissues the request, | ||
| 24 | otherwise dies with appropriate explanation. | ||
| 25 | |||
| 26 | =head1 METHODS | ||
| 27 | |||
| 28 | =over 4 | ||
| 29 | |||
| 30 | =item B<authenticate> [PARAMS] | ||
| 31 | |||
| 32 | Called by LWP::UserAgent internally. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub authenticate | ||
| 37 | { | ||
| 38 | my ($class, $agent, $proxy, $challenge, $response, | ||
| 39 | $request, $arg, $size) = @_; | ||
| 40 | |||
| 41 | # Not an useful 401 from backend, pass the response | ||
| 42 | return $response unless $challenge->{cookie}; | ||
| 43 | |||
| 44 | # Need to obtain super-secure token | ||
| 45 | die 'Login required' if $challenge->{cookie} eq 'GDCAuthSST'; | ||
| 46 | |||
| 47 | # Everything else should be a token refresh | ||
| 48 | die 'Required authentication not supported by client' | ||
| 49 | if $challenge->{cookie} ne 'GDCAuthTT'; | ||
| 50 | |||
| 51 | # Refresh the token cookie | ||
| 52 | # We should obtain the URI from WWW::GoodData somehow... | ||
| 53 | my $token_uri = $request->uri->clone; | ||
| 54 | $token_uri->path ('/gdc/account/token'); | ||
| 55 | $token_uri->fragment (undef); | ||
| 56 | $agent->get ($token_uri); | ||
| 57 | |||
| 58 | $request->header (Cookie => ''); | ||
| 59 | |||
| 60 | # Retry the request | ||
| 61 | return $agent->simple_request ($request); | ||
| 62 | } | ||
| 63 | |||
| 64 | =back | ||
| 65 | |||
| 66 | =head1 SEE ALSO | ||
| 67 | |||
| 68 | =over | ||
| 69 | |||
| 70 | =item * | ||
| 71 | |||
| 72 | L<http://developer.gooddata.com/api/auth.html> -- Specification of the GoodData authentization mechanism | ||
| 73 | |||
| 74 | =item * | ||
| 75 | |||
| 76 | L<LWP::UserAgent> -- The Perl HTTP agent | ||
| 77 | |||
| 78 | =back | ||
| 79 | |||
| 80 | =head1 BUGS | ||
| 81 | |||
| 82 | GoodData authentization mechanism is not an internet standard and thus | ||
| 83 | puts an interoperability barrier. Unfortunatelly, no standard and widely | ||
| 84 | supported mechanism provides comparable benefits (mostly server-side | ||
| 85 | performance coupled with sanity of implementation). Probably an alternative | ||
| 86 | mechanism should be provided (Basic or Digest, which are both widely | ||
| 87 | available) for the client to negotiate. | ||
| 88 | |||
| 89 | =head1 COPYRIGHT | ||
| 90 | |||
| 91 | Copyright 2011, 2012, 2013 Lubomir Rintel | ||
| 92 | |||
| 93 | This program is free software; you can redistribute it and/or modify it | ||
| 94 | under the same terms as Perl itself. | ||
| 95 | |||
| 96 | =head1 AUTHOR | ||
| 97 | |||
| 98 | Lubomir Rintel C<lkundrak@v3.sk> | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | 1; |