| 1 | package Plack::Middleware::OIDC::Redirect; | ||
| 2 | 11 | ▶ | use 5.020; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 11 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 11 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 11 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 11 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 11 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 11 | ▶ | use Plack::Response; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | =encoding utf8 | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Plack::Middleware::OIDC::Redirect - Internal exception class used to signal a redirect | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | L<OIDC::Client::Plugin> is designed for frameworks where the current request | ||
| 21 | has a mutable response object that a controller can alter at any point | ||
| 22 | (C<$c->redirect_to(...)> in Mojolicious, for example). | ||
| 23 | |||
| 24 | PSGI has no such object: the response is simply the return value of the | ||
| 25 | application coderef. So when application code calls | ||
| 26 | C<$env->{oidc}->redirect_to_authorize()> or | ||
| 27 | C<$env->{oidc}->redirect_to_logout()> from deep within its own logic, | ||
| 28 | there is no direct way to make the enclosing PSGI application return early | ||
| 29 | with a redirect response. | ||
| 30 | |||
| 31 | To work around this, L<Plack::Middleware::OIDC> gives L<OIDC::Client::Plugin> | ||
| 32 | a C<redirect> coderef that throws an instance of this class instead of | ||
| 33 | performing the redirection itself. L<Plack::Middleware::OIDC> then wraps the | ||
| 34 | call to the downstream application in an C<eval>, catches this exception, and | ||
| 35 | turns it into a real C<302> PSGI response. | ||
| 36 | |||
| 37 | This mirrors the way L<Plack::Middleware::HTTPExceptions> turns exception | ||
| 38 | objects into HTTP responses, and this class follows the same convention : it | ||
| 39 | implements an L</as_psgi()> method, so that it is also understood by the | ||
| 40 | components which look for it. | ||
| 41 | |||
| 42 | Frameworks which rethrow the exceptions implementing C<as_psgi()> instead of | ||
| 43 | turning them into an error page will let this exception reach | ||
| 44 | L<Plack::Middleware::OIDC>. | ||
| 45 | |||
| 46 | =head1 METHODS | ||
| 47 | |||
| 48 | =head2 new( url => $url ) | ||
| 49 | |||
| 50 | my $redirect = Plack::Middleware::OIDC::Redirect->new(url => $url); | ||
| 51 | |||
| 52 | Creates an exception object to be thrown. The C<url> parameter is the URL | ||
| 53 | the browser must be redirected to. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | 12 | ▶ | sub new ($class, %args) { |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||
| 58 | 12 | return bless { url => $args{url} }, $class; | |
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | =head2 url() | ||
| 63 | |||
| 64 | Returns the URL to redirect to. | ||
| 65 | |||
| 66 | my $url = $redirect->url(); | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | 13 | ▶ | sub url ($self) { |
|
Subroutine
url: called CC=1 SCAR=0.0
Pod
documented
| |||
| 71 | return $self->{url} | ||
| 72 | 13 | } | |
| 73 | |||
| 74 | |||
| 75 | =head2 as_psgi() | ||
| 76 | |||
| 77 | my $psgi_response = $redirect->as_psgi(); | ||
| 78 | |||
| 79 | Returns the PSGI response (an arrayref C<[ $status, \@headers, \@body ]>) | ||
| 80 | corresponding to this redirection : a C<302> status with the C<Location> | ||
| 81 | header set to the L</url()>. | ||
| 82 | |||
| 83 | This method follows the convention used by L<Plack::Middleware::HTTPExceptions> | ||
| 84 | and by the frameworks which look for it : an exception which implements it is | ||
| 85 | considered as "knowing what it wants", and can be handed over to the PSGI layer | ||
| 86 | instead of being handled as an ordinary application error. | ||
| 87 | L<Plack::Middleware::OIDC> calls it to build the response when it catches the | ||
| 88 | exception, and L<Plack::Middleware::HTTPExceptions> would do the same if the | ||
| 89 | exception happened to reach it. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | 12 | ▶ | sub as_psgi ($self) { |
|
Subroutine
as_psgi: called CC=1 SCAR=0.0
Pod
documented
| |||
| 94 | |||
| 95 | 12 | my $res = Plack::Response->new(); | |
| 96 | 12 | $res->redirect($self->url); | |
| 97 | |||
| 98 | 12 | return $res->finalize(); | |
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | 11 | 1; | |