From: Errietta Kostala Date: Wed, 28 Jan 2015 15:19:54 +0000 (+0000) Subject: Cache certs X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e490a3d8af8dec43ee328da99341063d6d793a8e;p=scpubgit%2Fstemmaweb.git Cache certs --- diff --git a/Makefile.PL b/Makefile.PL index fa760b1..ab6c768 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -16,6 +16,7 @@ requires 'Text::Tradition::Analysis' => '2.0.0'; requires 'Text::Tradition::StemmaUtil'; # Catalyst libraries requires 'Catalyst::Runtime' => '5.90002'; +requires 'Catalyst::Plugin::Cache'; requires 'Catalyst::Plugin::ConfigLoader'; requires 'Catalyst::Plugin::StackTrace'; requires 'Catalyst::Plugin::Static::Simple'; @@ -52,6 +53,7 @@ requires 'TryCatch'; requires 'namespace::autoclean'; requires 'Config::General'; # This should reflect the config file format you've chosen # See Catalyst::Plugin::ConfigLoader for supported formats +requires 'Cache::FileCache'; test_requires 'Test::More' => '0.88'; test_requires 'LWP::Protocol::PSGI'; test_requires 'Test::WWW::Mechanize'; diff --git a/lib/stemmaweb.pm b/lib/stemmaweb.pm index 1a9da7d..f9e1092 100644 --- a/lib/stemmaweb.pm +++ b/lib/stemmaweb.pm @@ -29,10 +29,12 @@ use Catalyst qw/ Session::State::Cookie StatusMessage StackTrace + Cache /; extends 'Catalyst'; +use Cache::FileCache; use stemmaweb::Authentication::FormHandler; our $VERSION = '0.01'; @@ -61,6 +63,15 @@ __PACKAGE__->config( stemmaweb->path_to( 'root', 'src' ), ], }, + + 'Plugin::Cache' => { + backend => { + class => 'Cache::FileCache', + namespace => 'cache', + default_expires_in => 86400, + }, + }, + ## kiokudb auth store testing 'Plugin::Authentication' => { default => { diff --git a/lib/stemmaweb/Authentication/Credential/Google.pm b/lib/stemmaweb/Authentication/Credential/Google.pm index 056082b..5a7e3d8 100644 --- a/lib/stemmaweb/Authentication/Credential/Google.pm +++ b/lib/stemmaweb/Authentication/Credential/Google.pm @@ -90,8 +90,42 @@ Decoded JSON object containing certificates. sub retrieve_certs { my ($self, $url) = @_; - $url ||= ( $self->{_app}->config->{'Authentication::Credential::Google'}->{public_cert_url} || 'https://www.googleapis.com/oauth2/v1/certs' ); - return decode_json(get($url)); + my $c = $self->{_app}; + my $cached = 0; + my $certs; + my $cache; + + $url ||= ( $c->config->{'Authentication::Credential::Google'}->{public_cert_url} || 'https://www.googleapis.com/oauth2/v1/certs' ); + + if ( ($c->registered_plugins('Catalyst::Plugin::Cache')) && ($cache = $c->cache) ) { + if ($certs = $cache->get('certs')) { + $certs = decode_json($certs); + + foreach my $key (keys %$certs) { + my $cert = $certs->{$key}; + my $x509 = Crypt::OpenSSL::X509->new_from_string($cert); + + if ($self->is_cert_expired($x509)) { + $cached = 0; + last; + } else { + $cached = 1; + } + } + } + } + + if (!$cached) { + my $certs_encoded = get($url); + + if ($cache) { + $cache->set('certs', $certs_encoded); + } + + $certs = decode_json($certs_encoded); + } + + return $certs; } =head2 get_key_from_cert