| 1 | package OpenID::Lite::Agent::Paranoid; | ||
| 2 | |||
| 3 | use Any::Moose; | ||
| 4 | use HTTP::Date; | ||
| 5 | use LWPx::ParanoidAgent; | ||
| 6 | |||
| 7 | has '_agent' => ( | ||
| 8 | is => 'ro', | ||
| 9 | isa => 'LWPx::ParanoidAgent', | ||
| 10 | lazy_build => 1, | ||
| 11 | ); | ||
| 12 | |||
| 13 | has 'blocked_hosts' => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => 'ArrayRef', | ||
| 16 | default => sub { [] }, | ||
| 17 | ); | ||
| 18 | |||
| 19 | has 'whitelisted_hosts' => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => 'ArrayRef', | ||
| 22 | default => sub { [] }, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has 'https_debug' => ( | ||
| 26 | is => 'ro', | ||
| 27 | isa => 'Bool', | ||
| 28 | default => 0, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has 'https_ca_dir' => ( | ||
| 32 | is => 'ro', | ||
| 33 | isa => 'Str', | ||
| 34 | ); | ||
| 35 | |||
| 36 | has 'https_ca_file' => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'Str', | ||
| 39 | ); | ||
| 40 | |||
| 41 | has 'https_proxy' => ( | ||
| 42 | is => 'ro', | ||
| 43 | isa => 'Str', | ||
| 44 | ); | ||
| 45 | |||
| 46 | has 'https_proxy_username' => ( | ||
| 47 | is => 'ro', | ||
| 48 | isa => 'Str', | ||
| 49 | ); | ||
| 50 | |||
| 51 | has 'https_proxy_password' => ( | ||
| 52 | is => 'ro', | ||
| 53 | isa => 'Str', | ||
| 54 | ); | ||
| 55 | |||
| 56 | has 'https_cert_file' => ( | ||
| 57 | is => 'ro', | ||
| 58 | isa => 'Str', | ||
| 59 | ); | ||
| 60 | |||
| 61 | has 'https_key_file' => ( | ||
| 62 | is => 'ro', | ||
| 63 | isa => 'Str', | ||
| 64 | ); | ||
| 65 | |||
| 66 | sub get { | ||
| 67 | my ( $self, $url ) = @_; | ||
| 68 | local $ENV{HTTPS_DEBUG} = $self->https_debug if $self->https_debug; | ||
| 69 | local $ENV{HTTPS_CA_FILE} = $self->https_ca_file if $self->https_ca_file; | ||
| 70 | local $ENV{HTTPS_CA_DIR} = $self->https_ca_dir if $self->https_ca_dir; | ||
| 71 | local $ENV{HTTPS_CERT_FILE} = $self->https_cert_file | ||
| 72 | if $self->https_cert_file; | ||
| 73 | local $ENV{HTTPS_KEY_FILE} = $self->https_key_file | ||
| 74 | if $self->https_key_file; | ||
| 75 | local $ENV{HTTPS_VERSION} = $self->https_version if $self->https_version; | ||
| 76 | local $ENV{HTTPS_PROXY} = $self->https_proxy if $self->https_proxy; | ||
| 77 | local $ENV{HTTPS_PROXY_USERNAME} = $self->https_proxy_username | ||
| 78 | if $self->https_proxy_username; | ||
| 79 | local $ENV{HTTPS_PROXY_PASSWORD} = $self->https_proxy_password | ||
| 80 | if $self->https_proxy_password; | ||
| 81 | my $response = $self->_agent->get($url); | ||
| 82 | return $self->_filter_response($response); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub request { | ||
| 86 | my ( $self, $request ) = @_; | ||
| 87 | local $ENV{HTTPS_DEBUG} = $self->https_debug if $self->https_debug; | ||
| 88 | local $ENV{HTTPS_CA_FILE} = $self->https_ca_file if $self->https_ca_file; | ||
| 89 | local $ENV{HTTPS_CA_DIR} = $self->https_ca_dir if $self->https_ca_dir; | ||
| 90 | local $ENV{HTTPS_CERT_FILE} = $self->https_cert_file | ||
| 91 | if $self->https_cert_file; | ||
| 92 | local $ENV{HTTPS_KEY_FILE} = $self->https_key_file | ||
| 93 | if $self->https_key_file; | ||
| 94 | local $ENV{HTTPS_VERSION} = $self->https_version if $self->https_version; | ||
| 95 | local $ENV{HTTPS_PROXY} = $self->https_proxy if $self->https_proxy; | ||
| 96 | local $ENV{HTTPS_PROXY_USERNAME} = $self->https_proxy_username | ||
| 97 | if $self->https_proxy_username; | ||
| 98 | local $ENV{HTTPS_PROXY_PASSWORD} = $self->https_proxy_password | ||
| 99 | if $self->https_proxy_password; | ||
| 100 | my $response = $self->_agent->request($request); | ||
| 101 | return $self->_filter_response($response); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _filter_response { | ||
| 105 | my ( $self, $response ) = @_; | ||
| 106 | if ( $response->header('Client-SSL-Warning') ) { | ||
| 107 | my $err = HTTP::Response->new( 403, | ||
| 108 | q{Unauthorized access to no verified certification} ); | ||
| 109 | $err->header( 'Client-Date' => HTTP::Date::time2str( time() ) ); | ||
| 110 | $err->header( 'Client-Warning' => 'Internal response' ); | ||
| 111 | $err->header( 'Content-Type' => 'text/plain' ); | ||
| 112 | $err->content(q{403 Unauthorized access to blocked host}); | ||
| 113 | return $err; | ||
| 114 | } | ||
| 115 | return $response; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _build__agent { | ||
| 119 | my $self = shift; | ||
| 120 | return LWPx::ParanoidAgent->new( | ||
| 121 | blocked_hosts => $self->blocked_hosts, | ||
| 122 | whitelisted_hosts => $self->whitelisted_hosts, | ||
| 123 | ); | ||
| 124 | } | ||
| 125 | |||
| 126 | no Any::Moose; | ||
| 127 | __PACKAGE__->meta->make_immutable; | ||
| 128 | 1; | ||
| 129 |