Toby Inkster avatar Toby Inkster committed 8c9ba5f

adds cookies to Auth::WebID

Comments (0)

Files changed (10)

+use inc::Module::Package 'RDF:standard';
+

lib/Plack/MiddleWare/Auth/FasterWebID.pm

+package Plack::Middleware::Auth::FasterWebID;
+
+{
+	$Plack::Middleware::Auth::FasterWebID::AUTHORITY = 'cpan:TOBYINK';
+	$Plack::Middleware::Auth::FasterWebID::VERSION   = '0.001';
+}
+
+use strict;
+use base qw(Plack::Middleware::Auth::WebID);
+use UUID::Tiny qw(:std);
+use Plack::Request;
+use Plack::Response;
+use Plack::Util::Accessor qw(
+	cookie_name
+);
+
+sub prepare_app
+{
+	my ($self) = @_;
+	
+	$self->cookie_name('_webid_session')
+		unless $self->cookie_name;
+	
+	die "need to provide a cache"
+		unless $self->cache;
+
+	die "need to have webid object in $env"
+		unless $self->no_object_please;
+
+	$self->SUPER::prepare_app;
+}
+
+sub _run_app
+{
+	my ($self, $env) = @_;
+	my $res = $self->app->($env);
+	
+	$res->cookies->{ $self->cookie_name } = +{
+		value => (my $cookie_val = create_uuid()),
+		path  => '/',
+	};
+	
+	$self->cache->set('_Faster_'.$cookie_val, $env{WEBID_OBJECT});
+	
+	$res->finalize;
+}
+
+sub _get_webid
+{
+	my ($self, $cert, $env) = @_;
+	my $req = Plack::Request->new($env);
+	
+	if (my $cookie_val = $req->cookies->{ $self->cookie_name })
+	{
+		my $cached = $self->cache->get('_Faster_'.$cookie_val);
+		return ($cached, '2') if $cached;
+	}
+	
+	return $self->SUPER::_get_webid($cert, $env);
+}
+
+__PACKAGE__
+__END__
+
+=head1 NAME
+
+Plack::Middleware::Auth::FasterWebID - like Plack::Middleware::Auth::WebID but faster
+
+=head1 SYNOPSIS
+
+  use Plack::Builder;
+  
+  my $app   = sub { ... };
+  my $cache = CHI->new( ... );
+  
+  sub unauthenticated
+  {
+    my ($self, $env) = @_;
+    return [
+      403,
+      [ 'Content-Type' => 'text/plain' ],
+      [ '403 Forbidden' ],
+    ];
+  }
+  
+  builder
+  {
+    enable "Auth::FasterWebID",
+        cache     => $cache,
+        on_unauth => \&unauthenticated;
+    $app;
+  };
+
+=head1 DESCRIPTION
+
+This module is a subclass of Plack::Middleware::Auth::WebID, but
+it's faster.
+
+How does it work? It just sets a cookie on the client browser, and
+caches WebID authentication info against that cookie. Easy peasy.
+
+=head1 CONFIGURATION
+
+The parent module's C<cache> option which was optional, becomes
+required. The C<no_object_please> option must B<not> be used.
+
+=over
+
+=item cookie_name
+
+A name for cookies. Defaults to '_webid_session' which you'd have to
+be mad to want to change.
+
+=back
+
+=head1 BUGS
+
+Please report any bugs to
+L<http://rt.cpan.org/Dist/Display.html?Queue=Plack-Middleware-Auth-FasterWebID>.
+
+=head1 SEE ALSO
+
+L<Plack::Auth::Middleware::WebID>.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+This software is copyright (c) 2012 by Toby Inkster.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=head1 DISCLAIMER OF WARRANTIES
+
+THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+
+# This file acts as the project's changelog.
+
+@prefix :        <http://usefulinc.com/ns/doap#> .
+@prefix dcs:     <http://ontologi.es/doap-changeset#> .
+@prefix dc:      <http://purl.org/dc/terms/> .
+@prefix dist:    <http://purl.org/NET/cpan-uri/dist/Plack-MiddleWare-Auth-FasterWebID/> .
+@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
+
+dist:project :release dist:v_0-001 .
+dist:v_0-001
+	a               :Version ;
+	dc:issued       "2012-05-08"^^xsd:date ;
+	:revision       "0.001"^^xsd:string ;
+	:file-release   <http://backpan.cpan.org/authors/id/T/TO/TOBYINK/Plack-MiddleWare-Auth-FasterWebID-0.001.tar.gz> ;
+	rdfs:label      "Initial release" .
+
+# This file contains general metadata about the project.
+
+@prefix :        <http://usefulinc.com/ns/doap#> .
+@prefix dc:      <http://purl.org/dc/terms/> .
+@prefix foaf:    <http://xmlns.com/foaf/0.1/> .
+@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
+
+<http://purl.org/NET/cpan-uri/dist/Plack-MiddleWare-Auth-FasterWebID/project>
+	a               :Project ;
+	:programming-language "Perl" ;
+	:name           "Plack-MiddleWare-Auth-FasterWebID" ;
+	:shortdesc      "like Plack::Middleware::Auth::WebID but faster" ;
+	:homepage       <https://metacpan.org/release/Plack-MiddleWare-Auth-FasterWebID> ;
+	:download-page  <https://metacpan.org/release/Plack-MiddleWare-Auth-FasterWebID> ;
+	:bug-database   <http://rt.cpan.org/Dist/Display.html?Queue=Plack-MiddleWare-Auth-FasterWebID> ;
+	:created        "2012-05-08"^^xsd:date ;
+	:license        <http://dev.perl.org/licenses/> ;
+	:developer      [ a foaf:Person ; foaf:name "Toby Inkster" ; foaf:mbox <mailto:tobyink@cpan.org> ] .
+
+<http://dev.perl.org/licenses/>
+	dc:title        "the same terms as the perl 5 programming language system itself" .
+

meta/makefile.ttl

+# This file provides instructions for packaging.
+
+@prefix : <http://purl.org/NET/cpan-uri/terms#> .
+
+<http://purl.org/NET/cpan-uri/dist/Plack-MiddleWare-Auth-FasterWebID/project>
+	:perl_version_from _:main ;
+	:version_from _:main ;
+	:readme_from _:main ;
+	:test_requires "Test::More 0.61"  .
+
+_:main <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName> "lib/Plack/MiddleWare/Auth/FasterWebID.pm" .
+
+use Test::More tests => 1;
+BEGIN { use_ok('Plack::MiddleWare::Auth::FasterWebID') };
+
+use Test::More;
+eval "use Test::Pod 1.00";
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
+all_pod_files_ok();
+

xt/02pod_coverage.t

+use Test::More;
+use Test::Pod::Coverage;
+
+my @modules = qw(Plack::MiddleWare::Auth::FasterWebID);
+pod_coverage_ok($_, "$_ is covered")
+	foreach @modules;
+done_testing(scalar @modules);
+

xt/03meta_uptodate.t

+use Test::More tests => 1;
+use Test::RDF::DOAP::Version;
+doap_version_ok('Plack-MiddleWare-Auth-FasterWebID', 'Plack::MiddleWare::Auth::FasterWebID');
+
+use Test::EOL;
+all_perl_files_ok();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.