| 1 | package Plack::Middleware::Auth::WebID; | ||
| 2 | |||
| 3 | { | ||
| 4 | $Plack::Middleware::Auth::WebID::AUTHORITY = 'cpan:TOBYINK'; | ||
| 5 | $Plack::Middleware::Auth::WebID::VERSION = '1.927'; | ||
| 6 | } | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use base qw(Plack::Middleware); | ||
| 10 | use Plack::Util;; | ||
| 11 | use Plack::Util::Accessor qw( | ||
| 12 | webid_class | ||
| 13 | certificate_env_key | ||
| 14 | on_unauth | ||
| 15 | no_object_please | ||
| 16 | cache | ||
| 17 | ); | ||
| 18 | |||
| 19 | my $default_unauth = sub | ||
| 20 | { | ||
| 21 | my ($self, $env) = @_; | ||
| 22 | $env->{WEBID} = Plack::Util::FALSE; | ||
| 23 | $env->{WEBID_OBJECT} = Plack::Util::FALSE; | ||
| 24 | $self->app->($env); | ||
| 25 | }; | ||
| 26 | |||
| 27 | sub prepare_app | ||
| 28 | { | ||
| 29 | my ($self) = @_; | ||
| 30 | |||
| 31 | $self->certificate_env_key('SSL_CLIENT_CERT') | ||
| 32 | unless defined $self->certificate_env_key; | ||
| 33 | |||
| 34 | $self->webid_class('Web::ID') | ||
| 35 | unless defined $self->webid_class; | ||
| 36 | |||
| 37 | $self->on_unauth($default_unauth) | ||
| 38 | unless defined $self->on_unauth; | ||
| 39 | |||
| 40 | Plack::Util::load_class('Web::ID'); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub call | ||
| 44 | { | ||
| 45 | my ($self, $env) = @_; | ||
| 46 | my $unauth = $self->on_unauth; | ||
| 47 | |||
| 48 | my $cert = $env->{ $self->certificate_env_key } | ||
| 49 | or return $self->$unauth($env); | ||
| 50 | |||
| 51 | my ($webid, $was_cached) = $self->_get_webid($cert, $env); | ||
| 52 | |||
| 53 | if ($webid->valid) | ||
| 54 | { | ||
| 55 | $env->{WEBID} = $webid->uri . ''; | ||
| 56 | $env->{WEBID_OBJECT} = $webid unless $self->no_object_please; | ||
| 57 | $env->{WEBID_CACHE_HIT} = $was_cached; | ||
| 58 | |||
| 59 | return $self->_run_app($env); | ||
| 60 | } | ||
| 61 | |||
| 62 | return $self->$unauth($env); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _run_app | ||
| 66 | { | ||
| 67 | my ($self, $env) = @_; | ||
| 68 | my $app = $self->app; | ||
| 69 | @_ = $env; | ||
| 70 | goto $app; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _get_webid | ||
| 74 | { | ||
| 75 | my ($self, $cert) = @_; | ||
| 76 | |||
| 77 | my $webid = $self->webid_class->new(certificate => $cert); | ||
| 78 | return ($webid, '') unless $self->cache; | ||
| 79 | |||
| 80 | # I know what you're thinking... what's the point in caching these | ||
| 81 | # objects, if we're already constructed it above?! | ||
| 82 | # | ||
| 83 | # Well, much of the heavy work for Web::ID is done in lazy builders. | ||
| 84 | # If we return a cached copy of the object, then we avoid running | ||
| 85 | # those builders again. | ||
| 86 | # | ||
| 87 | my $cached = $self->cache->get( $webid->certificate->fingerprint ); | ||
| 88 | return ($cached, '1') if $cached; | ||
| 89 | |||
| 90 | $self->cache->set($webid->certificate->fingerprint, $webid); | ||
| 91 | return ($webid, '0'); | ||
| 92 | } | ||
| 93 | |||
| 94 | __PACKAGE__ | ||
| 95 | __END__ |